fix: expire stale call pushes
This commit is contained in:
@@ -25,7 +25,7 @@ const (
|
||||
defaultAPIDomain = "https://api.jpush.cn"
|
||||
defaultPushPath = "/v3/push"
|
||||
defaultTTL = 86400
|
||||
defaultCallTTL = 60
|
||||
defaultCallTTL = 30
|
||||
)
|
||||
|
||||
var handler Handler
|
||||
@@ -327,6 +327,9 @@ func preparePayloads(rcpt *push.Receipt, config *configType) ([]*jpushPayload, [
|
||||
logs.Warn.Println("jpush: could not build payload:", err)
|
||||
return nil, nil
|
||||
}
|
||||
if pushExpired(data) {
|
||||
return nil, nil
|
||||
}
|
||||
useAlias := true
|
||||
if config.UseAlias != nil {
|
||||
useAlias = *config.UseAlias
|
||||
@@ -399,6 +402,9 @@ func pushData(pl push.Payload) (map[string]string, error) {
|
||||
}
|
||||
if pl.Webrtc != "" {
|
||||
data["webrtc"] = pl.Webrtc
|
||||
if pl.ExpiresAt != nil && !pl.ExpiresAt.IsZero() {
|
||||
data["expires_at"] = pl.ExpiresAt.UTC().Format(time.RFC3339Nano)
|
||||
}
|
||||
if callID := payloadContentString(pl.Content, "callId"); callID != "" {
|
||||
data["call_id"] = callID
|
||||
}
|
||||
@@ -437,11 +443,31 @@ func pushData(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 buildPayload(audience map[string][]string, topic string, data map[string]string, unread int, config *configType) *jpushPayload {
|
||||
alert, title := alertText(data, config)
|
||||
ttl := config.DefaultTTLSeconds
|
||||
if data["type"] == "call_invite" {
|
||||
ttl = config.CallTTLSeconds
|
||||
if expiresAt := data["expires_at"]; expiresAt != "" {
|
||||
if parsed, err := time.Parse(time.RFC3339Nano, expiresAt); err == nil {
|
||||
remaining := int(time.Until(parsed).Seconds())
|
||||
if remaining < ttl {
|
||||
ttl = remaining
|
||||
}
|
||||
}
|
||||
}
|
||||
if ttl < 1 {
|
||||
ttl = 1
|
||||
}
|
||||
}
|
||||
payload := &jpushPayload{
|
||||
Platform: "all",
|
||||
|
||||
Reference in New Issue
Block a user