fix: expire stale call pushes

This commit is contained in:
richjohnrambo
2026-09-14 12:11:43 +08:00
parent 4854e090ef
commit 789e662bdb
8 changed files with 164 additions and 14 deletions
+24 -1
View File
@@ -36,6 +36,9 @@ func payloadToData(pl *push.Payload) (map[string]string, error) {
}
data["topic"] = pl.Topic
data["ts"] = pl.Timestamp.Format(time.RFC3339Nano)
if pl.ExpiresAt != nil && !pl.ExpiresAt.IsZero() {
data["expires_at"] = pl.ExpiresAt.UTC().Format(time.RFC3339Nano)
}
// Must use "xfrom" because "from" is a reserved word. Google did not bother to document it anywhere.
data["xfrom"] = pl.From
if pl.What == push.ActMsg {
@@ -89,6 +92,15 @@ func payloadToData(pl *push.Payload) (map[string]string, error) {
return data, nil
}
func pushExpired(data map[string]string) bool {
expiresAt := data["expires_at"]
if expiresAt == "" {
return false
}
parsed, err := time.Parse(time.RFC3339Nano, expiresAt)
return err == nil && !time.Now().UTC().Before(parsed)
}
func clonePayload(src map[string]string) map[string]string {
dst := make(map[string]string, len(src))
maps.Copy(dst, src)
@@ -103,6 +115,9 @@ func PrepareV1Notifications(rcpt *push.Receipt, config *configType) ([]*fcmv1.Me
logs.Warn.Println("fcm push: could not parse payload:", err)
return nil, nil
}
if pushExpired(data) {
return nil, nil
}
// Device IDs to send pushes to.
var devices map[t.Uid][]t.DeviceDef
@@ -322,6 +337,11 @@ func apnsNotificationConfig(what, topic string, data map[string]string, unread i
if config.TimeToLive > 0 {
expires = time.Now().UTC().Add(time.Duration(config.TimeToLive) * time.Second)
}
if expiresAt := data["expires_at"]; expiresAt != "" {
if parsed, err := time.Parse(time.RFC3339Nano, expiresAt); err == nil && parsed.Before(expires) {
expires = parsed
}
}
bundleId := config.ApnsBundleID
pushType := common.ApnsPushTypeAlert
priority := 10
@@ -335,7 +355,10 @@ func apnsNotificationConfig(what, topic string, data map[string]string, unread i
// a different adapter.
// pushType = common.ApnsPushTypeVoip
// bundleId += ".voip"
expires = time.Now().UTC().Add(time.Duration(voipTimeToLive) * time.Second)
callExpires := time.Now().UTC().Add(time.Duration(voipTimeToLive) * time.Second)
if expires.After(callExpires) {
expires = callExpires
}
} else if what == push.ActRead {
priority = 5
interruptionLevel = common.InterruptionLevelPassive