forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.go
324 lines (262 loc) · 7.51 KB
/
member.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
会员
*/
package gopher
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
at = "at"
reply = "reply"
)
func returnJson(w http.ResponseWriter, input interface{}) {
js, err := json.Marshal(input)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
// 显示最新加入的会员
// URL: /members
func membersHandler(handler *Handler) {
c := handler.DB.C(USERS)
var newestMembers []User
c.Find(nil).Sort("-joinedat").Limit(40).All(&newestMembers)
membersCount, _ := c.Find(nil).Count()
handler.renderTemplate("member/index.html", BASE, map[string]interface{}{
"newestMembers": newestMembers,
"membersCount": membersCount,
"active": "members",
})
}
// 显示所有会员
// URL: /members/all
func allMembersHandler(handler *Handler) {
page, err := getPage(handler.Request)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
c := handler.DB.C(USERS)
pagination := NewPagination(c.Find(nil).Sort("joinedat"), "/members/all", 40)
var members []User
query, err := pagination.Page(page)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
query.(*mgo.Query).All(&members)
handler.renderTemplate("member/list.html", BASE, map[string]interface{}{
"members": members,
"active": "members",
"pagination": pagination,
"page": page,
})
}
// URL: /member/{username}
// 显示用户信息
func memberInfoHandler(handler *Handler) {
vars := mux.Vars(handler.Request)
username := vars["username"]
c := handler.DB.C(USERS)
user := User{}
err := c.Find(bson.M{"username": username}).One(&user)
if err != nil {
message(handler, "会员未找到", "会员未找到", "error")
return
}
handler.renderTemplate("account/info.html", BASE, map[string]interface{}{
"user": user,
"active": "members",
})
}
// URL: /member/{username}/collect/
// 用户收集的topic
func memberTopicsCollectedHandler(handler *Handler) {
page, err := getPage(handler.Request)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
}
vars := mux.Vars(handler.Request)
username := vars["username"]
c := handler.DB.C(USERS)
user := User{}
err = c.Find(bson.M{"username": username}).One(&user)
if err != nil {
message(handler, "会员未找到", "会员未找到", "error")
}
pagination := NewPagination(user.TopicsCollected, "/member/"+username+"/collect", 3)
collects, err := pagination.Page(page)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
}
handler.renderTemplate("account/collects.html", BASE, map[string]interface{}{
"user": user,
"collects": collects,
"pagination": pagination,
"page": page,
"active": "members",
})
}
// URL: /member/{username}/topics
// 用户发表的所有主题
func memberTopicsHandler(handler *Handler) {
page, err := getPage(handler.Request)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
vars := mux.Vars(handler.Request)
username := vars["username"]
c := handler.DB.C(USERS)
user := User{}
err = c.Find(bson.M{"username": username}).One(&user)
if err != nil {
message(handler, "会员未找到", "会员未找到", "error")
return
}
c = handler.DB.C("contents")
pagination := NewPagination(c.Find(bson.M{"content.createdby": user.Id_, "content.type": TypeTopic}).Sort("-latestrepliedat"), "/member/"+username+"/topics", PerPage)
var topics []Topic
query, err := pagination.Page(page)
if err != nil {
message(handler, "没有找到页面", "没有找到页面", "error")
return
}
query.(*mgo.Query).All(&topics)
handler.renderTemplate("account/topics.html", BASE, map[string]interface{}{
"user": user,
"topics": topics,
"pagination": pagination,
"page": page,
"active": "members",
})
}
// /member/{username}/replies
// 用户的所有回复
func memberRepliesHandler(handler *Handler) {
page, err := getPage(handler.Request)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
vars := mux.Vars(handler.Request)
username := vars["username"]
c := handler.DB.C(USERS)
user := User{}
err = c.Find(bson.M{"username": username}).One(&user)
if err != nil {
message(handler, "会员未找到", "会员未找到", "error")
return
}
if err != nil {
message(handler, "没有找到页面", "没有找到页面", "error")
return
}
var replies []Comment
c = handler.DB.C(COMMENTS)
pagination := NewPagination(c.Find(bson.M{"createdby": user.Id_, "type": TypeTopic}).Sort("-createdat"), "/member/"+username+"/replies", PerPage)
query, err := pagination.Page(page)
query.(*mgo.Query).All(&replies)
handler.renderTemplate("account/replies.html", BASE, map[string]interface{}{
"user": user,
"pagination": pagination,
"page": page,
"replies": replies,
"active": "members",
})
}
// URL: /member/{username}/clear/{t}
func memmberNewsClear(handler *Handler) {
vars := mux.Vars(handler.Request)
username := vars["username"]
t := vars["t"]
res := map[string]interface{}{}
user, ok := currentUser(handler)
if ok {
if user.Username == username {
var user User
c := handler.DB.C(USERS)
c.Find(bson.M{"username": username}).One(&user)
if t == at {
user.RecentAts = user.RecentAts[:0]
c.Update(bson.M{"username": username}, bson.M{"$set": bson.M{"recentats": user.RecentAts}})
res["status"] = true
} else if t == reply {
user.RecentReplies = user.RecentReplies[:0]
c.Update(bson.M{"username": username}, bson.M{"$set": bson.M{"recentreplies": user.RecentReplies}})
res["status"] = true
} else {
res["status"] = false
res["error"] = "Wrong Type"
}
} else {
res["status"] = false
res["error"] = "Need authentication"
}
} else {
res["status"] = false
res["error"] = "No such User"
}
returnJson(handler.ResponseWriter, res)
}
// URL: /member/{username}/comments
func memmberNewsHandler(handler *Handler) {
page, err := getPage(handler.Request)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
vars := mux.Vars(handler.Request)
username := vars["username"]
c := handler.DB.C(USERS)
user := User{}
err = c.Find(bson.M{"username": username}).One(&user)
if err != nil {
message(handler, "会员未找到", "会员未找到", "error")
return
}
handler.renderTemplate("account/news.html", BASE, map[string]interface{}{
"user": user,
"page": page,
"comments": user.RecentReplies,
"ats": user.RecentAts,
"active": "members",
})
}
// URL: /member/{username}/comments
func memberAtsHandler(handler *Handler) {
return
}
// URL: /members/city/{cityName}
// 同城会员
func membersInTheSameCityHandler(handler *Handler) {
page, err := getPage(handler.Request)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
cityName := mux.Vars(handler.Request)["cityName"]
c := handler.DB.C(USERS)
pagination := NewPagination(c.Find(bson.M{"location": cityName}).Sort("joinedat"), "/members/city/"+cityName, 40)
var members []User
query, err := pagination.Page(page)
if err != nil {
message(handler, "页码错误", "页码错误", "error")
return
}
query.(*mgo.Query).All(&members)
handler.renderTemplate("member/list.html", BASE, map[string]interface{}{
"members": members,
"active": "members",
"pagination": pagination,
"page": page,
"city": cityName,
})
}