chore: update
This commit is contained in:
@@ -27,6 +27,32 @@ type newTelegram struct {
|
||||
Telegram string `json:"telegram" binding:"required,min=3,max=64"`
|
||||
}
|
||||
|
||||
type alertSettingsRequest struct {
|
||||
DiscordWebhookURL string `json:"discord_webhook_url" binding:"omitempty,max=500,url"`
|
||||
TelegramBotToken string `json:"telegram_bot_token" binding:"omitempty,max=200"`
|
||||
TelegramChatID string `json:"telegram_chat_id" binding:"omitempty,max=64"`
|
||||
}
|
||||
|
||||
type alertSettingsResponse struct {
|
||||
DiscordWebhookURL string `json:"discord_webhook_url"`
|
||||
TelegramBotToken string `json:"telegram_bot_token"`
|
||||
TelegramChatID string `json:"telegram_chat_id"`
|
||||
}
|
||||
|
||||
func toAlertSettingsResponse(user auth.User) alertSettingsResponse {
|
||||
deref := func(s *string) string {
|
||||
if s == nil {
|
||||
return ""
|
||||
}
|
||||
return *s
|
||||
}
|
||||
return alertSettingsResponse{
|
||||
DiscordWebhookURL: deref(user.AlertDiscordWebhookURL),
|
||||
TelegramBotToken: deref(user.AlertTelegramBotToken),
|
||||
TelegramChatID: deref(user.AlertTelegramChatID),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) UpdateUsernameById(c *gin.Context) {
|
||||
var u newUsername
|
||||
|
||||
@@ -110,3 +136,40 @@ func (h *Handler) SetTelegramById(c *gin.Context) {
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"telegram": user.Telegram})
|
||||
}
|
||||
|
||||
// GetAlerts : réglages d'alerte monitoring (webhook Discord / bot Telegram)
|
||||
// de l'admin authentifié — chacun configure les siens, rien de partagé.
|
||||
func (h *Handler) GetAlerts(c *gin.Context) {
|
||||
p := auth.PrincipalFrom(c)
|
||||
if p == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
||||
return
|
||||
}
|
||||
user, err := h.store.GetAlertSettings(p.UserID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toAlertSettingsResponse(user))
|
||||
}
|
||||
|
||||
// SetAlerts : enregistre les réglages d'alerte de l'admin authentifié. Un
|
||||
// champ vide désactive le canal correspondant.
|
||||
func (h *Handler) SetAlerts(c *gin.Context) {
|
||||
var req alertSettingsRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "requête invalide"})
|
||||
return
|
||||
}
|
||||
p := auth.PrincipalFrom(c)
|
||||
if p == nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "non authentifié"})
|
||||
return
|
||||
}
|
||||
user, err := h.store.SetAlertSettings(p.UserID, req.DiscordWebhookURL, req.TelegramBotToken, req.TelegramChatID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "erreur serveur"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toAlertSettingsResponse(user))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user