-
Notifications
You must be signed in to change notification settings - Fork 0
/
friend.go
202 lines (180 loc) · 4.88 KB
/
friend.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package v1
import (
"database/sql"
"zxq.co/ripple/rippleapi/common"
)
type friendData struct {
userData
IsMutual bool `json:"is_mutual"`
}
type friendsGETResponse struct {
common.ResponseBase
Friends []friendData `json:"friends"`
}
// FriendsGET is the API request handler for GET /friends.
// It retrieves an user's friends, and whether the friendship is mutual or not.
func FriendsGET(md common.MethodData) common.CodeMessager {
var myFrienders []int
myFriendersRaw, err := md.DB.Query("SELECT user1 FROM users_relationships WHERE user2 = ?", md.ID())
if err != nil {
md.Err(err)
return Err500
}
defer myFriendersRaw.Close()
for myFriendersRaw.Next() {
var i int
err := myFriendersRaw.Scan(&i)
if err != nil {
md.Err(err)
continue
}
myFrienders = append(myFrienders, i)
}
if err := myFriendersRaw.Err(); err != nil {
md.Err(err)
}
myFriendsQuery := `
SELECT
users.id, users.username, users.register_datetime, users.privileges, users.latest_activity,
users_stats.username_aka,
users_stats.country
FROM users_relationships
LEFT JOIN users
ON users_relationships.user2 = users.id
LEFT JOIN users_stats
ON users_relationships.user2=users_stats.id
WHERE users_relationships.user1=?
`
myFriendsQuery += common.Sort(md, common.SortConfiguration{
Allowed: []string{
"id",
"username",
"latest_activity",
},
Default: "users.id asc",
Table: "users",
}) + "\n"
results, err := md.DB.Query(myFriendsQuery+common.Paginate(md.Query("p"), md.Query("l"), 100), md.ID())
if err != nil {
md.Err(err)
return Err500
}
var myFriends []friendData
defer results.Close()
for results.Next() {
newFriend := friendPuts(md, results)
for _, uid := range myFrienders {
if uid == newFriend.ID {
newFriend.IsMutual = true
break
}
}
myFriends = append(myFriends, newFriend)
}
if err := results.Err(); err != nil {
md.Err(err)
}
r := friendsGETResponse{}
r.Code = 200
r.Friends = myFriends
return r
}
func friendPuts(md common.MethodData, row *sql.Rows) (user friendData) {
var err error
err = row.Scan(&user.ID, &user.Username, &user.RegisteredOn, &user.Privileges, &user.LatestActivity, &user.UsernameAKA, &user.Country)
if err != nil {
md.Err(err)
return
}
return
}
type friendsWithResponse struct {
common.ResponseBase
Friends bool `json:"friend"`
Mutual bool `json:"mutual"`
}
// FriendsWithGET checks the current user is friends with the one passed in the request path.
func FriendsWithGET(md common.MethodData) common.CodeMessager {
var r friendsWithResponse
r.Code = 200
uid := common.Int(md.Query("id"))
if uid == 0 {
return r
}
err := md.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user1 = ? AND user2 = ? LIMIT 1), EXISTS(SELECT 1 FROM users_relationships WHERE user2 = ? AND user1 = ? LIMIT 1)", md.ID(), uid, md.ID(), uid).Scan(&r.Friends, &r.Mutual)
if err != sql.ErrNoRows && err != nil {
md.Err(err)
return Err500
}
if !r.Friends {
r.Mutual = false
}
return r
}
// FriendsAddPOST adds an user to the friends.
func FriendsAddPOST(md common.MethodData) common.CodeMessager {
var u struct {
User int `json:"user"`
}
md.Unmarshal(&u)
return addFriend(md, u.User)
}
func addFriend(md common.MethodData, u int) common.CodeMessager {
if md.ID() == u {
return common.SimpleResponse(406, "Just so you know: you can't add yourself to your friends.")
}
if !userExists(md, u) {
return common.SimpleResponse(404, "I'd also like to be friends with someone who doesn't even exist (???), however that's NOT POSSIBLE.")
}
var (
relExists bool
isMutual bool
)
err := md.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM users_relationships WHERE user1 = ? AND user2 = ?), EXISTS(SELECT 1 FROM users_relationships WHERE user2 = ? AND user1 = ?)", md.ID(), u, md.ID(), u).Scan(&relExists, &isMutual)
if err != nil && err != sql.ErrNoRows {
md.Err(err)
return Err500
}
if !relExists {
_, err := md.DB.Exec("INSERT INTO users_relationships(user1, user2) VALUES (?, ?)", md.User.UserID, u)
if err != nil {
md.Err(err)
return Err500
}
}
var r friendsWithResponse
r.Code = 200
r.Friends = true
r.Mutual = isMutual
return r
}
// userExists makes sure an user exists.
func userExists(md common.MethodData, u int) (r bool) {
err := md.DB.QueryRow("SELECT EXISTS(SELECT 1 FROM users WHERE id = ? AND "+
md.User.OnlyUserPublic(true)+")", u).Scan(&r)
if err != nil && err != sql.ErrNoRows {
md.Err(err)
}
return
}
// FriendsDelPOST deletes an user's friend.
func FriendsDelPOST(md common.MethodData) common.CodeMessager {
var u struct {
User int `json:"user"`
}
md.Unmarshal(&u)
return delFriend(md, u.User)
}
func delFriend(md common.MethodData, u int) common.CodeMessager {
_, err := md.DB.Exec("DELETE FROM users_relationships WHERE user1 = ? AND user2 = ?", md.ID(), u)
if err != nil {
md.Err(err)
return Err500
}
r := friendsWithResponse{
Friends: false,
Mutual: false,
}
r.Code = 200
return r
}