-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
269 lines (218 loc) · 6.64 KB
/
user.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package insapp
import (
"time"
"gopkg.in/mgo.v2/bson"
)
var promotions = []string{"", "1STPI", "2STPI",
"3CDTI", "3EII", "3GM", "3GCU", "3GMA", "3INFO", "3SGM", "3SRC",
"4CDTI", "4EII", "4GM", "4GCU", "4GMA", "4INFO", "4SGM", "4SRC",
"5CDTI", "5EII", "5GM", "5GCU", "5GMA", "5INFO", "5SGM", "5SRC",
"STAFF"}
var genders = []string{"", "female", "male"}
// User defines how to model a User
type User struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Name string `json:"name"`
Username string `json:"username"`
Description string `json:"description"`
Email string `json:"email"`
EmailPublic bool `json:"emailpublic"`
Promotion string `json:"promotion"`
Gender string `json:"gender"`
Events []bson.ObjectId `json:"events"`
PostsLiked []bson.ObjectId `json:"postsliked"`
}
// AssociationUser defines how to model an AssociationUser
type AssociationUser struct {
ID bson.ObjectId `bson:"_id,omitempty"`
Username string `json:"username"`
Association bson.ObjectId `json:"association" bson:"association"`
Password string `json:"-"`
Master bool `json:"master"`
Owner bson.ObjectId `json:"owner" bson:"owner,omitempty"`
}
// Users is an array of User
type Users []User
// NewUser creates a new User
func NewUser(username string) *User {
return &User{
Name: "",
Username: username,
Description: "",
Email: "",
EmailPublic: false,
Promotion: "",
Events: []bson.ObjectId{},
PostsLiked: []bson.ObjectId{},
}
}
// AddUser will add the given user from JSON body to the database
func AddUser(user *User) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
db.Insert(user)
var result User
db.Find(bson.M{"username": user.Username}).One(&result)
return result
}
// UpdateUser will update the user link to the given ID,
// with the field of the given user, in the database
func UpdateUser(id bson.ObjectId, user User) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
promotion := ""
for _, promo := range promotions {
if user.Promotion == promo {
promotion = promo
break
}
}
gender := ""
for _, gen := range genders {
if user.Gender == gen {
gender = gen
break
}
}
userID := bson.M{"_id": id}
change := bson.M{"$set": bson.M{
"name": user.Name,
"description": user.Description,
"email": user.Email,
"emailpublic": user.EmailPublic,
"promotion": promotion,
"gender": gender,
}}
db.Update(userID, change)
var result User
db.Find(bson.M{"_id": id}).One(&result)
return result
}
// DeleteUser will delete the given user from the database
func DeleteUser(user User) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
DeleteNotificationsForUser(user.ID)
DeleteNotificationTokenForUser(user.ID)
for _, eventID := range user.Events {
RemoveAttendee(eventID, user.ID, "going")
RemoveAttendee(eventID, user.ID, "notgoing")
RemoveAttendee(eventID, user.ID, "maybe")
}
for _, postID := range user.PostsLiked {
DislikePostWithUser(postID, user.ID)
}
DeleteTagsForUser(user.ID)
DeleteTagsForUserOnEvents(user.ID)
DeleteCommentsForUser(user.ID)
DeleteCommentsForUserOnEvents(user.ID)
db.RemoveId(user.ID)
var result User
db.FindId(user.ID).One(result)
return result
}
// GetAllUser will return an User object from the given ID
func GetAllUser() Users {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
var result Users
db.Find(bson.M{}).All(&result)
return result
}
// GetUser return the User object with the given ID.
func GetUser(id bson.ObjectId) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
var result User
db.FindId(id).One(&result)
return result
}
// LikePost will add the postID to the list of liked post
// of the user linked to the given id
func LikePost(id bson.ObjectId, postID bson.ObjectId) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
userID := bson.M{"_id": id}
change := bson.M{"$addToSet": bson.M{
"postsliked": postID,
}}
db.Update(userID, change)
var result User
db.Find(bson.M{"_id": id}).One(&result)
return result
}
// DislikePost will remove the postID from the list of liked
// post of the user linked to the given id
func DislikePost(id bson.ObjectId, postID bson.ObjectId) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
userID := bson.M{"_id": id}
change := bson.M{"$pull": bson.M{
"postsliked": postID,
}}
db.Update(userID, change)
var result User
db.Find(bson.M{"_id": id}).One(&result)
return result
}
// AddEventToUser will add the eventID to the list
// of the user's event linked to the given id
func AddEventToUser(id bson.ObjectId, eventID bson.ObjectId) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
userID := bson.M{"_id": id}
change := bson.M{"$addToSet": bson.M{
"events": eventID,
}}
db.Update(userID, change)
var result User
db.Find(bson.M{"_id": id}).One(&result)
return result
}
// RemoveEventFromUser will remove the eventID from the list
// of the user's event linked to the given ID.
func RemoveEventFromUser(id bson.ObjectId, eventID bson.ObjectId) User {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
userID := bson.M{"_id": id}
change := bson.M{"$pull": bson.M{
"events": eventID,
}}
db.Update(userID, change)
var result User
db.Find(bson.M{"_id": id}).One(&result)
return result
}
func SearchUser(name string) Users {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
var result Users
db.Find(bson.M{"$or": []interface{}{
bson.M{"username": bson.M{"$regex": bson.RegEx{`^.*` + name + `.*`, "i"}}},
bson.M{"name": bson.M{"$regex": bson.RegEx{`^.*` + name + `.*`, "i"}}},
}}).All(&result)
return result
}
func ReportUser(id bson.ObjectId, reporterID bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("user")
var user User
db.Find(bson.M{"_id": id}).One(&user)
var reporter User
db.Find(bson.M{"_id": reporterID}).One(&reporter)
SendEmail("[email protected]", "Un utilisateur a été reporté sur Insapp",
"Cet utilisateur a été reporté le "+time.Now().String()+
"\n\nReporteur:\n"+reporter.ID.Hex()+"\n"+reporter.Username+"\n"+reporter.Name+
"\n\nSignaler:\n"+user.ID.Hex()+"\n"+user.Username+"\n"+user.Name+"\n"+user.Description)
}