Skip to content

Commit

Permalink
Merge branch 'user-mentions' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-entourage committed Jan 9, 2025
2 parents 87bcdd6 + 9658736 commit 26ca223
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
15 changes: 11 additions & 4 deletions app/models/concerns/mentionable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,28 @@ 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, a').each { |node| node.remove }
document.text.strip
Mentionable.no_html(@instance.content)
end

def fragments
@fragments ||= Nokogiri::HTML.fragment(@instance.content)
end

def contains_html?
fragments.children.any?
fragments.children.any? { |node| node.element? }
end

def contains_anchor_with_href?
Expand Down Expand Up @@ -50,6 +55,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
Expand Down
4 changes: 2 additions & 2 deletions app/services/push_notification_trigger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
95 changes: 95 additions & 0 deletions spec/models/concerns/mentionable_spec.rb
Original file line number Diff line number Diff line change
@@ -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) { '<p>Hello, <a href="https://preprod.entourage.social/app/users/123">User</a></p>' }

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) { '<a href="https://preprod.entourage.social/app/users/456">John</a>' }

it { }
end

context 'when there are no user mentions' do
let(:content) { '<a href="https://preprod.entourage.social/app/groups/123">Group</a>' }

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) { '<a href="https://preprod.entourage.social/app/users/789">Doe</a>' }

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

0 comments on commit 26ca223

Please sign in to comment.