Removed old user follows storage
This commit is contained in:
parent
803d303420
commit
afc59ebb97
@ -54,7 +54,6 @@ var DB = Node.Namespace("arn").RegisterTypes(
|
||||
(*Thread)(nil),
|
||||
(*TwitterToUser)(nil),
|
||||
(*User)(nil),
|
||||
(*UserFollows)(nil),
|
||||
(*UserNotifications)(nil),
|
||||
)
|
||||
|
||||
|
@ -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)
|
||||
|
||||
go func() {
|
||||
for obj := range DB.All("UserFollows") {
|
||||
channel <- obj.(*UserFollows)
|
||||
if user.IsFollowing(followUserID) {
|
||||
return errors.New("User " + followUserID + " has already been added")
|
||||
}
|
||||
|
||||
close(channel)
|
||||
}()
|
||||
user.FollowIDs = append(user.FollowIDs, followUserID)
|
||||
|
||||
return channel
|
||||
// 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
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
},
|
||||
}
|
||||
}
|
@ -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()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user