From 99886d7f669499b88b5136dc3dcbdbc4b34e8ba6 Mon Sep 17 00:00:00 2001 From: Hunter Kehoe Date: Sun, 17 Jul 2022 15:40:24 -0600 Subject: [PATCH] change icon from object to string --- client/client.go | 9 +----- cmd/publish_test.go | 1 + server/message_cache.go | 51 ++++++++++------------------------ server/server.go | 9 +----- server/server_firebase.go | 6 +--- server/server_firebase_test.go | 14 ++-------- server/server_test.go | 2 +- server/types.go | 8 +----- 8 files changed, 23 insertions(+), 77 deletions(-) diff --git a/client/client.go b/client/client.go index 27e75fe5..baec3011 100644 --- a/client/client.go +++ b/client/client.go @@ -47,7 +47,7 @@ type Message struct { // TODO combine with server.message Priority int Tags []string Click string - Icon *Icon + Icon string Attachment *Attachment // Additional fields @@ -66,13 +66,6 @@ type Attachment struct { Owner string `json:"-"` // IP address of uploader, used for rate limiting } -// Icon represents a message icon -type Icon struct { - URL string `json:"url"` - Type string `json:"type,omitempty"` - Size int64 `json:"size,omitempty"` -} - type subscription struct { ID string topicURL string diff --git a/cmd/publish_test.go b/cmd/publish_test.go index ee8b116c..dde279da 100644 --- a/cmd/publish_test.go +++ b/cmd/publish_test.go @@ -74,6 +74,7 @@ func TestCLI_Publish_All_The_Things(t *testing.T) { require.Equal(t, "", m.Attachment.Owner) require.Equal(t, int64(0), m.Attachment.Expires) require.Equal(t, "", m.Attachment.Type) + require.Equal(t, "https://ntfy.sh/static/img/ntfy.png", m.Icon) } func TestCLI_Publish_Wait_PID_And_Cmd(t *testing.T) { diff --git a/server/message_cache.go b/server/message_cache.go index 0905e009..f37e5a9d 100644 --- a/server/message_cache.go +++ b/server/message_cache.go @@ -39,46 +39,44 @@ const ( sender TEXT NOT NULL, encoding TEXT NOT NULL, published INT NOT NULL, - icon_url TEXT NOT NULL, - icon_type TEXT NOT NULL, - icon_size INT NOT NULL + icon TEXT NOT NULL ); CREATE INDEX IF NOT EXISTS idx_mid ON messages (mid); CREATE INDEX IF NOT EXISTS idx_topic ON messages (topic); COMMIT; ` insertMessageQuery = ` - INSERT INTO messages (mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, published, icon_url, icon_type, icon_size) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO messages (mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, published, icon) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ` pruneMessagesQuery = `DELETE FROM messages WHERE time < ? AND published = 1` selectRowIDFromMessageID = `SELECT id FROM messages WHERE mid = ?` // Do not include topic, see #336 and TestServer_PollSinceID_MultipleTopics selectMessagesSinceTimeQuery = ` - SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon_url, icon_type, icon_size + SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon FROM messages WHERE topic = ? AND time >= ? AND published = 1 ORDER BY time, id ` selectMessagesSinceTimeIncludeScheduledQuery = ` - SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon_url, icon_type, icon_size + SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon FROM messages WHERE topic = ? AND time >= ? ORDER BY time, id ` selectMessagesSinceIDQuery = ` - SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon_url, icon_type, icon_size + SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon FROM messages WHERE topic = ? AND id > ? AND published = 1 ORDER BY time, id ` selectMessagesSinceIDIncludeScheduledQuery = ` - SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon_url, icon_type, icon_size + SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon FROM messages WHERE topic = ? AND (id > ? OR published = 0) ORDER BY time, id ` selectMessagesDueQuery = ` - SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon_url, icon_type, icon_size + SELECT mid, time, topic, message, title, priority, tags, click, actions, attachment_name, attachment_type, attachment_size, attachment_expires, attachment_url, sender, encoding, icon FROM messages WHERE time <= ? AND published = 0 ORDER BY time, id @@ -183,9 +181,7 @@ const ( // 7 -> 8 migrate7To8AlterMessagesTableQuery = ` - ALTER TABLE messages ADD COLUMN icon_url TEXT NOT NULL DEFAULT(''); - ALTER TABLE messages ADD COLUMN icon_type TEXT NOT NULL DEFAULT(''); - ALTER TABLE messages ADD COLUMN icon_size INT NOT NULL DEFAULT('0'); + ALTER TABLE messages ADD COLUMN icon TEXT NOT NULL DEFAULT(''); ` ) @@ -258,13 +254,6 @@ func (c *messageCache) addMessages(ms []*message) error { attachmentExpires = m.Attachment.Expires attachmentURL = m.Attachment.URL } - var iconURL, iconType string - var iconSize int64 - if m.Icon != nil { - iconURL = m.Icon.URL - iconType = m.Icon.Type - iconSize = m.Icon.Size - } var actionsStr string if len(m.Actions) > 0 { actionsBytes, err := json.Marshal(m.Actions) @@ -292,9 +281,7 @@ func (c *messageCache) addMessages(ms []*message) error { m.Sender, m.Encoding, published, - iconURL, - iconType, - iconSize, + m.Icon, ) if err != nil { return err @@ -432,9 +419,9 @@ func readMessages(rows *sql.Rows) ([]*message, error) { defer rows.Close() messages := make([]*message, 0) for rows.Next() { - var timestamp, attachmentSize, attachmentExpires, iconSize int64 + var timestamp, attachmentSize, attachmentExpires int64 var priority int - var id, topic, msg, title, tagsStr, click, actionsStr, attachmentName, attachmentType, attachmentURL, sender, encoding, iconURL, iconType string + var id, topic, msg, title, tagsStr, click, actionsStr, attachmentName, attachmentType, attachmentURL, sender, encoding, icon string err := rows.Scan( &id, ×tamp, @@ -452,9 +439,7 @@ func readMessages(rows *sql.Rows) ([]*message, error) { &attachmentURL, &sender, &encoding, - &iconURL, - &iconType, - &iconSize, + &icon, ) if err != nil { return nil, err @@ -479,14 +464,6 @@ func readMessages(rows *sql.Rows) ([]*message, error) { URL: attachmentURL, } } - var ico *icon - if iconURL != "" { - ico = &icon{ - URL: iconURL, - Type: iconType, - Size: iconSize, - } - } messages = append(messages, &message{ ID: id, Time: timestamp, @@ -497,7 +474,7 @@ func readMessages(rows *sql.Rows) ([]*message, error) { Priority: priority, Tags: tags, Click: click, - Icon: ico, + Icon: icon, Actions: actions, Attachment: att, Sender: sender, diff --git a/server/server.go b/server/server.go index 6d25e2d5..be2ae9c1 100644 --- a/server/server.go +++ b/server/server.go @@ -569,7 +569,7 @@ func (s *Server) parsePublishParams(r *http.Request, v *visitor, m *message) (ca firebase = readBoolParam(r, true, "x-firebase", "firebase") m.Title = readParam(r, "x-title", "title", "t") m.Click = readParam(r, "x-click", "click") - ico := readParam(r, "x-icon", "icon") + m.Icon = readParam(r, "x-icon", "icon") filename := readParam(r, "x-filename", "filename", "file", "f") attach := readParam(r, "x-attach", "attach", "a") if attach != "" || filename != "" { @@ -596,13 +596,6 @@ func (s *Server) parsePublishParams(r *http.Request, v *visitor, m *message) (ca m.Attachment.Name = "attachment" } } - if ico != "" { - m.Icon = &icon{} - if !iconURLRegex.MatchString(ico) { - return false, false, "", false, errHTTPBadRequestIconURLInvalid - } - m.Icon.URL = ico - } email = readParam(r, "x-email", "x-e-mail", "email", "e-mail", "mail", "e") if email != "" { if err := v.EmailAllowed(); err != nil { diff --git a/server/server_firebase.go b/server/server_firebase.go index 016f54be..ec3e6112 100644 --- a/server/server_firebase.go +++ b/server/server_firebase.go @@ -151,6 +151,7 @@ func toFirebaseMessage(m *message, auther auth.Auther) (*messaging.Message, erro "title": m.Title, "message": m.Message, "encoding": m.Encoding, + "icon": m.Icon, } if len(m.Actions) > 0 { actions, err := json.Marshal(m.Actions) @@ -166,11 +167,6 @@ func toFirebaseMessage(m *message, auther auth.Auther) (*messaging.Message, erro data["attachment_expires"] = fmt.Sprintf("%d", m.Attachment.Expires) data["attachment_url"] = m.Attachment.URL } - if m.Icon != nil { - data["icon_url"] = m.Icon.URL - data["icon_type"] = m.Icon.Type - data["icon_size"] = fmt.Sprintf("%d", m.Icon.Size) - } apnsConfig = createAPNSAlertConfig(m, data) } else { // If anonymous read for a topic is not allowed, we cannot send the message along diff --git a/server/server_firebase_test.go b/server/server_firebase_test.go index 7301057f..8d898db1 100644 --- a/server/server_firebase_test.go +++ b/server/server_firebase_test.go @@ -123,11 +123,7 @@ func TestToFirebaseMessage_Message_Normal_Allowed(t *testing.T) { m.Priority = 4 m.Tags = []string{"tag 1", "tag2"} m.Click = "https://google.com" - m.Icon = &icon{ - URL: "https://ntfy.sh/static/img/ntfy.png", - Type: "image/jpeg", - Size: 4567, - } + m.Icon = "https://ntfy.sh/static/img/ntfy.png" m.Title = "some title" m.Actions = []*action{ { @@ -178,9 +174,7 @@ func TestToFirebaseMessage_Message_Normal_Allowed(t *testing.T) { "priority": "4", "tags": strings.Join(m.Tags, ","), "click": "https://google.com", - "icon_url": "https://ntfy.sh/static/img/ntfy.png", - "icon_type": "image/jpeg", - "icon_size": "4567", + "icon": "https://ntfy.sh/static/img/ntfy.png", "title": "some title", "message": "this is a message", "actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`, @@ -201,9 +195,7 @@ func TestToFirebaseMessage_Message_Normal_Allowed(t *testing.T) { "priority": "4", "tags": strings.Join(m.Tags, ","), "click": "https://google.com", - "icon_url": "https://ntfy.sh/static/img/ntfy.png", - "icon_type": "image/jpeg", - "icon_size": "4567", + "icon": "https://ntfy.sh/static/img/ntfy.png", "title": "some title", "message": "this is a message", "actions": `[{"id":"123","action":"view","label":"Open page","clear":true,"url":"https://ntfy.sh"},{"id":"456","action":"http","label":"Close door","clear":false,"url":"https://door.com/close","method":"PUT","headers":{"really":"yes"}}]`, diff --git a/server/server_test.go b/server/server_test.go index 13e51377..ea3495d6 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -1058,7 +1058,7 @@ func TestServer_PublishAsJSON(t *testing.T) { require.Equal(t, "http://google.com", m.Attachment.URL) require.Equal(t, "google.pdf", m.Attachment.Name) require.Equal(t, "http://ntfy.sh", m.Click) - require.Equal(t, "https://ntfy.sh/static/img/ntfy.png", m.Icon.URL) + require.Equal(t, "https://ntfy.sh/static/img/ntfy.png", m.Icon) require.Equal(t, 4, m.Priority) require.True(t, m.Time > time.Now().Unix()+29*60) diff --git a/server/types.go b/server/types.go index 3a5e9fa9..7a5badf8 100644 --- a/server/types.go +++ b/server/types.go @@ -31,7 +31,7 @@ type message struct { Click string `json:"click,omitempty"` Actions []*action `json:"actions,omitempty"` Attachment *attachment `json:"attachment,omitempty"` - Icon *icon `json:"icon,omitempty"` + Icon string `json:"icon,omitempty"` PollID string `json:"poll_id,omitempty"` Sender string `json:"-"` // IP address of uploader, used for rate limiting Encoding string `json:"encoding,omitempty"` // empty for raw UTF-8, or "base64" for encoded bytes @@ -45,12 +45,6 @@ type attachment struct { URL string `json:"url"` } -type icon struct { - URL string `json:"url"` - Type string `json:"type,omitempty"` - Size int64 `json:"size,omitempty"` -} - type action struct { ID string `json:"id"` Action string `json:"action"` // "view", "broadcast", or "http"