mirror of
https://github.com/binwiederhier/ntfy.git
synced 2025-07-20 10:04:08 +00:00
commit
ebb61fcccf
2 changed files with 50 additions and 49 deletions
|
@ -138,41 +138,7 @@ func toFirebaseMessage(m *message, auther user.Auther) (*messaging.Message, erro
|
||||||
}
|
}
|
||||||
apnsConfig = createAPNSAlertConfig(m, data)
|
apnsConfig = createAPNSAlertConfig(m, data)
|
||||||
case messageEvent:
|
case messageEvent:
|
||||||
allowForward := true
|
|
||||||
if auther != nil {
|
if auther != nil {
|
||||||
allowForward = auther.Authorize(nil, m.Topic, user.PermissionRead) == nil
|
|
||||||
}
|
|
||||||
if allowForward {
|
|
||||||
data = map[string]string{
|
|
||||||
"id": m.ID,
|
|
||||||
"time": fmt.Sprintf("%d", m.Time),
|
|
||||||
"event": m.Event,
|
|
||||||
"topic": m.Topic,
|
|
||||||
"priority": fmt.Sprintf("%d", m.Priority),
|
|
||||||
"tags": strings.Join(m.Tags, ","),
|
|
||||||
"click": m.Click,
|
|
||||||
"icon": m.Icon,
|
|
||||||
"title": m.Title,
|
|
||||||
"message": m.Message,
|
|
||||||
"content_type": m.ContentType,
|
|
||||||
"encoding": m.Encoding,
|
|
||||||
}
|
|
||||||
if len(m.Actions) > 0 {
|
|
||||||
actions, err := json.Marshal(m.Actions)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
data["actions"] = string(actions)
|
|
||||||
}
|
|
||||||
if m.Attachment != nil {
|
|
||||||
data["attachment_name"] = m.Attachment.Name
|
|
||||||
data["attachment_type"] = m.Attachment.Type
|
|
||||||
data["attachment_size"] = fmt.Sprintf("%d", m.Attachment.Size)
|
|
||||||
data["attachment_expires"] = fmt.Sprintf("%d", m.Attachment.Expires)
|
|
||||||
data["attachment_url"] = m.Attachment.URL
|
|
||||||
}
|
|
||||||
apnsConfig = createAPNSAlertConfig(m, data)
|
|
||||||
} else {
|
|
||||||
// If "anonymous read" for a topic is not allowed, we cannot send the message along
|
// 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.
|
// via Firebase. Instead, we send a "poll_request" message, asking the client to poll.
|
||||||
//
|
//
|
||||||
|
@ -180,23 +146,42 @@ func toFirebaseMessage(m *message, auther user.Auther) (*messaging.Message, erro
|
||||||
// fields are set, the iOS app fails to decode the message.
|
// fields are set, the iOS app fails to decode the message.
|
||||||
//
|
//
|
||||||
// See https://github.com/binwiederhier/ntfy/pull/1345
|
// See https://github.com/binwiederhier/ntfy/pull/1345
|
||||||
data = map[string]string{
|
if err := auther.Authorize(nil, m.Topic, user.PermissionRead); err != nil {
|
||||||
"id": m.ID,
|
m = toPollRequest(m)
|
||||||
"time": fmt.Sprintf("%d", m.Time),
|
|
||||||
"event": pollRequestEvent,
|
|
||||||
"topic": m.Topic,
|
|
||||||
"priority": fmt.Sprintf("%d", m.Priority),
|
|
||||||
"tags": "",
|
|
||||||
"click": "",
|
|
||||||
"icon": "",
|
|
||||||
"title": "",
|
|
||||||
"message": newMessageBody,
|
|
||||||
"content_type": m.ContentType,
|
|
||||||
"encoding": m.Encoding,
|
|
||||||
"poll_id": m.ID,
|
|
||||||
}
|
}
|
||||||
apnsConfig = createAPNSAlertConfig(m, data)
|
|
||||||
}
|
}
|
||||||
|
data = map[string]string{
|
||||||
|
"id": m.ID,
|
||||||
|
"time": fmt.Sprintf("%d", m.Time),
|
||||||
|
"event": m.Event,
|
||||||
|
"topic": m.Topic,
|
||||||
|
"priority": fmt.Sprintf("%d", m.Priority),
|
||||||
|
"tags": strings.Join(m.Tags, ","),
|
||||||
|
"click": m.Click,
|
||||||
|
"icon": m.Icon,
|
||||||
|
"title": m.Title,
|
||||||
|
"message": m.Message,
|
||||||
|
"content_type": m.ContentType,
|
||||||
|
"encoding": m.Encoding,
|
||||||
|
}
|
||||||
|
if len(m.Actions) > 0 {
|
||||||
|
actions, err := json.Marshal(m.Actions)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
data["actions"] = string(actions)
|
||||||
|
}
|
||||||
|
if m.Attachment != nil {
|
||||||
|
data["attachment_name"] = m.Attachment.Name
|
||||||
|
data["attachment_type"] = m.Attachment.Type
|
||||||
|
data["attachment_size"] = fmt.Sprintf("%d", m.Attachment.Size)
|
||||||
|
data["attachment_expires"] = fmt.Sprintf("%d", m.Attachment.Expires)
|
||||||
|
data["attachment_url"] = m.Attachment.URL
|
||||||
|
}
|
||||||
|
if m.PollID != "" {
|
||||||
|
data["poll_id"] = m.PollID
|
||||||
|
}
|
||||||
|
apnsConfig = createAPNSAlertConfig(m, data)
|
||||||
}
|
}
|
||||||
var androidConfig *messaging.AndroidConfig
|
var androidConfig *messaging.AndroidConfig
|
||||||
if m.Priority >= 4 {
|
if m.Priority >= 4 {
|
||||||
|
@ -290,3 +275,17 @@ func maybeTruncateAPNSBodyMessage(s string) string {
|
||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// toPollRequest converts a message to a poll request message.
|
||||||
|
//
|
||||||
|
// This empties all the fields that are not needed for a poll request and just sets the required fields,
|
||||||
|
// most importantly, the PollID.
|
||||||
|
func toPollRequest(m *message) *message {
|
||||||
|
pr := newPollRequestMessage(m.Topic, m.ID)
|
||||||
|
pr.ID = m.ID
|
||||||
|
pr.Time = m.Time
|
||||||
|
pr.Priority = m.Priority // Keep priority
|
||||||
|
pr.ContentType = m.ContentType
|
||||||
|
pr.Encoding = m.Encoding
|
||||||
|
return pr
|
||||||
|
}
|
||||||
|
|
|
@ -240,6 +240,8 @@ func TestToFirebaseMessage_Message_Normal_Not_Allowed(t *testing.T) {
|
||||||
"content_type": "",
|
"content_type": "",
|
||||||
"poll_id": m.ID,
|
"poll_id": m.ID,
|
||||||
}, fbm.Data)
|
}, fbm.Data)
|
||||||
|
require.Equal(t, "", fbm.APNS.Payload.Aps.Alert.Title)
|
||||||
|
require.Equal(t, "New message", fbm.APNS.Payload.Aps.Alert.Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestToFirebaseMessage_PollRequest(t *testing.T) {
|
func TestToFirebaseMessage_PollRequest(t *testing.T) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue