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) 10 11type SubscriptionOwner struct { 12 Id string `json:"id"` 13 Uid string `json:"uid"` 14 FirstName string `json:"firstName"` 15 LastName string `json:"lastName"` 16} 17 18type Subscription struct { 19 Webpush webpush.Subscription `json:"webpush"` 20 Owner SubscriptionOwner `json:"owner"` 21} 22 23func subscriptionsOfUsers(ids []string) (subscriptions []Subscription, err error) { 24 if err := prisma.Prisma.Connect(); err != nil { 25 return nil, fmt.Errorf("could not connect to prisma: %w", err) 26 } 27 subs, err := prisma.NotificationSubscription.FindMany( 28 db.NotificationSubscription.OwnerID.In(ids), 29 ).With(db.NotificationSubscription.Owner.Fetch()).Exec(context.Background()) 30 31 if err != nil { 32 return subscriptions, fmt.Errorf("while getting notification subscriptions from database: %w", err) 33 } 34 35 for _, sub := range subs { 36 subscriptions = append(subscriptions, Subscription{ 37 Webpush: webpush.Subscription{ 38 Endpoint: sub.Endpoint, 39 Keys: webpush.Keys{ 40 Auth: sub.AuthKey, 41 P256dh: sub.P256DhKey, 42 }, 43 }, 44 Owner: SubscriptionOwner{ 45 Id: sub.OwnerID, 46 Uid: sub.Owner().UID, 47 FirstName: sub.Owner().FirstName, 48 LastName: sub.Owner().LastName, 49 }, 50 }) 51 } 52 53 return subscriptions, nil 54} 55 56func (sub Subscription) Destroy() error { 57 _, err := prisma.NotificationSubscription.FindUnique( 58 db.NotificationSubscription.Endpoint.Equals(sub.Webpush.Endpoint), 59 ).Delete().Exec(context.Background()) 60 return err 61} 62 63func FindSubscriptionByNativeToken(token string, subs []Subscription) (Subscription, bool) { 64 for _, sub := range subs { 65 if sub.FirebaseToken() == token { 66 return sub, true 67 } 68 } 69 return Subscription{}, false 70}