diff --git a/pages/index/apiroutes/apiroutes.go b/pages/index/apiroutes/apiroutes.go index 2eaee0d6..738896bd 100644 --- a/pages/index/apiroutes/apiroutes.go +++ b/pages/index/apiroutes/apiroutes.go @@ -13,7 +13,6 @@ import ( "github.com/animenotifier/notify.moe/pages/editor/jobs" "github.com/animenotifier/notify.moe/pages/me" "github.com/animenotifier/notify.moe/pages/notifications" - "github.com/animenotifier/notify.moe/pages/notifications/feed" "github.com/animenotifier/notify.moe/pages/popular" "github.com/animenotifier/notify.moe/pages/soundtrack" "github.com/animenotifier/notify.moe/pages/upload" @@ -34,6 +33,7 @@ func Register(l *layout.Layout, app *aero.Application) { app.Get("/api/test/notification", notifications.Test) app.Get("/api/count/notifications/unseen", notifications.CountUnseen) app.Get("/api/mark/notifications/seen", notifications.MarkNotificationsAsSeen) + app.Get("/api/user/:id/notifications/latest", notifications.Latest) app.Get("/api/random/soundtrack", soundtrack.Random) app.Get("/api/next/soundtrack", soundtrack.Next) @@ -50,9 +50,4 @@ func Register(l *layout.Layout, app *aero.Application) { // Jobs app.Post("/api/job/:job/start", jobs.Start) - - // Feed - app.Get("/api/user/:id/notifications/feed", notificationsfeed.JSON) - app.Get("/api/user/:id/notifications/feed/atom", notificationsfeed.Atom) - app.Get("/api/user/:id/notifications/feed/rss", notificationsfeed.RSS) } diff --git a/pages/notifications/api.go b/pages/notifications/api.go index 982597ea..11c4e0ab 100644 --- a/pages/notifications/api.go +++ b/pages/notifications/api.go @@ -2,6 +2,7 @@ package notifications import ( "net/http" + "sort" "strconv" "github.com/aerogo/aero" @@ -40,6 +41,29 @@ func MarkNotificationsAsSeen(ctx *aero.Context) string { return "ok" } +// Latest returns the latest notifications. +func Latest(ctx *aero.Context) string { + userID := ctx.Get("id") + user, err := arn.GetUser(userID) + + if err != nil { + return ctx.Error(http.StatusBadRequest, "Invalid user ID") + } + + notifications := user.Notifications().Notifications() + + // Sort by date + sort.Slice(notifications, func(i, j int) bool { + return notifications[i].Created > notifications[j].Created + }) + + if len(notifications) > maxNotifications { + notifications = notifications[:maxNotifications] + } + + return ctx.JSON(notifications) +} + // Test sends a test notification to the logged in user. func Test(ctx *aero.Context) string { user := utils.GetUser(ctx) diff --git a/pages/notifications/feed/feed.go b/pages/notifications/feed/feed.go deleted file mode 100644 index b70e6a5b..00000000 --- a/pages/notifications/feed/feed.go +++ /dev/null @@ -1,23 +0,0 @@ -package notificationsfeed - -import ( - "github.com/aerogo/aero" -) - -// maxNotifications indicates how many notifications are shown in the feed. -const maxNotifications = 20 - -// RSS returns a notifications feed in RSS format. -func RSS(ctx *aero.Context) string { - return "reserved" -} - -// JSON returns a notifications feed in JSON format. -func JSON(ctx *aero.Context) string { - return "reserved" -} - -// Atom returns a notifications feed in Atom format. -func Atom(ctx *aero.Context) string { - return "reserved" -}