-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ef9d18
commit 33904f9
Showing
1 changed file
with
59 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,64 @@ | ||
package cron | ||
|
||
//lint:ignore U1000 stub | ||
import ( | ||
"github.com/TUM-Dev/Campus-Backend/server/model" | ||
log "github.com/sirupsen/logrus" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func (c *CronService) alarmCron() error { | ||
// TODO: implement | ||
return notifyAllUsers(c.db, sendGCMNotification) | ||
} | ||
|
||
func sendGCMNotification(*[]model.Device) error { | ||
log.Trace("sendGCMNotification") | ||
return nil | ||
} | ||
|
||
func notifyAllUsers(db *gorm.DB, callbacks ...func(*[]model.Device) error) error { | ||
var pendingNotifications []model.Notification | ||
if err := db. | ||
Joins("notification_confirmation"). | ||
Where("notification_confirmation.sent = ?", false). | ||
Group("notification"). | ||
Find(&pendingNotifications).Error; err != nil { | ||
log.WithError(err).Error("failed to get pending notifications from the db") | ||
return err | ||
} | ||
for _, pending := range pendingNotifications { | ||
// Get a few targets | ||
var targets []model.Device | ||
if err := db. | ||
Distinct("device"). | ||
Where("notification = ?", pending.Notification). | ||
Where("gcmStatus IS NULL"). | ||
Where("gcmToken IS NOT NULL"). | ||
Joins("notification_confirmation"). | ||
Where("notification_confirmation.sent = ?", false). | ||
Limit(998). | ||
Find(&targets). | ||
Error; err != nil { | ||
log.WithError(err).Error("failed to get devices which should receive the notification") | ||
continue | ||
} | ||
if len(targets) > 0 { | ||
for _, callback := range callbacks { | ||
if err := callback(&targets); err != nil { | ||
log.WithError(err).Error("callback failed") | ||
continue | ||
} | ||
} | ||
// mark as sent for these targets | ||
if err := db. | ||
Model(&model.NotificationConfirmation{}). | ||
Where("notification = ?", pending.Notification). | ||
Where("device IN ?", targets). | ||
Update("sent", true). | ||
Error; err != nil { | ||
log.WithError(err).Error("failed to update notification_confirmation") | ||
continue | ||
} | ||
} | ||
} | ||
return nil | ||
} |