✨ Admin notify all API
This commit is contained in:
		
							
								
								
									
										9
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										9
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							| @@ -4,9 +4,10 @@ | ||||
|     <option name="autoReloadType" value="ALL" /> | ||||
|   </component> | ||||
|   <component name="ChangeListManager"> | ||||
|     <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix status query condition"> | ||||
|     <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix disturbable condition"> | ||||
|       <change afterPath="$PROJECT_DIR$/pkg/internal/server/admin/notify_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$/pkg/internal/services/statuses.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/statuses.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" /> | ||||
|     </list> | ||||
|     <option name="SHOW_DIALOG" value="false" /> | ||||
|     <option name="HIGHLIGHT_CONFLICTS" value="true" /> | ||||
| @@ -152,7 +153,6 @@ | ||||
|     </option> | ||||
|   </component> | ||||
|   <component name="VcsManagerConfiguration"> | ||||
|     <MESSAGE value=":arrow_up: Fix notification listen" /> | ||||
|     <MESSAGE value=":bug: Fix magic token's foreign key" /> | ||||
|     <MESSAGE value=":sparkles: Better avatar and banner APIs" /> | ||||
|     <MESSAGE value=":bug: Fix avatar and banner APIs" /> | ||||
| @@ -177,7 +177,8 @@ | ||||
|     <MESSAGE value=":bug: Fix status validation issue" /> | ||||
|     <MESSAGE value=":bug: Fix bugs in status" /> | ||||
|     <MESSAGE value=":bug: Fix status query condition" /> | ||||
|     <option name="LAST_COMMIT_MESSAGE" value=":bug: Fix status query condition" /> | ||||
|     <MESSAGE value=":bug: Fix disturbable condition" /> | ||||
|     <option name="LAST_COMMIT_MESSAGE" value=":bug: Fix disturbable condition" /> | ||||
|   </component> | ||||
|   <component name="VgoProject"> | ||||
|     <settings-migrated>true</settings-migrated> | ||||
|   | ||||
| @@ -9,5 +9,7 @@ func MapAdminEndpoints(app *fiber.App) { | ||||
| 	{ | ||||
| 		admin.Post("/badges", grantBadge) | ||||
| 		admin.Delete("/badges/:badgeId", revokeBadge) | ||||
|  | ||||
| 		admin.Post("/notify/all", notifyAllUser) | ||||
| 	} | ||||
| } | ||||
|   | ||||
							
								
								
									
										64
									
								
								pkg/internal/server/admin/notify_api.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								pkg/internal/server/admin/notify_api.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,64 @@ | ||||
| package admin | ||||
|  | ||||
| import ( | ||||
| 	"git.solsynth.dev/hydrogen/passport/pkg/internal/database" | ||||
| 	"git.solsynth.dev/hydrogen/passport/pkg/internal/models" | ||||
| 	"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts" | ||||
| 	"git.solsynth.dev/hydrogen/passport/pkg/internal/services" | ||||
| 	"github.com/gofiber/fiber/v2" | ||||
| 	"github.com/rs/zerolog/log" | ||||
| ) | ||||
|  | ||||
| func notifyAllUser(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=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" validate:"required"` | ||||
| 	} | ||||
|  | ||||
| 	if err := exts.BindAndValidate(c, &data); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	var users []models.Account | ||||
| 	if err := database.C.Find(&users).Error; err != nil { | ||||
| 		return fiber.NewError(fiber.StatusInternalServerError, err.Error()) | ||||
| 	} | ||||
|  | ||||
| 	go func() { | ||||
| 		for _, user := range users { | ||||
| 			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) | ||||
| } | ||||
		Reference in New Issue
	
	Block a user