-
Notifications
You must be signed in to change notification settings - Fork 11
/
counters.go
220 lines (187 loc) · 5.53 KB
/
counters.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
package ttrss
import (
"strconv"
"time"
"github.com/pkg/errors"
"github.com/urandom/readeef/content"
"github.com/urandom/readeef/content/repo"
)
type countersContent []counter
type counter struct {
Id interface{} `json:"id"`
Counter int64 `json:"counter"`
AuxCounter int64 `json:"auxcounter,omitempty"`
Kind string `json:"kind,omitempty"`
}
func getUnread(req request, user content.User, service repo.Service) (interface{}, error) {
opts := []content.QueryOpt{
content.Filters(content.GetUserFilters(user)),
}
var feedGenerator func() ([]content.Feed, error)
if req.IsCat {
if req.FeedId > 0 {
tag, err := service.TagRepo().Get(content.TagID(req.FeedId), user)
if err != nil {
return nil, errors.WithMessage(err, "getting tag for user")
}
feedGenerator = func() ([]content.Feed, error) {
return service.FeedRepo().ForTag(tag, user)
}
} else if req.FeedId == CAT_UNCATEGORIZED {
opts = append(opts, content.UntaggedOnly)
} else if req.FeedId == CAT_SPECIAL {
opts = append(opts, content.FavoriteOnly)
}
} else {
switch req.FeedId {
case FAVORITE_ID:
opts = append(opts, content.FavoriteOnly)
case FRESH_ID:
opts = append(opts, content.TimeRange(time.Now().Add(FRESH_DURATION), time.Time{}))
default:
if req.FeedId > 0 {
feed, err := service.FeedRepo().Get(req.FeedId, user)
if err != nil {
return nil, errors.WithMessage(err, "getting user feed")
}
feedGenerator = func() ([]content.Feed, error) {
return []content.Feed{feed}, nil
}
}
}
}
if feedGenerator != nil {
feeds, err := feedGenerator()
if err != nil {
return nil, errors.WithMessage(err, "getting feeds")
}
ids := make([]content.FeedID, len(feeds))
for i := range feeds {
ids[i] = feeds[i].ID
}
opts = append(opts, content.FeedIDs(ids))
}
count, err := service.ArticleRepo().Count(user, opts...)
if err != nil {
return nil, err
}
return genericContent{Unread: strconv.FormatInt(count, 10)}, nil
}
func getCounters(req request, user content.User, service repo.Service) (interface{}, error) {
if req.OutputMode == "" {
req.OutputMode = "flc"
}
cContent := countersContent{}
articleRepo := service.ArticleRepo()
unreadCount, err := articleRepo.Count(user, content.UnreadOnly,
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting user unread count")
}
cContent = append(cContent,
counter{Id: "global-unread", Counter: unreadCount})
feeds, err := service.FeedRepo().ForUser(user)
if err != nil {
return nil, errors.WithMessage(err, "getting user feeds")
}
cContent = append(cContent,
counter{Id: "subscribed-feeds", Counter: int64(len(feeds))})
cContent = append(cContent, counter{Id: ARCHIVED_ID})
unreadFavCount, err := articleRepo.Count(user, content.UnreadOnly, content.FavoriteOnly,
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting favorite unread count")
}
favCount, err := articleRepo.Count(user, content.FavoriteOnly,
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting favorite count")
}
cContent = append(cContent,
counter{Id: FAVORITE_ID,
Counter: unreadFavCount,
AuxCounter: favCount})
cContent = append(cContent, counter{Id: PUBLISHED_ID})
freshTime := time.Now().Add(FRESH_DURATION)
freshCount, err := articleRepo.Count(user, content.UnreadOnly,
content.TimeRange(freshTime, time.Time{}),
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting fresh unread count")
}
cContent = append(cContent,
counter{Id: FRESH_ID,
Counter: freshCount,
AuxCounter: 0})
userCount, err := articleRepo.Count(user,
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting user count")
}
cContent = append(cContent,
counter{Id: ALL_ID,
Counter: userCount,
AuxCounter: 0})
for _, f := range feeds {
feedCount, err := articleRepo.Count(user,
content.FeedIDs([]content.FeedID{f.ID}),
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting feed count")
}
cContent = append(cContent,
counter{Id: int64(f.ID), Counter: feedCount},
)
}
cContent = append(cContent, counter{Id: CAT_LABELS, Counter: 0, Kind: "cat"})
tagRepo := service.TagRepo()
tags, err := tagRepo.ForUser(user)
if err != nil {
return nil, errors.WithMessage(err, "getting user tags")
}
for _, tag := range tags {
ids, err := tagRepo.FeedIDs(tag, user)
if err != nil {
return nil, errors.WithMessage(err, "getting tag feed ids")
}
tagCount, err := articleRepo.Count(user, content.UnreadOnly,
content.FeedIDs(ids),
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting tag unread count")
}
cContent = append(cContent,
counter{
Id: int64(tag.ID),
Counter: tagCount,
Kind: "cat",
},
)
}
unreadUntaggedCount, err := articleRepo.Count(user,
content.UnreadOnly, content.UntaggedOnly,
content.Filters(content.GetUserFilters(user)),
)
if err != nil {
return nil, errors.WithMessage(err, "getting unread untagged count")
}
cContent = append(cContent,
counter{
Id: CAT_UNCATEGORIZED,
Counter: unreadUntaggedCount,
Kind: "cat",
},
)
return cContent, nil
}
func init() {
actions["getUnread"] = getUnread
actions["getCounters"] = getCounters
}