fix: add call push category

This commit is contained in:
richjohnrambo
2026-09-18 13:38:57 +08:00
parent 192bdede33
commit 153f541078
3 changed files with 111 additions and 20 deletions
+82 -8
View File
@@ -26,6 +26,7 @@ const (
defaultPushPath = "/v3/push"
defaultTTL = 86400
defaultCallTTL = 30
callCategory = "im_call"
)
var handler Handler
@@ -78,10 +79,11 @@ type androidPayload struct {
}
type iosPayload struct {
Alert iosAlert `json:"alert"`
Sound string `json:"sound,omitempty"`
Badge string `json:"badge,omitempty"`
Extras map[string]string `json:"extras,omitempty"`
Alert iosAlert `json:"alert"`
Sound string `json:"sound,omitempty"`
Badge string `json:"badge,omitempty"`
Category string `json:"category,omitempty"`
Extras map[string]string `json:"extras,omitempty"`
}
type iosAlert struct {
@@ -327,6 +329,7 @@ func preparePayloads(rcpt *push.Receipt, config *configType) ([]*jpushPayload, [
logs.Warn.Println("jpush: could not build payload:", err)
return nil, nil
}
hydrateCallInviteData(rcpt.TenantID, data)
if pushExpired(data) {
return nil, nil
}
@@ -424,6 +427,9 @@ func pushData(pl push.Payload) (map[string]string, error) {
if pl.From != "" {
data["caller_id"] = pl.From
}
if display := payloadContentFirstString(pl.Content, "caller_display", "callerDisplay", "caller_name", "callerName", "display_name", "from_display"); display != "" {
data["caller_display"] = display
}
}
if pl.Replace != "" {
data["replace"] = pl.Replace
@@ -495,10 +501,11 @@ func buildPayload(audience map[string][]string, topic string, data map[string]st
Priority: 1,
},
IOS: &iosPayload{
Alert: iosAlert{Title: title, Body: alert},
Sound: "default",
Badge: badge,
Extras: data,
Alert: iosAlert{Title: title, Body: alert},
Sound: "default",
Badge: badge,
Category: iosCategory(data),
Extras: data,
},
HMOS: &hmosPayload{
Title: title,
@@ -514,9 +521,16 @@ func alertText(data map[string]string, config *configType) (string, string) {
title := config.Title
switch data["type"] {
case "call_invite":
caller := strings.TrimSpace(data["caller_display"])
if data["call_type"] == "audio" {
if caller != "" {
return caller + "邀请你语音通话", "语音通话"
}
return "收到一通语音通话", "语音通话"
}
if caller != "" {
return caller + "邀请你视频通话", "视频通话"
}
return "收到一通视频通话", "视频通话"
case "call_missed":
return "你有一通未接来电", "未接来电"
@@ -527,6 +541,57 @@ func alertText(data map[string]string, config *configType) (string, string) {
}
}
func iosCategory(data map[string]string) string {
if data["type"] == "call_invite" {
return callCategory
}
return ""
}
func hydrateCallInviteData(tenantID t.TenantID, data map[string]string) {
if data["type"] != "call_invite" || strings.TrimSpace(data["caller_display"]) != "" {
return
}
callerID := strings.TrimSpace(data["caller_id"])
if callerID == "" || store.Users == nil {
return
}
uid := t.ParseUserId(callerID)
if uid.IsZero() {
return
}
user, err := store.Users.Get(tenantID, uid)
if err != nil || user == nil {
return
}
if display := displayNameFromPublic(user.Public); display != "" {
data["caller_display"] = display
}
}
func displayNameFromPublic(public any) string {
for _, key := range []string{"display_name", "fn", "nickname", "name"} {
if value := publicMapString(public, key); value != "" {
return value
}
}
return ""
}
func publicMapString(public any, key string) string {
switch values := public.(type) {
case map[string]any:
value, ok := values[key]
if !ok || value == nil {
return ""
}
return strings.TrimSpace(fmt.Sprint(value))
case map[string]string:
return strings.TrimSpace(values[key])
}
return ""
}
func cidFor(topic string, data map[string]string) string {
parts := []string{data["type"], topic, data["seq"], data["replace"]}
return strings.Trim(strings.Join(parts, "_"), "_")
@@ -560,6 +625,15 @@ func payloadContentString(content any, key string) string {
return ""
}
func payloadContentFirstString(content any, keys ...string) string {
for _, key := range keys {
if value := payloadContentString(content, key); value != "" {
return value
}
}
return ""
}
func truncateRunes(value string, limit int) string {
if limit <= 0 || len(value) <= limit {
return value