feat: Add the ability to mention team in private message (#11758)
This PR allows agents to mention entire teams in private messages using `@team_name` syntax. When a team is mentioned, all team members with inbox access are automatically notified. The scheme changes can be found [here](https://github.com/chatwoot/prosemirror-schema/pull/34). --------- Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
@@ -19,14 +19,33 @@ class Messages::MentionService
|
||||
end
|
||||
|
||||
def mentioned_ids
|
||||
@mentioned_ids ||= message.content.scan(%r{\(mention://(user|team)/(\d+)/(.+?)\)}).map(&:second).uniq
|
||||
user_mentions = message.content.scan(%r{\(mention://user/(\d+)/(.+?)\)}).map(&:first)
|
||||
team_mentions = message.content.scan(%r{\(mention://team/(\d+)/(.+?)\)}).map(&:first)
|
||||
|
||||
expanded_user_ids = expand_team_mentions_to_users(team_mentions)
|
||||
|
||||
(user_mentions + expanded_user_ids).uniq
|
||||
end
|
||||
|
||||
def expand_team_mentions_to_users(team_ids)
|
||||
return [] if team_ids.blank?
|
||||
|
||||
message.inbox.account.teams
|
||||
.joins(:team_members)
|
||||
.where(id: team_ids)
|
||||
.pluck('team_members.user_id')
|
||||
.map(&:to_s)
|
||||
end
|
||||
|
||||
def valid_mentionable_user_ids
|
||||
@valid_mentionable_user_ids ||= begin
|
||||
inbox = message.inbox
|
||||
inbox.account.administrators.pluck(:id) + inbox.members.pluck(:id)
|
||||
end
|
||||
end
|
||||
|
||||
def filter_mentioned_ids_by_inbox
|
||||
inbox = message.inbox
|
||||
valid_mentionable_ids = inbox.account.administrators.map(&:id) + inbox.members.map(&:id)
|
||||
# Intersection of ids
|
||||
mentioned_ids & valid_mentionable_ids.uniq.map(&:to_s)
|
||||
mentioned_ids & valid_mentionable_user_ids.map(&:to_s)
|
||||
end
|
||||
|
||||
def generate_notifications_for_mentions(validated_mentioned_ids)
|
||||
|
||||
Reference in New Issue
Block a user