47 lines
971 B
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"
2017-07-14 21:50:34 +00:00
"github.com/animenotifier/arn"
2017-07-14 21:50:34 +00:00
"github.com/aerogo/aero"
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-28 23:10:35 +00:00
const maxNotifications = 30
2018-02-28 23:06:03 +00:00
2018-03-01 22:18:29 +00:00
// ByUser shows all notifications sent to the given user.
func ByUser(ctx *aero.Context) string {
2018-02-27 14:27:16 +00:00
user := utils.GetUser(ctx)
if user == nil {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusBadRequest, "Not logged in")
2018-02-27 14:27:16 +00:00
}
var viewUser *arn.User
nick := ctx.Get("nick")
if nick != "" {
viewUser, _ = arn.GetUserByNick(nick)
} else {
viewUser = user
}
notifications := viewUser.Notifications().Notifications()
2018-02-27 14:27:16 +00:00
// Sort by date
sort.Slice(notifications, func(i, j int) bool {
return notifications[i].Created > notifications[j].Created
})
2018-02-28 23:06:03 +00:00
// Limit results
if len(notifications) > maxNotifications {
notifications = notifications[:maxNotifications]
2018-02-28 15:26:49 +00:00
}
return ctx.HTML(components.Notifications(notifications, viewUser, user))
2017-07-14 21:50:34 +00:00
}