Notification preferences

This commit is contained in:
2024-09-17 14:50:05 +08:00
parent df9fb0a92a
commit f287e295e5
9 changed files with 182 additions and 6 deletions

View File

@ -24,6 +24,12 @@ func MapAPIs(app *fiber.App, baseURL string) {
notify.Put("/read/:notificationId", markNotificationRead)
}
preferences := api.Group("/preferences").Name("Preferences API")
{
preferences.Get("/notifications", getNotificationPreference)
preferences.Put("/notifications", updateNotificationPreference)
}
api.Get("/users/lookup", lookupAccount)
api.Get("/users/search", searchAccount)

View File

@ -2,6 +2,7 @@ package api
import (
"fmt"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
@ -52,6 +53,7 @@ func notifyUser(c *fiber.Ctx) error {
Picture: data.Picture,
IsRealtime: data.IsRealtime,
IsForcePush: data.IsForcePush,
Account: target,
AccountID: target.ID,
SenderID: &client.ID,
}

View File

@ -0,0 +1,43 @@
package api
import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)
func getNotificationPreference(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
notification, err := services.GetNotificationPreference(user)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.JSON(notification)
}
func updateNotificationPreference(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var data struct {
Config map[string]bool `json:"config"`
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
notification, err := services.UpdateNotificationPreference(user, data.Config)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(notification)
}