{"action":"created","comment":{"url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/comments/310300547","pull_request_review_id":270399252,"id":310300547,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDMxMDMwMDU0Nw==","diff_hunk":"@@ -1,14 +1,60 @@\n-let _recentSuggestions = [];\n+let _lastFetch = 0;\n+let _attachments = [];\n \n-// Returns an array of recent attachments that have been suggested via the\n-// extension.\n-function recentSuggestions() {\n-  return Promise.resolve(_recentSuggestions);\n+// Returns an array of attachments that have been suggested via the extension.\n+function getAttachments() {\n+  if (Date.now() - _lastFetch < 1000 * 60) {\n+    return Promise.resolve(_attachments);\n+  }\n+  return loadAttachments()\n+    .then(() => _attachments)\n+    .catch(e => {\n+      console.error('Failed to load attachments: ', e);\n+      return _attachments;\n+    });\n+}\n+\n+// Sort, remove duplicates via source_id, and keep track of only last 100 suggestions.\n+function cleanAttachments() {\n+  let seen = {};\n+  _attachments.forEach(\n+    a => (a.display_date = a.display_date || a.date_modified || a.date_created || a.created_at)\n+  );\n+  _attachments.sort((a, b) => new Date(b.display_date) - new Date(a.display_date));\n+  _attachments = _attachments.filter(attachment =>\n+    seen[attachment.source_id] ? false : (seen[attachment.source_id] = 1)\n+  );\n+  _attachments.length = Math.min(_attachments.length, 100);\n+}\n+\n+function storeAttachments() {\n+  chrome.storage.local.set({ attachments: _attachments });\n+}\n+\n+function loadAttachments() {\n+  let after = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();\n+  return orgs()\n+    .then(slugs => Promise.all(slugs.map(getSession)))\n+    .then(sessions =>\n+      Promise.all(\n+        sessions.map(s => {\n+          return listAttachments(s.user.user_id, after, authorize(s)).then(resp => {\n+            console.log(resp);\n+            resp.attachments.forEach(attachment => _attachments.push(attachment));\n+          });\n+        })\n+      )\n+    )\n+    .then(() => {\n+      cleanAttachments();\n+      storeAttachments();\n+      _lastFetch = Date.now();\n+    });\n }\n \n // Background page is ephemeral, so load stored version of suggestions.\n-chrome.storage.local.get('suggestions', function(result) {\n-  _recentSuggestions = result.suggestions || [];\n+chrome.storage.local.get('attachments', function(result) {\n+  _attachments = result.suggestions || [];","path":"ext/background/suggestions.js","position":64,"original_position":64,"commit_id":"d413661b14af68c161751a2704e9de4c31383926","original_commit_id":"d413661b14af68c161751a2704e9de4c31383926","user":{"login":"stephyeung","id":1282415,"node_id":"MDQ6VXNlcjEyODI0MTU=","avatar_url":"https://avatars1.githubusercontent.com/u/1282415?v=4","gravatar_id":"","url":"https://api.github.com/users/stephyeung","html_url":"https://github.com/stephyeung","followers_url":"https://api.github.com/users/stephyeung/followers","following_url":"https://api.github.com/users/stephyeung/following{/other_user}","gists_url":"https://api.github.com/users/stephyeung/gists{/gist_id}","starred_url":"https://api.github.com/users/stephyeung/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/stephyeung/subscriptions","organizations_url":"https://api.github.com/users/stephyeung/orgs","repos_url":"https://api.github.com/users/stephyeung/repos","events_url":"https://api.github.com/users/stephyeung/events{/privacy}","received_events_url":"https://api.github.com/users/stephyeung/received_events","type":"User","site_admin":false},"body":"think this needs to be `= result.attachments` per line 31's `set({attachments: _attachments})`","created_at":"2019-08-02T21:16:40Z","updated_at":"2019-08-02T21:18:09Z","html_url":"https://github.com/range-labs/range-for-chrome/pull/6#discussion_r310300547","pull_request_url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6","author_association":"NONE","_links":{"self":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/comments/310300547"},"html":{"href":"https://github.com/range-labs/range-for-chrome/pull/6#discussion_r310300547"},"pull_request":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6","id":303931300,"node_id":"MDExOlB1bGxSZXF1ZXN0MzAzOTMxMzAw","html_url":"https://github.com/range-labs/range-for-chrome/pull/6","diff_url":"https://github.com/range-labs/range-for-chrome/pull/6.diff","patch_url":"https://github.com/range-labs/range-for-chrome/pull/6.patch","issue_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues/6","number":6,"state":"open","locked":false,"title":"Show attachments generated by the API and first-party integrations","user":{"login":"dpup","id":206453,"node_id":"MDQ6VXNlcjIwNjQ1Mw==","avatar_url":"https://avatars1.githubusercontent.com/u/206453?v=4","gravatar_id":"","url":"https://api.github.com/users/dpup","html_url":"https://github.com/dpup","followers_url":"https://api.github.com/users/dpup/followers","following_url":"https://api.github.com/users/dpup/following{/other_user}","gists_url":"https://api.github.com/users/dpup/gists{/gist_id}","starred_url":"https://api.github.com/users/dpup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dpup/subscriptions","organizations_url":"https://api.github.com/users/dpup/orgs","repos_url":"https://api.github.com/users/dpup/repos","events_url":"https://api.github.com/users/dpup/events{/privacy}","received_events_url":"https://api.github.com/users/dpup/received_events","type":"User","site_admin":false},"body":"#### What's this PR do?\n\nRequests attachment metadata from the Range backend, which allows us to show\nsuggestions created via the backend API and first-party integrations, as well\nas the chrome extension\n\n#### Where should the reviewer start?\n\nDiff\n\n#### Any background context you want to provide?\n\nThe extension wasn't super useful when it only showed attachments detected by\nthe extension itself.\n\n#### What are the relevant tickets?\n\nN/A\n\n#### Screenshots (if appropriate)\n\nN/A\n\n#### What gif best describes this PR or how it makes you feel?\n\n![](https://media.giphy.com/media/1GJRIgTY4sS6k/giphy-downsized.gif)\n\n#### Definition of Done:\n\n- [x] Code is easy to understand and conforms with Prettier & eslint configs\n- [x] Incomplete code is marked with TODOs\n- [x] Code is suitably instrumented with logging and metrics\n- [x] Documentation has been updated as appropriate\n- [x] Manifest has been updated and version incremented correctly\n- [x] [OWASP Top 10](https://www.owasp.org/index.php/Top_10-2017_Top_10) have been considered","created_at":"2019-08-02T21:10:35Z","updated_at":"2019-08-02T21:18:09Z","closed_at":null,"merged_at":null,"merge_commit_sha":"03ca12b8a8867d79af098b4a5416fcf467edb5b0","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6/commits","review_comments_url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6/comments","review_comment_url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/comments{/number}","comments_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues/6/comments","statuses_url":"https://api.github.com/repos/range-labs/range-for-chrome/statuses/d413661b14af68c161751a2704e9de4c31383926","head":{"label":"range-labs:dpup/other-suggestions","ref":"dpup/other-suggestions","sha":"d413661b14af68c161751a2704e9de4c31383926","user":{"login":"range-labs","id":29342939,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI5MzQyOTM5","avatar_url":"https://avatars0.githubusercontent.com/u/29342939?v=4","gravatar_id":"","url":"https://api.github.com/users/range-labs","html_url":"https://github.com/range-labs","followers_url":"https://api.github.com/users/range-labs/followers","following_url":"https://api.github.com/users/range-labs/following{/other_user}","gists_url":"https://api.github.com/users/range-labs/gists{/gist_id}","starred_url":"https://api.github.com/users/range-labs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/range-labs/subscriptions","organizations_url":"https://api.github.com/users/range-labs/orgs","repos_url":"https://api.github.com/users/range-labs/repos","events_url":"https://api.github.com/users/range-labs/events{/privacy}","received_events_url":"https://api.github.com/users/range-labs/received_events","type":"Organization","site_admin":false},"repo":{"id":188131693,"node_id":"MDEwOlJlcG9zaXRvcnkxODgxMzE2OTM=","name":"range-for-chrome","full_name":"range-labs/range-for-chrome","private":false,"owner":{"login":"range-labs","id":29342939,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI5MzQyOTM5","avatar_url":"https://avatars0.githubusercontent.com/u/29342939?v=4","gravatar_id":"","url":"https://api.github.com/users/range-labs","html_url":"https://github.com/range-labs","followers_url":"https://api.github.com/users/range-labs/followers","following_url":"https://api.github.com/users/range-labs/following{/other_user}","gists_url":"https://api.github.com/users/range-labs/gists{/gist_id}","starred_url":"https://api.github.com/users/range-labs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/range-labs/subscriptions","organizations_url":"https://api.github.com/users/range-labs/orgs","repos_url":"https://api.github.com/users/range-labs/repos","events_url":"https://api.github.com/users/range-labs/events{/privacy}","received_events_url":"https://api.github.com/users/range-labs/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/range-labs/range-for-chrome","description":"Range helps teams know what’s happening, stay in sync, and actually feel like a team. Range for Chrome allows you to easily capture work from your browser based tools and share it with your team.","fork":false,"url":"https://api.github.com/repos/range-labs/range-for-chrome","forks_url":"https://api.github.com/repos/range-labs/range-for-chrome/forks","keys_url":"https://api.github.com/repos/range-labs/range-for-chrome/keys{/key_id}","collaborators_url":"https://api.github.com/repos/range-labs/range-for-chrome/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/range-labs/range-for-chrome/teams","hooks_url":"https://api.github.com/repos/range-labs/range-for-chrome/hooks","issue_events_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues/events{/number}","events_url":"https://api.github.com/repos/range-labs/range-for-chrome/events","assignees_url":"https://api.github.com/repos/range-labs/range-for-chrome/assignees{/user}","branches_url":"https://api.github.com/repos/range-labs/range-for-chrome/branches{/branch}","tags_url":"https://api.github.com/repos/range-labs/range-for-chrome/tags","blobs_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/refs{/sha}","trees_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/trees{/sha}","statuses_url":"https://api.github.com/repos/range-labs/range-for-chrome/statuses/{sha}","languages_url":"https://api.github.com/repos/range-labs/range-for-chrome/languages","stargazers_url":"https://api.github.com/repos/range-labs/range-for-chrome/stargazers","contributors_url":"https://api.github.com/repos/range-labs/range-for-chrome/contributors","subscribers_url":"https://api.github.com/repos/range-labs/range-for-chrome/subscribers","subscription_url":"https://api.github.com/repos/range-labs/range-for-chrome/subscription","commits_url":"https://api.github.com/repos/range-labs/range-for-chrome/commits{/sha}","git_commits_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/commits{/sha}","comments_url":"https://api.github.com/repos/range-labs/range-for-chrome/comments{/number}","issue_comment_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues/comments{/number}","contents_url":"https://api.github.com/repos/range-labs/range-for-chrome/contents/{+path}","compare_url":"https://api.github.com/repos/range-labs/range-for-chrome/compare/{base}...{head}","merges_url":"https://api.github.com/repos/range-labs/range-for-chrome/merges","archive_url":"https://api.github.com/repos/range-labs/range-for-chrome/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/range-labs/range-for-chrome/downloads","issues_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues{/number}","pulls_url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls{/number}","milestones_url":"https://api.github.com/repos/range-labs/range-for-chrome/milestones{/number}","notifications_url":"https://api.github.com/repos/range-labs/range-for-chrome/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/range-labs/range-for-chrome/labels{/name}","releases_url":"https://api.github.com/repos/range-labs/range-for-chrome/releases{/id}","deployments_url":"https://api.github.com/repos/range-labs/range-for-chrome/deployments","created_at":"2019-05-23T00:20:47Z","updated_at":"2019-06-05T14:47:14Z","pushed_at":"2019-08-02T21:14:16Z","git_url":"git://github.com/range-labs/range-for-chrome.git","ssh_url":"git@github.com:range-labs/range-for-chrome.git","clone_url":"https://github.com/range-labs/range-for-chrome.git","svn_url":"https://github.com/range-labs/range-for-chrome","homepage":"https://www.range.co","size":113,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":2,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"range-labs:master","ref":"master","sha":"ffaba05ee7159d2e34fedfb9a3b04d61867f329e","user":{"login":"range-labs","id":29342939,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI5MzQyOTM5","avatar_url":"https://avatars0.githubusercontent.com/u/29342939?v=4","gravatar_id":"","url":"https://api.github.com/users/range-labs","html_url":"https://github.com/range-labs","followers_url":"https://api.github.com/users/range-labs/followers","following_url":"https://api.github.com/users/range-labs/following{/other_user}","gists_url":"https://api.github.com/users/range-labs/gists{/gist_id}","starred_url":"https://api.github.com/users/range-labs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/range-labs/subscriptions","organizations_url":"https://api.github.com/users/range-labs/orgs","repos_url":"https://api.github.com/users/range-labs/repos","events_url":"https://api.github.com/users/range-labs/events{/privacy}","received_events_url":"https://api.github.com/users/range-labs/received_events","type":"Organization","site_admin":false},"repo":{"id":188131693,"node_id":"MDEwOlJlcG9zaXRvcnkxODgxMzE2OTM=","name":"range-for-chrome","full_name":"range-labs/range-for-chrome","private":false,"owner":{"login":"range-labs","id":29342939,"node_id":"MDEyOk9yZ2FuaXphdGlvbjI5MzQyOTM5","avatar_url":"https://avatars0.githubusercontent.com/u/29342939?v=4","gravatar_id":"","url":"https://api.github.com/users/range-labs","html_url":"https://github.com/range-labs","followers_url":"https://api.github.com/users/range-labs/followers","following_url":"https://api.github.com/users/range-labs/following{/other_user}","gists_url":"https://api.github.com/users/range-labs/gists{/gist_id}","starred_url":"https://api.github.com/users/range-labs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/range-labs/subscriptions","organizations_url":"https://api.github.com/users/range-labs/orgs","repos_url":"https://api.github.com/users/range-labs/repos","events_url":"https://api.github.com/users/range-labs/events{/privacy}","received_events_url":"https://api.github.com/users/range-labs/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/range-labs/range-for-chrome","description":"Range helps teams know what’s happening, stay in sync, and actually feel like a team. Range for Chrome allows you to easily capture work from your browser based tools and share it with your team.","fork":false,"url":"https://api.github.com/repos/range-labs/range-for-chrome","forks_url":"https://api.github.com/repos/range-labs/range-for-chrome/forks","keys_url":"https://api.github.com/repos/range-labs/range-for-chrome/keys{/key_id}","collaborators_url":"https://api.github.com/repos/range-labs/range-for-chrome/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/range-labs/range-for-chrome/teams","hooks_url":"https://api.github.com/repos/range-labs/range-for-chrome/hooks","issue_events_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues/events{/number}","events_url":"https://api.github.com/repos/range-labs/range-for-chrome/events","assignees_url":"https://api.github.com/repos/range-labs/range-for-chrome/assignees{/user}","branches_url":"https://api.github.com/repos/range-labs/range-for-chrome/branches{/branch}","tags_url":"https://api.github.com/repos/range-labs/range-for-chrome/tags","blobs_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/refs{/sha}","trees_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/trees{/sha}","statuses_url":"https://api.github.com/repos/range-labs/range-for-chrome/statuses/{sha}","languages_url":"https://api.github.com/repos/range-labs/range-for-chrome/languages","stargazers_url":"https://api.github.com/repos/range-labs/range-for-chrome/stargazers","contributors_url":"https://api.github.com/repos/range-labs/range-for-chrome/contributors","subscribers_url":"https://api.github.com/repos/range-labs/range-for-chrome/subscribers","subscription_url":"https://api.github.com/repos/range-labs/range-for-chrome/subscription","commits_url":"https://api.github.com/repos/range-labs/range-for-chrome/commits{/sha}","git_commits_url":"https://api.github.com/repos/range-labs/range-for-chrome/git/commits{/sha}","comments_url":"https://api.github.com/repos/range-labs/range-for-chrome/comments{/number}","issue_comment_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues/comments{/number}","contents_url":"https://api.github.com/repos/range-labs/range-for-chrome/contents/{+path}","compare_url":"https://api.github.com/repos/range-labs/range-for-chrome/compare/{base}...{head}","merges_url":"https://api.github.com/repos/range-labs/range-for-chrome/merges","archive_url":"https://api.github.com/repos/range-labs/range-for-chrome/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/range-labs/range-for-chrome/downloads","issues_url":"https://api.github.com/repos/range-labs/range-for-chrome/issues{/number}","pulls_url":"https://api.github.com/repos/range-labs/range-for-chrome/pulls{/number}","milestones_url":"https://api.github.com/repos/range-labs/range-for-chrome/milestones{/number}","notifications_url":"https://api.github.com/repos/range-labs/range-for-chrome/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/range-labs/range-for-chrome/labels{/name}","releases_url":"https://api.github.com/repos/range-labs/range-for-chrome/releases{/id}","deployments_url":"https://api.github.com/repos/range-labs/range-for-chrome/deployments","created_at":"2019-05-23T00:20:47Z","updated_at":"2019-06-05T14:47:14Z","pushed_at":"2019-08-02T21:14:16Z","git_url":"git://github.com/range-labs/range-for-chrome.git","ssh_url":"git@github.com:range-labs/range-for-chrome.git","clone_url":"https://github.com/range-labs/range-for-chrome.git","svn_url":"https://github.com/range-labs/range-for-chrome","homepage":"https://www.range.co","size":113,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"disabled":false,"open_issues_count":2,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6"},"html":{"href":"https://github.com/range-labs/range-for-chrome/pull/6"},"issue":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/issues/6"},"comments":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/range-labs/range-for-chrome/statuses/d413661b14af68c161751a2704e9de4c31383926"}},"author_association":"MEMBER"}}
{"actor":{"display_login":"stephyeung"}}