-
Notifications
You must be signed in to change notification settings - Fork 0
/
churros.go
113 lines (95 loc) · 3 KB
/
churros.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
package notella
import (
"context"
"fmt"
"strings"
"git.inpt.fr/churros/notella/db"
)
var prisma = db.NewClient()
type ChurrosId struct {
Type string
LocalID string
}
func (id ChurrosId) String() string {
return fmt.Sprintf("%s:%s", id.Type, id.LocalID)
}
func ParseChurrosId(churrosId string) (ChurrosId, error) {
parts := strings.Split(churrosId, ":")
if len(parts) != 2 {
return ChurrosId{}, fmt.Errorf("malformed churros global id: %q", churrosId)
}
return ChurrosId{
Type: parts[0],
LocalID: parts[1],
}, nil
}
// UnmarshalText implements the encoding.TextUnmarshaler interface for ChurrosId.
func (id *ChurrosId) UnmarshalText(text []byte) error {
s := string(text)
parsed, err := ParseChurrosId(s)
if err != nil {
return err
}
id.Type = parsed.Type
id.LocalID = parsed.LocalID
return nil
}
func (msg Message) CreateInDatabaseNotifications(groupId string, subs []Subscription) {
// Create sequentially: this is not something that has to be done fast, and parallelizing would swamp the database connections
for _, sub := range subs {
prisma.Notification.CreateOne(
db.Notification.Subscription.Link(
db.NotificationSubscription.Endpoint.Equals(sub.Webpush.Endpoint),
),
db.Notification.Title.Set(msg.Title),
db.Notification.Body.Set(msg.Body),
db.Notification.ID.Set(msg.Id),
db.Notification.Channel.Set(msg.Channel()),
db.Notification.Group.Link(db.Group.ID.Equals(groupId)),
db.Notification.Goto.Set(msg.Action),
db.Notification.Timestamp.Set(msg.SendAt),
).Exec(context.Background())
}
}
func ConnectToDababase() error {
return prisma.Connect()
}
// Group returns the Churros group ID responsible for the notification
func (msg Message) Group() (string, error) {
switch msg.Event {
case EventNewPost:
post, err := prisma.Article.FindUnique(
db.Article.ID.Equals(msg.ChurrosObjectId),
).Select(
db.Article.GroupID.Field(),
).Exec(context.Background())
if err != nil {
return "", fmt.Errorf("while getting the group responsible for the notification: %w", err)
}
return post.GroupID, nil
case EventShotgunClosesSoon, EventShotgunOpensSoon:
event, err := prisma.Event.FindUnique(
db.Event.ID.Equals(msg.ChurrosObjectId),
).Exec(context.Background())
if err != nil {
return "", fmt.Errorf("while getting the group responsible for the notification: %w", err)
}
return event.GroupID, nil
case EventCustom, EventGodchildAccepted, EventGodchildRejected, EventGodchildRequest, EventTest:
return "", nil
}
return "", fmt.Errorf("unknown event type %q", msg.Event)
}
func (msg Message) Channel() db.NotificationChannel {
switch msg.Event {
case EventNewPost:
return db.NotificationChannelArticles
case EventShotgunClosesSoon, EventShotgunOpensSoon:
return db.NotificationChannelShotguns
case EventCommentReply, EventNewComment:
return db.NotificationChannelComments
case EventGodchildRequest, EventGodchildAccepted, EventGodchildRejected:
return db.NotificationChannelGodparentRequests
}
return db.NotificationChannelOther
}