forked from jimmykuu/gopher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
542 lines (457 loc) · 12.6 KB
/
models.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/*
和MongoDB对应的struct
*/
package gopher
import (
"errors"
"fmt"
"html/template"
"strings"
"time"
"github.com/gorilla/sessions"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
const (
TypeTopic = 'T'
TypeArticle = 'A'
TypeSite = 'S'
TypePackage = 'P'
DefaultAvatar = "gopher_teal.jpg"
ADS = "ads"
ARTICLE_CATEGORIES = "articlecategories"
BOOKS = "books"
COMMENTS = "comments"
CONTENTS = "contents"
NODES = "nodes"
PACKAGES = "packages"
PACKAGE_CATEGORIES = "packagecategories"
LINK_EXCHANGES = "link_exchanges"
SITE_CATEGORIES = "sitecategories"
SITES = "sites"
STATUS = "status"
USERS = "users"
CODE = "code"
DOWNLOADED_PACKAGES = "downloaded_packages"
GITHUB_COM = "github.com"
)
var colors = []string{"#FFCC66", "#66CCFF", "#6666FF", "#FF8000", "#0080FF", "#008040", "#008080"}
//主题id和评论id,用于定位到专门的评论
type At struct {
User string
ContentId string
CommentId string
}
//主题id和主题标题
type Reply struct {
ContentId string
TopicTitle string
}
//收藏的话题
type CollectTopic struct {
TopicId string
TimeCollected time.Time
}
func (ct *CollectTopic) Topic(db *mgo.Database) *Topic {
c := db.C(CONTENTS)
var topic Topic
err := c.Find(bson.M{"_id": bson.ObjectIdHex(ct.TopicId), "content.type": TypeTopic}).One(&topic)
if err != nil {
panic(err)
return nil
}
return &topic
}
// 用户
type User struct {
Id_ bson.ObjectId `bson:"_id"`
Username string //如果关联社区帐号,默认使用社区的用户名
Password string
Salt string `bson:"salt"`
Email string //如果关联社区帐号,默认使用社区的邮箱
Avatar string
Website string
Location string
Tagline string
Bio string
Twitter string
Weibo string // 微博
GitHubUsername string // GitHub 用户名
JoinedAt time.Time // 加入时间
Follow []string
Fans []string
RecentReplies []Reply //存储的是最近回复的主题的objectid.hex
RecentAts []At //存储的是最近评论被AT的主题的objectid.hex
TopicsCollected []CollectTopic //用户收藏的topic数组
IsSuperuser bool // 是否是超级用户
IsActive bool
IsBlocked bool `bson:"is_blocked"` // 是否被禁止发帖,回帖
ValidateCode string
ResetCode string
Index int // 第几个加入社区
AccountRef string //帐号关联的社区
IdRef string //关联社区的帐号
LinkRef string //关联社区的主页链接
OrgRef string //关联社区的组织或者公司
PictureRef string //关联社区的头像链接
Provider string //关联社区名称,比如 github.com
}
// 增加最近被@
func (u *User) AtBy(c *mgo.Collection, username, contentIdStr, commentIdStr string) error {
if username == "" || contentIdStr == "" || commentIdStr == "" {
return errors.New("string parameters can not be empty string")
}
if len(u.RecentAts) == 0 {
var user User
err := c.Find(bson.M{"username": u.Username}).One(&user)
if err != nil {
return err
}
u = &user
}
u.RecentAts = append(u.RecentAts, At{username, contentIdStr, commentIdStr})
err := c.Update(bson.M{"username": u.Username}, bson.M{"$set": bson.M{"recentats": u.RecentAts}})
if err != nil {
return err
}
return nil
}
// 是否是默认头像
func (u *User) IsDefaultAvatar(avatar string) bool {
filename := u.Avatar
if filename == "" {
filename = DefaultAvatar
}
return filename == avatar
}
// 插入github注册的用户
func (u *User) GetGithubValues(session *sessions.Session) {
u.Website = session.Values[GITHUB_LINK].(string)
u.GitHubUsername = session.Values[GITHUB_ID].(string)
u.AccountRef = session.Values[GITHUB_NAME].(string)
u.IdRef = session.Values[GITHUB_ID].(string)
u.LinkRef = session.Values[GITHUB_LINK].(string)
u.OrgRef = session.Values[GITHUB_ORG].(string)
u.PictureRef = session.Values[GITHUB_PICTURE].(string)
u.Provider = session.Values[GITHUB_PROVIDER].(string)
}
// 检查密码是否正确
func (u User) CheckPassword(password string) bool {
return u.Password == encryptPassword(password, u.Salt)
}
// 删除通过session传的默认信息
func deleteGithubValues(session *sessions.Session) {
// 删除session传过来的默认信息
delete(session.Values, GITHUB_EMAIL)
delete(session.Values, GITHUB_ID)
delete(session.Values, GITHUB_LINK)
delete(session.Values, GITHUB_NAME)
delete(session.Values, GITHUB_ORG)
delete(session.Values, GITHUB_PICTURE)
delete(session.Values, GITHUB_PROVIDER)
}
// 头像的图片地址
func (u *User) AvatarImgSrc(size int) string {
// 如果没有设置头像,用默认头像
if u.Avatar == "" {
return fmt.Sprintf("http://identicon.relucks.org/%s?size=%d", u.Username, size)
}
return fmt.Sprintf("http://77fkk5.com1.z0.glb.clouddn.com/avatar/%s?imageView2/2/w/%d/h/%d/q/100", u.Avatar, size, size)
}
// 用户发表的最近10个主题
func (u *User) LatestTopics(db *mgo.Database) *[]Topic {
c := db.C("contents")
var topics []Topic
c.Find(bson.M{"content.createdby": u.Id_, "content.type": TypeTopic}).Sort("-content.createdat").Limit(10).All(&topics)
return &topics
}
// 用户的最近10个回复
func (u *User) LatestReplies(db *mgo.Database) *[]Comment {
c := db.C("comments")
var replies []Comment
c.Find(bson.M{"createdby": u.Id_, "type": TypeTopic}).Sort("-createdat").Limit(10).All(&replies)
return &replies
}
// 是否被某人关注
func (u *User) IsFollowedBy(who string) bool {
for _, username := range u.Fans {
if username == who {
return true
}
}
return false
}
// 是否关注某人
func (u *User) IsFans(who string) bool {
for _, username := range u.Follow {
if username == who {
return true
}
}
return false
}
// getUserByName
func getUserByName(c *mgo.Collection, name string) (*User, error) {
u := new(User)
err := c.Find(bson.M{"username": name}).One(u)
if err != nil {
return nil, err
}
return u, nil
}
// 节点
type Node struct {
Id_ bson.ObjectId `bson:"_id"`
Id string
Name string
Description string
TopicCount int
}
// 通用的内容
type Content struct {
Id_ bson.ObjectId // 同外层Id_
Type int
Title string
Markdown string
Html template.HTML
CommentCount int
Hits int // 点击数量
CreatedAt time.Time
CreatedBy bson.ObjectId
UpdatedAt time.Time
UpdatedBy string
}
func (c *Content) Creater(db *mgo.Database) *User {
c_ := db.C(USERS)
user := User{}
c_.Find(bson.M{"_id": c.CreatedBy}).One(&user)
return &user
}
func (c *Content) Updater(db *mgo.Database) *User {
if c.UpdatedBy == "" {
return nil
}
c_ := db.C(USERS)
user := User{}
c_.Find(bson.M{"_id": bson.ObjectIdHex(c.UpdatedBy)}).One(&user)
return &user
}
func (c *Content) Comments(db *mgo.Database) *[]Comment {
c_ := db.C("comments")
var comments []Comment
c_.Find(bson.M{"contentid": c.Id_}).Sort("createdat").All(&comments)
return &comments
}
// 只能收藏未收藏过的主题
func (c *Content) CanCollect(username string, db *mgo.Database) bool {
var user User
c_ := db.C(USERS)
err := c_.Find(bson.M{"username": username}).One(&user)
if err != nil {
return false
}
has := false
for _, v := range user.TopicsCollected {
if v.TopicId == c.Id_.Hex() {
has = true
}
}
return !has
}
// 是否有权编辑主题
func (c *Content) CanEdit(username string, db *mgo.Database) bool {
var user User
c_ := db.C(USERS)
err := c_.Find(bson.M{"username": username}).One(&user)
if err != nil {
return false
}
if user.IsSuperuser {
return true
}
return c.CreatedBy == user.Id_
}
func (c *Content) CanDelete(username string, db *mgo.Database) bool {
var user User
c_ := db.C("users")
err := c_.Find(bson.M{"username": username}).One(&user)
if err != nil {
return false
}
return user.IsSuperuser
}
// 主题
type Topic struct {
Content
Id_ bson.ObjectId `bson:"_id"`
NodeId bson.ObjectId
LatestReplierId string
LatestRepliedAt time.Time
IsTop bool `bson:"is_top"` // 置顶
}
// 主题所属节点
func (t *Topic) Node(db *mgo.Database) *Node {
c := db.C("nodes")
node := Node{}
c.Find(bson.M{"_id": t.NodeId}).One(&node)
return &node
}
// 主题链接
func (t *Topic) Link(id bson.ObjectId) string {
return "http://golangtc.com/t/" + id.Hex()
}
//格式化日期
func (t *Topic) Format(tm time.Time) string {
return tm.Format(time.RFC822)
}
// 主题的最近的一个回复
func (t *Topic) LatestReplier(db *mgo.Database) *User {
if t.LatestReplierId == "" {
return nil
}
c := db.C("users")
user := User{}
err := c.Find(bson.M{"_id": bson.ObjectIdHex(t.LatestReplierId)}).One(&user)
if err != nil {
return nil
}
return &user
}
// 状态,MongoDB中只存储一个状态
type Status struct {
Id_ bson.ObjectId `bson:"_id"`
UserCount int
TopicCount int
ReplyCount int
UserIndex int
}
// 站点分类
type SiteCategory struct {
Id_ bson.ObjectId `bson:"_id"`
Name string
}
// 分类下的所有站点
func (sc *SiteCategory) Sites(db *mgo.Database) *[]Site {
var sites []Site
c := db.C("contents")
c.Find(bson.M{"categoryid": sc.Id_, "content.type": TypeSite}).All(&sites)
return &sites
}
// 站点
type Site struct {
Content
Id_ bson.ObjectId `bson:"_id"`
Url string
CategoryId bson.ObjectId
}
func (s *Site) TrimUrlHttpPrefix() string {
return strings.TrimPrefix(s.Url, "http://")
}
// 文章分类
type ArticleCategory struct {
Id_ bson.ObjectId `bson:"_id"`
Name string
}
// 文章
type Article struct {
Content
Id_ bson.ObjectId `bson:"_id"`
CategoryId bson.ObjectId
OriginalSource string
OriginalUrl string
}
// 主题所属类型
func (a *Article) Category(db *mgo.Database) *ArticleCategory {
c := db.C("articlecategories")
category := ArticleCategory{}
c.Find(bson.M{"_id": a.CategoryId}).One(&category)
return &category
}
// 评论
type Comment struct {
Id_ bson.ObjectId `bson:"_id"`
Type int
ContentId bson.ObjectId
Markdown string
Html template.HTML
CreatedBy bson.ObjectId
CreatedAt time.Time
UpdatedBy string
UpdatedAt time.Time
}
// 评论人
func (c *Comment) Creater(db *mgo.Database) *User {
c_ := db.C("users")
user := User{}
c_.Find(bson.M{"_id": c.CreatedBy}).One(&user)
return &user
}
// 是否有权删除评论,管理员和作者可删除
func (c *Comment) CanDeleteOrEdit(username string, db *mgo.Database) bool {
if c.Creater(db).Username == username {
return true
}
var user User
c_ := db.C("users")
err := c_.Find(bson.M{"username": username}).One(&user)
if err != nil {
return false
}
return user.IsSuperuser
}
// 主题
func (c *Comment) Topic(db *mgo.Database) *Topic {
// 内容
var topic Topic
c_ := db.C("contents")
c_.Find(bson.M{"_id": c.ContentId, "content.type": TypeTopic}).One(&topic)
return &topic
}
// 包分类
type PackageCategory struct {
Id_ bson.ObjectId `bson:"_id"`
Id string
Name string
PackageCount int
}
type Package struct {
Content
Id_ bson.ObjectId `bson:"_id"`
CategoryId bson.ObjectId
Url string
}
func (p *Package) Category(db *mgo.Database) *PackageCategory {
category := PackageCategory{}
c := db.C("packagecategories")
c.Find(bson.M{"_id": p.CategoryId}).One(&category)
return &category
}
type LinkExchange struct {
Id_ bson.ObjectId `bson:"_id"`
Name string `bson:"name"`
URL string `bson:"url"`
Description string `bson:"description"`
Logo string `bson:"logo"`
IsOnHome bool `bson:"is_on_home"` // 是否在首页右侧显示
IsOnBottom bool `bson:"is_on_bottom"` // 是否在底部显示
}
type AD struct {
Id_ bson.ObjectId `bson:"_id"`
Position string `bson:"position"`
Name string `bson:"name"`
Code string `bson:"code"`
Index int `bons:"index"`
}
type Book struct {
Id_ bson.ObjectId `bson:"_id"`
Title string `bson:"title"`
Cover string `bson:"cover"`
Author string `bson:"author"`
Translator string `bson:"translator"`
Pages int `bson:"pages"`
Introduction string `bson:"introduction"`
Publisher string `bson:"publisher"`
Language string `bson:"language"`
PublicationDate string `bson:"publication_date"`
ISBN string `bson:"isbn"`
}