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

If a group mention could be notified on preview it was given an `<a>`
tag with the `.notify` class. When cooked it would display differently.
This patch makes the server side cooking match the client preview.
This commit is contained in:
Robin Ward 2020-02-18 15:45:02 -05:00
parent 163cbb4aa7
commit 041168c9b6
4 changed files with 43 additions and 21 deletions

View File

@ -564,7 +564,7 @@ export default Controller.extend({
max: group.max_mentions, max: group.max_mentions,
group_link: groupLink group_link: groupLink
}); });
} else { } else if (group.user_count > 0) {
body = I18n.t("composer.group_mentioned", { body = I18n.t("composer.group_mentioned", {
group: `@${group.name}`, group: `@${group.name}`,
count: group.user_count, count: group.user_count,
@ -572,11 +572,13 @@ export default Controller.extend({
}); });
} }
if (body) {
this.appEvents.trigger("composer-messages:create", { this.appEvents.trigger("composer-messages:create", {
extraClass: "custom-body", extraClass: "custom-body",
templateName: "custom-body", templateName: "custom-body",
body body
}); });
}
}); });
} }
}, },

View File

@ -440,6 +440,7 @@ module PrettyText
USER_TYPE ||= 'user' USER_TYPE ||= 'user'
GROUP_TYPE ||= 'group' GROUP_TYPE ||= 'group'
GROUP_MENTIONABLE_TYPE ||= 'group-mentionable'
def self.add_mentions(doc, user_id: nil) def self.add_mentions(doc, user_id: nil)
elements = doc.css("span.mention") elements = doc.css("span.mention")
@ -461,6 +462,9 @@ module PrettyText
case type case type
when USER_TYPE when USER_TYPE
element['href'] = "#{Discourse::base_uri}/u/#{name}" 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 when GROUP_TYPE
element['class'] = 'mention-group' element['class'] = 'mention-group'
element['href'] = "#{Discourse::base_uri}/groups/#{name}" element['href'] = "#{Discourse::base_uri}/groups/#{name}"
@ -486,8 +490,16 @@ module PrettyText
:group_type AS type, :group_type AS type,
lower(name) AS name lower(name) AS name
FROM groups 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 SQL
user = User.find_by(id: user_id) user = User.find_by(id: user_id)
@ -497,6 +509,7 @@ module PrettyText
names: names, names: names,
user_type: USER_TYPE, user_type: USER_TYPE,
group_type: GROUP_TYPE, group_type: GROUP_TYPE,
group_mentionable_type: GROUP_MENTIONABLE_TYPE,
levels: Group.alias_levels(user), levels: Group.alias_levels(user),
user_id: user_id user_id: user_id
) )

View File

@ -347,12 +347,8 @@ describe PrettyText do
Fabricate(:user, username: username) Fabricate(:user, username: username)
end end
['Group', 'group2'].each do |name| Fabricate(:group, name: 'Group', mentionable_level: Group::ALIAS_LEVELS[:everyone])
Fabricate(:group, Fabricate(:group, name: 'Group2', mentionable_level: Group::ALIAS_LEVELS[:members_mods_and_admins])
name: name,
mentionable_level: Group::ALIAS_LEVELS[:everyone]
)
end
[ [
[ [
@ -361,7 +357,7 @@ describe PrettyText do
], ],
[ [
"hi\n@user. @GROUP @somemention @group2", "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| ].each do |input, expected|
expect(PrettyText.cook(input)).to eq(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]) Fabricate(:group, name: 'groupA', mentionable_level: Group::ALIAS_LEVELS[:everyone])
input = 'hi there @user1 and @groupA' 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) expect(PrettyText.cook(input)).to eq(expected)
end end
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, group = Fabricate(:group,
visibility_level: Group.visibility_levels[:members], visibility_level: Group.visibility_levels[:members],
mentionable_level: Group::ALIAS_LEVELS[:nobody] mentionable_level: Group::ALIAS_LEVELS[:nobody]
) )
expect(PrettyText.cook("test @#{group.name} test")).to eq( 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 end

View File

@ -1046,13 +1046,13 @@ describe Post do
end end
describe 'when user can not mention a group' do 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 = Fabricate(:post, raw: "hello @#{group.name}")
post.trigger_post_process post.trigger_post_process
post.reload post.reload
expect(post.cooked).to eq( 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
end end
@ -1068,7 +1068,7 @@ describe Post do
post.reload post.reload
expect(post.cooked).to eq( 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
end end
@ -1085,7 +1085,7 @@ describe Post do
post.reload post.reload
expect(post.cooked).to eq( 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
end end