{"action":"created","comment":{"url":"https://api.github.com/repos/medic/medic-api/pulls/comments/64713051","id":64713051,"diff_hunk":"@@ -0,0 +1,186 @@\n+/**\n+ * This module implements GET and POST to support medic-gateway's API\n+ * @see https://github.com/medic/medic-gateway\n+ */\n+\n+var _ = require('underscore'),\n+    db = require('../db'),\n+    http = require('http'),\n+    querystring = require('querystring'),\n+    utils = require('./utils'),\n+    messageUtils = require('./messages');\n+\n+require('lie/polyfill');\n+\n+var UNUSED = null; // this var is declared in various functions in the messages controller but never actually used\n+\n+function readBody(stream) {\n+  var body = '';\n+  return new Promise(function(resolve, reject) {\n+    stream.on('data', function(data) {\n+      body += data.toString();\n+    });\n+    stream.on('end', function() {\n+      resolve(body);\n+    });\n+    stream.on('error', reject);\n+  });\n+}\n+\n+function readResBody(res) {\n+  return readBody(res)\n+    .then(function(body) {\n+      if (res.statusCode >= 400) {\n+        throw new Error('Bad status received: ' + res.statusCode + ': ' + body);\n+      }\n+      return body;\n+    });\n+}\n+\n+function saveToDb(gatewayRequest, wtMessage) {\n+  var messageBody = querystring.stringify({\n+    from: wtMessage.from,\n+    message: wtMessage.content,\n+    'medic-gateway_id': wtMessage.id,\n+  });\n+\n+  new Promise(function(resolve, reject) {\n+    var req = http.request(\n+      {\n+        hostname: 'localhost',\n+        port: 5984,\n+        path: '/medic/_design/medic/_rewrite/add',\n+        method: 'POST',\n+        headers: {\n+          'Content-Length': messageBody.length,\n+          'Content-Type': 'application/x-www-form-urlencoded',\n+          'Authorization': gatewayRequest.headers.authorization,\n+        },\n+      },\n+      function(res) {\n+        readResBody(res)\n+          .then(JSON.parse)\n+          .then(function(response) {\n+            return resolve();\n+          })\n+          .catch(reject);\n+      });\n+\n+    req.on('error', reject);\n+\n+    req.write(messageBody);\n+    req.end();\n+  })\n+  .catch(function(err) {\n+    console.log('sms-gateway', 'saveToDb', 'error saving', wtMessage, err);\n+  });\n+}\n+\n+function getWebappState(update) {\n+  switch(update.status) {\n+    case 'SENT':\n+      return 'sent';\n+    case 'DELIVERED':\n+      return 'delivered';\n+    case 'FAILED':\n+      return 'failed';\n+  }\n+}\n+\n+function updateStateFor(gatewayRequest, update) {\n+  var newState = getWebappState(update);\n+  if (!newState) {\n+    return Promise.reject(new Error('Could not work out new state for update: ' + JSON.stringify(update)));\n+  }\n+\n+  return updateState(\n+      gatewayRequest,\n+      gatewayRequest.headers['user-agent'],\n+      update.id,\n+      newState);\n+}\n+\n+function updateState(gatewayRequest, userAgent, messageId, newState) {\n+  var updateBody = {\n+    state: newState,\n+    details: {\n+      useragent: userAgent,","path":"controllers/sms-gateway.js","position":107,"original_position":107,"commit_id":"c3fa44540c77360540365e5919bc5fcc8242ecd7","original_commit_id":"c3fa44540c77360540365e5919bc5fcc8242ecd7","user":{"login":"alxndrsn","id":191496,"avatar_url":"https://avatars.githubusercontent.com/u/191496?v=3","gravatar_id":"","url":"https://api.github.com/users/alxndrsn","html_url":"https://github.com/alxndrsn","followers_url":"https://api.github.com/users/alxndrsn/followers","following_url":"https://api.github.com/users/alxndrsn/following{/other_user}","gists_url":"https://api.github.com/users/alxndrsn/gists{/gist_id}","starred_url":"https://api.github.com/users/alxndrsn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alxndrsn/subscriptions","organizations_url":"https://api.github.com/users/alxndrsn/orgs","repos_url":"https://api.github.com/users/alxndrsn/repos","events_url":"https://api.github.com/users/alxndrsn/events{/privacy}","received_events_url":"https://api.github.com/users/alxndrsn/received_events","type":"User","site_admin":false},"body":"Me neither.  Can't see it being very useful in our normal use case though, so maybe better of leaving it out.  I'm not sure if leaving `details` undefined is valid though.","created_at":"2016-05-26T09:04:29Z","updated_at":"2016-05-26T09:04:29Z","html_url":"https://github.com/medic/medic-api/pull/69#discussion_r64713051","pull_request_url":"https://api.github.com/repos/medic/medic-api/pulls/69","_links":{"self":{"href":"https://api.github.com/repos/medic/medic-api/pulls/comments/64713051"},"html":{"href":"https://github.com/medic/medic-api/pull/69#discussion_r64713051"},"pull_request":{"href":"https://api.github.com/repos/medic/medic-api/pulls/69"}}},"pull_request":{"url":"https://api.github.com/repos/medic/medic-api/pulls/69","id":71447720,"html_url":"https://github.com/medic/medic-api/pull/69","diff_url":"https://github.com/medic/medic-api/pull/69.diff","patch_url":"https://github.com/medic/medic-api/pull/69.patch","issue_url":"https://api.github.com/repos/medic/medic-api/issues/69","number":69,"state":"open","locked":false,"title":"Add integration with `medic-gateway` for sending and receiving SMS","user":{"login":"alxndrsn","id":191496,"avatar_url":"https://avatars.githubusercontent.com/u/191496?v=3","gravatar_id":"","url":"https://api.github.com/users/alxndrsn","html_url":"https://github.com/alxndrsn","followers_url":"https://api.github.com/users/alxndrsn/followers","following_url":"https://api.github.com/users/alxndrsn/following{/other_user}","gists_url":"https://api.github.com/users/alxndrsn/gists{/gist_id}","starred_url":"https://api.github.com/users/alxndrsn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alxndrsn/subscriptions","organizations_url":"https://api.github.com/users/alxndrsn/orgs","repos_url":"https://api.github.com/users/alxndrsn/repos","events_url":"https://api.github.com/users/alxndrsn/events{/privacy}","received_events_url":"https://api.github.com/users/alxndrsn/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-05-25T19:19:11Z","updated_at":"2016-05-26T09:04:29Z","closed_at":null,"merged_at":null,"merge_commit_sha":"3612afd101a0109921263529be6e8bb89d30c1f8","assignee":{"login":"alxndrsn","id":191496,"avatar_url":"https://avatars.githubusercontent.com/u/191496?v=3","gravatar_id":"","url":"https://api.github.com/users/alxndrsn","html_url":"https://github.com/alxndrsn","followers_url":"https://api.github.com/users/alxndrsn/followers","following_url":"https://api.github.com/users/alxndrsn/following{/other_user}","gists_url":"https://api.github.com/users/alxndrsn/gists{/gist_id}","starred_url":"https://api.github.com/users/alxndrsn/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alxndrsn/subscriptions","organizations_url":"https://api.github.com/users/alxndrsn/orgs","repos_url":"https://api.github.com/users/alxndrsn/repos","events_url":"https://api.github.com/users/alxndrsn/events{/privacy}","received_events_url":"https://api.github.com/users/alxndrsn/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/medic/medic-api/pulls/69/commits","review_comments_url":"https://api.github.com/repos/medic/medic-api/pulls/69/comments","review_comment_url":"https://api.github.com/repos/medic/medic-api/pulls/comments{/number}","comments_url":"https://api.github.com/repos/medic/medic-api/issues/69/comments","statuses_url":"https://api.github.com/repos/medic/medic-api/statuses/c3fa44540c77360540365e5919bc5fcc8242ecd7","head":{"label":"medic:medic-gateway-support-pr","ref":"medic-gateway-support-pr","sha":"c3fa44540c77360540365e5919bc5fcc8242ecd7","user":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"repo":{"id":18199775,"name":"medic-api","full_name":"medic/medic-api","owner":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/medic/medic-api","description":"Node server to support medic-webapp","fork":false,"url":"https://api.github.com/repos/medic/medic-api","forks_url":"https://api.github.com/repos/medic/medic-api/forks","keys_url":"https://api.github.com/repos/medic/medic-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/medic/medic-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/medic/medic-api/teams","hooks_url":"https://api.github.com/repos/medic/medic-api/hooks","issue_events_url":"https://api.github.com/repos/medic/medic-api/issues/events{/number}","events_url":"https://api.github.com/repos/medic/medic-api/events","assignees_url":"https://api.github.com/repos/medic/medic-api/assignees{/user}","branches_url":"https://api.github.com/repos/medic/medic-api/branches{/branch}","tags_url":"https://api.github.com/repos/medic/medic-api/tags","blobs_url":"https://api.github.com/repos/medic/medic-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/medic/medic-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/medic/medic-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/medic/medic-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/medic/medic-api/statuses/{sha}","languages_url":"https://api.github.com/repos/medic/medic-api/languages","stargazers_url":"https://api.github.com/repos/medic/medic-api/stargazers","contributors_url":"https://api.github.com/repos/medic/medic-api/contributors","subscribers_url":"https://api.github.com/repos/medic/medic-api/subscribers","subscription_url":"https://api.github.com/repos/medic/medic-api/subscription","commits_url":"https://api.github.com/repos/medic/medic-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/medic/medic-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/medic/medic-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/medic/medic-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/medic/medic-api/contents/{+path}","compare_url":"https://api.github.com/repos/medic/medic-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/medic/medic-api/merges","archive_url":"https://api.github.com/repos/medic/medic-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/medic/medic-api/downloads","issues_url":"https://api.github.com/repos/medic/medic-api/issues{/number}","pulls_url":"https://api.github.com/repos/medic/medic-api/pulls{/number}","milestones_url":"https://api.github.com/repos/medic/medic-api/milestones{/number}","notifications_url":"https://api.github.com/repos/medic/medic-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/medic/medic-api/labels{/name}","releases_url":"https://api.github.com/repos/medic/medic-api/releases{/id}","deployments_url":"https://api.github.com/repos/medic/medic-api/deployments","created_at":"2014-03-28T03:20:43Z","updated_at":"2016-01-12T16:42:13Z","pushed_at":"2016-05-25T20:01:44Z","git_url":"git://github.com/medic/medic-api.git","ssh_url":"git@github.com:medic/medic-api.git","clone_url":"https://github.com/medic/medic-api.git","svn_url":"https://github.com/medic/medic-api","homepage":"","size":708,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":15,"forks":5,"open_issues":15,"watchers":1,"default_branch":"develop"}},"base":{"label":"medic:master","ref":"master","sha":"ae9f5eeb1ae1c1f243f340a46a0d8b0a0bb67884","user":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"repo":{"id":18199775,"name":"medic-api","full_name":"medic/medic-api","owner":{"login":"medic","id":474424,"avatar_url":"https://avatars.githubusercontent.com/u/474424?v=3","gravatar_id":"","url":"https://api.github.com/users/medic","html_url":"https://github.com/medic","followers_url":"https://api.github.com/users/medic/followers","following_url":"https://api.github.com/users/medic/following{/other_user}","gists_url":"https://api.github.com/users/medic/gists{/gist_id}","starred_url":"https://api.github.com/users/medic/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/medic/subscriptions","organizations_url":"https://api.github.com/users/medic/orgs","repos_url":"https://api.github.com/users/medic/repos","events_url":"https://api.github.com/users/medic/events{/privacy}","received_events_url":"https://api.github.com/users/medic/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/medic/medic-api","description":"Node server to support medic-webapp","fork":false,"url":"https://api.github.com/repos/medic/medic-api","forks_url":"https://api.github.com/repos/medic/medic-api/forks","keys_url":"https://api.github.com/repos/medic/medic-api/keys{/key_id}","collaborators_url":"https://api.github.com/repos/medic/medic-api/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/medic/medic-api/teams","hooks_url":"https://api.github.com/repos/medic/medic-api/hooks","issue_events_url":"https://api.github.com/repos/medic/medic-api/issues/events{/number}","events_url":"https://api.github.com/repos/medic/medic-api/events","assignees_url":"https://api.github.com/repos/medic/medic-api/assignees{/user}","branches_url":"https://api.github.com/repos/medic/medic-api/branches{/branch}","tags_url":"https://api.github.com/repos/medic/medic-api/tags","blobs_url":"https://api.github.com/repos/medic/medic-api/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/medic/medic-api/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/medic/medic-api/git/refs{/sha}","trees_url":"https://api.github.com/repos/medic/medic-api/git/trees{/sha}","statuses_url":"https://api.github.com/repos/medic/medic-api/statuses/{sha}","languages_url":"https://api.github.com/repos/medic/medic-api/languages","stargazers_url":"https://api.github.com/repos/medic/medic-api/stargazers","contributors_url":"https://api.github.com/repos/medic/medic-api/contributors","subscribers_url":"https://api.github.com/repos/medic/medic-api/subscribers","subscription_url":"https://api.github.com/repos/medic/medic-api/subscription","commits_url":"https://api.github.com/repos/medic/medic-api/commits{/sha}","git_commits_url":"https://api.github.com/repos/medic/medic-api/git/commits{/sha}","comments_url":"https://api.github.com/repos/medic/medic-api/comments{/number}","issue_comment_url":"https://api.github.com/repos/medic/medic-api/issues/comments{/number}","contents_url":"https://api.github.com/repos/medic/medic-api/contents/{+path}","compare_url":"https://api.github.com/repos/medic/medic-api/compare/{base}...{head}","merges_url":"https://api.github.com/repos/medic/medic-api/merges","archive_url":"https://api.github.com/repos/medic/medic-api/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/medic/medic-api/downloads","issues_url":"https://api.github.com/repos/medic/medic-api/issues{/number}","pulls_url":"https://api.github.com/repos/medic/medic-api/pulls{/number}","milestones_url":"https://api.github.com/repos/medic/medic-api/milestones{/number}","notifications_url":"https://api.github.com/repos/medic/medic-api/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/medic/medic-api/labels{/name}","releases_url":"https://api.github.com/repos/medic/medic-api/releases{/id}","deployments_url":"https://api.github.com/repos/medic/medic-api/deployments","created_at":"2014-03-28T03:20:43Z","updated_at":"2016-01-12T16:42:13Z","pushed_at":"2016-05-25T20:01:44Z","git_url":"git://github.com/medic/medic-api.git","ssh_url":"git@github.com:medic/medic-api.git","clone_url":"https://github.com/medic/medic-api.git","svn_url":"https://github.com/medic/medic-api","homepage":"","size":708,"stargazers_count":1,"watchers_count":1,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":5,"mirror_url":null,"open_issues_count":15,"forks":5,"open_issues":15,"watchers":1,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/medic/medic-api/pulls/69"},"html":{"href":"https://github.com/medic/medic-api/pull/69"},"issue":{"href":"https://api.github.com/repos/medic/medic-api/issues/69"},"comments":{"href":"https://api.github.com/repos/medic/medic-api/issues/69/comments"},"review_comments":{"href":"https://api.github.com/repos/medic/medic-api/pulls/69/comments"},"review_comment":{"href":"https://api.github.com/repos/medic/medic-api/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/medic/medic-api/pulls/69/commits"},"statuses":{"href":"https://api.github.com/repos/medic/medic-api/statuses/c3fa44540c77360540365e5919bc5fcc8242ecd7"}}}}
{"actor":{"display_login":"alxndrsn"}}