Added latest notifications API route

This commit is contained in:
Eduard Urbach 2018-10-17 09:31:15 +09:00
parent 89adf2942e
commit c6c7a305fe
3 changed files with 25 additions and 29 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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"
}