This repository has been archived by the owner on Jun 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
UserHelper.go
166 lines (123 loc) · 3.44 KB
/
UserHelper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package arn
import (
"errors"
"sort"
"github.com/aerogo/nano"
)
// GetUser fetches the user with the given ID from the database.
func GetUser(id UserID) (*User, error) {
obj, err := DB.Get("User", id)
if err != nil {
return nil, err
}
return obj.(*User), nil
}
// GetUserByNick fetches the user with the given nick from the database.
func GetUserByNick(nick string) (*User, error) {
obj, err := DB.Get("NickToUser", nick)
if err != nil {
return nil, err
}
userID := obj.(*NickToUser).UserID
user, err := GetUser(userID)
return user, err
}
// GetUserByEmail fetches the user with the given email from the database.
func GetUserByEmail(email string) (*User, error) {
if email == "" {
return nil, errors.New("Email is empty")
}
obj, err := DB.Get("EmailToUser", email)
if err != nil {
return nil, err
}
userID := obj.(*EmailToUser).UserID
user, err := GetUser(userID)
return user, err
}
// GetUserByFacebookID fetches the user with the given Facebook ID from the database.
func GetUserByFacebookID(facebookID string) (*User, error) {
obj, err := DB.Get("FacebookToUser", facebookID)
if err != nil {
return nil, err
}
userID := obj.(*FacebookToUser).UserID
user, err := GetUser(userID)
return user, err
}
// GetUserByTwitterID fetches the user with the given Twitter ID from the database.
func GetUserByTwitterID(twitterID string) (*User, error) {
obj, err := DB.Get("TwitterToUser", twitterID)
if err != nil {
return nil, err
}
userID := obj.(*TwitterToUser).UserID
user, err := GetUser(userID)
return user, err
}
// GetUserByGoogleID fetches the user with the given Google ID from the database.
func GetUserByGoogleID(googleID string) (*User, error) {
obj, err := DB.Get("GoogleToUser", googleID)
if err != nil {
return nil, err
}
userID := obj.(*GoogleToUser).UserID
user, err := GetUser(userID)
return user, err
}
// StreamUsers returns a stream of all users.
func StreamUsers() <-chan *User {
channel := make(chan *User, nano.ChannelBufferSize)
go func() {
for obj := range DB.All("User") {
channel <- obj.(*User)
}
close(channel)
}()
return channel
}
// AllUsers returns a slice of all users.
func AllUsers() ([]*User, error) {
all := make([]*User, 0, DB.Collection("User").Count())
for obj := range StreamUsers() {
all = append(all, obj)
}
return all, nil
}
// FilterUsers filters all users by a custom function.
func FilterUsers(filter func(*User) bool) []*User {
var filtered []*User
for obj := range StreamUsers() {
if filter(obj) {
filtered = append(filtered, obj)
}
}
return filtered
}
// SortUsersLastSeenFirst sorts a list of users by their last seen date.
func SortUsersLastSeenFirst(users []*User) {
sort.Slice(users, func(i, j int) bool {
return users[i].LastSeen > users[j].LastSeen
})
}
// SortUsersLastSeenLast sorts a list of users by their last seen date.
func SortUsersLastSeenLast(users []*User) {
sort.Slice(users, func(i, j int) bool {
return users[i].LastSeen < users[j].LastSeen
})
}
// SortUsersFollowers sorts a list of users by their number of followers.
func SortUsersFollowers(users []*User) {
followCount := UserFollowerCountMap()
sort.Slice(users, func(i, j int) bool {
if users[i].HasAvatar() != users[j].HasAvatar() {
return users[i].HasAvatar()
}
followersA := followCount[users[i].ID]
followersB := followCount[users[j].ID]
if followersA == followersB {
return users[i].Nick < users[j].Nick
}
return followersA > followersB
})
}