/
friendship_handlers.go
244 lines (194 loc) · 5.07 KB
/
friendship_handlers.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package v2
import (
"net/http"
"partisan/auth"
"partisan/db"
"partisan/logger"
"partisan/matcher"
"partisan/models.v2/friendships"
"partisan/models.v2/notifications"
"partisan/models.v2/users"
"github.com/gin-gonic/gin"
)
func FriendshipIndex(c *gin.Context) {
db := db.GetDB(c)
user, err := auth.CurrentUser(c)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
}
fs, err := friendships.ListConfirmedByUserID(user.ID, db)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
friendIDs := collectUserIDs(user.ID, fs)
friends, err := users.ListByIDs(friendIDs, db)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
collectUsers(user, &fs, friends)
c.JSON(http.StatusOK, gin.H{"friendships": fs})
}
func FriendshipShow(c *gin.Context) {
db := db.GetDB(c)
user, err := auth.CurrentUser(c)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
}
friendID := c.Param("friend_id")
f, err := friendships.GetByUserIDs(user.ID, friendID, db)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
c.JSON(http.StatusOK, gin.H{"friendship": f})
}
func FriendshipCreate(c *gin.Context) {
db := db.GetDB(c)
user, err := auth.CurrentUser(c)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
}
var binding friendships.CreatorBinding
if err := c.BindJSON(&binding); err != nil {
c.AbortWithError(http.StatusNotAcceptable, ErrBinding)
return
}
if friendships.Exists(user.ID, binding.FriendID, db) {
c.AbortWithError(http.StatusConflict, ErrAlreadyExists)
return
}
friend, err := users.GetByID(binding.FriendID, db)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
// the friend is not looking for enemies, disallow if
// match % isn't high enough
if friend.LookingFor < 4 && !isPartisan(user, friend) {
m, _ := matcher.Match(user.PoliticalMap, friend.PoliticalMap)
if m < 0.75 {
c.AbortWithError(http.StatusForbidden, ErrEnemy)
return
}
}
f, errs := friendships.New(user.ID, binding)
if len(errs) > 0 {
c.AbortWithError(http.StatusNotAcceptable, errs)
return
}
if err := db.Save(&f).Error; err != nil {
c.AbortWithError(http.StatusNotAcceptable, err)
return
}
n, errs := notifications.New(user.ID, binding.FriendID, f)
if len(errs) == 0 {
db.Save(&n)
if pn, err := n.NewPushNotification(db); err == nil {
pushNotif := pn.Prepare()
pushClient.Send(pushNotif)
} else {
logger.Error.Println("Error sending push notif:", err)
}
}
c.JSON(http.StatusCreated, gin.H{"friendship": f})
}
func FriendshipUpdate(c *gin.Context) {
db := db.GetDB(c)
user, err := auth.CurrentUser(c)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
}
friendID := c.Param("friend_id")
f, err := friendships.GetByUserIDs(user.ID, friendID, db)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
var binding friendships.UpdaterBinding
if err := c.BindJSON(&binding); err != nil {
c.AbortWithError(http.StatusNotAcceptable, ErrBinding)
return
}
if errs := f.Update(binding); len(errs) > 0 {
c.AbortWithError(http.StatusNotAcceptable, errs)
return
}
if err := db.Save(&f).Error; err != nil {
c.AbortWithError(http.StatusNotAcceptable, err)
return
}
n, errs := notifications.New(user.ID, friendID, f)
if len(errs) == 0 {
db.Save(&n)
if pn, err := n.NewPushNotification(db); err == nil {
pushNotif := pn.Prepare()
pushClient.Send(pushNotif)
} else {
logger.Error.Println("Error sending push notif:", err)
}
}
c.JSON(http.StatusOK, gin.H{"friendship": f})
}
func FriendshipDestroy(c *gin.Context) {
db := db.GetDB(c)
user, err := auth.CurrentUser(c)
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
return
}
friendID := c.Param("user_id")
f, err := friendships.GetByUserIDs(user.ID, friendID, db)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
if !f.CanDelete(user.ID) {
c.AbortWithError(http.StatusUnauthorized, err)
return
}
if err := db.Delete(&f).Error; err != nil {
c.AbortWithError(http.StatusNotAcceptable, err)
return
}
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
}
func collectUserIDs(userID string, fs friendships.Friendships) (ids []string) {
for _, f := range fs {
if f.UserID == userID {
ids = append(ids, f.FriendID)
}
if f.FriendID == userID {
ids = append(ids, f.UserID)
}
}
return
}
func collectUsers(user users.User, fs *friendships.Friendships, friends []users.User) {
xfs := []friendships.Friendship(*fs)
for i := range xfs {
for _, friend := range friends {
if xfs[i].UserID == friend.ID || xfs[i].FriendID == friend.ID {
xfs[i].Friend = friend
if match, err := matcher.Match(user.PoliticalMap, friend.PoliticalMap); err == nil {
xfs[i].Match = float64(int(match*1000)) / 10
}
}
}
}
*fs = friendships.Friendships(xfs)
}
// We wanna allow everyone to be able to friend partisan
func isPartisan(us ...users.User) bool {
for _, u := range us {
if u.Email == "jon@partisan.io" {
return true
}
}
return false
}