Skip to content

Commit 7d006b2

Browse files
committed
actually send rich content pushes
1 parent 4b68fde commit 7d006b2

File tree

5 files changed

+29
-57
lines changed

5 files changed

+29
-57
lines changed

server/drafty/drafty.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package drafty
33

44
import (
5+
"encoding/json"
56
"errors"
67
"sort"
78
"strings"
@@ -115,7 +116,7 @@ var tags = map[string]spanfmt{
115116

116117
// Preview shortens Drafty to the specified length (in runes) and removes large content from entities making them
117118
// suitable for a one-line preview, for example for showing in push notifications.
118-
func Preview(content interface{}, length int) (interface{}, error) {
119+
func Preview(content interface{}, length int) (string, error) {
119120
if content == nil {
120121
return "", nil
121122
}
@@ -210,7 +211,9 @@ func Preview(content interface{}, length int) (interface{}, error) {
210211
}
211212
}
212213

213-
return preview, nil
214+
data, err := json.Marshal(preview)
215+
216+
return string(data), err
214217
}
215218

216219
var lightData = []string{"mime", "name", "width", "height", "size"}

server/drafty/drafty_test.go

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package drafty
22

33
import (
44
"encoding/json"
5-
"reflect"
65
"testing"
76
)
87

98
var validInputs = []string{
109
`{
11-
"ent":[{"data":{"mime":"image/jpeg","name":"hello.jpg","val":"<38992, bytes: ...>"},"tp":"EX"}],
10+
"ent":[{"data":{"mime":"image/jpeg","name":"hello.jpg","val":"<38992, bytes: ...>","width":100, "height":80},"tp":"EX"}],
1211
"fmt":[{"at":-1, "key":0}]
1312
}`,
1413
`{
@@ -96,50 +95,14 @@ func TestToPlainText(t *testing.T) {
9695
}
9796

9897
func TestPreview(t *testing.T) {
99-
expect := []map[string]interface{}{
100-
map[string]interface{}{
101-
"fmt": []map[string]interface{}{map[string]interface{}{"at": -1, "key": 0}},
102-
"ent": []map[string]interface{}{map[string]interface{}{
103-
"tp": "EX",
104-
"data": map[string]interface{}{"mime": "image/jpeg", "name": "hello.jpg"}},
105-
},
106-
},
107-
map[string]interface{}{
108-
"txt": "https://api.tin",
109-
"fmt": []map[string]interface{}{map[string]interface{}{"len": 22, "key": 0}},
110-
"ent": []map[string]interface{}{map[string]interface{}{"tp": "LN"}},
111-
},
112-
map[string]interface{}{
113-
"txt": "https://api.tin",
114-
"fmt": []map[string]interface{}{map[string]interface{}{"len": 22, "key": 0}},
115-
"ent": []map[string]interface{}{map[string]interface{}{"tp": "LN"}},
116-
},
117-
map[string]interface{}{
118-
"txt": " ",
119-
"fmt": []map[string]interface{}{map[string]interface{}{"len": 1, "key": 0}},
120-
"ent": []map[string]interface{}{map[string]interface{}{
121-
"tp": "IM",
122-
"data": map[string]interface{}{
123-
"width": 638.0,
124-
"height": 213.0,
125-
"mime": "image/jpeg",
126-
"name": "roses.jpg",
127-
},
128-
}},
129-
},
130-
map[string]interface{}{
131-
"txt": "This text is fo",
132-
"fmt": []map[string]interface{}{
133-
map[string]interface{}{"at": 5, "len": 4, "tp": "ST"},
134-
map[string]interface{}{"at": 13, "len": 9, "tp": "EM"},
135-
},
136-
},
137-
map[string]interface{}{
138-
"txt": "мультибайтовый ",
139-
"fmt": []map[string]interface{}{map[string]interface{}{"tp": "ST", "len": 14}},
140-
},
98+
expect := []string{
99+
`{"ent":[{"data":{"height":80,"mime":"image/jpeg","name":"hello.jpg","width":100},"tp":"EX"}],"fmt":[{"at":-1,"key":0}]}`,
100+
`{"ent":[{"tp":"LN"}],"fmt":[{"key":0,"len":22}],"txt":"https://api.tin"}`,
101+
`{"ent":[{"tp":"LN"}],"fmt":[{"key":0,"len":22}],"txt":"https://api.tin"}`,
102+
`{"ent":[{"data":{"height":213,"mime":"image/jpeg","name":"roses.jpg","width":638},"tp":"IM"}],"fmt":[{"key":0,"len":1}],"txt":" "}`,
103+
`{"fmt":[{"at":5,"len":4,"tp":"ST"},{"at":13,"len":9,"tp":"EM"}],"txt":"This text is fo"}`,
104+
`{"fmt":[{"len":14,"tp":"ST"}],"txt":"мультибайтовый "}`,
141105
}
142-
143106
for i := range validInputs {
144107
var val interface{}
145108
json.Unmarshal([]byte(validInputs[i]), &val)
@@ -148,7 +111,7 @@ func TestPreview(t *testing.T) {
148111
t.Error(err)
149112
}
150113

151-
if !reflect.DeepEqual(res, expect[i]) {
114+
if res != expect[i] {
152115
t.Errorf("%d output '%s' does not match '%s'", i, res, expect[i])
153116
}
154117
}

server/push/fcm/payload.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,19 +151,26 @@ func payloadToData(pl *push.Payload) (map[string]string, error) {
151151
if pl.What == push.ActMsg {
152152
data["seq"] = strconv.Itoa(pl.SeqId)
153153
data["mime"] = pl.ContentType
154+
155+
// Convert Drafty content to plain text (clients 0.16 and below).
154156
data["content"], err = drafty.ToPlainText(pl.Content)
155157
if err != nil {
156158
return nil, err
157159
}
158-
159-
// Trim long strings to 80 runes.
160+
// Trim long strings to 128 runes.
160161
// Check byte length first and don't waste time converting short strings.
161-
if len(data["content"]) > maxMessageLength {
162+
if len(data["content"]) > push.MaxPayloadLength {
162163
runes := []rune(data["content"])
163-
if len(runes) > maxMessageLength {
164-
data["content"] = string(runes[:maxMessageLength]) + "…"
164+
if len(runes) > push.MaxPayloadLength {
165+
data["content"] = string(runes[:push.MaxPayloadLength]) + "…"
165166
}
166167
}
168+
169+
// Rich content for clients version 0.17 and above.
170+
data["rc"], err = drafty.Preview(pl.Content, push.MaxPayloadLength)
171+
if err != nil {
172+
return nil, err
173+
}
167174
} else if pl.What == push.ActSub {
168175
data["modeWant"] = pl.ModeWant.String()
169176
data["modeGiven"] = pl.ModeGiven.String()

server/push/fcm/push_fcm.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ const (
3232

3333
// The number of sub/unsub requests sent in one batch. FCM constant.
3434
subBatchSize = 1000
35-
36-
// Maximum length of a text message in runes. The message is clipped if length is exceeded.
37-
// TODO: implement intelligent clipping of Drafty messages.
38-
maxMessageLength = 80
3935
)
4036

4137
// Handler represents the push handler; implements push.PushHandler interface.

server/push/push.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const (
1717
ActSub = "sub"
1818
)
1919

20+
// Maximum length of push payload in multibyte characters.
21+
const MaxPayloadLength = 128
22+
2023
// Recipient is a user targeted by the push.
2124
type Recipient struct {
2225
// Count of user's connections that were live when the packet was dispatched from the server

0 commit comments

Comments
 (0)