67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
|
package notifications
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
"strconv"
|
||
|
|
||
|
"github.com/aerogo/aero"
|
||
|
"github.com/animenotifier/arn"
|
||
|
"github.com/animenotifier/notify.moe/utils"
|
||
|
)
|
||
|
|
||
|
// CountUnseen sends the number of unseen notifications.
|
||
|
func CountUnseen(ctx *aero.Context) string {
|
||
|
user := utils.GetUser(ctx)
|
||
|
|
||
|
if user == nil {
|
||
|
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
|
||
|
}
|
||
|
|
||
|
notifications := user.Notifications().Notifications()
|
||
|
unseen := 0
|
||
|
|
||
|
for _, notification := range notifications {
|
||
|
if notification.Seen == "" {
|
||
|
unseen++
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ctx.Text(strconv.Itoa(unseen))
|
||
|
}
|
||
|
|
||
|
// MarkNotificationsAsSeen marks all notifications as seen.
|
||
|
func MarkNotificationsAsSeen(ctx *aero.Context) string {
|
||
|
user := utils.GetUser(ctx)
|
||
|
|
||
|
if user == nil {
|
||
|
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
|
||
|
}
|
||
|
|
||
|
notifications := user.Notifications().Notifications()
|
||
|
|
||
|
for _, notification := range notifications {
|
||
|
notification.Seen = arn.DateTimeUTC()
|
||
|
notification.Save()
|
||
|
}
|
||
|
|
||
|
return "ok"
|
||
|
}
|
||
|
|
||
|
// Test sends a test notification to the logged in user.
|
||
|
func Test(ctx *aero.Context) string {
|
||
|
user := utils.GetUser(ctx)
|
||
|
|
||
|
if user == nil {
|
||
|
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
|
||
|
}
|
||
|
|
||
|
user.SendNotification(&arn.PushNotification{
|
||
|
Title: "Anime Notifier",
|
||
|
Message: "Yay, it works!",
|
||
|
Icon: "https://" + ctx.App.Config.Domain + "/images/brand/220.png",
|
||
|
Type: arn.NotificationTypeTest,
|
||
|
})
|
||
|
|
||
|
return "ok"
|
||
|
}
|