Handle anonymous read restrictions by sending a poll_request event

If a topic does not allow anonymous reads, this change ensures that we send a "poll_request" event instead of relaying the message via Firebase. Additionally, we include generic text in the title and body/message. This way, if the client cannot retrieve the actual message, the user will still receive a notification, prompting them to update the client manually.
This commit is contained in:
barart 2025-03-05 13:04:21 -06:00 committed by GitHub
parent 630f2957de
commit 6b2cfb1d1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -175,13 +175,26 @@ func toFirebaseMessage(m *message, auther user.Auther) (*messaging.Message, erro
} else {
// If anonymous read for a topic is not allowed, we cannot send the message along
// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
//App function needs all the data to create a message object, if not, it fails,
//so we set it but put a placeholders to not to send the actual message
//but generic title and message instead, we also add the poll_id so client knowns
//what message is goint to "decode" (retrieve)
data = map[string]string{
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": pollRequestEvent,
"topic": m.Topic,
}
// TODO Handle APNS?
"id": m.ID,
"time": fmt.Sprintf("%d", m.Time),
"event": pollRequestEvent,
"topic": m.Topic,
"priority": fmt.Sprintf("%d", m.Priority),
"tags": strings.Join(m.Tags, ","),
"click": m.Click,
"icon": m.Icon,
"title": "Private",
"message": "Message",
"content_type": m.ContentType,
"encoding": m.Encoding,
"poll_id": m.ID,
}
apnsConfig = createAPNSAlertConfig(m, data)
}
}
var androidConfig *messaging.AndroidConfig
@ -225,14 +238,23 @@ func createAPNSAlertConfig(m *message, data map[string]string) *messaging.APNSCo
for k, v := range data {
apnsData[k] = v
}
alertTitle := m.Title
alertBody := maybeTruncateAPNSBodyMessage(m.Message)
// If the event is pollRequestEvent (server/topic is restricted) we dont want to
//send the actual message to Firebase/APNS, so we send a generic text
//if for some reason, client cant retrieve the message, it shows this as the message and title
if event, ok := data["event"]; ok && event == pollRequestEvent {
alertTitle = "New Notification received"
alertBody = "Message cant be retrieved, open the app and refresh content"
}
return &messaging.APNSConfig{
Payload: &messaging.APNSPayload{
CustomData: apnsData,
Aps: &messaging.Aps{
MutableContent: true,
Alert: &messaging.ApsAlert{
Title: m.Title,
Body: maybeTruncateAPNSBodyMessage(m.Message),
Title: alertTitle,
Body: alertBody,
},
},
},