From afc59ebb976f48f5b62a4ff32d0549e14d77049b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 19 Nov 2019 13:55:48 +0900 Subject: [PATCH] Removed old user follows storage --- arn/Database.go | 1 - arn/UserFollows.go | 184 ++++++++++++++++-- arn/UserFollows2.go | 180 ----------------- patches/user-copy-follows/user-copy-follow.go | 22 --- 4 files changed, 169 insertions(+), 218 deletions(-) delete mode 100644 arn/UserFollows2.go delete mode 100644 patches/user-copy-follows/user-copy-follow.go diff --git a/arn/Database.go b/arn/Database.go index f01c8b32..a6a6e56f 100644 --- a/arn/Database.go +++ b/arn/Database.go @@ -54,7 +54,6 @@ var DB = Node.Namespace("arn").RegisterTypes( (*Thread)(nil), (*TwitterToUser)(nil), (*User)(nil), - (*UserFollows)(nil), (*UserNotifications)(nil), ) diff --git a/arn/UserFollows.go b/arn/UserFollows.go index da71677c..58a95b89 100644 --- a/arn/UserFollows.go +++ b/arn/UserFollows.go @@ -1,26 +1,180 @@ package arn import ( - "github.com/aerogo/nano" + "errors" + + "github.com/aerogo/aero" + "github.com/aerogo/api" ) -// UserFollows is a list including IDs to users you follow. -type UserFollows struct { - UserID UserID `json:"userId" primary:"true"` - Items []string `json:"items"` -} +// Add adds an user to the user if it hasn't been added yet. +func (user *User) Follow(followUserID UserID) error { + if followUserID == user.ID { + return errors.New("You can't follow yourself") + } -// StreamUserFollows returns a stream of all user follows. -func StreamUserFollows() <-chan *UserFollows { - channel := make(chan *UserFollows, nano.ChannelBufferSize) + if user.IsFollowing(followUserID) { + return errors.New("User " + followUserID + " has already been added") + } - go func() { - for obj := range DB.All("UserFollows") { - channel <- obj.(*UserFollows) + user.FollowIDs = append(user.FollowIDs, followUserID) + + // Send notification + user, err := GetUser(followUserID) + + if err == nil { + if !user.Settings().Notification.NewFollowers { + return nil } - close(channel) - }() + follower, err := GetUser(user.ID) - return channel + if err == nil { + user.SendNotification(&PushNotification{ + Title: "You have a new follower!", + Message: follower.Nick + " started following you.", + Icon: "https:" + follower.AvatarLink("large"), + Link: "https://notify.moe" + follower.Link(), + Type: NotificationTypeFollow, + }) + } + } + + return nil +} + +// Unfollow removes the user ID from the follow list. +func (user *User) Unfollow(userID UserID) bool { + for index, item := range user.FollowIDs { + if item == userID { + user.FollowIDs = append(user.FollowIDs[:index], user.FollowIDs[index+1:]...) + return true + } + } + + return false +} + +// IsFollowing checks if the object follows the user ID. +func (user *User) IsFollowing(userID UserID) bool { + for _, item := range user.FollowIDs { + if item == userID { + return true + } + } + + return false +} + +// Follows returns a slice of all the users you are following. +func (user *User) Follows() []*User { + followsObj := DB.GetMany("User", user.FollowIDs) + follows := make([]*User, len(followsObj)) + + for i, user := range followsObj { + follows[i] = user.(*User) + } + + return follows +} + +// Friends returns a slice of all the users you are following that also follow you. +func (user *User) Friends() []*User { + followsObj := DB.GetMany("User", user.FollowIDs) + friends := make([]*User, 0, len(followsObj)) + + for _, friendObj := range followsObj { + friend := friendObj.(*User) + + if friend.IsFollowing(user.ID) { + friends = append(friends, friend) + } + } + + return friends +} + +// Followers returns the users who follow the user. +func (user *User) Followers() []*User { + var followerIDs []string + + for follower := range StreamUsers() { + if follower.IsFollowing(user.ID) { + followerIDs = append(followerIDs, follower.ID) + } + } + + usersObj := DB.GetMany("User", followerIDs) + users := make([]*User, len(usersObj)) + + for i, obj := range usersObj { + users[i] = obj.(*User) + } + + return users +} + +// FollowersCount returns how many followers the user has. +func (user *User) FollowersCount() int { + count := 0 + + for follower := range StreamUsers() { + if follower.IsFollowing(user.ID) { + count++ + } + } + + return count +} + +// UserFollowerCountMap returns a map of user ID keys and their corresping number of followers as the value. +func UserFollowerCountMap() map[string]int { + followCount := map[string]int{} + + for user := range StreamUsers() { + for _, followUserID := range user.FollowIDs { + followCount[followUserID]++ + } + } + + return followCount +} + +// FollowAction returns an API action that adds a user ID to the follow list. +func FollowAction() *api.Action { + return &api.Action{ + Name: "follow", + Route: "/follow/:follow-id", + Run: func(obj interface{}, ctx aero.Context) error { + user := obj.(*User) + followID := ctx.Get("follow-id") + err := user.Follow(followID) + + if err != nil { + return err + } + + user.Save() + return nil + }, + } +} + +// UnfollowAction returns an API action that removes a user ID from the follow list. +func UnfollowAction() *api.Action { + return &api.Action{ + Name: "unfollow", + Route: "/unfollow/:unfollow-id", + Run: func(obj interface{}, ctx aero.Context) error { + user := obj.(*User) + unfollowID := ctx.Get("unfollow-id") + + if !user.Unfollow(unfollowID) { + return errors.New("This item does not exist in the list") + } + + user.Save() + return nil + }, + } } diff --git a/arn/UserFollows2.go b/arn/UserFollows2.go deleted file mode 100644 index 58a95b89..00000000 --- a/arn/UserFollows2.go +++ /dev/null @@ -1,180 +0,0 @@ -package arn - -import ( - "errors" - - "github.com/aerogo/aero" - "github.com/aerogo/api" -) - -// Add adds an user to the user if it hasn't been added yet. -func (user *User) Follow(followUserID UserID) error { - if followUserID == user.ID { - return errors.New("You can't follow yourself") - } - - if user.IsFollowing(followUserID) { - return errors.New("User " + followUserID + " has already been added") - } - - user.FollowIDs = append(user.FollowIDs, followUserID) - - // Send notification - user, err := GetUser(followUserID) - - if err == nil { - if !user.Settings().Notification.NewFollowers { - return nil - } - - follower, err := GetUser(user.ID) - - if err == nil { - user.SendNotification(&PushNotification{ - Title: "You have a new follower!", - Message: follower.Nick + " started following you.", - Icon: "https:" + follower.AvatarLink("large"), - Link: "https://notify.moe" + follower.Link(), - Type: NotificationTypeFollow, - }) - } - } - - return nil -} - -// Unfollow removes the user ID from the follow list. -func (user *User) Unfollow(userID UserID) bool { - for index, item := range user.FollowIDs { - if item == userID { - user.FollowIDs = append(user.FollowIDs[:index], user.FollowIDs[index+1:]...) - return true - } - } - - return false -} - -// IsFollowing checks if the object follows the user ID. -func (user *User) IsFollowing(userID UserID) bool { - for _, item := range user.FollowIDs { - if item == userID { - return true - } - } - - return false -} - -// Follows returns a slice of all the users you are following. -func (user *User) Follows() []*User { - followsObj := DB.GetMany("User", user.FollowIDs) - follows := make([]*User, len(followsObj)) - - for i, user := range followsObj { - follows[i] = user.(*User) - } - - return follows -} - -// Friends returns a slice of all the users you are following that also follow you. -func (user *User) Friends() []*User { - followsObj := DB.GetMany("User", user.FollowIDs) - friends := make([]*User, 0, len(followsObj)) - - for _, friendObj := range followsObj { - friend := friendObj.(*User) - - if friend.IsFollowing(user.ID) { - friends = append(friends, friend) - } - } - - return friends -} - -// Followers returns the users who follow the user. -func (user *User) Followers() []*User { - var followerIDs []string - - for follower := range StreamUsers() { - if follower.IsFollowing(user.ID) { - followerIDs = append(followerIDs, follower.ID) - } - } - - usersObj := DB.GetMany("User", followerIDs) - users := make([]*User, len(usersObj)) - - for i, obj := range usersObj { - users[i] = obj.(*User) - } - - return users -} - -// FollowersCount returns how many followers the user has. -func (user *User) FollowersCount() int { - count := 0 - - for follower := range StreamUsers() { - if follower.IsFollowing(user.ID) { - count++ - } - } - - return count -} - -// UserFollowerCountMap returns a map of user ID keys and their corresping number of followers as the value. -func UserFollowerCountMap() map[string]int { - followCount := map[string]int{} - - for user := range StreamUsers() { - for _, followUserID := range user.FollowIDs { - followCount[followUserID]++ - } - } - - return followCount -} - -// FollowAction returns an API action that adds a user ID to the follow list. -func FollowAction() *api.Action { - return &api.Action{ - Name: "follow", - Route: "/follow/:follow-id", - Run: func(obj interface{}, ctx aero.Context) error { - user := obj.(*User) - followID := ctx.Get("follow-id") - err := user.Follow(followID) - - if err != nil { - return err - } - - user.Save() - return nil - }, - } -} - -// UnfollowAction returns an API action that removes a user ID from the follow list. -func UnfollowAction() *api.Action { - return &api.Action{ - Name: "unfollow", - Route: "/unfollow/:unfollow-id", - Run: func(obj interface{}, ctx aero.Context) error { - user := obj.(*User) - unfollowID := ctx.Get("unfollow-id") - - if !user.Unfollow(unfollowID) { - return errors.New("This item does not exist in the list") - } - - user.Save() - return nil - }, - } -} diff --git a/patches/user-copy-follows/user-copy-follow.go b/patches/user-copy-follows/user-copy-follow.go deleted file mode 100644 index a28f018c..00000000 --- a/patches/user-copy-follows/user-copy-follow.go +++ /dev/null @@ -1,22 +0,0 @@ -package main - -import ( - "github.com/akyoto/color" - "github.com/animenotifier/notify.moe/arn" -) - -func main() { - defer arn.Node.Close() - - for follows := range arn.StreamUserFollows() { - user, err := arn.GetUser(follows.UserID) - - if err != nil { - color.Red(err.Error()) - continue - } - - user.FollowIDs = follows.Items - user.Save() - } -}