Commit c89859bef9
Verified · cmc
Layout: unified · split
internal/control/notifications.go +9
| @@ -90,6 +90,7 @@ func notify(c *Ctx, userIDs []int64, n notice) { | |||
| 90 | body := noticeBody(c, n) | 90 | body := noticeBody(c, n) |
| 91 | for _, id := range recipients { | 91 | for _, id := range recipients { |
| 92 | c.Store.AddNotice(id, n.repo.ID, n.kind, c.User.Username, n.action, n.path) | 92 | c.Store.AddNotice(id, n.repo.ID, n.kind, c.User.Username, n.action, n.path) |
| 93 | c.Store.EnqueuePush(id, pushTitle(n), pushBody(c.User.Username, n), n.path) | ||
| 93 | if !sendMail { | 94 | if !sendMail { |
| 94 | continue | 95 | continue |
| 95 | } | 96 | } |
| @@ -155,6 +156,14 @@ func noticeBody(c *Ctx, n notice) string { | |||
| 155 | return b.String() | 156 | return b.String() |
| 156 | } | 157 | } |
| 157 | 158 | ||
| 159 | // pushTitle and pushBody are the alert's two lines. The body is built | ||
| 160 | // from the same two values AddNotice files, so the alert and the inbox | ||
| 161 | // row cannot disagree about what happened. The title is the repository, | ||
| 162 | // which also groups a repository's notices in Notification Center. | ||
| 163 | func pushTitle(n notice) string { return n.repo.Path() } | ||
| 164 | |||
| 165 | func pushBody(actor string, n notice) string { return actor + " " + n.action } | ||
| 166 | |||
| 158 | func issueSubject(repo store.Repo, number int64, title string) string { | 167 | func issueSubject(repo store.Repo, number int64, title string) string { |
| 159 | return fmt.Sprintf("[%s] #%d: %s", repo.Path(), number, title) | 168 | return fmt.Sprintf("[%s] #%d: %s", repo.Path(), number, title) |
| 160 | } | 169 | } |
internal/control/notifications_test.go +67
| @@ -36,6 +36,73 @@ func notifTestCtx(t *testing.T, username string) *Ctx { | |||
| 36 | } | 36 | } |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | // testRepoWithWatcher returns a Ctx acting as alice, a repository she | ||
| 40 | // owns, and bob's user id with a watch row on it — the shared setup for | ||
| 41 | // notify's recipient-widening tests. | ||
| 42 | func testRepoWithWatcher(t *testing.T) (*Ctx, store.Repo, int64) { | ||
| 43 | t.Helper() | ||
| 44 | c := notifTestCtx(t, "alice") | ||
| 45 | repoID, err := c.Store.CreateRepo("user", c.User.ID, "app", "public") | ||
| 46 | if err != nil { | ||
| 47 | t.Fatal(err) | ||
| 48 | } | ||
| 49 | repo, err := c.Store.RepoByID(repoID) | ||
| 50 | if err != nil { | ||
| 51 | t.Fatal(err) | ||
| 52 | } | ||
| 53 | bob, err := c.Store.CreateUser("bob", false) | ||
| 54 | if err != nil { | ||
| 55 | t.Fatal(err) | ||
| 56 | } | ||
| 57 | if err := c.Store.SetRepoWatch(repo.ID, bob, "watching"); err != nil { | ||
| 58 | t.Fatal(err) | ||
| 59 | } | ||
| 60 | return c, repo, bob | ||
| 61 | } | ||
| 62 | |||
| 63 | func TestNotifyQueuesPush(t *testing.T) { | ||
| 64 | c, repo, bob := testRepoWithWatcher(t) // alice acts, bob watches | ||
| 65 | c.Store.AddPushDevice(bob, "tok-b", "iphone") | ||
| 66 | |||
| 67 | notify(c, []int64{bob}, notice{repo: repo, kind: "issue", | ||
| 68 | subject: "[alice/app] #1: title", | ||
| 69 | action: "opened issue #1", | ||
| 70 | path: "alice/app/issues/1"}) | ||
| 71 | |||
| 72 | due, err := c.Store.DuePush(20) | ||
| 73 | if err != nil { | ||
| 74 | t.Fatalf("DuePush: %v", err) | ||
| 75 | } | ||
| 76 | if len(due) != 1 { | ||
| 77 | t.Fatalf("want one queued push, got %d", len(due)) | ||
| 78 | } | ||
| 79 | // The push body is the inbox row's summary, so the two surfaces | ||
| 80 | // cannot disagree about what happened. | ||
| 81 | if due[0].Title != "alice/app" { | ||
| 82 | t.Fatalf("title = %q", due[0].Title) | ||
| 83 | } | ||
| 84 | if due[0].Body != "alice opened issue #1" { | ||
| 85 | t.Fatalf("body = %q", due[0].Body) | ||
| 86 | } | ||
| 87 | if due[0].Path != "alice/app/issues/1" { | ||
| 88 | t.Fatalf("path = %q", due[0].Path) | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | func TestNotifyQueuesNoPushForTheActor(t *testing.T) { | ||
| 93 | c, repo, _ := testRepoWithWatcher(t) | ||
| 94 | c.Store.AddPushDevice(c.User.ID, "tok-self", "iphone") | ||
| 95 | |||
| 96 | notify(c, []int64{c.User.ID}, notice{repo: repo, kind: "issue", | ||
| 97 | subject: "s", action: "opened issue #1", path: "alice/app/issues/1"}) | ||
| 98 | |||
| 99 | // NotifyRecipients already drops the actor; push inherits that and | ||
| 100 | // must not find its own way around it. | ||
| 101 | if due, _ := c.Store.DuePush(20); len(due) != 0 { | ||
| 102 | t.Fatalf("queued a push to the actor") | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 39 | func TestNotificationsDeviceAddReadsStdin(t *testing.T) { | 106 | func TestNotificationsDeviceAddReadsStdin(t *testing.T) { |
| 40 | c := notifTestCtx(t, "alice") | 107 | c := notifTestCtx(t, "alice") |
| 41 | c.Stdin = strings.NewReader("DEVTOKEN\n") | 108 | c.Stdin = strings.NewReader("DEVTOKEN\n") |