{"action":"created","comment":{"url":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/comments/34903131","id":34903131,"diff_hunk":"@@ -98,53 +105,133 @@ function modifyFiles(action, project, user) {\n   });\n }\n \n-function getUser(project) {\n-  return project.user().query({}).fetch();\n+function getPublishedProject(project) {\n+  return PublishedProject.query({\n+    where: {\n+      project_id: project.published_id\n+    }\n+  }).fetch()\n+  .then(function(publishedProject) {\n+    if (!publishedProject) {\n+      return;\n+    }\n+    return publishedProject;\n+  })\n+  .catch(reject);\n }\n \n-exports.publish = function(project) {\n-  return getUser(project)\n-    .then(function(user) {\n-      return modifyFiles(uploadFile, project, user);\n-    })\n-    .then(function(url) {\n-      return project.set({\n-        publish_url: url\n-      }).save();\n-    })\n-    .then(function() {\n-      log.info(\n-        'Successfully published ' +\n-        project.get('title') +\n-        ' to ' + project.get('publish_url')\n-      );\n-    })\n-    .catch(function(e) {\n-      log.error(e);\n-\n-      return Promise.reject(e);\n+function getPublishedFiles(publishedProject) {\n+  return PublishedFiles.query({\n+    where: {\n+      project_id: publishedProject.id\n+    }\n+  }).fetchAll()\n+  .catch(reject);\n+}\n+\n+function clearPublishedProject(publishedProject, mainProject, user) {\n+  var publishedFiles;\n+\n+  // First, get project + files\n+  return Promise.resolve()\n+  .then(function() {\n+    return getPublishedFiles(publishedProject);\n+  })\n+  .then(function(files) {\n+    publishedFiles = files;\n+\n+    // Then, remove them from s3\n+    return modifyPublishedFiles(deleteFile, mainProject, user, files);\n+  })\n+  .then(function() {\n+    // Then, delete them from the DB\n+    var deletePublishedProject = publishedProject.destroy();\n+    var deletePublishedFiles = Promise.map(publishedFiles, function(file) {\n+      return file.destroy();\n     });\n-};\n \n-exports.unpublish = function(project) {\n+    return Promise.join(deletePublishedFiles, deletePublishedProject, function() {\n+      // We deleted things!\n+    });\n+  });\n+}\n+\n+exports.publish = function publish(project) {\n+  var user;\n+\n   return getUser(project)\n-    .then(function(user) {\n-      return modifyFiles(deleteFile, project, user);\n-    })\n-    .then(function() {\n-      return project.set({ publish_url: null }).save();\n-    })\n-    .then(function() {\n-      log.info(\n-        'Successfully unpublished ' +\n-        project.get('title')\n-      );\n-    })\n-    .catch(function(e) {\n-      log.error(e);\n-\n-      return Promise.reject(e);\n+  .then(function(u) {\n+    user = u;\n+\n+    return getPublishedProject(project)\n+    .then(function(oldPublishedProject) {","path":"api/classes/publisher.js","position":151,"original_position":151,"commit_id":"8910029b1ef3f8f1c53fedab78839bb0e677839f","original_commit_id":"8910029b1ef3f8f1c53fedab78839bb0e677839f","user":{"login":"gideonthomas","id":5816130,"avatar_url":"https://avatars.githubusercontent.com/u/5816130?v=3","gravatar_id":"","url":"https://api.github.com/users/gideonthomas","html_url":"https://github.com/gideonthomas","followers_url":"https://api.github.com/users/gideonthomas/followers","following_url":"https://api.github.com/users/gideonthomas/following{/other_user}","gists_url":"https://api.github.com/users/gideonthomas/gists{/gist_id}","starred_url":"https://api.github.com/users/gideonthomas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gideonthomas/subscriptions","organizations_url":"https://api.github.com/users/gideonthomas/orgs","repos_url":"https://api.github.com/users/gideonthomas/repos","events_url":"https://api.github.com/users/gideonthomas/events{/privacy}","received_events_url":"https://api.github.com/users/gideonthomas/received_events","type":"User","site_admin":false},"body":"why are you attaching it here instead of chaining it to the then on line 163","created_at":"2015-07-17T15:57:39Z","updated_at":"2015-07-17T15:57:39Z","html_url":"https://github.com/mozilla/publish.webmaker.org/pull/114#discussion_r34903131","pull_request_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114","_links":{"self":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/comments/34903131"},"html":{"href":"https://github.com/mozilla/publish.webmaker.org/pull/114#discussion_r34903131"},"pull_request":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114"}}},"pull_request":{"url":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114","id":40074043,"html_url":"https://github.com/mozilla/publish.webmaker.org/pull/114","diff_url":"https://github.com/mozilla/publish.webmaker.org/pull/114.diff","patch_url":"https://github.com/mozilla/publish.webmaker.org/pull/114.patch","issue_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/issues/114","number":114,"state":"open","locked":false,"title":"Fix #113 - Store an exact copy of the published files in a project","user":{"login":"sedge","id":1616860,"avatar_url":"https://avatars.githubusercontent.com/u/1616860?v=3","gravatar_id":"","url":"https://api.github.com/users/sedge","html_url":"https://github.com/sedge","followers_url":"https://api.github.com/users/sedge/followers","following_url":"https://api.github.com/users/sedge/following{/other_user}","gists_url":"https://api.github.com/users/sedge/gists{/gist_id}","starred_url":"https://api.github.com/users/sedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sedge/subscriptions","organizations_url":"https://api.github.com/users/sedge/orgs","repos_url":"https://api.github.com/users/sedge/repos","events_url":"https://api.github.com/users/sedge/events{/privacy}","received_events_url":"https://api.github.com/users/sedge/received_events","type":"User","site_admin":false},"body":"","created_at":"2015-07-15T21:51:36Z","updated_at":"2015-07-17T15:57:39Z","closed_at":null,"merged_at":null,"merge_commit_sha":"710732f06fdfe8ab9273e0330e40dfcc3dc458db","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114/commits","review_comments_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114/comments","review_comment_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/comments{/number}","comments_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/issues/114/comments","statuses_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/statuses/8910029b1ef3f8f1c53fedab78839bb0e677839f","head":{"label":"sedge:issue113-copyPublishedProjects","ref":"issue113-copyPublishedProjects","sha":"8910029b1ef3f8f1c53fedab78839bb0e677839f","user":{"login":"sedge","id":1616860,"avatar_url":"https://avatars.githubusercontent.com/u/1616860?v=3","gravatar_id":"","url":"https://api.github.com/users/sedge","html_url":"https://github.com/sedge","followers_url":"https://api.github.com/users/sedge/followers","following_url":"https://api.github.com/users/sedge/following{/other_user}","gists_url":"https://api.github.com/users/sedge/gists{/gist_id}","starred_url":"https://api.github.com/users/sedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sedge/subscriptions","organizations_url":"https://api.github.com/users/sedge/orgs","repos_url":"https://api.github.com/users/sedge/repos","events_url":"https://api.github.com/users/sedge/events{/privacy}","received_events_url":"https://api.github.com/users/sedge/received_events","type":"User","site_admin":false},"repo":{"id":37083519,"name":"publish.webmaker.org","full_name":"sedge/publish.webmaker.org","owner":{"login":"sedge","id":1616860,"avatar_url":"https://avatars.githubusercontent.com/u/1616860?v=3","gravatar_id":"","url":"https://api.github.com/users/sedge","html_url":"https://github.com/sedge","followers_url":"https://api.github.com/users/sedge/followers","following_url":"https://api.github.com/users/sedge/following{/other_user}","gists_url":"https://api.github.com/users/sedge/gists{/gist_id}","starred_url":"https://api.github.com/users/sedge/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sedge/subscriptions","organizations_url":"https://api.github.com/users/sedge/orgs","repos_url":"https://api.github.com/users/sedge/repos","events_url":"https://api.github.com/users/sedge/events{/privacy}","received_events_url":"https://api.github.com/users/sedge/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sedge/publish.webmaker.org","description":"The teach.org publishing service for goggles and thimble","fork":true,"url":"https://api.github.com/repos/sedge/publish.webmaker.org","forks_url":"https://api.github.com/repos/sedge/publish.webmaker.org/forks","keys_url":"https://api.github.com/repos/sedge/publish.webmaker.org/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sedge/publish.webmaker.org/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sedge/publish.webmaker.org/teams","hooks_url":"https://api.github.com/repos/sedge/publish.webmaker.org/hooks","issue_events_url":"https://api.github.com/repos/sedge/publish.webmaker.org/issues/events{/number}","events_url":"https://api.github.com/repos/sedge/publish.webmaker.org/events","assignees_url":"https://api.github.com/repos/sedge/publish.webmaker.org/assignees{/user}","branches_url":"https://api.github.com/repos/sedge/publish.webmaker.org/branches{/branch}","tags_url":"https://api.github.com/repos/sedge/publish.webmaker.org/tags","blobs_url":"https://api.github.com/repos/sedge/publish.webmaker.org/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sedge/publish.webmaker.org/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sedge/publish.webmaker.org/git/refs{/sha}","trees_url":"https://api.github.com/repos/sedge/publish.webmaker.org/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sedge/publish.webmaker.org/statuses/{sha}","languages_url":"https://api.github.com/repos/sedge/publish.webmaker.org/languages","stargazers_url":"https://api.github.com/repos/sedge/publish.webmaker.org/stargazers","contributors_url":"https://api.github.com/repos/sedge/publish.webmaker.org/contributors","subscribers_url":"https://api.github.com/repos/sedge/publish.webmaker.org/subscribers","subscription_url":"https://api.github.com/repos/sedge/publish.webmaker.org/subscription","commits_url":"https://api.github.com/repos/sedge/publish.webmaker.org/commits{/sha}","git_commits_url":"https://api.github.com/repos/sedge/publish.webmaker.org/git/commits{/sha}","comments_url":"https://api.github.com/repos/sedge/publish.webmaker.org/comments{/number}","issue_comment_url":"https://api.github.com/repos/sedge/publish.webmaker.org/issues/comments{/number}","contents_url":"https://api.github.com/repos/sedge/publish.webmaker.org/contents/{+path}","compare_url":"https://api.github.com/repos/sedge/publish.webmaker.org/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sedge/publish.webmaker.org/merges","archive_url":"https://api.github.com/repos/sedge/publish.webmaker.org/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sedge/publish.webmaker.org/downloads","issues_url":"https://api.github.com/repos/sedge/publish.webmaker.org/issues{/number}","pulls_url":"https://api.github.com/repos/sedge/publish.webmaker.org/pulls{/number}","milestones_url":"https://api.github.com/repos/sedge/publish.webmaker.org/milestones{/number}","notifications_url":"https://api.github.com/repos/sedge/publish.webmaker.org/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sedge/publish.webmaker.org/labels{/name}","releases_url":"https://api.github.com/repos/sedge/publish.webmaker.org/releases{/id}","created_at":"2015-06-08T18:02:41Z","updated_at":"2015-06-08T18:02:41Z","pushed_at":"2015-07-17T15:48:18Z","git_url":"git://github.com/sedge/publish.webmaker.org.git","ssh_url":"git@github.com:sedge/publish.webmaker.org.git","clone_url":"https://github.com/sedge/publish.webmaker.org.git","svn_url":"https://github.com/sedge/publish.webmaker.org","homepage":null,"size":630,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"mozilla:master","ref":"master","sha":"0e3d184cb45428c0ee3f2961a6bf3133bd65877c","user":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"repo":{"id":36752205,"name":"publish.webmaker.org","full_name":"mozilla/publish.webmaker.org","owner":{"login":"mozilla","id":131524,"avatar_url":"https://avatars.githubusercontent.com/u/131524?v=3","gravatar_id":"","url":"https://api.github.com/users/mozilla","html_url":"https://github.com/mozilla","followers_url":"https://api.github.com/users/mozilla/followers","following_url":"https://api.github.com/users/mozilla/following{/other_user}","gists_url":"https://api.github.com/users/mozilla/gists{/gist_id}","starred_url":"https://api.github.com/users/mozilla/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mozilla/subscriptions","organizations_url":"https://api.github.com/users/mozilla/orgs","repos_url":"https://api.github.com/users/mozilla/repos","events_url":"https://api.github.com/users/mozilla/events{/privacy}","received_events_url":"https://api.github.com/users/mozilla/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/mozilla/publish.webmaker.org","description":"The teach.org publishing service for goggles and thimble","fork":false,"url":"https://api.github.com/repos/mozilla/publish.webmaker.org","forks_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/forks","keys_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/keys{/key_id}","collaborators_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/teams","hooks_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/hooks","issue_events_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/issues/events{/number}","events_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/events","assignees_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/assignees{/user}","branches_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/branches{/branch}","tags_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/tags","blobs_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/git/refs{/sha}","trees_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/git/trees{/sha}","statuses_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/statuses/{sha}","languages_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/languages","stargazers_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/stargazers","contributors_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/contributors","subscribers_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/subscribers","subscription_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/subscription","commits_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/commits{/sha}","git_commits_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/git/commits{/sha}","comments_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/comments{/number}","issue_comment_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/issues/comments{/number}","contents_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/contents/{+path}","compare_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/compare/{base}...{head}","merges_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/merges","archive_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/downloads","issues_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/issues{/number}","pulls_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls{/number}","milestones_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/milestones{/number}","notifications_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/labels{/name}","releases_url":"https://api.github.com/repos/mozilla/publish.webmaker.org/releases{/id}","created_at":"2015-06-02T18:10:38Z","updated_at":"2015-06-15T17:40:35Z","pushed_at":"2015-07-17T15:48:18Z","git_url":"git://github.com/mozilla/publish.webmaker.org.git","ssh_url":"git@github.com:mozilla/publish.webmaker.org.git","clone_url":"https://github.com/mozilla/publish.webmaker.org.git","svn_url":"https://github.com/mozilla/publish.webmaker.org","homepage":"https://damp-anchorage-2948.herokuapp.com/","size":1468,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":3,"mirror_url":null,"open_issues_count":18,"forks":3,"open_issues":18,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114"},"html":{"href":"https://github.com/mozilla/publish.webmaker.org/pull/114"},"issue":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/issues/114"},"comments":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/issues/114/comments"},"review_comments":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114/comments"},"review_comment":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/pulls/114/commits"},"statuses":{"href":"https://api.github.com/repos/mozilla/publish.webmaker.org/statuses/8910029b1ef3f8f1c53fedab78839bb0e677839f"}}}}