-
Notifications
You must be signed in to change notification settings - Fork 7
/
comment.go
143 lines (111 loc) · 3.18 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
package feed
import (
"github.com/tryanzu/core/deps"
"gopkg.in/mgo.v2/bson"
"html"
"time"
"unicode/utf8"
)
type Comment struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"id,omitempty"`
PostId bson.ObjectId `bson:"post_id" json:"post_id"`
UserId bson.ObjectId `bson:"user_id" json:"user_id"`
Votes Votes `bson:"votes" json:"votes"`
User interface{} `bson:"-" json:"author,omitempty"`
Position int `bson:"position" json:"position"`
Liked int `bson:"-" json:"liked,omitempty"`
Content string `bson:"content" json:"content"`
Chosen bool `bson:"chosen,omitempty" json:"chosen,omitempty"`
Created time.Time `bson:"created_at" json:"created_at"`
// Runtime generated pointers
post *Post
}
type Comments struct {
Count int `bson:"count" json:"count"`
Total int `bson:"-" json:"total"`
Answer *Comment `bson:"-" json:"answer,omitempty"`
Set []*Comment `bson:"-" json:"set"`
}
func (self *Comment) SetDI(o *Post) {
self.post = o
}
func (self *Comment) GetContent() string {
return self.Content
}
func (self *Comment) UpdateContent(c string) bool {
self.Content = c
return true
}
func (self *Comment) OnParseFilterFinished(module string) bool {
return true
}
func (self *Comment) OnParseFinished() bool {
return true
}
func (self *Comment) GetParseableMeta() map[string]interface{} {
p := self.GetPost()
return map[string]interface{}{
"id": self.Id,
"type": "comment",
"position": self.Position,
"owner_id": self.UserId,
"comment": self,
"post": map[string]interface{}{
"id": p.Id,
"user_id": p.UserId,
"slug": p.Slug,
"title": p.Title,
},
}
}
func (self *Comment) GetPost() *Post {
return self.post
}
func (self *Comment) MarkAsAnswer() {
// Get database instance
database := deps.Container.Mgo()
// Update straight forward
err := database.C("comments").Update(bson.M{"_id": self.Id}, bson.M{"$set": bson.M{"chosen": true}})
if err != nil {
panic(err)
}
err = database.C("posts").Update(bson.M{"_id": self.PostId}, bson.M{"$set": bson.M{"solved": true}})
if err != nil {
panic(err)
}
}
func (self *Comment) Delete() {
// Get database instance
database := deps.Container.Mgo()
// Update straight forward
err := database.C("comments").Update(bson.M{"_id": self.Id}, bson.M{"$set": bson.M{"deleted_at": time.Now()}})
if err != nil {
panic(err)
}
// Decrement count of comments
err = database.C("posts").Update(bson.M{"_id": self.PostId}, bson.M{"$inc": bson.M{"comments.count": -1}})
if err != nil {
panic(err)
}
}
func (self *Comment) Update(c string) {
length := utf8.RuneCountInString(c)
if length > 0 {
if length > 3000 {
chars := []rune(c)
c = string(chars[:3000])
}
self.Content = html.EscapeString(c)
// Use content module to run processors chain
database := deps.Container.Mgo()
content := self.post.DI().Content
content.Parse(self)
// Update database with new content
err := database.C("comments").Update(bson.M{"_id": self.Id}, bson.M{"$set": bson.M{"content": self.Content, "updated_at": time.Now()}})
if err != nil {
panic(err)
}
// Finally parse the tags
content.ParseTags(self)
}
}