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
=== modified file 'plugins/twitter/twitter.go'
--- plugins/twitter/twitter.go 2014-07-25 18:31:45 +0000
+++ plugins/twitter/twitter.go 2014-07-25 18:31:45 +0000
@@ -22,6 +22,7 @@
22 "fmt"22 "fmt"
23 "net/http"23 "net/http"
24 "net/url"24 "net/url"
25 "sort"
25 "strings"26 "strings"
2627
27 "launchpad.net/account-polld/accounts"28 "launchpad.net/account-polld/accounts"
@@ -33,6 +34,13 @@
3334
34const twitterIcon = "/usr/share/click/preinstalled/.click/users/@all/com.ubuntu.developer.webapps.webapp-twitter/twitter.png"35const twitterIcon = "/usr/share/click/preinstalled/.click/users/@all/com.ubuntu.developer.webapps.webapp-twitter/twitter.png"
3536
37const (
38 maxIndividualStatuses = 2
39 consolidatedStatusIndexStart = maxIndividualStatuses
40 maxIndividualDirectMessages = 2
41 consolidatedDirectMessageIndexStart = maxIndividualDirectMessages
42)
43
36type twitterPlugin struct {44type twitterPlugin struct {
37 lastMentionId int6445 lastMentionId int64
38 lastDirectMessageId int6446 lastDirectMessageId int64
@@ -84,23 +92,54 @@
84 if err := decoder.Decode(&statuses); err != nil {92 if err := decoder.Decode(&statuses); err != nil {
85 return nil, err93 return nil, err
86 }94 }
95
96 sort.Sort(sort.Reverse(byStatusId(statuses)))
97 if len(statuses) < 1 {
98 return nil, nil
99 }
100 p.lastMentionId = statuses[0].Id
101
87 pushMsg := []plugins.PushMessage{}102 pushMsg := []plugins.PushMessage{}
88 latestStatus := p.lastMentionId
89 for _, s := range statuses {103 for _, s := range statuses {
90 pushMsg = append(pushMsg, plugins.PushMessage{104 pushMsg = append(pushMsg, plugins.PushMessage{
91 Notification: plugins.Notification{105 Notification: plugins.Notification{
92 Card: &plugins.Card{106 Card: &plugins.Card{
93 Summary: fmt.Sprintf("Mention from @%s", s.User.ScreenName),107 Summary: fmt.Sprintf("@%s mentioned you", s.User.ScreenName),
94 Body: s.Text,108 Body: s.Text,
95 Icon: twitterIcon,109 Actions: []string{fmt.Sprintf("http://mobile.twitter.com/%s/statuses/%d", s.User.ScreenName, s.Id)},
96 },110 Icon: twitterIcon,
97 },111 Persist: true,
98 })112 Popup: true,
99 if s.Id > latestStatus {113 },
100 latestStatus = s.Id114 Sound: plugins.DefaultSound(),
101 }115 Vibrate: plugins.DefaultVibration(),
102 }116 },
103 p.lastMentionId = latestStatus117 })
118 if len(pushMsg) == maxIndividualStatuses {
119 break
120 }
121 }
122 // Now we consolidate the remaining statuses
123 if len(statuses) > len(pushMsg) {
124 var screennames []string
125 for _, s := range statuses[consolidatedStatusIndexStart:] {
126 screennames = append(screennames, s.User.ScreenName)
127 }
128 pushMsg = append(pushMsg, plugins.PushMessage{
129 Notification: plugins.Notification{
130 Card: &plugins.Card{
131 Summary: "Multiple more mentions",
132 Body: fmt.Sprintf("From %s", strings.Join(screennames, ", ")),
133 Actions: []string{"http://mobile.twitter.com/i/connect"},
134 Icon: twitterIcon,
135 Persist: true,
136 Popup: true,
137 },
138 Sound: plugins.DefaultSound(),
139 Vibrate: plugins.DefaultVibration(),
140 },
141 })
142 }
104 return pushMsg, nil143 return pushMsg, nil
105}144}
106145
@@ -120,27 +159,54 @@
120 if err := decoder.Decode(&dms); err != nil {159 if err := decoder.Decode(&dms); err != nil {
121 return nil, err160 return nil, err
122 }161 }
162
163 sort.Sort(sort.Reverse(byDMId(dms)))
164 if len(dms) < 1 {
165 return nil, nil
166 }
167 p.lastDirectMessageId = dms[0].Id
168
123 pushMsg := []plugins.PushMessage{}169 pushMsg := []plugins.PushMessage{}
124 latestDM := p.lastDirectMessageId
125 for _, m := range dms {170 for _, m := range dms {
126 pushMsg = append(pushMsg, plugins.PushMessage{171 pushMsg = append(pushMsg, plugins.PushMessage{
127 Notification: plugins.Notification{172 Notification: plugins.Notification{
128 Card: &plugins.Card{173 Card: &plugins.Card{
129 Summary: fmt.Sprintf("Direct message from @%s", m.Sender.ScreenName),174 Summary: fmt.Sprintf("@%s sent you a DM", m.Sender.ScreenName),
130 Body: m.Text,175 Body: m.Text,
131 Icon: twitterIcon,176 Actions: []string{fmt.Sprintf("http://mobile.twitter.com/%s/messages", m.Sender.ScreenName)},
132 Persist: true,177 Icon: twitterIcon,
133 Popup: true,178 Persist: true,
134 },179 Popup: true,
135 Sound: plugins.DefaultSound(),180 },
136 Vibrate: plugins.DefaultVibration(),181 Sound: plugins.DefaultSound(),
137 },182 Vibrate: plugins.DefaultVibration(),
138 })183 },
139 if m.Id > latestDM {184 })
140 latestDM = m.Id185 if len(pushMsg) == maxIndividualDirectMessages {
141 }186 break
142 }187 }
143 p.lastDirectMessageId = latestDM188 }
189 // Now we consolidate the remaining messages
190 if len(dms) > len(pushMsg) {
191 var senders []string
192 for _, m := range dms[consolidatedDirectMessageIndexStart:] {
193 senders = append(senders, m.Sender.ScreenName)
194 }
195 pushMsg = append(pushMsg, plugins.PushMessage{
196 Notification: plugins.Notification{
197 Card: &plugins.Card{
198 Summary: "Multiple direct messages available",
199 Body: fmt.Sprintf("From %s", strings.Join(senders, ", ")),
200 Actions: []string{"http://mobile.twitter.com/messages"},
201 Icon: twitterIcon,
202 Persist: true,
203 Popup: true,
204 },
205 Sound: plugins.DefaultSound(),
206 Vibrate: plugins.DefaultVibration(),
207 },
208 })
209 }
144 return pushMsg, nil210 return pushMsg, nil
145}211}
146212
@@ -183,6 +249,14 @@
183 Text string `json:"text"`249 Text string `json:"text"`
184}250}
185251
252// ByStatusId implements sort.Interface for []status based on
253// the Id field.
254type byStatusId []status
255
256func (s byStatusId) Len() int { return len(s) }
257func (s byStatusId) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
258func (s byStatusId) Less(i, j int) bool { return s[i].Id < s[j].Id }
259
186// Direct message format is described here:260// Direct message format is described here:
187// https://dev.twitter.com/docs/api/1.1/get/direct_messages261// https://dev.twitter.com/docs/api/1.1/get/direct_messages
188type directMessage struct {262type directMessage struct {
@@ -193,6 +267,14 @@
193 Text string `json:"text"`267 Text string `json:"text"`
194}268}
195269
270// ByStatusId implements sort.Interface for []status based on
271// the Id field.
272type byDMId []directMessage
273
274func (s byDMId) Len() int { return len(s) }
275func (s byDMId) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
276func (s byDMId) Less(i, j int) bool { return s[i].Id < s[j].Id }
277
196type user struct {278type user struct {
197 Id int64 `json:"id"`279 Id int64 `json:"id"`
198 ScreenName string `json:"screen_name"`280 ScreenName string `json:"screen_name"`
199281
=== modified file 'plugins/twitter/twitter_test.go'
--- plugins/twitter/twitter_test.go 2014-07-18 11:37:04 +0000
+++ plugins/twitter/twitter_test.go 2014-07-25 18:31:45 +0000
@@ -403,9 +403,9 @@
403 messages, err := p.parseStatuses(resp)403 messages, err := p.parseStatuses(resp)
404 c.Assert(err, IsNil)404 c.Assert(err, IsNil)
405 c.Assert(len(messages), Equals, 2)405 c.Assert(len(messages), Equals, 2)
406 c.Check(messages[0].Notification.Card.Summary, Equals, "Mention from @spode")406 c.Check(messages[0].Notification.Card.Summary, Equals, "@spode mentioned you")
407 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?")407 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?")
408 c.Check(messages[1].Notification.Card.Summary, Equals, "Mention from @mikedroid")408 c.Check(messages[1].Notification.Card.Summary, Equals, "@mikedroid mentioned you")
409 c.Check(messages[1].Notification.Card.Body, Equals, "Got the shirt @jasoncosta thanks man! Loving the #twitter bird on the shirt :-)")409 c.Check(messages[1].Notification.Card.Body, Equals, "Got the shirt @jasoncosta thanks man! Loving the #twitter bird on the shirt :-)")
410 c.Check(p.lastMentionId, Equals, int64(242613977966850048))410 c.Check(p.lastMentionId, Equals, int64(242613977966850048))
411}411}
@@ -434,7 +434,7 @@
434 messages, err := p.parseDirectMessages(resp)434 messages, err := p.parseDirectMessages(resp)
435 c.Assert(err, IsNil)435 c.Assert(err, IsNil)
436 c.Assert(len(messages), Equals, 1)436 c.Assert(len(messages), Equals, 1)
437 c.Check(messages[0].Notification.Card.Summary, Equals, "Direct message from @theSeanCook")437 c.Check(messages[0].Notification.Card.Summary, Equals, "@theSeanCook sent you a DM")
438 c.Check(messages[0].Notification.Card.Body, Equals, "booyakasha")438 c.Check(messages[0].Notification.Card.Body, Equals, "booyakasha")
439 c.Check(p.lastDirectMessageId, Equals, int64(240136858829479936))439 c.Check(p.lastDirectMessageId, Equals, int64(240136858829479936))
440}440}

Subscribers

People subscribed via source and target branches