Merge lp:~sergiusens/account-polld/notif_max into lp:~phablet-team/account-polld/trunk

Proposed by Sergio Schvezov
Status: Merged
Approved by: Sergio Schvezov
Approved revision: 25
Merged at revision: 20
Proposed branch: lp:~sergiusens/account-polld/notif_max
Merge into: lp:~phablet-team/account-polld/trunk
Prerequisite: lp:~sergiusens/account-polld/card_q
Diff against target: 216 lines (+111/-29)
2 files modified
plugins/twitter/twitter.go (+108/-26)
plugins/twitter/twitter_test.go (+3/-3)
To merge this branch: bzr merge lp:~sergiusens/account-polld/notif_max
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Manuel de la Peña (community) Approve
Review via email: mp+228320@code.launchpad.net

Commit message

Only create a max of 2 notifications and 1 consolidated one for each status and direct messages poll in the twitter plugin.

Description of the change

Only create a max of 2 notifications and 1 consolidated one for each status and direct messages

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
22. By Sergio Schvezov

Polish text

23. By Sergio Schvezov

Missing one sep for Join

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Manuel de la Peña (mandel) wrote :

Approving with a small recommendation and with the knowledge that the duplicated logic is merged in a future branch.

review: Approve
24. By Sergio Schvezov

Removing the mistery from the magic numbers

25. By Sergio Schvezov

Updating tests

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'plugins/twitter/twitter.go'
2--- plugins/twitter/twitter.go 2014-07-25 18:31:45 +0000
3+++ plugins/twitter/twitter.go 2014-07-25 18:31:45 +0000
4@@ -22,6 +22,7 @@
5 "fmt"
6 "net/http"
7 "net/url"
8+ "sort"
9 "strings"
10
11 "launchpad.net/account-polld/accounts"
12@@ -33,6 +34,13 @@
13
14 const twitterIcon = "/usr/share/click/preinstalled/.click/users/@all/com.ubuntu.developer.webapps.webapp-twitter/twitter.png"
15
16+const (
17+ maxIndividualStatuses = 2
18+ consolidatedStatusIndexStart = maxIndividualStatuses
19+ maxIndividualDirectMessages = 2
20+ consolidatedDirectMessageIndexStart = maxIndividualDirectMessages
21+)
22+
23 type twitterPlugin struct {
24 lastMentionId int64
25 lastDirectMessageId int64
26@@ -84,23 +92,54 @@
27 if err := decoder.Decode(&statuses); err != nil {
28 return nil, err
29 }
30+
31+ sort.Sort(sort.Reverse(byStatusId(statuses)))
32+ if len(statuses) < 1 {
33+ return nil, nil
34+ }
35+ p.lastMentionId = statuses[0].Id
36+
37 pushMsg := []plugins.PushMessage{}
38- latestStatus := p.lastMentionId
39 for _, s := range statuses {
40 pushMsg = append(pushMsg, plugins.PushMessage{
41 Notification: plugins.Notification{
42 Card: &plugins.Card{
43- Summary: fmt.Sprintf("Mention from @%s", s.User.ScreenName),
44+ Summary: fmt.Sprintf("@%s mentioned you", s.User.ScreenName),
45 Body: s.Text,
46- Icon: twitterIcon,
47- },
48- },
49- })
50- if s.Id > latestStatus {
51- latestStatus = s.Id
52- }
53- }
54- p.lastMentionId = latestStatus
55+ Actions: []string{fmt.Sprintf("http://mobile.twitter.com/%s/statuses/%d", s.User.ScreenName, s.Id)},
56+ Icon: twitterIcon,
57+ Persist: true,
58+ Popup: true,
59+ },
60+ Sound: plugins.DefaultSound(),
61+ Vibrate: plugins.DefaultVibration(),
62+ },
63+ })
64+ if len(pushMsg) == maxIndividualStatuses {
65+ break
66+ }
67+ }
68+ // Now we consolidate the remaining statuses
69+ if len(statuses) > len(pushMsg) {
70+ var screennames []string
71+ for _, s := range statuses[consolidatedStatusIndexStart:] {
72+ screennames = append(screennames, s.User.ScreenName)
73+ }
74+ pushMsg = append(pushMsg, plugins.PushMessage{
75+ Notification: plugins.Notification{
76+ Card: &plugins.Card{
77+ Summary: "Multiple more mentions",
78+ Body: fmt.Sprintf("From %s", strings.Join(screennames, ", ")),
79+ Actions: []string{"http://mobile.twitter.com/i/connect"},
80+ Icon: twitterIcon,
81+ Persist: true,
82+ Popup: true,
83+ },
84+ Sound: plugins.DefaultSound(),
85+ Vibrate: plugins.DefaultVibration(),
86+ },
87+ })
88+ }
89 return pushMsg, nil
90 }
91
92@@ -120,27 +159,54 @@
93 if err := decoder.Decode(&dms); err != nil {
94 return nil, err
95 }
96+
97+ sort.Sort(sort.Reverse(byDMId(dms)))
98+ if len(dms) < 1 {
99+ return nil, nil
100+ }
101+ p.lastDirectMessageId = dms[0].Id
102+
103 pushMsg := []plugins.PushMessage{}
104- latestDM := p.lastDirectMessageId
105 for _, m := range dms {
106 pushMsg = append(pushMsg, plugins.PushMessage{
107 Notification: plugins.Notification{
108 Card: &plugins.Card{
109- Summary: fmt.Sprintf("Direct message from @%s", m.Sender.ScreenName),
110+ Summary: fmt.Sprintf("@%s sent you a DM", m.Sender.ScreenName),
111 Body: m.Text,
112- Icon: twitterIcon,
113- Persist: true,
114- Popup: true,
115- },
116- Sound: plugins.DefaultSound(),
117- Vibrate: plugins.DefaultVibration(),
118- },
119- })
120- if m.Id > latestDM {
121- latestDM = m.Id
122- }
123- }
124- p.lastDirectMessageId = latestDM
125+ Actions: []string{fmt.Sprintf("http://mobile.twitter.com/%s/messages", m.Sender.ScreenName)},
126+ Icon: twitterIcon,
127+ Persist: true,
128+ Popup: true,
129+ },
130+ Sound: plugins.DefaultSound(),
131+ Vibrate: plugins.DefaultVibration(),
132+ },
133+ })
134+ if len(pushMsg) == maxIndividualDirectMessages {
135+ break
136+ }
137+ }
138+ // Now we consolidate the remaining messages
139+ if len(dms) > len(pushMsg) {
140+ var senders []string
141+ for _, m := range dms[consolidatedDirectMessageIndexStart:] {
142+ senders = append(senders, m.Sender.ScreenName)
143+ }
144+ pushMsg = append(pushMsg, plugins.PushMessage{
145+ Notification: plugins.Notification{
146+ Card: &plugins.Card{
147+ Summary: "Multiple direct messages available",
148+ Body: fmt.Sprintf("From %s", strings.Join(senders, ", ")),
149+ Actions: []string{"http://mobile.twitter.com/messages"},
150+ Icon: twitterIcon,
151+ Persist: true,
152+ Popup: true,
153+ },
154+ Sound: plugins.DefaultSound(),
155+ Vibrate: plugins.DefaultVibration(),
156+ },
157+ })
158+ }
159 return pushMsg, nil
160 }
161
162@@ -183,6 +249,14 @@
163 Text string `json:"text"`
164 }
165
166+// ByStatusId implements sort.Interface for []status based on
167+// the Id field.
168+type byStatusId []status
169+
170+func (s byStatusId) Len() int { return len(s) }
171+func (s byStatusId) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
172+func (s byStatusId) Less(i, j int) bool { return s[i].Id < s[j].Id }
173+
174 // Direct message format is described here:
175 // https://dev.twitter.com/docs/api/1.1/get/direct_messages
176 type directMessage struct {
177@@ -193,6 +267,14 @@
178 Text string `json:"text"`
179 }
180
181+// ByStatusId implements sort.Interface for []status based on
182+// the Id field.
183+type byDMId []directMessage
184+
185+func (s byDMId) Len() int { return len(s) }
186+func (s byDMId) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
187+func (s byDMId) Less(i, j int) bool { return s[i].Id < s[j].Id }
188+
189 type user struct {
190 Id int64 `json:"id"`
191 ScreenName string `json:"screen_name"`
192
193=== modified file 'plugins/twitter/twitter_test.go'
194--- plugins/twitter/twitter_test.go 2014-07-18 11:37:04 +0000
195+++ plugins/twitter/twitter_test.go 2014-07-25 18:31:45 +0000
196@@ -403,9 +403,9 @@
197 messages, err := p.parseStatuses(resp)
198 c.Assert(err, IsNil)
199 c.Assert(len(messages), Equals, 2)
200- c.Check(messages[0].Notification.Card.Summary, Equals, "Mention from @spode")
201+ c.Check(messages[0].Notification.Card.Summary, Equals, "@spode mentioned you")
202 c.Check(messages[0].Notification.Card.Body, Equals, "@jasoncosta @themattharris Hey! Going to be in Frisco in October. Was hoping to have a meeting to talk about @thinkwall if you're around?")
203- c.Check(messages[1].Notification.Card.Summary, Equals, "Mention from @mikedroid")
204+ c.Check(messages[1].Notification.Card.Summary, Equals, "@mikedroid mentioned you")
205 c.Check(messages[1].Notification.Card.Body, Equals, "Got the shirt @jasoncosta thanks man! Loving the #twitter bird on the shirt :-)")
206 c.Check(p.lastMentionId, Equals, int64(242613977966850048))
207 }
208@@ -434,7 +434,7 @@
209 messages, err := p.parseDirectMessages(resp)
210 c.Assert(err, IsNil)
211 c.Assert(len(messages), Equals, 1)
212- c.Check(messages[0].Notification.Card.Summary, Equals, "Direct message from @theSeanCook")
213+ c.Check(messages[0].Notification.Card.Summary, Equals, "@theSeanCook sent you a DM")
214 c.Check(messages[0].Notification.Card.Body, Equals, "booyakasha")
215 c.Check(p.lastDirectMessageId, Equals, int64(240136858829479936))
216 }

Subscribers

People subscribed via source and target branches