Batch mark notify as read API

This commit is contained in:
LittleSheep 2024-04-28 19:59:27 +08:00
parent 3e9c84a284
commit 6ec48aaa8a
3 changed files with 28 additions and 5 deletions

View File

@ -6,6 +6,8 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Bug fixes"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Bug fixes">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/notifications_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/server/notifications_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/startup.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/server/startup.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />

View File

@ -48,17 +48,37 @@ func markNotificationRead(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("principal").(models.Account)
id, _ := c.ParamsInt("notificationId", 0) id, _ := c.ParamsInt("notificationId", 0)
var data models.Notification var notify models.Notification
if err := database.C.Where(&models.Notification{ if err := database.C.Where(&models.Notification{
BaseModel: models.BaseModel{ID: uint(id)}, BaseModel: models.BaseModel{ID: uint(id)},
RecipientID: user.ID, RecipientID: user.ID,
}).First(&data).Error; err != nil { }).First(&notify).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }
data.ReadAt = lo.ToPtr(time.Now()) notify.ReadAt = lo.ToPtr(time.Now())
if err := database.C.Save(&data).Error; err != nil { if err := database.C.Save(&notify).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
}
}
func markNotificationReadBatch(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data struct {
MessageIDs []uint `json:"messages"`
}
if err := utils.BindAndValidate(c, &data); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if err := database.C.Model(&models.Notification{}).
Where("recipient_id = ? AND id IN ?", user.ID, data.MessageIDs).
Updates(&models.Notification{ReadAt: lo.ToPtr(time.Now())}).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)

View File

@ -71,8 +71,9 @@ func NewServer() {
notify := api.Group("/notifications").Name("Notifications API") notify := api.Group("/notifications").Name("Notifications API")
{ {
notify.Get("/", authMiddleware, getNotifications) notify.Get("/", authMiddleware, getNotifications)
notify.Put("/:notificationId/read", authMiddleware, markNotificationRead)
notify.Post("/subscribe", authMiddleware, addNotifySubscriber) notify.Post("/subscribe", authMiddleware, addNotifySubscriber)
notify.Put("/batch/read", authMiddleware, markNotificationReadBatch)
notify.Put("/:notificationId/read", authMiddleware, markNotificationRead)
notify.Get("/listen", authMiddleware, websocket.New(listenNotifications)) notify.Get("/listen", authMiddleware, websocket.New(listenNotifications))
} }