forked from indes/flowerss-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.go
317 lines (275 loc) · 8.25 KB
/
core.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
package core
import (
"context"
"errors"
"strings"
"sync"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"github.com/nerdneilsfield/flowerss-bot/internal/config"
"github.com/nerdneilsfield/flowerss-bot/internal/log"
"github.com/nerdneilsfield/flowerss-bot/internal/model"
"github.com/nerdneilsfield/flowerss-bot/internal/storage"
)
var (
ErrSubscriptionExist = errors.New("already subscribed")
ErrSubscriptionNotExist = errors.New("subscription not exist")
ErrSourceNotExist = errors.New("source not exist")
)
type Core struct {
// Storage
userStorage storage.User
contentStorage storage.Content
sourceStorage storage.Source
subscriptionStorage storage.Subscription
}
func NewCore(
userStorage storage.User, contentStorage storage.Content, sourceStorage storage.Source,
subscriptionStorage storage.Subscription,
) *Core {
return &Core{
userStorage: userStorage,
contentStorage: contentStorage,
sourceStorage: sourceStorage,
subscriptionStorage: subscriptionStorage,
}
}
func NewCoreFormConfig() *Core {
var err error
var db *gorm.DB
if config.EnableMysql {
db, err = gorm.Open(mysql.Open(config.Mysql.GetMysqlConnectingString()))
} else {
db, err = gorm.Open(sqlite.Open(config.SQLitePath))
}
if err != nil {
log.Fatalf("connect db failed, err: %+v", err)
return nil
}
if config.DBLogMode {
db = db.Debug()
}
sqlDB, err := db.DB()
sqlDB.SetMaxIdleConns(10)
sqlDB.SetMaxOpenConns(50)
return NewCore(
storage.NewUserStorageImpl(db),
storage.NewContentStorageImpl(db),
storage.NewSourceStorageImpl(db),
storage.NewSubscriptionStorageImpl(db),
)
}
func (c *Core) Init() error {
c.userStorage.Init(context.Background())
c.contentStorage.Init(context.Background())
c.sourceStorage.Init(context.Background())
c.subscriptionStorage.Init(context.Background())
return nil
}
// GetUserSubscribedSources 获取用户订阅的订阅源
func (c *Core) GetUserSubscribedSources(ctx context.Context, userID int64) ([]*model.Source, error) {
opt := &storage.GetSubscriptionsOptions{Count: -1}
result, err := c.subscriptionStorage.GetSubscriptionsByUserID(ctx, userID, opt)
if err != nil {
return nil, err
}
var sources []*model.Source
for _, subs := range result.Subscriptions {
source, err := c.sourceStorage.GetSource(ctx, subs.SourceID)
if err != nil {
log.Errorf("get source %d failed, %v", subs.SourceID, err)
continue
}
sources = append(sources, source)
}
return sources, nil
}
// AddSubscription 添加订阅
func (c *Core) AddSubscription(ctx context.Context, userID int64, sourceID uint) error {
exist, err := c.subscriptionStorage.SubscriptionExist(ctx, userID, sourceID)
if err != nil {
return err
}
if exist {
return ErrSubscriptionExist
}
subscription := &model.Subscribe{
UserID: userID,
SourceID: sourceID,
EnableNotification: 1,
EnableTelegraph: 1,
Interval: config.UpdateInterval,
WaitTime: config.UpdateInterval,
}
return c.subscriptionStorage.AddSubscription(ctx, subscription)
}
// Unsubscribe 添加订阅
func (c *Core) Unsubscribe(ctx context.Context, userID int64, sourceID uint) error {
exist, err := c.subscriptionStorage.SubscriptionExist(ctx, userID, sourceID)
if err != nil {
return err
}
if !exist {
return ErrSubscriptionNotExist
}
// 移除该用户订阅
_, err = c.subscriptionStorage.DeleteSubscription(ctx, userID, sourceID)
if err != nil {
return err
}
// 获取源的订阅数量
count, err := c.subscriptionStorage.CountSourceSubscriptions(ctx, sourceID)
if err != nil {
return err
}
if count != 0 {
return nil
}
// 如果源不再有订阅用户,移除该订阅源
if err := c.removeSource(ctx, sourceID); err != nil {
return err
}
return nil
}
// removeSource 移除订阅源
func (c *Core) removeSource(ctx context.Context, sourceID uint) error {
if err := c.sourceStorage.Delete(ctx, sourceID); err != nil {
return err
}
count, err := c.contentStorage.DeleteSourceContents(ctx, sourceID)
if err != nil {
return err
}
log.Infof("remove source %d and %d contents", sourceID, count)
return nil
}
// GetSourceByURL 获取用户订阅的订阅源
func (c *Core) GetSourceByURL(ctx context.Context, sourceURL string) (*model.Source, error) {
source, err := c.sourceStorage.GetSourceByURL(ctx, sourceURL)
if err != nil {
if err == storage.ErrRecordNotFound {
return nil, ErrSourceNotExist
}
return nil, err
}
return source, nil
}
// GetSource 获取用户订阅的订阅源
func (c *Core) GetSource(ctx context.Context, id uint) (*model.Source, error) {
source, err := c.sourceStorage.GetSource(ctx, id)
if err != nil {
if err == storage.ErrRecordNotFound {
return nil, ErrSourceNotExist
}
return nil, err
}
return source, nil
}
// UnsubscribeAllSource 添加订阅
func (c *Core) UnsubscribeAllSource(ctx context.Context, userID int64) error {
sources, err := c.GetUserSubscribedSources(ctx, userID)
if err != nil {
return err
}
var wg sync.WaitGroup
for i := range sources {
wg.Add(1)
i := i
go func() {
defer wg.Done()
if err := c.Unsubscribe(ctx, userID, sources[i].ID); err != nil {
log.Errorf("user %d unsubscribe %d failed, %v", userID, sources[i].ID, err)
}
}()
}
wg.Wait()
return nil
}
// GetSubscription 获取订阅
func (c *Core) GetSubscription(ctx context.Context, userID int64, sourceID uint) (*model.Subscribe, error) {
subscription, err := c.subscriptionStorage.GetSubscription(ctx, userID, sourceID)
if err != nil {
if err == storage.ErrRecordNotFound {
return nil, ErrSubscriptionNotExist
}
return nil, err
}
return subscription, nil
}
// SetSubscriptionTag 设置订阅标签
func (c *Core) SetSubscriptionTag(ctx context.Context, userID int64, sourceID uint, tags []string) error {
subscription, err := c.GetSubscription(ctx, userID, sourceID)
if err != nil {
return err
}
subscription.Tag = "#" + strings.Join(tags, " #")
return c.subscriptionStorage.UpdateSubscription(ctx, userID, sourceID, subscription)
}
// SetSubscriptionInterval
func (c *Core) SetSubscriptionInterval(ctx context.Context, userID int64, sourceID uint, interval int) error {
subscription, err := c.GetSubscription(ctx, userID, sourceID)
if err != nil {
return err
}
subscription.Interval = interval
return c.subscriptionStorage.UpdateSubscription(ctx, userID, sourceID, subscription)
}
// EnableSourceUpdate 开启订阅源更新
func (c *Core) EnableSourceUpdate(ctx context.Context, sourceID uint) error {
return c.ClearSourceErrorCount(ctx, sourceID)
}
// DisableSourceUpdate 关闭订阅源更新
func (c *Core) DisableSourceUpdate(ctx context.Context, sourceID uint) error {
source, err := c.GetSource(ctx, sourceID)
if err != nil {
return err
}
source.ErrorCount = config.ErrorThreshold + 1
return c.sourceStorage.UpsertSource(ctx, sourceID, source)
}
// ClearSourceErrorCount 清空订阅源错误计数
func (c *Core) ClearSourceErrorCount(ctx context.Context, sourceID uint) error {
source, err := c.GetSource(ctx, sourceID)
if err != nil {
return err
}
source.ErrorCount = 0
return c.sourceStorage.UpsertSource(ctx, sourceID, source)
}
func (c *Core) ToggleSubscriptionNotice(ctx context.Context, userID int64, sourceID uint) error {
subscription, err := c.GetSubscription(ctx, userID, sourceID)
if err != nil {
return err
}
if subscription.EnableNotification == 1 {
subscription.EnableNotification = 0
} else {
subscription.EnableNotification = 1
}
return c.subscriptionStorage.UpsertSubscription(ctx, userID, sourceID, subscription)
}
func (c *Core) ToggleSourceUpdateStatus(ctx context.Context, sourceID uint) error {
source, err := c.GetSource(ctx, sourceID)
if err != nil {
return err
}
if source.ErrorCount < config.ErrorThreshold {
source.ErrorCount = config.ErrorThreshold + 1
} else {
source.ErrorCount = 0
}
return c.sourceStorage.UpsertSource(ctx, sourceID, source)
}
func (c *Core) ToggleSubscriptionTelegraph(ctx context.Context, userID int64, sourceID uint) error {
subscription, err := c.GetSubscription(ctx, userID, sourceID)
if err != nil {
return err
}
if subscription.EnableTelegraph == 1 {
subscription.EnableTelegraph = 0
} else {
subscription.EnableTelegraph = 1
}
return c.subscriptionStorage.UpsertSubscription(ctx, userID, sourceID, subscription)
}