/
comment.go
156 lines (138 loc) · 3.85 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
package model
import (
"encoding/json"
"errors"
"html/template"
"github.com/ego008/youdb"
"github.com/terminus2049/2049bbs/util"
)
type Comment struct {
Id uint64 `json:"id"`
Aid uint64 `json:"aid"`
Uid uint64 `json:"uid"`
Content string `json:"content"`
ClientIp string `json:"clientip"`
AddTime uint64 `json:"addtime"`
Fold bool `json:"fold"`
}
type CommentListItem struct {
Id uint64 `json:"id"`
Aid uint64 `json:"aid"`
Uid uint64 `json:"uid"`
Flag int `json:"flag"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Content string `json:"content"`
ContentFmt template.HTML
AddTime uint64 `json:"addtime"`
AddTimeFmt string `json:"addtimefmt"`
Fold bool `json:"fold"`
}
type CommentPageInfo struct {
Items []CommentListItem `json:"items"`
HasPrev bool `json:"hasprev"`
HasNext bool `json:"hasnext"`
FirstKey uint64 `json:"firstkey"`
LastKey uint64 `json:"lastkey"`
}
func CommentGetByKey(db *youdb.DB, aid string, cid uint64) (Comment, error) {
obj := Comment{}
rs := db.Hget("article_comment:"+aid, youdb.I2b(cid))
if rs.State == "ok" {
json.Unmarshal(rs.Data[0], &obj)
return obj, nil
}
return obj, errors.New(rs.State)
}
func CommentSetByKey(db *youdb.DB, aid string, cid uint64, obj Comment) error {
jb, _ := json.Marshal(obj)
return db.Hset("article_comment:"+aid, youdb.I2b(cid), jb)
}
func CommentDelByKey(db *youdb.DB, aid string, cid uint64) error {
return db.Hdel("article_comment:"+aid, youdb.I2b(cid))
}
func CommentList(db *youdb.DB, cmd, tb, key string, limit, tz int, ignorelimitedusers bool) CommentPageInfo {
var items []CommentListItem
var citems []Comment
userMap := map[uint64]UserMini{}
var userKeys [][]byte
var hasPrev, hasNext bool
var firstKey, lastKey uint64
keyStart := youdb.DS2b(key)
if cmd == "hrscan" {
rs := db.Hrscan(tb, keyStart, limit)
if rs.State == "ok" {
for i := len(rs.Data) - 2; i >= 0; i -= 2 {
item := Comment{}
json.Unmarshal(rs.Data[i+1], &item)
citems = append(citems, item)
userMap[item.Uid] = UserMini{}
userKeys = append(userKeys, youdb.I2b(item.Uid))
}
}
} else if cmd == "hscan" {
rs := db.Hscan(tb, keyStart, limit)
if rs.State == "ok" {
for i := 0; i < (len(rs.Data) - 1); i += 2 {
item := Comment{}
json.Unmarshal(rs.Data[i+1], &item)
citems = append(citems, item)
userMap[item.Uid] = UserMini{}
userKeys = append(userKeys, youdb.I2b(item.Uid))
}
}
}
if len(citems) > 0 {
rs := db.Hmget("user", userKeys)
if rs.State == "ok" {
for i := 0; i < (len(rs.Data) - 1); i += 2 {
item := UserMini{}
json.Unmarshal(rs.Data[i+1], &item)
userMap[item.Id] = item
}
}
for _, citem := range citems {
user := userMap[citem.Uid]
item := CommentListItem{
Id: citem.Id,
Aid: citem.Aid,
Uid: citem.Uid,
Name: user.Name,
Flag: user.Flag,
Avatar: user.Avatar,
AddTime: citem.AddTime,
AddTimeFmt: util.TimeFmt(citem.AddTime, "2006-01-02", tz),
ContentFmt: template.HTML(util.ContentFmt(db, citem.Content)),
Fold: citem.Fold,
}
if item.Flag == -1 {
item.ContentFmt = template.HTML("<p><strike>用户已注销,隐藏回帖</strike></p>")
}
if item.Flag == 0 || item.Flag == 6 {
item.Fold = true
}
if !(ignorelimitedusers && (item.Flag == 0 || item.Flag == 6)) {
items = append(items, item)
}
if firstKey == 0 {
firstKey = item.Id
}
lastKey = item.Id
}
rs = db.Hrscan(tb, youdb.I2b(firstKey), 1)
if rs.State == "ok" {
hasPrev = true
}
rs = db.Hscan(tb, youdb.I2b(lastKey), 1)
if rs.State == "ok" {
hasNext = true
}
}
return CommentPageInfo{
Items: items,
HasPrev: hasPrev,
HasNext: hasNext,
FirstKey: firstKey,
LastKey: lastKey,
}
}