From 75374c76b316c6cc8b3008e79b720d2d8bad78ac Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 27 Jul 2017 17:51:25 +0900 Subject: [PATCH] UX: Display button to message group on group page. https://meta.discourse.org/t/make-it-easier-to-send-a-message-to-groups/65065 --- .../discourse/controllers/group.js.es6 | 11 ++++++++++ .../javascripts/discourse/templates/group.hbs | 8 ++++++++ app/serializers/group_show_serializer.rb | 18 ++++++++++++++--- config/locales/client.en.yml | 1 + .../serializers/group_show_serializer_spec.rb | 20 ++++++++++++++++--- .../javascripts/acceptance/groups-test.js.es6 | 19 ++++++++++++++++-- .../fixtures/group-fixtures.js.es6 | 5 +++-- 7 files changed, 72 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/group.js.es6 b/app/assets/javascripts/discourse/controllers/group.js.es6 index 09466b4c4f9..d38f15290ec 100644 --- a/app/assets/javascripts/discourse/controllers/group.js.es6 +++ b/app/assets/javascripts/discourse/controllers/group.js.es6 @@ -48,6 +48,11 @@ export default Ember.Controller.extend({ }; }, + @computed("model.mentionable") + displayGroupMessageButton(mentionable) { + return this.currentUser && mentionable; + }, + @observes('model.user_count') _setMembersTabCount() { this.get('tabs')[0].set('count', this.get('model.user_count')); @@ -66,5 +71,11 @@ export default Ember.Controller.extend({ return canSee; }); + }, + + actions: { + messageGroup() { + this.send('createNewMessageViaParams', this.get('model.name')); + } } }); diff --git a/app/assets/javascripts/discourse/templates/group.hbs b/app/assets/javascripts/discourse/templates/group.hbs index 24afa5a0c70..2da7d242cb3 100644 --- a/app/assets/javascripts/discourse/templates/group.hbs +++ b/app/assets/javascripts/discourse/templates/group.hbs @@ -43,6 +43,14 @@ {{/each}} {{/mobile-nav}} + {{#if displayGroupMessageButton}} + {{d-button + action="messageGroup" + class="btn-primary group-message-button" + icon="envelope" + label="groups.message"}} + {{/if}} + {{group-membership-button model=model showLogin='showLogin'}} diff --git a/app/serializers/group_show_serializer.rb b/app/serializers/group_show_serializer.rb index dc9e0c7649a..3bd6bf31d15 100644 --- a/app/serializers/group_show_serializer.rb +++ b/app/serializers/group_show_serializer.rb @@ -1,8 +1,8 @@ class GroupShowSerializer < BasicGroupSerializer - attributes :is_group_user, :is_group_owner + attributes :is_group_user, :is_group_owner, :mentionable def include_is_group_user? - scope.authenticated? + authenticated? end def is_group_user @@ -10,15 +10,27 @@ class GroupShowSerializer < BasicGroupSerializer end def include_is_group_owner? - scope.authenticated? + authenticated? end def is_group_owner scope.is_admin? || fetch_group_user&.owner end + def include_mentionable? + authenticated? + end + + def mentionable + Group.mentionable(scope.user).exists?(id: object.id) + end + private + def authenticated? + scope.authenticated? + end + def fetch_group_user @group_user ||= object.group_users.find_by(user: scope.user) end diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index e13e91344d8..467e3a799b5 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -421,6 +421,7 @@ en: join: "Join Group" leave: "Leave Group" request: "Request to Join Group" + message: "Message" automatic_group: Automatic Group closed_group: Closed Group is_group_user: "You are a member of this group" diff --git a/spec/serializers/group_show_serializer_spec.rb b/spec/serializers/group_show_serializer_spec.rb index 5641ccd8d09..b90db108e5c 100644 --- a/spec/serializers/group_show_serializer_spec.rb +++ b/spec/serializers/group_show_serializer_spec.rb @@ -1,6 +1,9 @@ require 'rails_helper' describe GroupShowSerializer do + let(:user) { Fabricate(:user) } + let(:group) { Fabricate(:group) } + context 'admin user' do let(:user) { Fabricate(:admin) } let(:group) { Fabricate(:group, users: [user]) } @@ -14,9 +17,6 @@ describe GroupShowSerializer do end context 'group owner' do - let(:user) { Fabricate(:user) } - let(:group) { Fabricate(:group) } - before do group.add_owner(user) end @@ -28,4 +28,18 @@ describe GroupShowSerializer do expect(json[:group_show][:is_group_user]).to eq(true) end end + + describe '#mentionable' do + let(:group) { Fabricate(:group, alias_level: Group::ALIAS_LEVELS[:everyone]) } + + it 'should return the right value' do + json = GroupShowSerializer.new(group, scope: Guardian.new).as_json + + expect(json[:group_show][:mentionable]).to eq(nil) + + json = GroupShowSerializer.new(group, scope: Guardian.new(user)).as_json + + expect(json[:group_show][:mentionable]).to eq(true) + end + end end diff --git a/test/javascripts/acceptance/groups-test.js.es6 b/test/javascripts/acceptance/groups-test.js.es6 index 0534127ea7b..017f672a73e 100644 --- a/test/javascripts/acceptance/groups-test.js.es6 +++ b/test/javascripts/acceptance/groups-test.js.es6 @@ -42,12 +42,13 @@ QUnit.test("Browsing Groups", assert => { }); }); -QUnit.test("Viewing Group", assert => { +QUnit.test("Anonymous Viewing Group", assert => { visit("/groups/discourse"); andThen(() => { assert.ok(count('.avatar-flair .fa-adjust') === 1, "it displays the group's avatar flair"); assert.ok(count('.group-members tr') > 0, "it lists group members"); + assert.ok(count('.group-message-button') === 0, 'it does not show group message button'); }); click(".nav-pills li a[title='Activity']"); @@ -80,6 +81,20 @@ QUnit.test("Viewing Group", assert => { }); }); +QUnit.test("User Viewing Group", assert => { + logIn(); + Discourse.reset(); + + visit("/groups/discourse"); + + click('.group-message-button'); + + andThen(() => { + assert.ok(count('#reply-control') === 1, 'it opens the composer'); + assert.equal(find('.ac-wrap .item').text(), 'discourse', 'it prefills the group name'); + }); +}); + QUnit.test("Admin Viewing Group", assert => { logIn(); Discourse.reset(); @@ -102,4 +117,4 @@ QUnit.test("Admin Viewing Group", assert => { 'it should show messages tab if user is admin' ); }); -}); \ No newline at end of file +}); diff --git a/test/javascripts/fixtures/group-fixtures.js.es6 b/test/javascripts/fixtures/group-fixtures.js.es6 index a55d8303cb3..dbc9d32d579 100644 --- a/test/javascripts/fixtures/group-fixtures.js.es6 +++ b/test/javascripts/fixtures/group-fixtures.js.es6 @@ -6,11 +6,12 @@ export default { "name":"discourse", "full_name":"Awesome Team", "user_count":8, - "alias_level":0, + "alias_level":99, "visible":true, "public":true, "flair_url": 'fa-adjust', - "is_group_owner":true + "is_group_owner":true, + "mentionable":true } }, "/groups/discourse/counts.json":{