Admin notify one user

This commit is contained in:
LittleSheep 2024-07-03 23:07:59 +08:00
parent 74819c1c2b
commit 182a389180
5 changed files with 72 additions and 8 deletions

12
.idea/workspace.xml generated
View File

@ -4,12 +4,12 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":zap: Optimized audit, event logging system&#10;:sparkles: Audit logs&#10;:sparkles: Admin edit user permissions"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Admin force confirm account">
<change afterPath="$PROJECT_DIR$/pkg/internal/server/admin/user_api.go" afterDir="false" />
<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/internal/server/admin/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/admin/index.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/server/admin/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/admin/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/admin/permissions_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/admin/permissions_api.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/server/admin/notify_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/admin/notify_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/internal/services/events.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/events.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/notifications.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" />
@ -155,7 +155,6 @@
</option> </option>
</component> </component>
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<MESSAGE value=":bug: Fix status expired in cache" />
<MESSAGE value=":bug: Fix online condition" /> <MESSAGE value=":bug: Fix online condition" />
<MESSAGE value=":sparkles: Last seen at" /> <MESSAGE value=":sparkles: Last seen at" />
<MESSAGE value=":sparkles: Edit, delete current status" /> <MESSAGE value=":sparkles: Edit, delete current status" />
@ -180,7 +179,8 @@
<MESSAGE value=":sparkles: Password reset &amp; user lookup API" /> <MESSAGE value=":sparkles: Password reset &amp; user lookup API" />
<MESSAGE value=":recycle: Optimized the initial permission system" /> <MESSAGE value=":recycle: Optimized the initial permission system" />
<MESSAGE value=":zap: Optimized audit, event logging system&#10;:sparkles: Audit logs&#10;:sparkles: Admin edit user permissions" /> <MESSAGE value=":zap: Optimized audit, event logging system&#10;:sparkles: Audit logs&#10;:sparkles: Admin edit user permissions" />
<option name="LAST_COMMIT_MESSAGE" value=":zap: Optimized audit, event logging system&#10;:sparkles: Audit logs&#10;:sparkles: Admin edit user permissions" /> <MESSAGE value=":sparkles: Admin force confirm account" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Admin force confirm account" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -11,6 +11,7 @@ func MapAdminAPIs(app *fiber.App) {
admin.Delete("/badges/:badgeId", revokeBadge) admin.Delete("/badges/:badgeId", revokeBadge)
admin.Post("/notify/all", notifyAllUser) admin.Post("/notify/all", notifyAllUser)
admin.Post("/notify/:user", notifyOneUser)
admin.Put("/users/:user/permissions", editUserPermission) admin.Put("/users/:user/permissions", editUserPermission)
admin.Post("/users/:user/confirm", forceConfirmAccount) admin.Post("/users/:user/confirm", forceConfirmAccount)

View File

@ -27,10 +27,15 @@ func notifyAllUser(c *fiber.Ctx) error {
if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil { if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil {
return err return err
} }
operator := c.Locals("user").(models.Account)
var users []models.Account var users []models.Account
if err := database.C.Find(&users).Error; err != nil { if err := database.C.Find(&users).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) 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() { go func() {
@ -59,3 +64,57 @@ func notifyAllUser(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK) 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)
}

View File

@ -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() { func SaveEventChanges() {
if len(writeEventQueue) > 0 { if len(writeEventQueue) > 0 {
count := len(writeEventQueue) count := len(writeEventQueue)

View File

@ -42,6 +42,7 @@ func AddNotifySubscriber(user models.Account, provider, id, tk, ua string) (mode
return subscriber, err return subscriber, err
} }
// NewNotification will create a notification and push via the push method it
func NewNotification(notification models.Notification) error { func NewNotification(notification models.Notification) error {
if err := database.C.Save(&notification).Error; err != nil { if err := database.C.Save(&notification).Error; err != nil {
return err return err
@ -54,6 +55,9 @@ func NewNotification(notification models.Notification) error {
return nil 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 { func PushNotification(notification models.Notification) error {
for conn := range wsConn[notification.RecipientID] { for conn := range wsConn[notification.RecipientID] {
_ = conn.WriteMessage(1, models.UnifiedCommand{ _ = conn.WriteMessage(1, models.UnifiedCommand{
@ -62,7 +66,7 @@ func PushNotification(notification models.Notification) error {
}.Marshal()) }.Marshal())
} }
// Skip push notify // Skip push notification
if GetStatusDisturbable(notification.RecipientID) != nil { if GetStatusDisturbable(notification.RecipientID) != nil {
return nil return nil
} }