Commit bf708e55 authored by Jarka Kadlecova's avatar Jarka Kadlecova

make mentions working when project not specified

parent d6b11daf
...@@ -112,7 +112,10 @@ module CacheMarkdownField ...@@ -112,7 +112,10 @@ module CacheMarkdownField
invalidation_method = "#{html_field}_invalidated?".to_sym invalidation_method = "#{html_field}_invalidated?".to_sym
define_method(cache_method) do define_method(cache_method) do
html = Banzai::Renderer.cacheless_render_field(self, markdown_field) options = {
skip_project_check: is_a?(Note) && for_personal_snippet?
}
html = Banzai::Renderer.cacheless_render_field(self, markdown_field, options)
__send__("#{html_field}=", html) __send__("#{html_field}=", html)
true true
end end
......
...@@ -49,7 +49,11 @@ module Mentionable ...@@ -49,7 +49,11 @@ module Mentionable
self.class.mentionable_attrs.each do |attr, options| self.class.mentionable_attrs.each do |attr, options|
text = __send__(attr) text = __send__(attr)
options = options.merge(cache_key: [self, attr], author: author) options = options.merge(
cache_key: [self, attr],
author: author,
skip_project_check: is_a?(Note) && for_personal_snippet?
)
extractor.analyze(text, options) extractor.analyze(text, options)
end end
......
...@@ -53,6 +53,10 @@ module Banzai ...@@ -53,6 +53,10 @@ module Banzai
context[:project] context[:project]
end end
def skip_project_check?
context[:skip_project_check]
end
def reference_class(type) def reference_class(type)
"gfm gfm-#{type} has-tooltip" "gfm gfm-#{type} has-tooltip"
end end
......
...@@ -24,11 +24,10 @@ module Banzai ...@@ -24,11 +24,10 @@ module Banzai
end end
def call def call
return doc if project.nil? return doc if project.nil? && !skip_project_check?
ref_pattern = User.reference_pattern ref_pattern = User.reference_pattern
ref_pattern_start = /\A#{ref_pattern}\z/ ref_pattern_start = /\A#{ref_pattern}\z/
nodes.each do |node| nodes.each do |node|
if text_node?(node) if text_node?(node)
replace_text_when_pattern_matches(node, ref_pattern) do |content| replace_text_when_pattern_matches(node, ref_pattern) do |content|
...@@ -58,7 +57,7 @@ module Banzai ...@@ -58,7 +57,7 @@ module Banzai
# have `gfm` and `gfm-project_member` class names attached for styling. # have `gfm` and `gfm-project_member` class names attached for styling.
def user_link_filter(text, link_content: nil) def user_link_filter(text, link_content: nil)
self.class.references_in(text) do |match, username| self.class.references_in(text) do |match, username|
if username == 'all' if username == 'all' && !skip_project_check?
link_to_all(link_content: link_content) link_to_all(link_content: link_content)
elsif namespace = namespaces[username] elsif namespace = namespaces[username]
link_to_namespace(namespace, link_content: link_content) || match link_to_namespace(namespace, link_content: link_content) || match
......
...@@ -52,9 +52,9 @@ module Banzai ...@@ -52,9 +52,9 @@ module Banzai
end end
# Same as +render_field+, but without consulting or updating the cache field # Same as +render_field+, but without consulting or updating the cache field
def cacheless_render_field(object, field) def cacheless_render_field(object, field, options = {})
text = object.__send__(field) text = object.__send__(field)
context = object.banzai_render_context(field) context = object.banzai_render_context(field).merge(options)
cacheless_render(text, context) cacheless_render(text, context)
end end
......
...@@ -152,6 +152,27 @@ describe Banzai::Filter::UserReferenceFilter, lib: true do ...@@ -152,6 +152,27 @@ describe Banzai::Filter::UserReferenceFilter, lib: true do
end end
end end
context 'when a project is not specified' do
let(:project) { nil }
it 'does not link a User' do
doc = reference_filter("Hey #{reference}")
expect(doc).not_to include('a')
end
context 'when skip_project_check set to true' do
it 'links to a User' do
doc = reference_filter("Hey #{reference}", skip_project_check: true)
expect(doc.css('a').first.attr('href')).to eq urls.user_url(user)
end
it 'does not link users using @all reference' do
doc = reference_filter("Hey #{User.reference_prefix}all", skip_project_check: true)
expect(doc).not_to include('a')
end
end
end
describe '#namespaces' do describe '#namespaces' do
it 'returns a Hash containing all Namespaces' do it 'returns a Hash containing all Namespaces' do
document = Nokogiri::HTML.fragment("<p>#{user.to_reference}</p>") document = Nokogiri::HTML.fragment("<p>#{user.to_reference}</p>")
......
...@@ -30,12 +30,23 @@ describe Issue, "Mentionable" do ...@@ -30,12 +30,23 @@ describe Issue, "Mentionable" do
describe '#mentioned_users' do describe '#mentioned_users' do
let!(:user) { create(:user, username: 'stranger') } let!(:user) { create(:user, username: 'stranger') }
let!(:user2) { create(:user, username: 'john') } let!(:user2) { create(:user, username: 'john') }
let!(:issue) { create(:issue, description: "#{user.to_reference} mentioned") } let!(:user3) { create(:user, username: 'jim') }
let(:issue) { create(:issue, description: "#{user.to_reference} mentioned") }
subject { issue.mentioned_users } subject { issue.mentioned_users }
it { is_expected.to include(user) } it { is_expected.to include(user) }
it { is_expected.not_to include(user2) } it { is_expected.not_to include(user2) }
context 'when a note on personal snippet' do
let!(:note) { create(:note_on_personal_snippet, note: "#{user.to_reference} mentioned #{user3.to_reference}") }
subject { note.mentioned_users }
it { is_expected.to include(user) }
it { is_expected.to include(user3) }
it { is_expected.not_to include(user2) }
end
end end
describe '#referenced_mentionables' do describe '#referenced_mentionables' do
......
...@@ -152,6 +152,7 @@ describe Note, models: true do ...@@ -152,6 +152,7 @@ describe Note, models: true do
with([{ with([{
text: note1.note, text: note1.note,
context: { context: {
skip_project_check: false,
pipeline: :note, pipeline: :note,
cache_key: [note1, "note"], cache_key: [note1, "note"],
project: note1.project, project: note1.project,
...@@ -163,6 +164,7 @@ describe Note, models: true do ...@@ -163,6 +164,7 @@ describe Note, models: true do
with([{ with([{
text: note2.note, text: note2.note,
context: { context: {
skip_project_check: false,
pipeline: :note, pipeline: :note,
cache_key: [note2, "note"], cache_key: [note2, "note"],
project: note2.project, project: note2.project,
......
...@@ -306,8 +306,8 @@ describe NotificationService, services: true do ...@@ -306,8 +306,8 @@ describe NotificationService, services: true do
should_email(@u_watcher) should_email(@u_watcher)
should_email(@u_snippet_author) should_email(@u_snippet_author)
# TODO: make mentions working for pesronal snippets # it emails mentioned users
# should_email(@u_mentioned_level) should_email(@u_mentioned)
# it does not email participants with mention notification level # it does not email participants with mention notification level
should_not_email(@u_mentioned_level) should_not_email(@u_mentioned_level)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment