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
|
|
|
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2018-03-01 21:52:01 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
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.
|
2019-06-01 04:55:49 +00:00
|
|
|
func ByUser(ctx aero.Context) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2018-02-27 14:27:16 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-03-01 21:52:01 +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
|
|
|
}
|
|
|
|
|
2018-03-01 21:52:01 +00:00
|
|
|
return ctx.HTML(components.Notifications(notifications, viewUser, user))
|
2017-07-14 21:50:34 +00:00
|
|
|
}
|