99 lines
2.6 KiB
Go
99 lines
2.6 KiB
Go
package format
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
settingsmessaging "github.com/dujiao-next/internal/modules/settings/schema/messaging"
|
|
"github.com/dujiao-next/internal/queue"
|
|
"github.com/dujiao-next/internal/shared/jsonmap"
|
|
)
|
|
|
|
var notificationTemplateVariablePattern = regexp.MustCompile(`\{\{\s*([a-zA-Z0-9_]+)\s*\}\}`)
|
|
|
|
func BuildTemplateVariables(payload queue.NotificationDispatchPayload) map[string]interface{} {
|
|
data := CloneVariables(payload.Data)
|
|
if data == nil {
|
|
data = map[string]interface{}{}
|
|
}
|
|
data["event_type"] = strings.ToLower(strings.TrimSpace(payload.EventType))
|
|
data["biz_type"] = strings.TrimSpace(payload.BizType)
|
|
data["biz_id"] = fmt.Sprintf("%d", payload.BizID)
|
|
data["occurred_at"] = time.Now().Format("2006-01-02 15:04:05")
|
|
return data
|
|
}
|
|
|
|
func RenderTemplate(tmpl string, variables map[string]interface{}) string {
|
|
tmpl = strings.TrimSpace(tmpl)
|
|
if tmpl == "" {
|
|
return ""
|
|
}
|
|
return notificationTemplateVariablePattern.ReplaceAllStringFunc(tmpl, func(matched string) string {
|
|
submatch := notificationTemplateVariablePattern.FindStringSubmatch(matched)
|
|
if len(submatch) != 2 {
|
|
return matched
|
|
}
|
|
value, ok := variables[strings.TrimSpace(submatch[1])]
|
|
if !ok {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(fmt.Sprintf("%v", value))
|
|
})
|
|
}
|
|
|
|
func ResolveLocale(locale, fallback string) string {
|
|
locale = strings.TrimSpace(locale)
|
|
if locale == "" {
|
|
locale = strings.TrimSpace(fallback)
|
|
}
|
|
return normalizeNotificationLocale(locale)
|
|
}
|
|
|
|
func ComposePlainTextMessage(title, body string) string {
|
|
title = strings.TrimSpace(title)
|
|
body = strings.TrimSpace(body)
|
|
if title == "" {
|
|
return body
|
|
}
|
|
if body == "" {
|
|
return title
|
|
}
|
|
return title + "\n\n" + body
|
|
}
|
|
|
|
func JSONToMap(data jsonmap.JSON) map[string]interface{} {
|
|
if data == nil {
|
|
return map[string]interface{}{}
|
|
}
|
|
result := make(map[string]interface{}, len(data))
|
|
for key, value := range data {
|
|
result[key] = value
|
|
}
|
|
return result
|
|
}
|
|
|
|
func CloneVariables(data map[string]interface{}) map[string]interface{} {
|
|
if len(data) == 0 {
|
|
return map[string]interface{}{}
|
|
}
|
|
result := make(map[string]interface{}, len(data))
|
|
for key, value := range data {
|
|
result[key] = value
|
|
}
|
|
return result
|
|
}
|
|
|
|
func normalizeNotificationLocale(locale string) string {
|
|
return settingsmessaging.NormalizeNotificationLocale(locale)
|
|
}
|
|
|
|
func normalizeNotificationInventoryAlertInterval(seconds int) int {
|
|
return settingsmessaging.NormalizeNotificationInventoryAlertInterval(seconds)
|
|
}
|
|
|
|
func normalizeNotificationPaymentOrderAlertInterval(seconds int) int {
|
|
return settingsmessaging.NormalizeNotificationPaymentOrderAlertInterval(seconds)
|
|
}
|