feat(backend): enhance share creation with notification settings and webhook support

This commit is contained in:
keven1024
2026-04-30 08:28:05 +08:00
parent 2e5da4f7e9
commit 6934bba7a1
2 changed files with 44 additions and 7 deletions

View File

@@ -3,12 +3,14 @@ package controllers
import (
"backend/internal/utils"
"context"
"encoding/json"
"fmt"
"pkg/models"
u "pkg/utils"
"time"
"github.com/golang-jwt/jwt/v5"
"github.com/hibiken/asynq"
"github.com/labstack/echo/v5"
"github.com/samber/lo"
"github.com/spf13/cast"
@@ -134,6 +136,13 @@ func VaildateShare(c *echo.Context) error {
return utils.HTTPErrorHandler(c, err)
}
if len(shareInfo.NotifyEmails) > 0 || len(shareInfo.NotifyWebhooks) > 0 {
payload, err := json.Marshal(map[string]string{"share_id": r.ShareId})
if err == nil {
_, _ = u.GetQueueClient().Enqueue(asynq.NewTask("share:notify", payload))
}
}
if shareInfo.Type == models.ShareTypeFile {
return utils.HTTPSuccessHandler(c, map[string]any{
"token": downloadToken,

View File

@@ -23,13 +23,16 @@ type CreateShareProps struct {
}
type ShareConfig struct {
ExpireAt int `json:"expire_time"` // 分钟
ViewNum int64 `json:"download_nums"`
HasPassword bool `json:"has_password"`
Password string `json:"password"`
HasNotify bool `json:"has_notify"`
NotifyEmail []string `json:"notify_email"`
HasPickupCode bool `json:"has_pickup_code"`
ExpireAt int `json:"expire_time"` // 分钟
ViewNum int64 `json:"download_nums"`
HasPassword bool `json:"has_password"`
Password string `json:"password"`
HasNotify bool `json:"has_notify"`
NotifyTypes []string `json:"notify_types"`
NotifyEmails []string `json:"notify_emails"`
NotifyWebhooks []models.NotifyWebhook `json:"notify_webhooks"`
Locale string `json:"locale"`
HasPickupCode bool `json:"has_pickup_code"`
}
func CreateShareInfo(c *echo.Context) error {
@@ -73,6 +76,28 @@ func CreateShareInfo(c *echo.Context) error {
password = hash
}
var notifyEmails []string
var notifyWebhooks []models.NotifyWebhook
if r.Config.HasNotify {
hasEmail, hasWebhook := false, false
for _, nt := range r.Config.NotifyTypes {
switch nt {
case "email":
hasEmail = true
case "webhook":
hasWebhook = true
default:
return utils.HTTPErrorHandler(c, ErrInvalidRequest)
}
}
if hasEmail {
notifyEmails = r.Config.NotifyEmails
}
if hasWebhook {
notifyWebhooks = r.Config.NotifyWebhooks
}
}
_, err = models.SetRedisShareInfo(id, func(shareInfo *models.RedisShareInfo) *models.RedisShareInfo {
shareInfo.Data = r.Data
shareInfo.Type = r.Type
@@ -81,6 +106,9 @@ func CreateShareInfo(c *echo.Context) error {
shareInfo.ViewNum = r.Config.ViewNum
shareInfo.Password = password
shareInfo.FileName = r.FileName
shareInfo.NotifyEmails = notifyEmails
shareInfo.NotifyWebhooks = notifyWebhooks
shareInfo.Locale = r.Config.Locale
shareInfo.ExpireAt = ExpireTime.Unix()
return shareInfo
})