46 lines
944 B
Go
Raw Normal View History

2017-07-14 23:50:34 +02:00
package notifications
import (
"net/http"
2018-02-27 15:27:16 +01:00
"sort"
2017-07-14 23:50:34 +02:00
2019-06-03 18:32:43 +09:00
"github.com/animenotifier/notify.moe/arn"
2017-07-14 23:50:34 +02:00
"github.com/aerogo/aero"
2018-02-27 15:27:16 +01:00
"github.com/animenotifier/notify.moe/components"
2017-07-14 23:50:34 +02:00
)
2018-03-01 00:10:35 +01:00
const maxNotifications = 30
2018-03-01 00:06:03 +01:00
2018-03-01 23:18:29 +01:00
// ByUser shows all notifications sent to the given user.
2019-06-01 13:55:49 +09:00
func ByUser(ctx aero.Context) error {
2019-11-17 16:59:34 +09:00
user := arn.GetUserFromContext(ctx)
2018-02-27 15:27:16 +01:00
if user == nil {
2018-07-07 12:42:00 +09:00
return ctx.Error(http.StatusBadRequest, "Not logged in")
2018-02-27 15:27:16 +01: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 15:27:16 +01:00
// Sort by date
sort.Slice(notifications, func(i, j int) bool {
return notifications[i].Created > notifications[j].Created
})
2018-03-01 00:06:03 +01:00
// Limit results
if len(notifications) > maxNotifications {
notifications = notifications[:maxNotifications]
2018-02-28 16:26:49 +01:00
}
return ctx.HTML(components.Notifications(notifications, viewUser, user))
2017-07-14 23:50:34 +02:00
}