Skip to content

A platform for community discussion. Free, open, simple.

FIX: Group mentions were not being cooked the same was as previewed

Robin Ward041168c9b68a
diff --git a/app/assets/javascripts/discourse/controllers/composer.js.es6 b/app/assets/javascripts/discourse/controllers/composer.js.es6
index a04fcd45a98..e7d02fbd3c9 100644
--- a/app/assets/javascripts/discourse/controllers/composer.js.es6
+++ b/app/assets/javascripts/discourse/controllers/composer.js.es6
@@ -564,7 +564,7 @@ export default Controller.extend({
max: group.max_mentions,
group_link: groupLink
});
- } else {
+ } else if (group.user_count > 0) {
body = I18n.t("composer.group_mentioned", {
group: `@${group.name}`,
count: group.user_count,
@@ -572,11 +572,13 @@ export default Controller.extend({
});
}
- this.appEvents.trigger("composer-messages:create", {
- extraClass: "custom-body",
- templateName: "custom-body",
- body
- });
+ if (body) {
+ this.appEvents.trigger("composer-messages:create", {
+ extraClass: "custom-body",
+ templateName: "custom-body",
+ body
+ });
+ }
});
}
},
diff --git a/lib/pretty_text.rb b/lib/pretty_text.rb
index de9c1c8cdbb..a4b6312d556 100644
--- a/lib/pretty_text.rb
+++ b/lib/pretty_text.rb
@@ -440,6 +440,7 @@ module PrettyText
USER_TYPE ||= 'user'
GROUP_TYPE ||= 'group'
+ GROUP_MENTIONABLE_TYPE ||= 'group-mentionable'
def self.add_mentions(doc, user_id: nil)
elements = doc.css("span.mention")
@@ -461,6 +462,9 @@ module PrettyText
case type
when USER_TYPE
element['href'] = "#{Discourse::base_uri}/u/#{name}"
+ when GROUP_MENTIONABLE_TYPE
+ element['class'] = 'mention-group notify'
+ element['href'] = "#{Discourse::base_uri}/groups/#{name}"
when GROUP_TYPE
element['class'] = 'mention-group'
element['href'] = "#{Discourse::base_uri}/groups/#{name}"
@@ -486,8 +490,16 @@ module PrettyText
:group_type AS type,
lower(name) AS name
FROM groups
- WHERE lower(name) IN (:names) AND (#{Group.mentionable_sql_clause})
)
+ UNION
+ (
+ SELECT
+ :group_mentionable_type AS type,
+ lower(name) AS name
+ FROM groups
+ WHERE lower(name) IN (:names) AND (#{Group.mentionable_sql_clause(include_public: false)})
+ )
+ ORDER BY type
SQL
user = User.find_by(id: user_id)
@@ -497,6 +509,7 @@ module PrettyText
names: names,
user_type: USER_TYPE,
group_type: GROUP_TYPE,
+ group_mentionable_type: GROUP_MENTIONABLE_TYPE,
levels: Group.alias_levels(user),
user_id: user_id
)
diff --git a/spec/components/pretty_text_spec.rb b/spec/components/pretty_text_spec.rb
index f3218293f5c..f2ab77be420 100644
--- a/spec/components/pretty_text_spec.rb
+++ b/spec/components/pretty_text_spec.rb
@@ -347,12 +347,8 @@ describe PrettyText do
Fabricate(:user, username: username)
end
- ['Group', 'group2'].each do |name|
- Fabricate(:group,
- name: name,
- mentionable_level: Group::ALIAS_LEVELS[:everyone]
- )
- end
+ Fabricate(:group, name: 'Group', mentionable_level: Group::ALIAS_LEVELS[:everyone])
+ Fabricate(:group, name: 'Group2', mentionable_level: Group::ALIAS_LEVELS[:members_mods_and_admins])
[
[
@@ -361,7 +357,7 @@ describe PrettyText do
],
[
"hi\n@user. @GROUP @somemention @group2",
- %Q|<p>hi<br>\n<a class="mention" href="/u/user">@user</a>. <a class="mention-group" href="/groups/group">@GROUP</a> <span class="mention">@somemention</span> <a class="mention-group" href="/groups/group2">@group2</a></p>|
+ %Q|<p>hi<br>\n<a class="mention" href="/u/user">@user</a>. <a class="mention-group notify" href="/groups/group">@GROUP</a> <span class="mention">@somemention</span> <a class="mention-group" href="/groups/group2">@group2</a></p>|
]
].each do |input, expected|
expect(PrettyText.cook(input)).to eq(expected)
@@ -376,20 +372,31 @@ describe PrettyText do
Fabricate(:group, name: 'groupA', mentionable_level: Group::ALIAS_LEVELS[:everyone])
input = 'hi there @user1 and @groupA'
- expected = '<p>hi there <a class="mention" href="/forum/u/user1">@user1</a> and <a class="mention-group" href="/forum/groups/groupa">@groupA</a></p>'
+ expected = '<p>hi there <a class="mention" href="/forum/u/user1">@user1</a> and <a class="mention-group notify" href="/forum/groups/groupa">@groupA</a></p>'
expect(PrettyText.cook(input)).to eq(expected)
end
end
- it "does not create mention for a non mentionable group" do
+ it "does not assign the notify class to a group that can't be mentioned" do
group = Fabricate(:group,
visibility_level: Group.visibility_levels[:members],
mentionable_level: Group::ALIAS_LEVELS[:nobody]
)
expect(PrettyText.cook("test @#{group.name} test")).to eq(
- %Q|<p>test <span class="mention">@#{group.name}</span> test</p>|
+ %Q|<p>test <a class="mention-group" href="/groups/#{group.name}">@#{group.name}</a> test</p>|
+ )
+ end
+
+ it "assigns the notify class if the user can mention" do
+ group = Fabricate(:group,
+ visibility_level: Group.visibility_levels[:members],
+ mentionable_level: Group::ALIAS_LEVELS[:members_mods_and_admins]
+ )
+
+ expect(PrettyText.cook("test @#{group.name} test", user_id: Fabricate(:admin).id)).to eq(
+ %Q|<p>test <a class="mention-group notify" href="/groups/#{group.name}">@#{group.name}</a> test</p>|
)
end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 80efa2ed1f5..2001a996e3f 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -1046,13 +1046,13 @@ describe Post do
end
describe 'when user can not mention a group' do
- it "should not create the mention" do
+ it "should not create the mention with the notify class" do
post = Fabricate(:post, raw: "hello @#{group.name}")
post.trigger_post_process
post.reload
expect(post.cooked).to eq(
- %Q|<p>hello <span class="mention">@#{group.name}</span></p>|
+ %Q|<p>hello <a class="mention-group" href="/groups/#{group.name}">@#{group.name}</a></p>|
)
end
end
@@ -1068,7 +1068,7 @@ describe Post do
post.reload
expect(post.cooked).to eq(
- %Q|<p>hello <a class="mention-group" href="/groups/#{group.name}">@#{group.name}</a></p>|
+ %Q|<p>hello <a class="mention-group notify" href="/groups/#{group.name}">@#{group.name}</a></p>|
)
end
end
@@ -1085,7 +1085,7 @@ describe Post do
post.reload
expect(post.cooked).to eq(
- %Q|<p>hello <a class="mention-group" href="/groups/#{group.name}">@#{group.name}</a></p>|
+ %Q|<p>hello <a class="mention-group notify" href="/groups/#{group.name}">@#{group.name}</a></p>|
)
end
end