From bfaa4cdb379ade4bb39625768435abdaae45ec78 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Wed, 2 Mar 2016 23:48:17 +0530 Subject: [PATCH] FEATURE: compose a new pre-filled private message to a group via URL --- .../discourse/routes/new-message.js.es6 | 40 ++++++++++++++----- app/models/group.rb | 4 ++ app/serializers/basic_group_serializer.rb | 7 +++- .../admin/groups_controller_spec.rb | 3 +- 4 files changed, 41 insertions(+), 13 deletions(-) diff --git a/app/assets/javascripts/discourse/routes/new-message.js.es6 b/app/assets/javascripts/discourse/routes/new-message.js.es6 index c5d3ab849b5..05767d3422d 100644 --- a/app/assets/javascripts/discourse/routes/new-message.js.es6 +++ b/app/assets/javascripts/discourse/routes/new-message.js.es6 @@ -1,20 +1,38 @@ +import Group from 'discourse/models/group'; + export default Discourse.Route.extend({ beforeModel: function(transition) { const self = this; if (Discourse.User.current()) { // User is logged in self.replaceWith('discovery.latest').then(function(e) { - Discourse.User.findByUsername(transition.queryParams.username).then((user) => { - if (user.can_send_private_message_to_user) { - Ember.run.next(function() { - e.send('createNewMessageViaParams', user.username, transition.queryParams.title, transition.queryParams.body); - }); - } else { - bootbox.alert(I18n.t("composer.cant_send_pm", {username: user.username})); - } - }).catch(() => { - bootbox.alert(I18n.t("generic_error")); - }); + if (transition.queryParams.username) { + // send a message to user + Discourse.User.findByUsername(transition.queryParams.username).then((user) => { + if (user.can_send_private_message_to_user) { + Ember.run.next(function() { + e.send('createNewMessageViaParams', user.username, transition.queryParams.title, transition.queryParams.body); + }); + } else { + bootbox.alert(I18n.t("composer.cant_send_pm", {username: user.username})); + } + }).catch(() => { + bootbox.alert(I18n.t("generic_error")); + }); + } else { + // send a message to group + Group.find(transition.queryParams.groupname).then((group) => { + if (!group.automatic && group.mentionable) { + Ember.run.next(function() { + e.send('createNewMessageViaParams', group.name, transition.queryParams.title, transition.queryParams.body); + }); + } else { + bootbox.alert(I18n.t("composer.cant_send_pm", {username: group.name})); + } + }).catch(() => { + bootbox.alert(I18n.t("generic_error")); + }); + } }); } else { // User is not logged in diff --git a/app/models/group.rb b/app/models/group.rb index dfb21f2f04e..527955fbd6f 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -362,6 +362,10 @@ class Group < ActiveRecord::Base true end + def mentionable?(user, group_id) + Group.mentionable(user).where(id: group_id).exists? + end + protected def name_format_validator diff --git a/app/serializers/basic_group_serializer.rb b/app/serializers/basic_group_serializer.rb index d9a50ef124b..725de396e5a 100644 --- a/app/serializers/basic_group_serializer.rb +++ b/app/serializers/basic_group_serializer.rb @@ -12,7 +12,8 @@ class BasicGroupSerializer < ApplicationSerializer :grant_trust_level, :incoming_email, :notification_level, - :has_messages + :has_messages, + :mentionable def include_incoming_email? scope.is_staff? @@ -27,4 +28,8 @@ class BasicGroupSerializer < ApplicationSerializer scope.authenticated? end + def mentionable + object.mentionable?(scope.user, object.id) + end + end diff --git a/spec/controllers/admin/groups_controller_spec.rb b/spec/controllers/admin/groups_controller_spec.rb index 028945d2269..96fb3d229ab 100644 --- a/spec/controllers/admin/groups_controller_spec.rb +++ b/spec/controllers/admin/groups_controller_spec.rb @@ -33,7 +33,8 @@ describe Admin::GroupsController do "grant_trust_level"=>nil, "incoming_email"=>nil, "notification_level"=>2, - "has_messages"=>false + "has_messages"=>false, + "mentionable"=>false }]) end