This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

1package notella 2 3import ( 4 "context" 5 "fmt" 6 7 "git.inpt.fr/churros/notella/db" 8 "github.com/SherClockHolmes/webpush-go" 9 ll "github.com/gwennlbh/label-logger-go" 10) 11 12type SubscriptionOwner struct { 13 Id string `json:"id"` 14 Uid string `json:"uid"` 15 FirstName string `json:"firstName"` 16 LastName string `json:"lastName"` 17} 18 19type Subscription struct { 20 Webpush webpush.Subscription `json:"webpush"` 21 Owner SubscriptionOwner `json:"owner"` 22} 23 24func (msg Message) ShouldSendTo() (subs []Subscription, userIds []string, err error) { 25 if msg.Event == EventTest { 26 sub, err := prisma.NotificationSubscription.FindUnique(db.NotificationSubscription.ID.Equals(msg.ChurrosObjectId)).With(db.NotificationSubscription.Owner.Fetch()).Exec(context.Background()) 27 return []Subscription{SubscriptionFromDatabase(*sub)}, []string{}, err 28 } 29 30 users, err := Receivers(msg) 31 if err != nil { 32 return []Subscription{}, users, fmt.Errorf("could not determine who to send the notification to: %w", err) 33 } 34 35 ll.Debug("Sending notification for %s on %s to %d users: %v", msg.Event, msg.ChurrosObjectId, len(users), users) 36 37 subs, err = subscriptionsOfUsers(users) 38 if err != nil { 39 return []Subscription{}, users, fmt.Errorf("could not determine which subscriptions to send the notification to: %w", err) 40 } 41 42 return subs, []string{}, nil 43} 44 45func subscriptionsOfUsers(ids []string) (subscriptions []Subscription, err error) { 46 if err := prisma.Prisma.Connect(); err != nil { 47 return nil, fmt.Errorf("could not connect to prisma: %w", err) 48 } 49 subs, err := prisma.NotificationSubscription.FindMany( 50 db.NotificationSubscription.OwnerID.In(ids), 51 ).With(db.NotificationSubscription.Owner.Fetch()).Exec(context.Background()) 52 53 if err != nil { 54 return subscriptions, fmt.Errorf("while getting notification subscriptions from database: %w", err) 55 } 56 57 for _, sub := range subs { 58 subscriptions = append(subscriptions, SubscriptionFromDatabase(sub)) 59 } 60 61 ll.Debug("Found %d subscriptions for %d users %v", len(subscriptions), len(ids), ids) 62 63 return subscriptions, nil 64} 65 66func SubscriptionFromDatabase(sub db.NotificationSubscriptionModel) Subscription { 67 return Subscription{ 68 Webpush: webpush.Subscription{ 69 Endpoint: sub.Endpoint, 70 Keys: webpush.Keys{ 71 Auth: sub.AuthKey, 72 P256dh: sub.P256DhKey, 73 }, 74 }, 75 Owner: SubscriptionOwner{ 76 Id: sub.OwnerID, 77 Uid: sub.Owner().UID, 78 FirstName: sub.Owner().FirstName, 79 LastName: sub.Owner().LastName, 80 }, 81 } 82} 83 84func (sub Subscription) Destroy() error { 85 _, err := prisma.NotificationSubscription.FindUnique( 86 db.NotificationSubscription.Endpoint.Equals(sub.Webpush.Endpoint), 87 ).Delete().Exec(context.Background()) 88 return err 89} 90 91func FindSubscriptionByNativeToken(token string, subs []Subscription) (Subscription, bool) { 92 for _, sub := range subs { 93 if sub.FirebaseToken() == token { 94 return sub, true 95 } 96 } 97 return Subscription{}, false 98}