/
comment.go
240 lines (206 loc) · 6.03 KB
/
comment.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
package insapp
import (
"errors"
"time"
"gopkg.in/mgo.v2/bson"
)
// Comment defines how to model a Comment of a Post
type Comment struct {
ID bson.ObjectId `bson:"_id,omitempty"`
User bson.ObjectId `json:"user"`
Content string `json:"content"`
Date time.Time `json:"date"`
Tags Tags `json:"tags"`
}
// Comments is an array of Comment
type Comments []Comment
type Tag struct {
ID bson.ObjectId `bson:"_id,omitempty"`
User string `json:"user"`
Name string `json:"name"`
}
type Tags []Tag
// CommentPost will add the given comment object to the
// list of comments of the post linked to the given id
func CommentPost(id bson.ObjectId, comment Comment) Post {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("post")
postID := bson.M{"_id": id}
change := bson.M{"$addToSet": bson.M{
"comments": comment,
}}
_ = db.Update(postID, change)
var post Post
_ = db.Find(bson.M{"_id": id}).One(&post)
return post
}
// UncommentPost will remove the given comment object from the
// list of comments of the post linked to the given id
func UncommentPost(id bson.ObjectId, commentID bson.ObjectId) Post {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("post")
DeleteNotificationsForComment(commentID)
postID := bson.M{"_id": id}
change := bson.M{"$pull": bson.M{
"comments": bson.M{"_id": commentID},
}}
_ = db.Update(postID, change)
var post Post
_ = db.Find(bson.M{"_id": id}).One(&post)
return post
}
func CommentEvent(id bson.ObjectId, comment Comment) Event {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("event")
eventID := bson.M{"_id": id}
change := bson.M{"$addToSet": bson.M{
"comments": comment,
}}
_ = db.Update(eventID, change)
var event Event
_ = db.Find(bson.M{"_id": id}).One(&event)
return event
}
func UncommentEvent(id bson.ObjectId, commentID bson.ObjectId) Event {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("event")
DeleteNotificationsForComment(commentID)
eventID := bson.M{"_id": id}
change := bson.M{"$pull": bson.M{
"comments": bson.M{"_id": commentID},
}}
_ = db.Update(eventID, change)
var event Event
_ = db.Find(bson.M{"_id": id}).One(&event)
return event
}
func ReportComment(id bson.ObjectId, commentID bson.ObjectId, reporterID bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("post")
var post Post
_ = db.Find(bson.M{"_id": id}).One(&post)
db = session.DB("insapp").C("user")
var reporter User
_ = db.Find(bson.M{"_id": reporterID}).One(&reporter)
for _, comment := range post.Comments {
if comment.ID == commentID {
var sender User
db = session.DB("insapp").C("user")
_ = db.Find(bson.M{"_id": comment.User}).One(&sender)
SendEmail("aeir@insa-rennes.fr", "Un commentaire a été reporté sur Insapp",
"Ce commentaire a été reporté le "+time.Now().String()+
"\n\nReporteur:\n"+reporter.ID.Hex()+"\n"+reporter.Username+
"\n\nCommentaire:\n"+comment.ID.Hex()+"\n"+comment.Content+
"\n\nPost:\n"+post.Title+
"\n\nUser:\n"+sender.ID.Hex()+"\n"+sender.Username+"\n"+sender.Name)
}
}
}
func GetComment(postID bson.ObjectId, id bson.ObjectId) (Comment, error) {
post := GetPost(postID)
for _, comment := range post.Comments {
if comment.ID == id {
return comment, nil
}
}
return Comment{}, errors.New("no comment found")
}
func GetCommentForEvent(eventID bson.ObjectId, id bson.ObjectId) (Comment, error) {
event := GetEvent(eventID)
for _, comment := range event.Comments {
if comment.ID == id {
return comment, nil
}
}
return Comment{}, errors.New("no comment found")
}
func getCommentforUser(id bson.ObjectId, userID bson.ObjectId) []bson.ObjectId {
post := GetPost(id)
comments := post.Comments
var results []bson.ObjectId
for _, comment := range comments {
if comment.User == userID {
results = append(results, comment.ID)
}
}
return results
}
func getCommentForUserOnEvent(id bson.ObjectId, userID bson.ObjectId) []bson.ObjectId {
event := GetEvent(id)
comments := event.Comments
var results []bson.ObjectId
for _, comment := range comments {
if comment.User == userID {
results = append(results, comment.ID)
}
}
return results
}
func DeleteCommentsForUser(userID bson.ObjectId) {
posts := GetLatestPosts(100)
for _, post := range posts {
comments := getCommentforUser(post.ID, userID)
for _, commentID := range comments {
UncommentPost(post.ID, commentID)
}
}
}
func DeleteCommentsForUserOnEvents(userID bson.ObjectId) {
events := GetEvents()
for _, event := range events {
comments := getCommentForUserOnEvent(event.ID, userID)
for _, commentID := range comments {
UncommentEvent(event.ID, commentID)
}
}
}
func DeleteTagsForUserOnEvents(userID bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("event")
events := GetEvents()
for _, event := range events {
comments := event.Comments
finalComments := Comments{}
for _, comment := range comments {
tags := comment.Tags
finalTags := Tags{}
for _, tag := range tags {
if tag.User != userID.Hex() {
finalTags = append(finalTags, tag)
}
}
comment.Tags = finalTags
finalComments = append(finalComments, comment)
}
_ = db.Update(bson.M{"_id": event.ID}, bson.M{"$set": bson.M{"comments": finalComments}})
}
}
func DeleteTagsForUser(userID bson.ObjectId) {
session := GetMongoSession()
defer session.Close()
db := session.DB("insapp").C("post")
var posts Posts
_ = db.Find(bson.M{}).All(&posts)
for _, post := range posts {
comments := post.Comments
finalComments := Comments{}
for _, comment := range comments {
tags := comment.Tags
finalTags := Tags{}
for _, tag := range tags {
if tag.User != userID.Hex() {
finalTags = append(finalTags, tag)
}
}
comment.Tags = finalTags
finalComments = append(finalComments, comment)
}
_ = db.Update(bson.M{"_id": post.ID}, bson.M{"$set": bson.M{"comments": finalComments}})
}
}