/
load_general_articles.go
102 lines (86 loc) · 2.46 KB
/
load_general_articles.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
package bbs
import (
"github.com/Ptt-official-app/go-pttbbs/ptt"
"github.com/Ptt-official-app/go-pttbbs/ptttype"
"github.com/Ptt-official-app/go-pttbbs/types"
)
// LoadGeneralArticles in descending mode.
func LoadGeneralArticles(
uuserID UUserID,
bboardID BBoardID,
startIdxStr string,
nArticles int,
isDesc bool) (
summaries []*ArticleSummary,
nextIdxStr string,
nextCreateTime types.Time4,
isNewest bool,
startNumIdx ptttype.SortIdx,
err error) {
if nArticles < 1 {
return nil, "", 0, false, -1, ErrInvalidParams
}
userIDRaw, err := uuserID.ToRaw()
if err != nil {
return nil, "", 0, false, -1, ErrInvalidParams
}
uid, userecRaw, err := ptt.InitCurrentUser(userIDRaw)
if err != nil {
return nil, "", 0, false, -1, err
}
bid, boardIDRaw, err := bboardID.ToRaw()
if err != nil {
return nil, "", 0, false, -1, ErrInvalidParams
}
// 1. find start idx. start-idx as nextCreateTime if unable to find startIdxStr
startIdx, err := loadGeneralArticlesToStartIdx(userecRaw, uid, boardIDRaw, bid, startIdxStr, isDesc)
if err != nil {
return nil, "", 0, false, -1, err
}
// 2. load articles.
var summariesRaw []*ptttype.ArticleSummaryRaw
var nextSummaryRaw *ptttype.ArticleSummaryRaw
if startIdx >= 0 {
summariesRaw, isNewest, nextSummaryRaw, startNumIdx, err = ptt.LoadGeneralArticles(userecRaw, uid, boardIDRaw, bid, startIdx, nArticles, isDesc)
if err != nil {
return nil, "", 0, false, -1, err
}
}
// 3. nextIdxStr
nextIdxStr = ""
nextCreateTime = 0
if nextSummaryRaw != nil {
nextSummary := NewArticleSummaryFromRaw(bboardID, nextSummaryRaw)
nextIdxStr = nextSummary.Idx
nextCreateTime = nextSummary.CreateTime
}
summaries = make([]*ArticleSummary, len(summariesRaw))
for idx, each := range summariesRaw {
eachSummary := NewArticleSummaryFromRaw(bboardID, each)
summaries[idx] = eachSummary
}
return summaries, nextIdxStr, nextCreateTime, isNewest, startNumIdx, nil
}
func loadGeneralArticlesToStartIdx(
userecRaw *ptttype.UserecRaw,
uid ptttype.UID,
boardIDRaw *ptttype.BoardID_t,
bid ptttype.Bid,
startIdxStr string,
isDesc bool,
) (startIdx ptttype.SortIdx, err error) {
if startIdxStr == "" {
if isDesc {
return 0, nil
} else {
return 1, nil
}
}
createTime, articleID, err := DeserializeArticleIdxStr(startIdxStr)
if err != nil {
return -1, err
}
filename := articleID.ToRaw()
startIdx, err = ptt.FindArticleStartIdx(userecRaw, uid, boardIDRaw, bid, createTime, filename, isDesc)
return startIdx, err
}