-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
124 lines (112 loc) · 2.73 KB
/
feed.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
package main
import (
"context"
"time"
"git.maharshi.ninja/root/rss2email/structures"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// MinimumFrequency == 10min
const MinimumFrequency = 10 * time.Minute
func (c *connection) handleAddFeed(mi *MessageInfo, buf []byte) {
var req structures.Feed
ok := c.decodeToInterface(buf, &req)
if !ok {
return
}
req.CreatedAt = time.Now()
req.UpdatedAt = time.Now()
req.ID = primitive.NewObjectID()
req.Owner = c.userID
req.Frequency = req.Frequency * time.Second
req.LastFetched = time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)
if req.Frequency < MinimumFrequency {
req.Frequency = MinimumFrequency
}
f, err := c.a.feeds.InsertOne(context.TODO(), req)
if err != nil {
c.writeError(mi, structures.ErrorInternal, err)
return
}
c.writeMessage(true, mi, structures.GenericIDResponse{
OK: true,
ID: f.InsertedID.(primitive.ObjectID),
})
}
func (c *connection) handleEditFeed(mi *MessageInfo, buf []byte) {
var req structures.Feed
ok := c.decodeToInterface(buf, &req)
if !ok {
return
}
if req.Frequency < MinimumFrequency {
req.Frequency = MinimumFrequency
}
f, err := c.a.feeds.UpdateOne(context.TODO(), bson.M{
"_id": req.ID,
"owner_id": c.userID,
}, bson.M{
"$set": bson.M{
"name": req.Name,
"feed_url": req.URL,
"frequency": req.Frequency,
},
})
if err != nil {
c.writeError(mi, structures.ErrorInternal, err)
return
}
c.writeMessage(true, mi, structures.UpdatedFeedResponse{
ModifiedCount: uint64(f.ModifiedCount),
})
}
func (c *connection) handleDeleteFeed(mi *MessageInfo, buf []byte) {
var req structures.DeleteFeedRequest
ok := c.decodeToInterface(buf, &req)
if !ok {
return
}
result, err := c.a.feeds.DeleteOne(context.TODO(), bson.M{
"_id": req.ID,
"owner_id": c.userID,
})
if err != nil {
c.writeError(mi, structures.ErrorInternal, err)
return
}
c.writeMessage(true, mi, structures.DeleteFeedResponse{
DeletedCount: result.DeletedCount,
})
}
func (c *connection) handleListFeeds(mi *MessageInfo, buf []byte) {
var req structures.ListFeedsRequest
ok := c.decodeToInterface(buf, &req)
if !ok {
return
}
var feeds []structures.Feed
cursor, err := c.a.feeds.Find(context.TODO(), bson.M{
"owner_id": c.userID,
})
if err != nil {
c.writeError(mi, structures.ErrorInternal, err)
return
}
err = cursor.All(context.TODO(), &feeds)
if err != nil {
c.writeError(mi, structures.ErrorInternal, err)
return
}
err = cursor.Close(context.TODO())
if err != nil {
c.writeError(mi, structures.ErrorInternal, err)
return
}
if feeds == nil {
feeds = []structures.Feed{}
}
c.writeMessage(true, mi, structures.ListFeedsResponse{
Count: uint64(len(feeds)),
Feeds: feeds,
})
}