{"action":"created","comment":{"url":"https://api.github.com/repos/keybase/client/pulls/comments/86153883","pull_request_review_id":6825154,"id":86153883,"diff_hunk":"@@ -0,0 +1,215 @@\n+package storage\n+\n+import (\n+\t\"fmt\"\n+\t\"sync\"\n+\n+\t\"golang.org/x/crypto/nacl/secretbox\"\n+\n+\t\"github.com/keybase/client/go/libkb\"\n+\t\"github.com/keybase/client/go/protocol/chat1\"\n+)\n+\n+type Outbox struct {\n+\tsync.Mutex\n+\tlibkb.Contextified\n+\n+\tgetSecretUI func() libkb.SecretUI\n+}\n+\n+type boxedOutbox struct {\n+\tV int\n+\tN [24]byte\n+\tE []byte\n+}\n+\n+func NewOutbox(g *libkb.GlobalContext, getSecretUI func() libkb.SecretUI) *Outbox {\n+\treturn &Outbox{\n+\t\tContextified: libkb.NewContextified(g),\n+\t\tgetSecretUI:  getSecretUI,\n+\t}\n+}\n+\n+func (o *Outbox) dbKey() libkb.DbKey {\n+\treturn libkb.DbKey{\n+\t\tTyp: libkb.DBChatOutbox,\n+\t\tKey: \"ob\",\n+\t}\n+}\n+\n+func (o *Outbox) readBox() ([]chat1.OutboxRecord, error) {\n+\tkey := o.dbKey()\n+\tb, found, err := o.G().LocalChatDb.GetRaw(key)\n+\tif err != nil {\n+\t\treturn nil, err\n+\t}\n+\tif !found {\n+\t\treturn []chat1.OutboxRecord{}, nil\n+\t}\n+\n+\t// Decode encrypted box\n+\tvar boxed boxedOutbox\n+\tif err := decode(b, &boxed); err != nil {\n+\t\treturn nil, err\n+\t}\n+\tif boxed.V > cryptoVersion {\n+\t\treturn []chat1.OutboxRecord{}, fmt.Errorf(\"bad crypto version: %d current: %d\", boxed.V,\n+\t\t\tcryptoVersion)\n+\t}\n+\tenckey, err := getSecretBoxKey(o.G(), o.getSecretUI)\n+\tif err != nil {\n+\t\treturn []chat1.OutboxRecord{}, err\n+\t}\n+\tpt, ok := secretbox.Open(nil, boxed.E, &boxed.N, &enckey)\n+\tif !ok {\n+\t\treturn []chat1.OutboxRecord{}, fmt.Errorf(\"failed to decrypt outbox\")\n+\t}\n+\tvar res []chat1.OutboxRecord\n+\tif err = decode(pt, &res); err != nil {\n+\t\treturn []chat1.OutboxRecord{}, err\n+\t}\n+\n+\treturn res, nil\n+}\n+\n+func (o *Outbox) writeBox(outbox []chat1.OutboxRecord) error {\n+\tkey := o.dbKey()\n+\n+\t// Encode outbox\n+\tdat, err := encode(outbox)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\n+\t// Encrypt outbox\n+\tenckey, err := getSecretBoxKey(o.G(), o.getSecretUI)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tvar nonce []byte\n+\tnonce, err = libkb.RandBytes(24)\n+\tif err != nil {\n+\t\treturn err\n+\t}\n+\tvar fnonce [24]byte\n+\tcopy(fnonce[:], nonce)\n+\tsealed := secretbox.Seal(nil, dat, &fnonce, &enckey)\n+\tboxed := boxedBlock{\n+\t\tV: cryptoVersion,\n+\t\tE: sealed,\n+\t\tN: fnonce,\n+\t}\n+\n+\t// Encode encrypted outbox\n+\tif dat, err = encode(boxed); err != nil {\n+\t\treturn err\n+\t}\n+\n+\t// Write out\n+\tif err = o.G().LocalChatDb.PutRaw(key, dat); err != nil {\n+\t\treturn err\n+\t}\n+\n+\treturn nil\n+}\n+\n+func (o *Outbox) maybeNuke(err libkb.ChatStorageError) libkb.ChatStorageError {\n+\tif err.ShouldClear() {\n+\t\tif err := o.G().LocalChatDb.Delete(o.dbKey()); err != nil {\n+\t\t\to.G().Log.Error(\"unable to clear inbox on error! err: %s\", err.Error())\n+\t\t}\n+\t}\n+\treturn err\n+}\n+\n+func (o *Outbox) Push(convID chat1.ConversationID, msg chat1.MessagePlaintext) (chat1.OutboxID, libkb.ChatStorageError) {\n+\to.Lock()\n+\tdefer o.Unlock()\n+\n+\t// Read outbox for the user\n+\tobox, err := o.readBox()\n+\tif err != nil {\n+\t\treturn nil, o.maybeNuke(libkb.NewChatStorageInternalError(o.G(),\n+\t\t\t\"error reading outbox: err: %s\", err.Error()))\n+\t}\n+\n+\t// Generate new outbox ID\n+\tvar outboxID chat1.OutboxID\n+\toutboxID, err = libkb.RandBytes(16)\n+\tif err != nil {\n+\t\treturn nil, o.maybeNuke(libkb.NewChatStorageInternalError(o.G(),\n+\t\t\t\"error getting outboxID: err: %s\", err.Error()))\n+\t}\n+\n+\t// Append record\n+\tobox = append(obox, chat1.OutboxRecord{\n+\t\tMsg:      msg,\n+\t\tConvID:   convID,\n+\t\tOutboxID: outboxID,\n+\t})\n+\n+\t// Write out box\n+\tif err := o.writeBox(obox); err != nil {\n+\t\treturn nil, o.maybeNuke(libkb.NewChatStorageInternalError(o.G(),\n+\t\t\t\"error writing outbox: err: %s\", err.Error()))\n+\t}\n+\n+\treturn outboxID, nil\n+}\n+\n+func (o *Outbox) Pull() ([]chat1.OutboxRecord, error) {\n+\to.Lock()\n+\tdefer o.Unlock()\n+\n+\t// Read outbox for the user\n+\tobox, err := o.readBox()\n+\tif err != nil {\n+\t\treturn nil, o.maybeNuke(libkb.NewChatStorageInternalError(o.G(),\n+\t\t\t\"error reading outbox: err: %s\", err.Error()))\n+\t}\n+\n+\treturn obox, nil\n+}\n+\n+func (o *Outbox) PullConv(convID chat1.ConversationID) ([]chat1.OutboxRecord, error) {\n+\to.Lock()\n+\tdefer o.Unlock()\n+\n+\t// Read outbox for the user\n+\tobox, err := o.readBox()\n+\tif err != nil {\n+\t\treturn nil, o.maybeNuke(libkb.NewChatStorageInternalError(o.G(),\n+\t\t\t\"error reading outbox: err: %s\", err.Error()))\n+\t}\n+\n+\tvar res []chat1.OutboxRecord\n+\tfor _, obr := range obox {\n+\t\tif obr.ConvID.Eq(convID) {\n+\t\t\tres = append(res, obr)\n+\t\t}\n+\t}\n+\treturn res, nil\n+}\n+\n+func (o *Outbox) PopN(n int) error {","path":"go/chat/storage/outbox.go","position":194,"original_position":194,"commit_id":"277d7f75ebf34d7e19b0e3b0b5b8b8fee855f577","original_commit_id":"daff9fcd8571a3939753dd32d2dfb86041eca54a","user":{"login":"mmaxim","id":1250314,"avatar_url":"https://avatars.githubusercontent.com/u/1250314?v=3","gravatar_id":"","url":"https://api.github.com/users/mmaxim","html_url":"https://github.com/mmaxim","followers_url":"https://api.github.com/users/mmaxim/followers","following_url":"https://api.github.com/users/mmaxim/following{/other_user}","gists_url":"https://api.github.com/users/mmaxim/gists{/gist_id}","starred_url":"https://api.github.com/users/mmaxim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mmaxim/subscriptions","organizations_url":"https://api.github.com/users/mmaxim/orgs","repos_url":"https://api.github.com/users/mmaxim/repos","events_url":"https://api.github.com/users/mmaxim/events{/privacy}","received_events_url":"https://api.github.com/users/mmaxim/received_events","type":"User","site_admin":false},"body":"Call `PopNOldestMessages`","created_at":"2016-11-02T14:31:12Z","updated_at":"2016-11-02T14:31:12Z","html_url":"https://github.com/keybase/client/pull/4769#discussion_r86153883","pull_request_url":"https://api.github.com/repos/keybase/client/pulls/4769","_links":{"self":{"href":"https://api.github.com/repos/keybase/client/pulls/comments/86153883"},"html":{"href":"https://github.com/keybase/client/pull/4769#discussion_r86153883"},"pull_request":{"href":"https://api.github.com/repos/keybase/client/pulls/4769"}}},"pull_request":{"url":"https://api.github.com/repos/keybase/client/pulls/4769","id":91863492,"html_url":"https://github.com/keybase/client/pull/4769","diff_url":"https://github.com/keybase/client/pull/4769.diff","patch_url":"https://github.com/keybase/client/pull/4769.patch","issue_url":"https://api.github.com/repos/keybase/client/issues/4769","number":4769,"state":"open","locked":false,"title":"add a nonblocking version of PostLocal CORE-4039","user":{"login":"mmaxim","id":1250314,"avatar_url":"https://avatars.githubusercontent.com/u/1250314?v=3","gravatar_id":"","url":"https://api.github.com/users/mmaxim","html_url":"https://github.com/mmaxim","followers_url":"https://api.github.com/users/mmaxim/followers","following_url":"https://api.github.com/users/mmaxim/following{/other_user}","gists_url":"https://api.github.com/users/mmaxim/gists{/gist_id}","starred_url":"https://api.github.com/users/mmaxim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mmaxim/subscriptions","organizations_url":"https://api.github.com/users/mmaxim/orgs","repos_url":"https://api.github.com/users/mmaxim/repos","events_url":"https://api.github.com/users/mmaxim/events{/privacy}","received_events_url":"https://api.github.com/users/mmaxim/received_events","type":"User","site_admin":false},"body":"@maxtaco r?\r\n\r\nThis adds the nonblocking version of `PostLocal` to the service, and a new `Outbox` and `Deliverer` to make it happen. I'll probably just need to walk through this with you, so that is all I will say 😄 ","created_at":"2016-11-01T21:06:11Z","updated_at":"2016-11-02T14:31:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e1f2b738752829c560fba19a7db3aa920a0db64c","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/keybase/client/pulls/4769/commits","review_comments_url":"https://api.github.com/repos/keybase/client/pulls/4769/comments","review_comment_url":"https://api.github.com/repos/keybase/client/pulls/comments{/number}","comments_url":"https://api.github.com/repos/keybase/client/issues/4769/comments","statuses_url":"https://api.github.com/repos/keybase/client/statuses/277d7f75ebf34d7e19b0e3b0b5b8b8fee855f577","head":{"label":"keybase:mike/CORE-4039","ref":"mike/CORE-4039","sha":"277d7f75ebf34d7e19b0e3b0b5b8b8fee855f577","user":{"login":"keybase","id":5400834,"avatar_url":"https://avatars.githubusercontent.com/u/5400834?v=3","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"repo":{"id":29099181,"name":"client","full_name":"keybase/client","owner":{"login":"keybase","id":5400834,"avatar_url":"https://avatars.githubusercontent.com/u/5400834?v=3","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/keybase/client","description":"Keybase Go Library, Client, Service, OS X, iOS, Android, Electron","fork":false,"url":"https://api.github.com/repos/keybase/client","forks_url":"https://api.github.com/repos/keybase/client/forks","keys_url":"https://api.github.com/repos/keybase/client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/keybase/client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/keybase/client/teams","hooks_url":"https://api.github.com/repos/keybase/client/hooks","issue_events_url":"https://api.github.com/repos/keybase/client/issues/events{/number}","events_url":"https://api.github.com/repos/keybase/client/events","assignees_url":"https://api.github.com/repos/keybase/client/assignees{/user}","branches_url":"https://api.github.com/repos/keybase/client/branches{/branch}","tags_url":"https://api.github.com/repos/keybase/client/tags","blobs_url":"https://api.github.com/repos/keybase/client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/keybase/client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/keybase/client/git/refs{/sha}","trees_url":"https://api.github.com/repos/keybase/client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/keybase/client/statuses/{sha}","languages_url":"https://api.github.com/repos/keybase/client/languages","stargazers_url":"https://api.github.com/repos/keybase/client/stargazers","contributors_url":"https://api.github.com/repos/keybase/client/contributors","subscribers_url":"https://api.github.com/repos/keybase/client/subscribers","subscription_url":"https://api.github.com/repos/keybase/client/subscription","commits_url":"https://api.github.com/repos/keybase/client/commits{/sha}","git_commits_url":"https://api.github.com/repos/keybase/client/git/commits{/sha}","comments_url":"https://api.github.com/repos/keybase/client/comments{/number}","issue_comment_url":"https://api.github.com/repos/keybase/client/issues/comments{/number}","contents_url":"https://api.github.com/repos/keybase/client/contents/{+path}","compare_url":"https://api.github.com/repos/keybase/client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/keybase/client/merges","archive_url":"https://api.github.com/repos/keybase/client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/keybase/client/downloads","issues_url":"https://api.github.com/repos/keybase/client/issues{/number}","pulls_url":"https://api.github.com/repos/keybase/client/pulls{/number}","milestones_url":"https://api.github.com/repos/keybase/client/milestones{/number}","notifications_url":"https://api.github.com/repos/keybase/client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/keybase/client/labels{/name}","releases_url":"https://api.github.com/repos/keybase/client/releases{/id}","deployments_url":"https://api.github.com/repos/keybase/client/deployments","created_at":"2015-01-11T17:17:12Z","updated_at":"2016-10-31T22:23:23Z","pushed_at":"2016-11-02T14:12:53Z","git_url":"git://github.com/keybase/client.git","ssh_url":"git@github.com:keybase/client.git","clone_url":"https://github.com/keybase/client.git","svn_url":"https://github.com/keybase/client","homepage":"","size":167332,"stargazers_count":830,"watchers_count":830,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":76,"mirror_url":null,"open_issues_count":564,"forks":76,"open_issues":564,"watchers":830,"default_branch":"master"}},"base":{"label":"keybase:master","ref":"master","sha":"c84dcde9e5b358bb5fd8e41ee60ce65fdf0001f0","user":{"login":"keybase","id":5400834,"avatar_url":"https://avatars.githubusercontent.com/u/5400834?v=3","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"repo":{"id":29099181,"name":"client","full_name":"keybase/client","owner":{"login":"keybase","id":5400834,"avatar_url":"https://avatars.githubusercontent.com/u/5400834?v=3","gravatar_id":"","url":"https://api.github.com/users/keybase","html_url":"https://github.com/keybase","followers_url":"https://api.github.com/users/keybase/followers","following_url":"https://api.github.com/users/keybase/following{/other_user}","gists_url":"https://api.github.com/users/keybase/gists{/gist_id}","starred_url":"https://api.github.com/users/keybase/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/keybase/subscriptions","organizations_url":"https://api.github.com/users/keybase/orgs","repos_url":"https://api.github.com/users/keybase/repos","events_url":"https://api.github.com/users/keybase/events{/privacy}","received_events_url":"https://api.github.com/users/keybase/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/keybase/client","description":"Keybase Go Library, Client, Service, OS X, iOS, Android, Electron","fork":false,"url":"https://api.github.com/repos/keybase/client","forks_url":"https://api.github.com/repos/keybase/client/forks","keys_url":"https://api.github.com/repos/keybase/client/keys{/key_id}","collaborators_url":"https://api.github.com/repos/keybase/client/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/keybase/client/teams","hooks_url":"https://api.github.com/repos/keybase/client/hooks","issue_events_url":"https://api.github.com/repos/keybase/client/issues/events{/number}","events_url":"https://api.github.com/repos/keybase/client/events","assignees_url":"https://api.github.com/repos/keybase/client/assignees{/user}","branches_url":"https://api.github.com/repos/keybase/client/branches{/branch}","tags_url":"https://api.github.com/repos/keybase/client/tags","blobs_url":"https://api.github.com/repos/keybase/client/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/keybase/client/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/keybase/client/git/refs{/sha}","trees_url":"https://api.github.com/repos/keybase/client/git/trees{/sha}","statuses_url":"https://api.github.com/repos/keybase/client/statuses/{sha}","languages_url":"https://api.github.com/repos/keybase/client/languages","stargazers_url":"https://api.github.com/repos/keybase/client/stargazers","contributors_url":"https://api.github.com/repos/keybase/client/contributors","subscribers_url":"https://api.github.com/repos/keybase/client/subscribers","subscription_url":"https://api.github.com/repos/keybase/client/subscription","commits_url":"https://api.github.com/repos/keybase/client/commits{/sha}","git_commits_url":"https://api.github.com/repos/keybase/client/git/commits{/sha}","comments_url":"https://api.github.com/repos/keybase/client/comments{/number}","issue_comment_url":"https://api.github.com/repos/keybase/client/issues/comments{/number}","contents_url":"https://api.github.com/repos/keybase/client/contents/{+path}","compare_url":"https://api.github.com/repos/keybase/client/compare/{base}...{head}","merges_url":"https://api.github.com/repos/keybase/client/merges","archive_url":"https://api.github.com/repos/keybase/client/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/keybase/client/downloads","issues_url":"https://api.github.com/repos/keybase/client/issues{/number}","pulls_url":"https://api.github.com/repos/keybase/client/pulls{/number}","milestones_url":"https://api.github.com/repos/keybase/client/milestones{/number}","notifications_url":"https://api.github.com/repos/keybase/client/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/keybase/client/labels{/name}","releases_url":"https://api.github.com/repos/keybase/client/releases{/id}","deployments_url":"https://api.github.com/repos/keybase/client/deployments","created_at":"2015-01-11T17:17:12Z","updated_at":"2016-10-31T22:23:23Z","pushed_at":"2016-11-02T14:12:53Z","git_url":"git://github.com/keybase/client.git","ssh_url":"git@github.com:keybase/client.git","clone_url":"https://github.com/keybase/client.git","svn_url":"https://github.com/keybase/client","homepage":"","size":167332,"stargazers_count":830,"watchers_count":830,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":76,"mirror_url":null,"open_issues_count":564,"forks":76,"open_issues":564,"watchers":830,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/keybase/client/pulls/4769"},"html":{"href":"https://github.com/keybase/client/pull/4769"},"issue":{"href":"https://api.github.com/repos/keybase/client/issues/4769"},"comments":{"href":"https://api.github.com/repos/keybase/client/issues/4769/comments"},"review_comments":{"href":"https://api.github.com/repos/keybase/client/pulls/4769/comments"},"review_comment":{"href":"https://api.github.com/repos/keybase/client/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/keybase/client/pulls/4769/commits"},"statuses":{"href":"https://api.github.com/repos/keybase/client/statuses/277d7f75ebf34d7e19b0e3b0b5b8b8fee855f577"}}}}
{"actor":{"display_login":"mmaxim"}}