From ef5984917fc9d45d1b7887147ce153168b2e491f Mon Sep 17 00:00:00 2001 From: nicolas-entourage <75681929+nicolas-entourage@users.noreply.github.com> Date: Thu, 9 Jan 2025 16:19:17 +0100 Subject: [PATCH 1/2] user-mentions various fixes on MentionsStruct methods --- app/models/concerns/mentionable.rb | 7 +- spec/models/concerns/mentionable_spec.rb | 95 ++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 spec/models/concerns/mentionable_spec.rb diff --git a/app/models/concerns/mentionable.rb b/app/models/concerns/mentionable.rb index 0c4476440..f28e21317 100644 --- a/app/models/concerns/mentionable.rb +++ b/app/models/concerns/mentionable.rb @@ -12,7 +12,8 @@ def initialize(instance: nil) def no_html document = Nokogiri::HTML(@instance.content) - document.css('img, a').each { |node| node.remove } + document.css('img').each { |node| node.remove } + document.css('a').each { |node| node.replace(node.text) } document.text.strip end @@ -21,7 +22,7 @@ def fragments end def contains_html? - fragments.children.any? + fragments.children.any? { |node| node.element? } end def contains_anchor_with_href? @@ -50,6 +51,8 @@ def has_mentions? end def has_mentions! + return unless has_mentions? + # @todo perform in a job PushNotificationTrigger.new(self, :mention, Hash.new).run end diff --git a/spec/models/concerns/mentionable_spec.rb b/spec/models/concerns/mentionable_spec.rb new file mode 100644 index 000000000..0b49b0c2d --- /dev/null +++ b/spec/models/concerns/mentionable_spec.rb @@ -0,0 +1,95 @@ +require 'rails_helper' + +RSpec.describe Mentionable, type: :module do + let(:chat_message) { create(:chat_message, content: content) } + + describe Mentionable::MentionsStruct do + subject { Mentionable::MentionsStruct.new(instance: chat_message) } + + context 'when content contains HTML' do + let(:content) { '

Hello, User

' } + + it 'detects HTML content' do + expect(subject.contains_html?).to be true + end + + it 'detects an anchor with href' do + expect(subject.contains_anchor_with_href?).to be true + end + + it 'detects a user link' do + expect(subject.contains_user_link?).to be true + end + + it 'extracts user UUIDs' do + expect(subject.extract_user_uuid).to eq(['123']) + end + + it 'removes HTML for no_html' do + expect(subject.no_html).to eq('Hello, User') + end + end + + context 'when content does not contain HTML' do + let(:content) { 'Hello, User' } + + it 'does not detect HTML content' do + expect(subject.contains_html?).to be false + end + + it 'does not detect an anchor with href' do + expect(subject.contains_anchor_with_href?).to be false + end + + it 'does not detect a user link' do + expect(subject.contains_user_link?).to be false + end + + it 'extracts no user UUIDs' do + expect(subject.extract_user_uuid).to eq([]) + end + end + end + + describe '#has_mentions?' do + subject { chat_message } + + context 'when there are user mentions' do + let(:content) { 'John' } + + it { } + end + + context 'when there are no user mentions' do + let(:content) { 'Group' } + + it { expect(subject.has_mentions?).to be false } + end + end + + describe '#has_mentions!' do + subject { chat_message } + + context 'when there are user mentions' do + let(:content) { 'Doe' } + + it 'triggers a push notification' do + expect(PushNotificationTrigger).to receive(:new) + .with(subject, :mention, {}) + .and_call_original + + subject.send(:has_mentions!) + end + end + + context 'when there are no user mentions' do + let(:content) { 'No user mentions here.' } + + it 'does not trigger a push notification' do + expect(PushNotificationTrigger).not_to receive(:new).with(subject, :mention, {}) + + subject.send(:has_mentions!) + end + end + end +end From 9658736d0f5522c651f227f3cc5ee9eafb4800a8 Mon Sep 17 00:00:00 2001 From: nicolas-entourage <75681929+nicolas-entourage@users.noreply.github.com> Date: Thu, 9 Jan 2025 16:30:27 +0100 Subject: [PATCH 2/2] user-mentions do not send push_notifications with html tags --- app/models/concerns/mentionable.rb | 12 ++++++++---- app/services/push_notification_trigger.rb | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/models/concerns/mentionable.rb b/app/models/concerns/mentionable.rb index f28e21317..ec7b16fa4 100644 --- a/app/models/concerns/mentionable.rb +++ b/app/models/concerns/mentionable.rb @@ -5,16 +5,20 @@ module Mentionable after_create :has_mentions!, if: :has_mentions? end + def self.no_html content + document = Nokogiri::HTML(content) + document.css('img').each { |node| node.remove } + document.css('a').each { |node| node.replace(node.text) } + document.text.strip + end + MentionsStruct = Struct.new(:instance) do def initialize(instance: nil) @instance = instance end def no_html - document = Nokogiri::HTML(@instance.content) - document.css('img').each { |node| node.remove } - document.css('a').each { |node| node.replace(node.text) } - document.text.strip + Mentionable.no_html(@instance.content) end def fragments diff --git a/app/services/push_notification_trigger.rb b/app/services/push_notification_trigger.rb index 246b33bb4..884438f02 100644 --- a/app/services/push_notification_trigger.rb +++ b/app/services/push_notification_trigger.rb @@ -26,8 +26,8 @@ def to lang return @i18ns[lang] = I18n.t(@i18n, locale: lang) % args_to(lang) if @i18n.present? if @instance.present? && @field.present? - return @i18ns[lang] = @instance.send(@field) unless @instance.respond_to?(:translation) && @instance.translation.present? - return @i18ns[lang] = @instance.translation.translate(field: @field, lang: lang) || @instance.send(@field) + return @i18ns[lang] = Mentionable.no_html(@instance.send(@field)) unless @instance.respond_to?(:translation) && @instance.translation.present? + return @i18ns[lang] = Mentionable.no_html(@instance.translation.translate(field: @field, lang: lang) || @instance.send(@field)) end @i18ns[lang] = @text % args_to(lang)