69 lines
1.5 KiB
Go
Raw Normal View History

2017-07-14 21:50:34 +00:00
package notifications
import (
"net/http"
2018-02-27 14:27:16 +00:00
"sort"
2018-02-28 15:26:49 +00:00
"strconv"
2017-07-14 21:50:34 +00:00
"github.com/aerogo/aero"
2017-07-14 23:32:06 +00:00
"github.com/animenotifier/arn"
2018-02-27 14:27:16 +00:00
"github.com/animenotifier/notify.moe/components"
2017-07-14 21:50:34 +00:00
"github.com/animenotifier/notify.moe/utils"
)
2018-02-27 14:27:16 +00:00
// All shows all notifications sent so far.
func All(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
notifications := user.Notifications().Notifications()
// Sort by date
sort.Slice(notifications, func(i, j int) bool {
return notifications[i].Created > notifications[j].Created
})
return ctx.HTML(components.Notifications(notifications, user))
}
2018-02-28 15:26:49 +00:00
// 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))
}
2018-02-27 14:27:16 +00:00
// Test sends a test notification to the logged in user.
2017-07-14 21:50:34 +00:00
func Test(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
2018-02-27 12:29:47 +00:00
user.SendNotification(&arn.PushNotification{
2017-07-14 23:32:06 +00:00
Title: "Anime Notifier",
Message: "Yay, it works!",
2017-11-04 21:23:07 +00:00
Icon: "https://" + ctx.App.Config.Domain + "/images/brand/220.png",
2018-02-28 10:30:47 +00:00
Type: arn.NotificationTypeTest,
2018-02-27 12:29:47 +00:00
})
2017-07-14 23:32:06 +00:00
2017-07-14 21:50:34 +00:00
return "ok"
}