Skip to content

Commit

Permalink
user-mentions: skeleton to send push_notifications on mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-entourage committed Jan 9, 2025
1 parent 9e85c98 commit 15800a6
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/models/chat_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class ChatMessage < ApplicationRecord
include ChatServices::Spam
include ChatServices::PrivateConversation
include Deeplinkable
include Mentionable
include Translatable
include Reactionnable
include Surveyable
Expand Down
56 changes: 56 additions & 0 deletions app/models/concerns/mentionable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
module Mentionable
extend ActiveSupport::Concern

included do
after_create :has_mentions!, if: :has_mentions?
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
end

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

def contains_html?
fragments.children.any?
end

def contains_anchor_with_href?
fragments.css('a[href]').any?
end

def contains_user_link?
fragments.css('a[href]').any? { |a| a['href'].include?('app/users') }
end

def extract_user_uuid
user_links = fragments.css('a[href]').select { |a| a['href'].include?('app/users') }

user_links.map do |a|
a['href'][%r{app/users/([^/]+)}, 1]
end
end
end

def mentions
@mentions ||= MentionsStruct.new(instance: self)
end

def has_mentions?
mentions.extract_user_uuid.any?
end

def has_mentions!
# @todo perform in a job
PushNotificationTrigger.new(self, :mention, Hash.new).run
end
end
27 changes: 27 additions & 0 deletions app/services/push_notification_trigger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,33 @@ def comment_on_create
end
end

def chat_message_on_mention
return unless @record.respond_to?(:mentions)
return unless @record.mentions.respond_to?(:extract_user_uuid)

user_ids = User.where(uuid: @record.mentions.extract_user_uuid).pluck(:id).uniq

return unless user_ids.any?

puts "-- push to: #{user_ids}"

User.where(id: user_ids).find_in_batches(batch_size: 100) do |batches|
notify(
sender_id: @record.user_id,
referent: @record.messageable,
instance: @record.messageable,
users: batches,
params: {
object: I18nStruct.new(i18n: 'push_notifications.chat_message.mention', i18n_args: [username(@record.user)]),
content: I18nStruct.new(instance: @record, field: :content),
extra: {
tracking: :chat_message_on_mention
}
}
)
end
end

def user_reaction_on_create
return unless @record.respond_to?(:instance)
return unless @record.instance.respond_to?(:user_id)
Expand Down
2 changes: 2 additions & 0 deletions config/locales/entourage.fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ fr:
day_before:
title: "🔔 Rappel : «%s» a lieu demain !"
content: "N'oubliez pas, cet événement est pour demain. Est-ce que vous venez toujours ?"
chat_message:
mention: "%s vous a mentionné dans son message"
post:
create: "%s vient de partager : \"%s\""
comment:
Expand Down

0 comments on commit 15800a6

Please sign in to comment.