Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion messaging/messaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,24 @@ type Message struct {
Webpush *WebpushConfig `json:"webpush,omitempty"`
APNS *APNSConfig `json:"apns,omitempty"`
Token string `json:"token,omitempty"`
Topic string `json:"topic,omitempty"`
Topic string `json:"-"`
Condition string `json:"condition,omitempty"`
}

// MarshalJSON marshals a Message into JSON (for internal use only).
func (m *Message) MarshalJSON() ([]byte, error) {
// Create a new type to prevent infinite recursion.
type messageInternal Message
s := &struct {
BareTopic string `json:"topic,omitempty"`
*messageInternal
}{
BareTopic: strings.TrimPrefix(m.Topic, "/topics/"),
messageInternal: (*messageInternal)(m),
}
return json.Marshal(s)
}

// Notification is the basic notification template to use across all platforms.
type Notification struct {
Title string `json:"title,omitempty"`
Expand Down
11 changes: 8 additions & 3 deletions messaging/messaging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ var validMessages = []struct {
req: &Message{Topic: "test-topic"},
want: map[string]interface{}{"topic": "test-topic"},
},
{
name: "PrefixedTopicOnly",
req: &Message{Topic: "/topics/test-topic"},
want: map[string]interface{}{"topic": "test-topic"},
},
{
name: "ConditionOnly",
req: &Message{Condition: "test-condition"},
Expand Down Expand Up @@ -370,11 +375,11 @@ var invalidMessages = []struct {
want: "exactly one of token, topic or condition must be specified",
},
{
name: "InvalidTopicPrefix",
name: "InvalidPrefixedTopicName",
req: &Message{
Topic: "/topics/foo",
Topic: "/topics/",
},
want: "topic name must not contain the /topics/ prefix",
want: "malformed topic name",
},
{
name: "InvalidTopicName",
Expand Down
6 changes: 2 additions & 4 deletions messaging/messaging_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ func validateMessage(message *Message) error {

// validate topic
if message.Topic != "" {
if strings.HasPrefix(message.Topic, "/topics/") {
return fmt.Errorf("topic name must not contain the /topics/ prefix")
}
if !bareTopicNamePattern.MatchString(message.Topic) {
bt := strings.TrimPrefix(message.Topic, "/topics/")
if !bareTopicNamePattern.MatchString(bt) {
return fmt.Errorf("malformed topic name")
}
}
Expand Down