Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/implement-status-command #11

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<h1>I started development of this program in golang</h1>
<h1>I finished basic program usages of this program.</h1>
<p>I don't use a fork because I can't open issue and for a while I'll need it. </p>
<p>Here is original repository</p>

Expand Down Expand Up @@ -32,15 +32,33 @@ make
follow [username] — Follow all followers of the specified user.
unfollow — Unfollow who do not follow back.
following — Shows count of users you follow.
followers — Show count of your followers.
followers — Shows count of your followers.
status - Show both, follower and following
</pre>

<br>

### Allow and Deny list.
To add a username to allow or deny list, you should add in json format in file `userlist.json` under the following path:
*`internal/app/model/userlist.json`* marking as `deny` or `allow` like this:

</div>

```json
{
"user1": "Deny",
"user2": "Allow",
"user3": "Allow"
}
```

* Allow list -> When you run unfollow command you can have a file in repository with usernames that you don't want
to unfollow even if them don't follow you back. Like Torvalds, Thompson and so forth.
* Deny list -> When you run follow [username] command and you don't want to follow someone is just put name in this file too.

<div align="center">

### Coming soon
<pre>
* Allow list -> When you run unfollow command you will can have a file in repository with usernames that you don't want unfollow even if them don't follow you back. Like Torvalds, Thompson and so forth.
* Deny list -> When you run follow [username] command and don't want to follow someone is just put name in a file to.
</pre>
Command to add user to allow or deny list file.

</div>
6 changes: 4 additions & 2 deletions cmd/application/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ func main() {
case "unfollow":
user.Unfollow()
case "followers":
fmt.Printf("You have %d followers.\n", len(user.Followers))
user.PrintFollowers()
case "following":
fmt.Printf("You follow %d users.\n", len(user.Following))
user.PrintFollowing()
case "status":
user.PrintStatus()
case "follow":
user.Follow()
default:
Expand Down
59 changes: 36 additions & 23 deletions internal/app/model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,64 +37,77 @@ type User struct {
Login string `json:"login"`
}

func (u *MyUser) FetchAllowDenyList() {
err := json.Unmarshal(userList, &u.UserStatus)
func (receiver *MyUser) PrintFollowing() {
Println("Following:", len(receiver.Following))
}

func (receiver *MyUser) PrintFollowers() {
Println("Followers:", len(receiver.Followers))
}

func (receiver *MyUser) PrintStatus() {
receiver.PrintFollowers()
receiver.PrintFollowing()
}

func (receiver *MyUser) FetchAllowDenyList() {
err := json.Unmarshal(userList, &receiver.UserStatus)
if err != nil {
Printf("Error to create allow and deny list")
}

for key, value := range u.UserStatus {
u.UserStatus[key] = strings.ToLower(value)
for key, value := range receiver.UserStatus {
receiver.UserStatus[key] = strings.ToLower(value)
}
}

func (u *MyUser) FetchFollowing(count *int) {
func (receiver *MyUser) FetchFollowing(count *int) {
var url string
if u.TargetUser != "" {
url = Sprintf(FOLLOWING_URL, u.TargetUser)
if receiver.TargetUser != "" {
url = Sprintf(FOLLOWING_URL, receiver.TargetUser)
} else {
url = Sprintf(FOLLOWING_URL, u.Login)
url = Sprintf(FOLLOWING_URL, receiver.Login)
}

fetchData(url, "following", u, count)
fetchData(url, "following", receiver, count)
}

func (u *MyUser) FetchFollowers(count *int) {
url := Sprintf(FOLLOWERS_URL, u.Login)
fetchData(url, "followers", u, count)
func (receiver *MyUser) FetchFollowers(count *int) {
url := Sprintf(FOLLOWERS_URL, receiver.Login)
fetchData(url, "followers", receiver, count)
}

func (u *MyUser) Unfollow() {
func (receiver *MyUser) Unfollow() {
var usersToUnfollow []string
for _, user := range u.Following {
if !userInList(user, u.Followers) || u.UserStatus[user.Login] == "allow" {
for _, user := range receiver.Following {
if !userInList(user, receiver.Followers) || receiver.UserStatus[user.Login] == "allow" {
usersToUnfollow = append(usersToUnfollow, user.Login)
}
}

processUsers(usersToUnfollow, "unfollow")
}

func (u *MyUser) Follow() {
if u.TargetUser == "" {
func (receiver *MyUser) Follow() {
if receiver.TargetUser == "" {
Print("User to fetch? ")
_, err := Scanln(&u.TargetUser)
_, err := Scanln(&receiver.TargetUser)
if err != nil {
Println(`Error fetching target user.!`)
os.Exit(1)
}
}

if u.Login != u.TargetUser {
u.FetchFollowers(new(int))
if receiver.Login != receiver.TargetUser {
receiver.FetchFollowers(new(int))
}

var usersToFollow []string
for _, user := range u.Followers {
if u.UserStatus[user.Login] == "deny" {
for _, user := range receiver.Followers {
if receiver.UserStatus[user.Login] == "deny" {
continue
}
if !userInList(user, u.Following) {
if !userInList(user, receiver.Following) {
usersToFollow = append(usersToFollow, user.Login)
}
}
Expand Down