From 65e808b26d6195b8dcc75d9c51e19b09a01891a6 Mon Sep 17 00:00:00 2001
From: Sam <sam.saffron@gmail.com>
Date: Tue, 12 Jan 2016 15:49:05 +1100
Subject: [PATCH] FEATURE: go to inbox after archiving a message

---
 .../discourse/controllers/topic.js.es6        | 20 +++++++++++++++++--
 .../javascripts/discourse/models/topic.js.es6 | 16 +++++++++++----
 app/controllers/topics_controller.rb          | 11 +++++++++-
 3 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/app/assets/javascripts/discourse/controllers/topic.js.es6 b/app/assets/javascripts/discourse/controllers/topic.js.es6
index 5722d2f5d..9c84a6d50 100644
--- a/app/assets/javascripts/discourse/controllers/topic.js.es6
+++ b/app/assets/javascripts/discourse/controllers/topic.js.es6
@@ -6,6 +6,7 @@ import Quote from 'discourse/lib/quote';
 import { popupAjaxError } from 'discourse/lib/ajax-error';
 import computed from 'ember-addons/ember-computed-decorators';
 import Composer from 'discourse/models/composer';
+import DiscourseURL from 'discourse/lib/url';
 
 export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
   needs: ['header', 'modal', 'composer', 'quote-button', 'topic-progress', 'application'],
@@ -96,6 +97,14 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
     return !isPrivateMessage && !containsMessages;
   },
 
+  gotoInbox(name) {
+    var url = '/users/' + this.get('currentUser.username_lower') + '/messages';
+    if (name) {
+      url = url + '/group/' + name;
+    }
+    DiscourseURL.routeTo(url);
+  },
+
   actions: {
     showTopicAdminMenu() {
       this.set('adminMenuVisible', true);
@@ -109,12 +118,19 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
       this.deleteTopic();
     },
 
+
     archiveMessage() {
-      this.get('model').archiveMessage();
+      const topic = this.get('model');
+      topic.archiveMessage().then(()=>{
+        this.gotoInbox(topic.get("inboxGroupName"));
+      });
     },
 
     moveToInbox() {
-      this.get('model').moveToInbox();
+      const topic = this.get('model');
+      topic.moveToInbox().then(()=>{
+        this.gotoInbox(topic.get("inboxGroupName"));
+      });
     },
 
     // Post related methods
diff --git a/app/assets/javascripts/discourse/models/topic.js.es6 b/app/assets/javascripts/discourse/models/topic.js.es6
index c7c867946..24d0638a5 100644
--- a/app/assets/javascripts/discourse/models/topic.js.es6
+++ b/app/assets/javascripts/discourse/models/topic.js.es6
@@ -424,8 +424,12 @@ const Topic = RestModel.extend({
     this.set("archiving", true);
     var promise = Discourse.ajax(`/t/${this.get('id')}/archive-message`, {type: 'PUT'});
 
-    promise.then(()=>this.set('message_archived', true))
-           .finally(()=>this.set('archiving', false));
+    promise.then((msg)=> {
+      this.set('message_archived', true);
+      if (msg && msg.group_name) {
+        this.set('inboxGroupName', msg.group_name);
+      }
+    }).finally(()=>this.set('archiving', false));
 
     return promise;
   },
@@ -434,8 +438,12 @@ const Topic = RestModel.extend({
     this.set("archiving", true);
     var promise = Discourse.ajax(`/t/${this.get('id')}/move-to-inbox`, {type: 'PUT'});
 
-    promise.then(()=>this.set('message_archived', false))
-           .finally(()=>this.set('archiving', false));
+    promise.then((msg)=> {
+      this.set('message_archived', false);
+      if (msg && msg.group_name) {
+        this.set('inboxGroupName', msg.group_name);
+      }
+    }).finally(()=>this.set('archiving', false));
 
     return promise;
   }
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 4a9c0fec3..bc53a2e95 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -281,6 +281,9 @@ class TopicsController < ApplicationController
 
   def toggle_archive_message(archive)
     topic = Topic.find(params[:id].to_i)
+
+    group_id = nil
+
     group_ids = current_user.groups.pluck(:id)
     if group_ids.present?
       allowed_groups = topic.allowed_groups
@@ -289,6 +292,7 @@ class TopicsController < ApplicationController
         GroupArchivedMessage.where(group_id: id, topic_id: topic.id).destroy_all
 
         if archive
+          group_id = id
           GroupArchivedMessage.create!(group_id: id, topic_id: topic.id)
         end
       end
@@ -302,7 +306,12 @@ class TopicsController < ApplicationController
       end
     end
 
-    render nothing: true
+    if group_id
+      name = Group.find_by(id: group_id).try(:name)
+      render_json_dump(group_name: name)
+    else
+      render nothing: true
+    end
   end
 
   def bookmark