Files
libredesk/cmd/user_notifications.go
T
Abhinav Raut 6655a9fcb0 fix reply alerts and browser push subscriptions
Send every reply alert while batching email for five minutes. Keep shared browser subscriptions tied to the signed-in agent.
2026-09-16 00:16:12 +05:30

171 lines
4.9 KiB
Go

package main
import (
"strconv"
amodels "github.com/abhinavxd/libredesk/internal/auth/models"
"github.com/abhinavxd/libredesk/internal/envelope"
notifier "github.com/abhinavxd/libredesk/internal/notification"
nmodels "github.com/abhinavxd/libredesk/internal/notification/models"
"github.com/valyala/fasthttp"
"github.com/zerodha/fastglue"
)
func handleGetUserNotifications(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
limit = 20
offset = 0
)
if l := r.RequestCtx.QueryArgs().GetUintOrZero("limit"); l > 0 && l <= 100 {
limit = l
}
if o := r.RequestCtx.QueryArgs().GetUintOrZero("offset"); o > 0 {
offset = o
}
notifications, err := app.userNotification.GetAll(auser.ID, limit, offset)
if err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(notifications)
}
func handleGetUserNotificationStats(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
)
stats, err := app.userNotification.GetStats(auser.ID)
if err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(stats)
}
func handleMarkNotificationAsRead(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest,
app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.InputError)
}
if err := app.userNotification.MarkAsRead(id, auser.ID); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
func handleMarkAllNotificationsAsRead(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
)
if err := app.userNotification.MarkAllAsRead(auser.ID); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
func handleDeleteNotification(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
id, _ = strconv.Atoi(r.RequestCtx.UserValue("id").(string))
)
if id <= 0 {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest,
app.i18n.T("globals.messages.somethingWentWrong"), nil, envelope.InputError)
}
if err := app.userNotification.Delete(id, auser.ID); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
func handleDeleteAllNotifications(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
)
if err := app.userNotification.DeleteAll(auser.ID); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
func handleGetNotificationPreferences(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
)
prefs, err := app.notificationPref.GetMatrix(auser.ID)
if err != nil {
return sendErrorEnvelope(r, err)
}
pushEndpoints, err := app.pushNotification.Endpoints(auser.ID)
if err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(map[string]any{
"preferences": prefs,
"email_enabled": ko.Bool("notification.email.enabled"),
"vapid_public_key": app.pushNotification.PublicKey(),
"push_endpoints": pushEndpoints,
})
}
func handleUpdateNotificationPreferences(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
prefs []nmodels.NotificationPreference
)
if err := r.Decode(&prefs, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest,
app.i18n.T("errors.parsingRequest"), nil, envelope.InputError)
}
if err := app.notificationPref.Update(auser.ID, prefs); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
func handleCreatePushSubscription(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
subscription notifier.PushSubscriptionInput
)
if err := r.Decode(&subscription, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("errors.parsingRequest"), nil, envelope.InputError)
}
if err := app.pushNotification.Upsert(auser.ID, subscription); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}
func handleDeletePushSubscription(r *fastglue.Request) error {
var (
app = r.Context.(*App)
auser = r.RequestCtx.UserValue("user").(amodels.User)
request struct {
Endpoint string `json:"endpoint"`
}
)
if err := r.Decode(&request, "json"); err != nil {
return r.SendErrorEnvelope(fasthttp.StatusBadRequest, app.i18n.T("errors.parsingRequest"), nil, envelope.InputError)
}
if err := app.pushNotification.Delete(auser.ID, request.Endpoint); err != nil {
return sendErrorEnvelope(r, err)
}
return r.SendEnvelope(true)
}