diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 1d3786f..7191f15 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,12 +4,12 @@
-
-
+
-
-
+
+
+
@@ -155,7 +155,6 @@
-
@@ -180,7 +179,8 @@
-
+
+
true
diff --git a/pkg/internal/server/admin/index.go b/pkg/internal/server/admin/index.go
index d3557c6..a2bace7 100644
--- a/pkg/internal/server/admin/index.go
+++ b/pkg/internal/server/admin/index.go
@@ -11,6 +11,7 @@ func MapAdminAPIs(app *fiber.App) {
admin.Delete("/badges/:badgeId", revokeBadge)
admin.Post("/notify/all", notifyAllUser)
+ admin.Post("/notify/:user", notifyOneUser)
admin.Put("/users/:user/permissions", editUserPermission)
admin.Post("/users/:user/confirm", forceConfirmAccount)
diff --git a/pkg/internal/server/admin/notify_api.go b/pkg/internal/server/admin/notify_api.go
index 69b50bd..9c034e0 100644
--- a/pkg/internal/server/admin/notify_api.go
+++ b/pkg/internal/server/admin/notify_api.go
@@ -27,10 +27,15 @@ func notifyAllUser(c *fiber.Ctx) error {
if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil {
return err
}
+ operator := c.Locals("user").(models.Account)
var users []models.Account
if err := database.C.Find(&users).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
+ } else {
+ services.AddAuditRecord(operator, "notify.all", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{
+ "payload": data,
+ })
}
go func() {
@@ -59,3 +64,57 @@ func notifyAllUser(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
+
+func notifyOneUser(c *fiber.Ctx) error {
+ var data struct {
+ Type string `json:"type" validate:"required"`
+ Subject string `json:"subject" validate:"required,max=1024"`
+ Content string `json:"content" validate:"required,max=4096"`
+ Metadata map[string]any `json:"metadata"`
+ Links []models.NotificationLink `json:"links"`
+ IsForcePush bool `json:"is_force_push"`
+ IsRealtime bool `json:"is_realtime"`
+ UserID uint `json:"user_id"`
+ }
+
+ if err := exts.BindAndValidate(c, &data); err != nil {
+ return err
+ }
+
+ if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil {
+ return err
+ }
+ operator := c.Locals("user").(models.Account)
+
+ var user models.Account
+ if err := database.C.Where("id = ?", data.UserID).First(&user).Error; err != nil {
+ return fiber.NewError(fiber.StatusInternalServerError, err.Error())
+ } else {
+ services.AddAuditRecord(operator, "notify.one", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{
+ "user_id": user.ID,
+ "payload": data,
+ })
+ }
+
+ notification := models.Notification{
+ Type: data.Type,
+ Subject: data.Subject,
+ Content: data.Content,
+ Links: data.Links,
+ IsRealtime: data.IsRealtime,
+ IsForcePush: data.IsForcePush,
+ RecipientID: user.ID,
+ }
+
+ if data.IsRealtime {
+ if err := services.PushNotification(notification); err != nil {
+ log.Error().Err(err).Uint("user", user.ID).Msg("Failed to push notification...")
+ }
+ } else {
+ if err := services.NewNotification(notification); err != nil {
+ log.Error().Err(err).Uint("user", user.ID).Msg("Failed to create notification...")
+ }
+ }
+
+ return c.SendStatus(fiber.StatusOK)
+}
diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go
index e2dba6e..769b65d 100644
--- a/pkg/internal/services/events.go
+++ b/pkg/internal/services/events.go
@@ -31,7 +31,7 @@ func AddAuditRecord(operator models.Account, act, ip, ua string, metadata map[st
})
}
-// SaveEventChanges runs every 60 seconds to save events / audits changes into database
+// SaveEventChanges runs every 60 seconds to save events / audits changes into the database
func SaveEventChanges() {
if len(writeEventQueue) > 0 {
count := len(writeEventQueue)
diff --git a/pkg/internal/services/notifications.go b/pkg/internal/services/notifications.go
index c863e5b..bfbd18a 100644
--- a/pkg/internal/services/notifications.go
+++ b/pkg/internal/services/notifications.go
@@ -42,6 +42,7 @@ func AddNotifySubscriber(user models.Account, provider, id, tk, ua string) (mode
return subscriber, err
}
+// NewNotification will create a notification and push via the push method it
func NewNotification(notification models.Notification) error {
if err := database.C.Save(¬ification).Error; err != nil {
return err
@@ -54,6 +55,9 @@ func NewNotification(notification models.Notification) error {
return nil
}
+// PushNotification will push the notification what ever it is exists record in the database
+// Recommend push another goroutine when you need to push a lot of notification
+// And just use block statement when you just push one notification, the time of create a new sub-process is much more than push notification
func PushNotification(notification models.Notification) error {
for conn := range wsConn[notification.RecipientID] {
_ = conn.WriteMessage(1, models.UnifiedCommand{
@@ -62,7 +66,7 @@ func PushNotification(notification models.Notification) error {
}.Marshal())
}
- // Skip push notify
+ // Skip push notification
if GetStatusDisturbable(notification.RecipientID) != nil {
return nil
}