-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
376 lines (335 loc) · 8.39 KB
/
post.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
// TODO: 文章是否可以删除?
// TODO: 文章的访问权限问题?
package orm
import (
err "../../cc/err"
"fmt"
"stgogo/comn/convert"
"time"
)
// 上传的文章
type (
Post struct {
Id int64
Title string
Text string
TagIds[] int64
OwnerId int64
IsPrivate bool
Date string
CreateDate string
Path string
}
PostInfo struct {
Id int64
Title string
CreateDate string
Date string
IsPrivate bool
OwnerId int64
Path string
TagIds[] int64
}
CountOf struct {
Text string
Count int
}
PostInfoMono struct {
Id int64
Title string
}
)
func Map2CountOf( m map[string]int ) ( c []CountOf ){
c = []CountOf{}
for text, count := range m {
c = append(c, CountOf{Text: text, Count: count})
}
return
}
// 上传的tag标签结构
// tag同时将作为分区的主要依据
// TODO:tag达到一定数量则升级为分区
type (
Tag struct {
Id int64
Text string
IsCatalog bool
Percentage float32
}
)
func Sync2Post() {
e := engine_post.Sync2(new(Post))
err.Assert( e )
e = engine_post.Sync2(new(Tag))
err.Assert( e )
}
// 通过某一个人获取所有他的文章
// 仅通过atk
func GetPostInfosByOwnerAll( OwnerId int64 ) ( []PostInfo, error ) {
var posts []PostInfo
e := engine_post.Table("Post").Where( "owner_id = ?", OwnerId).Find(&posts)
return posts, e
}
func GetAllPublicPostInfosLimited( start, count int ) ( []PostInfo, error ) {
var posts []PostInfo
e := engine_post.Table("Post").Where( "is_private = 0" ).Limit( count, start ).Find(&posts)
return posts, e
}
// 通过某一个人获取所有公开文章
func GetPostInfosByOwnerPublic( OwnerId int64 ) ( []PostInfo, error ) {
var posts []PostInfo
e := engine_post.Table("Post").Where( "owner_id = ? and is_private = 0", OwnerId).Find(&posts)
return posts, e
}
func GetPostsByOwnerPublic( OwnerId int64 ) ( []Post, error ) {
var posts []Post
e := engine_post.Table("Post").Where( "owner_id = ? and is_private = 0", OwnerId).Find(&posts)
return posts, e
}
func GetPostsPublicAll() ( []Post, error ) {
var posts []Post
e := engine_post.Table("Post").Where( "is_private = 0").Find(&posts)
return posts, e
}
func GetPostInfosAll() ( []PostInfo, error ) {
var posts []PostInfo
e := engine_post.Table("Post").Where( "is_private = 0" ).Find(&posts)
return posts, e
}
func GetPostInfoById( id int64 ) ( PostInfo, error ) {
post := new(PostInfo)
_, e := engine_post.Table("Post").ID( id ).Get(post)
return *post, e
}
func GetPostById( id int64 ) ( Post, error ) {
post := new(Post)
_, e := engine_post.Table("Post").ID( id ).Get(post)
return *post, e
}
func GetPostInfosByIds( ids []int64 ) ( []PostInfo, error ) {
var ps []PostInfo
for _, id := range ids {
if p, e := GetPostInfoById(id); e != nil {
return nil, e
} else {
ps = append( ps, p )
}
}
return ps, nil
}
// 向数据库添加一笔新文章
func NewPost( title, text string, owner int64, tags []string, private bool, path string) (int64, error) {
tagIds, e := GetTagIds( tags )
newPost := &Post{
Title: title,
Text: text,
TagIds: tagIds,
OwnerId: owner,
IsPrivate: private,
Date: time.Now().Format("2006-01-02 15:04:05"),
CreateDate: time.Now().Format("2006-01-02 15:04:05"),
Path: path,
}
_, e = engine_post.Table("Post").Insert( newPost )
return newPost.Id, e
}
// 修改文章
func ModifyPost( id int64, title, text string, owner int64, isPrivate bool, tags []string, path string) error {
tagIds, e := GetTagIds( tags )
mp := &Post{
Title: title,
Text: text,
TagIds: tagIds,
OwnerId: owner,
IsPrivate: isPrivate,
Date: time.Now().Format("2006-01-02 15:04:05"),
Path: path,
}
_, e = engine_post.Table("Post").Cols().ID(id).Update(mp)
return e
}
func ModifyPostPath( id int64, path string) error {
mp := &Post{
Path: path,
}
_, e := engine_post.Table("Post").Cols().ID(id).Update(mp)
return e
}
// 修改文章,不修改内容
// 减轻流量负担
func ModifyPostNoText( id int64, title string, owner int64, tags []string) error {
tagIds, e := GetTagIds( tags )
_, e = engine_post.Table("Post").ID(id).Update(&Post{
Title: title,
TagIds: tagIds,
OwnerId: owner,
})
return e
}
// 根据一系列tag的名字获取所有的tag的id
// 如果tag不存在则会被创建
func GetTagIds( tags []string ) ( []int64, error ) {
var tagIds []int64
for _, tagText := range tags {
t := new(Tag)
GetTag:
exists, _ := engine_post.Table("Tag").Where( "Text = ?", tagText).Get(t)
// 如果当前没有该tag,则创建一个新tag
if !exists {
_, e := engine_post.Table("Tag").Insert( &Tag {
Text:tagText,
IsCatalog:false,
})
if e != nil {
return nil, e
}
goto GetTag // 再次获取tag
}
// 这里t应该已经被填充
tagIds = append(tagIds, t.Id)
}
return tagIds, nil
}
func GetTagNames( tagIds []int64 ) ( []string, error ) {
var (
tags []string
)
for _, id := range tagIds {
tag := new(Tag)
if exists, e := engine_post.Table("Tag").ID(id).Get(tag); exists && e == nil {
tags = append(tags, tag.Text)
} else {
return nil, e
}
}
return tags, nil
}
func GetPostInfosByTags( tags []string ) ( []PostInfo, error ) {
var (
pis []PostInfo
findEx string
)
tagIds, e := GetTagIds( tags )
findEx = ""
for i, id := range tagIds {
sid := convert.I64toa(id)
// tag交集
// id = 1
// tag_ids like '[1,%' or like '%,1,%' or like '%,1]'
findEx += fmt.Sprintf( "(tag_ids like '[%s,%%' or tag_ids like '%%,%s,%%' or tag_ids like '%%,%s]')", sid, sid, sid )
if len(tagIds) == 1 {
findEx += fmt.Sprintf( "or (tag_ids like '[%s]')", sid )
}
if i != len(tagIds) - 1 {
findEx += "and"
}
}
findEx += " and is_private = 0"
e = engine_post.Table("Post").Where( findEx ).Find(&pis)
return pis, e
}
func GetAllTags() ( []Tag, error ) {
var (
tags []Tag
e error
)
e = engine_post.Table("Tag").Find(&tags)
return tags, e
}
// 获取某个人的文章日期列表,按月来算。
// 返回的列表应该形如:
// key: 2000-1 value: 2
// 表示在2000的1月有两篇文章
//
// 于2020/12/2通过测试
func GetOnesAllPostInfoDate( id int64 ) ( c []CountOf, e error) {
var (
ps []PostInfo
)
cd := make( map[string]int )
c = []CountOf{}
if ps, e = GetPostInfosByOwnerAll( id ); e != nil {return}
for _, p := range ps {
// p.CreateDate[0:6] == 2006-01
monDate := p.CreateDate[0:7]
if _, ok := cd[monDate]; !ok {
cd[monDate] = 1
} else {
cd[monDate]++
}
}
c = Map2CountOf( cd )
return
}
// 返回某人拥有的所有tag
func GetOnesAllPostInfoTags( id int64 ) ( t []CountOf, e error ) {
var (
ps []PostInfo
)
if ps, e = GetPostInfosByOwnerAll(id); e != nil {
return
}
tagIds := make( map[int64]int )
tags := make(map[string]int)
t = []CountOf{}
// 获取所有的tag id,并且不重复
for _, p := range ps {
for _, tag := range p.TagIds {
if _, ok := tagIds[tag]; !ok {
tagIds[tag] = 1
} else {
tagIds[tag]++
}
}
}
for tagId, postCount := range tagIds {
tagName := new(Tag)
if exists, e := engine_post.Table("Tag").ID(tagId).Get(tagName); exists && e == nil {
tags[tagName.Text] = postCount
}
}
t = Map2CountOf(tags)
return
}
// 最新文章
// 获取最近的8篇文章标题
func GetOnesRecentPostTitle( id int64 ) ( titles []PostInfoMono, e error ) {
var ps []PostInfo
if e = engine_post.Table("Post").Where("id > (SELECT MAX(id) FROM Post) - 10 and owner_id = ?", id ).Find(&ps)
e != nil {
return
}
titles = []PostInfoMono{}
for _, p := range ps {
titles = append(titles, PostInfoMono{
Id: p.Id,
Title: p.Title,
})
}
return
}
func GetPostInfosByCateDate( id int64, date string ) ( ps []PostInfo, e error ) {
query := fmt.Sprintf( "create_date like '%s%%' and owner_id = %d", date, id )
e = engine_post.Table("Post").Where(query).Find( &ps )
return
}
func GetInfoComponentPost( id int64, name string ) ( p []Post, e error ) {
e = engine_post.Table("Post").Where("owner_id = ? and title = ?", id, name ).Find( &p )
return
}
func GetPostCustomStyleById( id int64 ) ( css string ) {
css = ""
ps, _ := GetInfoComponentPost( id, "MyMarkdownStyle" )
if len(ps) > 0 {
css = ps[0].Text
}
return
}
// --------- 搜索模块 ---------
func VagueSearchPostAndTagName( text string ) ( ps []PostInfo, tags []Tag, e error ) {
where := fmt.Sprintf("title like '%%%s%%'", text )
e = engine_post.Table("Post").Where(where).Find(&ps)
where = fmt.Sprintf("text like '%%%s%%'", text )
e = engine_post.Table("Tag").Where(where).Find(&tags)
return
}