offline-twitter/pkg/webserver/handler_notifications.go

60 lines
1.4 KiB
Go
Raw Normal View History

2024-08-31 23:23:22 -07:00
package webserver
import (
"net/http"
2024-11-01 23:23:03 -07:00
"strconv"
2024-11-06 21:54:21 -08:00
"strings"
2024-08-31 23:23:22 -07:00
)
func (app *Application) Notifications(w http.ResponseWriter, r *http.Request) {
2024-11-06 21:54:21 -08:00
app.traceLog.Printf("'Notifications' handler (path: %q)", r.URL.Path)
2024-12-02 20:30:56 -08:00
if app.ActiveUser.ID == 0 {
app.error_401(w, r)
return
}
2024-11-06 21:54:21 -08:00
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if parts[0] == "mark-all-as-read" {
app.NotificationsMarkAsRead(w, r)
return
}
2024-11-01 23:23:03 -07:00
cursor_val := 0
cursor_param := r.URL.Query().Get("cursor")
if cursor_param != "" {
var err error
cursor_val, err = strconv.Atoi(cursor_param)
if err != nil {
app.error_400_with_message(w, r, "invalid cursor (must be a number)")
return
}
}
2024-08-31 23:23:22 -07:00
2024-11-01 23:23:03 -07:00
feed := app.Profile.GetNotificationsForUser(app.ActiveUser.ID, int64(cursor_val), 50) // TODO: parameterizable
if is_htmx(r) && cursor_val != 0 {
// It's a Show More request
app.buffered_render_htmx(w, "timeline", PageGlobalData{TweetTrove: feed.TweetTrove}, feed)
} else {
app.buffered_render_page(w, "tpl/notifications.tpl", PageGlobalData{TweetTrove: feed.TweetTrove}, feed)
}
2024-08-31 23:23:22 -07:00
}
2024-11-06 21:54:21 -08:00
func (app *Application) NotificationsMarkAsRead(w http.ResponseWriter, r *http.Request) {
2024-12-02 20:30:56 -08:00
if app.IsScrapingDisabled {
app.error_401(w, r)
return
}
2024-11-06 21:54:21 -08:00
err := app.API.MarkNotificationsAsRead()
if err != nil {
panic(err)
}
app.toast(w, r, 200, Toast{
2024-11-06 21:54:21 -08:00
Title: "Success",
Message: `Notifications marked as "read"`,
Type: "success",
AutoCloseDelay: 2000,
})
}