-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add controllers for conversation participants (#6462)
Co-authored-by: Aswin Dev P.S <aswindevps@gmail.com> Co-authored-by: Sojan Jose <sojan@chatwoot.com>
- Loading branch information
1 parent
949ddf6
commit 7044eda
Showing
34 changed files
with
546 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/controllers/api/v1/accounts/conversations/participants_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class Api::V1::Accounts::Conversations::ParticipantsController < Api::V1::Accounts::Conversations::BaseController | ||
def show | ||
@participants = @conversation.conversation_participants | ||
end | ||
|
||
def create | ||
ActiveRecord::Base.transaction do | ||
@participants = participants_to_be_added_ids.map { |user_id| @conversation.conversation_participants.find_or_create_by(user_id: user_id) } | ||
end | ||
end | ||
|
||
def update | ||
ActiveRecord::Base.transaction do | ||
participants_to_be_added_ids.each { |user_id| @conversation.conversation_participants.find_or_create_by(user_id: user_id) } | ||
participants_to_be_removed_ids.each { |user_id| @conversation.conversation_participants.find_by(user_id: user_id)&.destroy } | ||
end | ||
@participants = @conversation.conversation_participants | ||
render action: 'show' | ||
end | ||
|
||
def destroy | ||
ActiveRecord::Base.transaction do | ||
params[:user_ids].map { |user_id| @conversation.conversation_participants.find_by(user_id: user_id)&.destroy } | ||
end | ||
head :ok | ||
end | ||
|
||
private | ||
|
||
def participants_to_be_added_ids | ||
params[:user_ids] - current_participant_ids | ||
end | ||
|
||
def participants_to_be_removed_ids | ||
current_participant_ids - params[:user_ids] | ||
end | ||
|
||
def current_participant_ids | ||
@current_participant_ids ||= @conversation.conversation_participants.pluck(:user_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
module UserAttributeHelpers | ||
extend ActiveSupport::Concern | ||
|
||
def available_name | ||
self[:display_name].presence || name | ||
end | ||
|
||
def availability_status | ||
current_account_user&.availability_status | ||
end | ||
|
||
def auto_offline | ||
current_account_user&.auto_offline | ||
end | ||
|
||
def inviter | ||
current_account_user&.inviter | ||
end | ||
|
||
def active_account_user | ||
account_users.order(active_at: :desc)&.first | ||
end | ||
|
||
def current_account_user | ||
# We want to avoid subsequent queries in case where the association is preloaded. | ||
# using where here will trigger n+1 queries. | ||
account_users.find { |ac_usr| ac_usr.account_id == Current.account.id } if Current.account | ||
end | ||
|
||
def account | ||
current_account_user&.account | ||
end | ||
|
||
def administrator? | ||
current_account_user&.administrator? | ||
end | ||
|
||
def agent? | ||
current_account_user&.agent? | ||
end | ||
|
||
def role | ||
current_account_user&.role | ||
end | ||
|
||
# Used internally for Chatwoot in Chatwoot | ||
def hmac_identifier | ||
hmac_key = GlobalConfig.get('CHATWOOT_INBOX_HMAC_KEY')['CHATWOOT_INBOX_HMAC_KEY'] | ||
return OpenSSL::HMAC.hexdigest('sha256', hmac_key, email) if hmac_key.present? | ||
|
||
'' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# == Schema Information | ||
# | ||
# Table name: conversation_participants | ||
# | ||
# id :bigint not null, primary key | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# account_id :bigint not null | ||
# conversation_id :bigint not null | ||
# user_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_conversation_participants_on_account_id (account_id) | ||
# index_conversation_participants_on_conversation_id (conversation_id) | ||
# index_conversation_participants_on_user_id (user_id) | ||
# index_conversation_participants_on_user_id_and_conversation_id (user_id,conversation_id) UNIQUE | ||
# | ||
class ConversationParticipant < ApplicationRecord | ||
validates :account_id, presence: true | ||
validates :conversation_id, presence: true | ||
validates :user_id, presence: true | ||
validates :user_id, uniqueness: { scope: [:conversation_id] } | ||
validate :ensure_inbox_access | ||
|
||
belongs_to :account | ||
belongs_to :conversation | ||
belongs_to :user | ||
|
||
before_validation :ensure_account_id | ||
|
||
private | ||
|
||
def ensure_account_id | ||
self.account_id = conversation&.account_id | ||
end | ||
|
||
def ensure_inbox_access | ||
errors.add(:user, 'must have inbox access') if conversation && conversation.inbox.assignable_agents.exclude?(user) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/views/api/v1/accounts/conversations/participants/create.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
json.array! @participants do |participant| | ||
json.partial! 'api/v1/models/agent', format: :json, resource: participant.user | ||
end |
Oops, something went wrong.