This repository has no description
1package notella
2
3import (
4 "context"
5 "fmt"
6 "strings"
7
8 "git.inpt.fr/churros/notella/db"
9 ll "github.com/gwennlbh/label-logger-go"
10)
11
12var prisma = db.NewClient()
13
14type ChurrosId struct {
15 Type string
16 LocalID string
17}
18
19func (id ChurrosId) String() string {
20 return fmt.Sprintf("%s:%s", id.Type, id.LocalID)
21}
22
23func ParseChurrosId(churrosId string) (ChurrosId, error) {
24 parts := strings.Split(churrosId, ":")
25 if len(parts) != 2 {
26 return ChurrosId{}, fmt.Errorf("malformed churros global id: %q", churrosId)
27 }
28
29 return ChurrosId{
30 Type: parts[0],
31 LocalID: parts[1],
32 }, nil
33}
34
35// UnmarshalText implements the encoding.TextUnmarshaler interface for ChurrosId.
36func (id *ChurrosId) UnmarshalText(text []byte) error {
37 s := string(text)
38
39 parsed, err := ParseChurrosId(s)
40 if err != nil {
41 return err
42 }
43
44 id.Type = parsed.Type
45 id.LocalID = parsed.LocalID
46
47 return nil
48}
49
50func (msg Message) CreateInDatabaseNotifications(groupId string, subs []Subscription) {
51 if config.DryRunMode {
52 ll.Warn("dry run mode enabled, not creating notifications in database")
53 return
54 }
55 // Create sequentially: this is not something that has to be done fast, and parallelizing would swamp the database connections
56 for _, sub := range subs {
57 prisma.Notification.CreateOne(
58 db.Notification.Subscription.Link(
59 db.NotificationSubscription.Endpoint.Equals(sub.Webpush.Endpoint),
60 ),
61 db.Notification.Title.Set(msg.Title),
62 db.Notification.Body.Set(msg.Body),
63 db.Notification.ID.Set(msg.Id),
64 db.Notification.Channel.Set(msg.Channel()),
65 db.Notification.Group.Link(db.Group.ID.Equals(groupId)),
66 db.Notification.Goto.Set(msg.Action),
67 db.Notification.Timestamp.Set(msg.SendAt),
68 ).Exec(context.Background())
69 }
70}
71
72func ConnectToDababase() error {
73 return prisma.Connect()
74}
75
76// Group returns the Churros group ID responsible for the notification
77func (msg Message) Group() (string, error) {
78 switch msg.Event {
79 case EventNewPost:
80 post, err := prisma.Article.FindUnique(
81 db.Article.ID.Equals(msg.ChurrosObjectId),
82 ).Select(
83 db.Article.GroupID.Field(),
84 ).Exec(context.Background())
85 if err != nil {
86 return "", fmt.Errorf("while getting the group responsible for the notification: %w", err)
87 }
88
89 return post.GroupID, nil
90 case EventShotgunClosesSoon, EventShotgunOpensSoon:
91 event, err := prisma.Event.FindUnique(
92 db.Event.ID.Equals(msg.ChurrosObjectId),
93 ).Exec(context.Background())
94 if err != nil {
95 return "", fmt.Errorf("while getting the group responsible for the notification: %w", err)
96 }
97 return event.GroupID, nil
98 case EventCustom, EventGodchildAccepted, EventGodchildRejected, EventGodchildRequest, EventTest:
99 return "", nil
100 }
101
102 return "", fmt.Errorf("unknown event type %q", msg.Event)
103}
104
105func (msg Message) Channel() db.NotificationChannel {
106 switch msg.Event {
107 case EventNewPost:
108 return db.NotificationChannelArticles
109 case EventShotgunClosesSoon, EventShotgunOpensSoon:
110 return db.NotificationChannelShotguns
111 case EventGodchildRequest, EventGodchildAccepted, EventGodchildRejected:
112 return db.NotificationChannelGodparentRequests
113 }
114
115 return db.NotificationChannelOther
116}