feat(notifications): align chatwoot notification payloads
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
applogger "github.com/gochat/gochat/pkg/logger"
|
||||
"github.com/gochat/gochat/pkg/pagination"
|
||||
@@ -34,19 +37,34 @@ func (h *NotificationHandler) List(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
userID := getUserID(c)
|
||||
p := pagination.Parse(c)
|
||||
if p.PerPage == pagination.DefaultPerPage && c.Query("per_page") == "" {
|
||||
p.PerPage = 15
|
||||
}
|
||||
|
||||
notifications, total, err := h.notificationService.ListNotificationsByAccount(c.Request.Context(), userID, accountID, p.Page, p.PerPage)
|
||||
result, err := h.notificationService.ListNotificationsByAccountWithOptions(c.Request.Context(), userID, accountID, p.Page, p.PerPage, service.NotificationListOptions{
|
||||
IncludeRead: notificationIncludes(c, "read"),
|
||||
IncludeSnoozed: notificationIncludes(c, "snoozed"),
|
||||
SortOrder: c.DefaultQuery("sort_order", "desc"),
|
||||
})
|
||||
if err != nil {
|
||||
applogger.L().Errorf("List notifications: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to fetch notifications")
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{
|
||||
"notifications": notifications,
|
||||
"page": p.Page,
|
||||
"per_page": p.PerPage,
|
||||
"total": total,
|
||||
payload := make([]gin.H, 0, len(result.Notifications))
|
||||
for i := range result.Notifications {
|
||||
payload = append(payload, serializeNotification(&result.Notifications[i]))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": gin.H{
|
||||
"meta": gin.H{
|
||||
"unread_count": result.UnreadCount,
|
||||
"count": result.Total,
|
||||
"current_page": p.Page,
|
||||
},
|
||||
"payload": payload,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -69,7 +87,7 @@ func (h *NotificationHandler) Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, notification)
|
||||
c.JSON(http.StatusOK, serializeNotification(notification))
|
||||
}
|
||||
|
||||
// Update marks a notification as read.
|
||||
@@ -95,7 +113,7 @@ func (h *NotificationHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, notification)
|
||||
c.JSON(http.StatusOK, serializeNotification(notification))
|
||||
}
|
||||
|
||||
// MarkAllRead marks all unread notifications as read for the current user in account.
|
||||
@@ -104,8 +122,19 @@ func (h *NotificationHandler) Update(c *gin.Context) {
|
||||
func (h *NotificationHandler) MarkAllRead(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
userID := getUserID(c)
|
||||
var req struct {
|
||||
PrimaryActorType string `json:"primary_actor_type"`
|
||||
PrimaryActorID uint `json:"primary_actor_id"`
|
||||
}
|
||||
_ = bindOptionalNotificationJSON(c, &req)
|
||||
|
||||
if err := h.notificationService.MarkAllReadByAccount(c.Request.Context(), userID, accountID); err != nil {
|
||||
var err error
|
||||
if req.PrimaryActorType != "" && req.PrimaryActorID > 0 {
|
||||
err = h.notificationService.MarkPrimaryActorReadByAccount(c.Request.Context(), userID, accountID, req.PrimaryActorType, req.PrimaryActorID)
|
||||
} else {
|
||||
err = h.notificationService.MarkAllReadByAccount(c.Request.Context(), userID, accountID)
|
||||
}
|
||||
if err != nil {
|
||||
applogger.L().Errorf("MarkAllRead notifications: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to mark notifications as read")
|
||||
return
|
||||
@@ -127,7 +156,7 @@ func (h *NotificationHandler) UnreadCount(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{"unread_count": count})
|
||||
c.JSON(http.StatusOK, count)
|
||||
}
|
||||
|
||||
// Snooze snoozes a notification until a specified time.
|
||||
@@ -163,7 +192,7 @@ func (h *NotificationHandler) Snooze(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, notification)
|
||||
c.JSON(http.StatusOK, serializeNotification(notification))
|
||||
}
|
||||
|
||||
// Unread marks a notification as unread.
|
||||
@@ -185,7 +214,7 @@ func (h *NotificationHandler) Unread(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, notification)
|
||||
c.JSON(http.StatusOK, serializeNotification(notification))
|
||||
}
|
||||
|
||||
// Destroy deletes a single notification.
|
||||
@@ -214,11 +243,21 @@ func (h *NotificationHandler) Destroy(c *gin.Context) {
|
||||
func (h *NotificationHandler) DestroyAll(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
userID := getUserID(c)
|
||||
// Chatwoot supports type=read filter; GoChat currently deletes all regardless
|
||||
// TODO: implement type=read filter when async job system is available
|
||||
_ = c.Query("type")
|
||||
deleteType := c.Query("type")
|
||||
var req struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
if err := bindOptionalNotificationJSON(c, &req); err == nil && req.Type != "" {
|
||||
deleteType = req.Type
|
||||
}
|
||||
|
||||
if err := h.notificationService.DeleteAllNotifications(c.Request.Context(), userID, accountID); err != nil {
|
||||
var err error
|
||||
if deleteType == "read" {
|
||||
err = h.notificationService.DeleteReadNotifications(c.Request.Context(), userID, accountID)
|
||||
} else {
|
||||
err = h.notificationService.DeleteAllNotifications(c.Request.Context(), userID, accountID)
|
||||
}
|
||||
if err != nil {
|
||||
applogger.L().Errorf("DestroyAll notifications: %v", err)
|
||||
response.AbortWithStatusError(c, http.StatusInternalServerError, response.ErrInternal, "Failed to delete notifications")
|
||||
return
|
||||
@@ -226,3 +265,79 @@ func (h *NotificationHandler) DestroyAll(c *gin.Context) {
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
func notificationIncludes(c *gin.Context, value string) bool {
|
||||
for _, raw := range append(c.QueryArray("includes[]"), c.QueryArray("includes")...) {
|
||||
for _, part := range strings.Split(raw, ",") {
|
||||
if strings.TrimSpace(part) == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func serializeNotification(notification *model.Notification) gin.H {
|
||||
meta := gin.H{}
|
||||
if len(notification.AdditionalAttributes) > 0 {
|
||||
_ = json.Unmarshal(notification.AdditionalAttributes, &meta)
|
||||
}
|
||||
return gin.H{
|
||||
"id": notification.ID,
|
||||
"notification_type": notification.NotificationType,
|
||||
"push_message_title": notificationTitle(notification),
|
||||
"push_message_body": notificationBody(notification),
|
||||
"primary_actor_type": notification.PrimaryActorType,
|
||||
"primary_actor_id": notification.PrimaryActorID,
|
||||
"primary_actor": notificationActor(notification.PrimaryActorType, notification.PrimaryActorID),
|
||||
"read_at": notification.ReadAt,
|
||||
"secondary_actor": notificationActor(notification.SecondaryActorType, notification.SecondaryActorID),
|
||||
"user": gin.H{"id": notification.UserID},
|
||||
"created_at": notification.CreatedAt.Unix(),
|
||||
"last_activity_at": notification.UpdatedAt.Unix(),
|
||||
"snoozed_until": notification.SnoozedUntil,
|
||||
"meta": meta,
|
||||
"additional_attributes": meta,
|
||||
}
|
||||
}
|
||||
|
||||
func notificationActor(actorType string, actorID uint) interface{} {
|
||||
if actorType == "" || actorID == 0 {
|
||||
return nil
|
||||
}
|
||||
return gin.H{"id": actorID, "type": actorType, "meta": gin.H{}}
|
||||
}
|
||||
|
||||
func notificationTitle(notification *model.Notification) string {
|
||||
if title := stringFromNotificationAttrs(notification.AdditionalAttributes, "push_message_title", "title"); title != "" {
|
||||
return title
|
||||
}
|
||||
return notification.NotificationType
|
||||
}
|
||||
|
||||
func notificationBody(notification *model.Notification) string {
|
||||
return stringFromNotificationAttrs(notification.AdditionalAttributes, "push_message_body", "body", "message")
|
||||
}
|
||||
|
||||
func stringFromNotificationAttrs(raw json.RawMessage, keys ...string) string {
|
||||
if len(raw) == 0 {
|
||||
return ""
|
||||
}
|
||||
var attrs map[string]interface{}
|
||||
if err := json.Unmarshal(raw, &attrs); err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, key := range keys {
|
||||
if value, ok := attrs[key].(string); ok {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func bindOptionalNotificationJSON(c *gin.Context, target interface{}) error {
|
||||
if c.Request.Body == nil || c.Request.ContentLength == 0 {
|
||||
return nil
|
||||
}
|
||||
return c.ShouldBindJSON(target)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user