-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriptions.go
70 lines (60 loc) · 1.76 KB
/
subscriptions.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
package notella
import (
"context"
"fmt"
"git.inpt.fr/churros/notella/db"
"github.com/SherClockHolmes/webpush-go"
)
type SubscriptionOwner struct {
Id string `json:"id"`
Uid string `json:"uid"`
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
}
type Subscription struct {
Webpush webpush.Subscription `json:"webpush"`
Owner SubscriptionOwner `json:"owner"`
}
func subscriptionsOfUsers(ids []string) (subscriptions []Subscription, err error) {
if err := prisma.Prisma.Connect(); err != nil {
return nil, fmt.Errorf("could not connect to prisma: %w", err)
}
subs, err := prisma.NotificationSubscription.FindMany(
db.NotificationSubscription.OwnerID.In(ids),
).With(db.NotificationSubscription.Owner.Fetch()).Exec(context.Background())
if err != nil {
return subscriptions, fmt.Errorf("while getting notification subscriptions from database: %w", err)
}
for _, sub := range subs {
subscriptions = append(subscriptions, Subscription{
Webpush: webpush.Subscription{
Endpoint: sub.Endpoint,
Keys: webpush.Keys{
Auth: sub.AuthKey,
P256dh: sub.P256DhKey,
},
},
Owner: SubscriptionOwner{
Id: sub.OwnerID,
Uid: sub.Owner().UID,
FirstName: sub.Owner().FirstName,
LastName: sub.Owner().LastName,
},
})
}
return subscriptions, nil
}
func (sub Subscription) Destroy() error {
_, err := prisma.NotificationSubscription.FindUnique(
db.NotificationSubscription.Endpoint.Equals(sub.Webpush.Endpoint),
).Delete().Exec(context.Background())
return err
}
func FindSubscriptionByNativeToken(token string, subs []Subscription) (Subscription, bool) {
for _, sub := range subs {
if sub.FirebaseToken() == token {
return sub, true
}
}
return Subscription{}, false
}