♻️ Better notification system
This commit is contained in:
		@@ -1,14 +1,11 @@
 | 
			
		||||
package server
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"git.solsynth.dev/hydrogen/passport/pkg/utils"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"git.solsynth.dev/hydrogen/passport/pkg/database"
 | 
			
		||||
	"git.solsynth.dev/hydrogen/passport/pkg/models"
 | 
			
		||||
	"git.solsynth.dev/hydrogen/passport/pkg/services"
 | 
			
		||||
	"git.solsynth.dev/hydrogen/passport/pkg/utils"
 | 
			
		||||
	"github.com/gofiber/fiber/v2"
 | 
			
		||||
	"github.com/samber/lo"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func getNotifications(c *fiber.Ctx) error {
 | 
			
		||||
@@ -16,12 +13,7 @@ func getNotifications(c *fiber.Ctx) error {
 | 
			
		||||
	take := c.QueryInt("take", 0)
 | 
			
		||||
	offset := c.QueryInt("offset", 0)
 | 
			
		||||
 | 
			
		||||
	only_unread := !c.QueryBool("past", false)
 | 
			
		||||
 | 
			
		||||
	tx := database.C.Where(&models.Notification{RecipientID: user.ID}).Model(&models.Notification{})
 | 
			
		||||
	if only_unread {
 | 
			
		||||
		tx = tx.Where("read_at IS NULL")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var count int64
 | 
			
		||||
	var notifications []models.Notification
 | 
			
		||||
@@ -33,7 +25,6 @@ func getNotifications(c *fiber.Ctx) error {
 | 
			
		||||
	if err := tx.
 | 
			
		||||
		Limit(take).
 | 
			
		||||
		Offset(offset).
 | 
			
		||||
		Order("read_at desc").
 | 
			
		||||
		Find(¬ifications).Error; err != nil {
 | 
			
		||||
		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
			
		||||
	}
 | 
			
		||||
@@ -56,9 +47,7 @@ func markNotificationRead(c *fiber.Ctx) error {
 | 
			
		||||
		return fiber.NewError(fiber.StatusNotFound, err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	notify.ReadAt = lo.ToPtr(time.Now())
 | 
			
		||||
 | 
			
		||||
	if err := database.C.Save(¬ify).Error; err != nil {
 | 
			
		||||
	if err := database.C.Delete(¬ify).Error; err != nil {
 | 
			
		||||
		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
			
		||||
	} else {
 | 
			
		||||
		return c.SendStatus(fiber.StatusOK)
 | 
			
		||||
@@ -78,7 +67,7 @@ func markNotificationReadBatch(c *fiber.Ctx) 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 {
 | 
			
		||||
		Delete(&models.Notification{}).Error; err != nil {
 | 
			
		||||
		return fiber.NewError(fiber.StatusInternalServerError, err.Error())
 | 
			
		||||
	} else {
 | 
			
		||||
		return c.SendStatus(fiber.StatusOK)
 | 
			
		||||
 
 | 
			
		||||
@@ -11,10 +11,12 @@ func notifyUser(c *fiber.Ctx) error {
 | 
			
		||||
	var data struct {
 | 
			
		||||
		ClientID     string                    `json:"client_id" validate:"required"`
 | 
			
		||||
		ClientSecret string                    `json:"client_secret" validate:"required"`
 | 
			
		||||
		Type         string                    `json:"type" validate:"required"`
 | 
			
		||||
		Subject      string                    `json:"subject" validate:"required,max=1024"`
 | 
			
		||||
		Content      string                    `json:"content" validate:"required,max=3072"`
 | 
			
		||||
		Content      string                    `json:"content" validate:"required,max=4096"`
 | 
			
		||||
		Metadata     map[string]any            `json:"metadata"`
 | 
			
		||||
		Links        []models.NotificationLink `json:"links"`
 | 
			
		||||
		IsImportant  bool                      `json:"is_important"`
 | 
			
		||||
		IsForcePush  bool                      `json:"is_force_push"`
 | 
			
		||||
		IsRealtime   bool                      `json:"is_realtime"`
 | 
			
		||||
		UserID       uint                      `json:"user_id" validate:"required"`
 | 
			
		||||
	}
 | 
			
		||||
@@ -34,12 +36,12 @@ func notifyUser(c *fiber.Ctx) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	notification := models.Notification{
 | 
			
		||||
		Type:        data.Type,
 | 
			
		||||
		Subject:     data.Subject,
 | 
			
		||||
		Content:     data.Content,
 | 
			
		||||
		Links:       data.Links,
 | 
			
		||||
		IsImportant: data.IsImportant,
 | 
			
		||||
		IsRealtime:  data.IsRealtime,
 | 
			
		||||
		ReadAt:      nil,
 | 
			
		||||
		IsForcePush: data.IsForcePush,
 | 
			
		||||
		RecipientID: user.ID,
 | 
			
		||||
		SenderID:    &client.ID,
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user