This repository has no description
0

Configure Feed

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

at main 2.1 kB View raw
1package notella 2 3import ( 4 "encoding/json" 5 "fmt" 6 "sync" 7 "time" 8 9 ll "github.com/gwennlbh/label-logger-go" 10) 11 12func (msg Message) ShouldRun() bool { 13 return time.Now().After(msg.SendAt) 14} 15 16func (msg Message) Run() error { 17 subs, users, err := msg.ShouldSendTo() 18 if err != nil { 19 return fmt.Errorf("could not determine what subscriptions to send the notification to: %w", err) 20 } 21 22 if len(subs) == 0 { 23 ll.Warn("no subscriptions to send notification [dim]%s[reset] ([bold]%s on %s[reset]) to", msg.Id, msg.Event, msg.ChurrosObjectId) 24 return nil 25 } 26 27 group, err := msg.Group() 28 if err != nil { 29 return fmt.Errorf("could not get churros responsible group for %s: %w", msg.ChurrosObjectId, err) 30 } 31 32 ll.Log("Sending", "green", "notification for %s on %s to %d users (%d subscriptions)", msg.Event, msg.ChurrosObjectId, len(users), len(subs)) 33 34 // Separate native and webpush subscriptions 35 nativeSubs := make([]Subscription, 0, len(subs)) 36 webpushSubs := make([]Subscription, 0, len(subs)) 37 for _, sub := range subs { 38 if sub.IsNative() { 39 nativeSubs = append(nativeSubs, sub) 40 } else if sub.IsWebpush() { 41 webpushSubs = append(webpushSubs, sub) 42 } else { 43 ll.Warn("invalid subscription %#v", sub) 44 } 45 } 46 47 var wg sync.WaitGroup 48 wg.Add(3) 49 go func(wg *sync.WaitGroup) { 50 defer wg.Done() 51 msg.CreateInDatabaseNotifications(group, subs) 52 }(&wg) 53 go func(wg *sync.WaitGroup) { 54 defer wg.Done() 55 err := msg.SendToFirebase(group, nativeSubs) 56 if err != nil { 57 ll.ErrorDisplay("could not send notification via firebase", err) 58 } 59 }(&wg) 60 go func(wg *sync.WaitGroup) { 61 defer wg.Done() 62 err := msg.SendWebPush(group, webpushSubs) 63 if err != nil { 64 ll.ErrorDisplay("could not send notification via webpush", err) 65 } 66 }(&wg) 67 wg.Wait() 68 69 return nil 70} 71 72func (msg Message) JSONString() string { 73 out, err := json.Marshal(msg) 74 if err != nil { 75 return "" 76 } 77 return string(out) 78} 79 80func (msg Message) JSONBytes() []byte { 81 out, err := json.Marshal(msg) 82 if err != nil { 83 return nil 84 } 85 return out 86} 87 88func (msg Message) String() string { 89 return fmt.Sprintf("%-10s | %-10s on %s", msg.Id, msg.Event, msg.ChurrosObjectId) 90}