Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cu 866a1r6kk | Code refactoring #313

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
16 changes: 9 additions & 7 deletions app/channels/chat_channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ class ChatChannel < ApplicationCable::Channel
def subscribed
stream_from case params[:type]
when 'profiles'
conversation = BenchConversation.profile_to_profile_conversation(params[:id], params[:current_profile_id])
@conversation = BenchConversation.profile_to_profile_conversation(params[:id], params[:current_profile_id])

if conversation.blank?
conversation = BenchConversation.new(conversationable_type: 'Profile', conversationable_id: params[:id],
sender_id: params[:current_profile_id])
conversation.save
end
create_conversation if @conversation.blank?

"ChatChannelProfile#{conversation.conversationable_id}-#{conversation.sender_id}"
"ChatChannelProfile#{@conversation.conversationable_id}-#{@conversation.sender_id}"
when 'groups'
"ChatChannelGroup#{params[:id]}"
when 'channels'
Expand All @@ -25,4 +21,10 @@ def receive(data)
message: data['message'].upcase
})
end

def create_conversation
@conversation = BenchConversation.new(conversationable_type: 'Profile', conversationable_id: params[:id],
sender_id: params[:current_profile_id])
@conversation.save!
end
end
5 changes: 3 additions & 2 deletions app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class Api::ApiController < ApplicationController
private

def set_workspace_in_session
if session[:current_workspace_id].nil?
current_workspace_id = session[:current_workspace_id]
if current_workspace_id.nil?
render json: { success: false, error: t('api.no_workspace') }, status: :unprocessable_entity
else
Current.workspace = Workspace.find_by(id: session[:current_workspace_id])
Current.workspace = Workspace.find_by(id: current_workspace_id)
end
end

Expand Down
65 changes: 9 additions & 56 deletions app/controllers/api/v1/bench_channels_controller.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
class Api::V1::BenchChannelsController < Api::ApiController
include Pagination
include CanAuthorization
include SearchChannels

before_action :set_bench_channel, :authenticate_channel, only: %i[show update destroy leave_channel]
before_action :set_channel_participant, only: :leave_channel
before_action :bench_channel_cannot_be_public_again, only: :update

def index
@bench_channels = current_workspace.bench_channels

if params[:query].present?
@bench_channels = BenchChannel.search(params[:query], where: { workspace_id: current_workspace.id },
match: :word_start)
@bench_channels = BenchChannel.where(id: @bench_channels.map(&:id))
end

filter_bench_channels
hide_profile_bench_channels
sort_bench_channels if params[:sort_by].present?
@bench_channels = search_channels(search_channel_params)
paginate_bench_channels
end

def show; end

def create
@bench_channel = BenchChannel.new(bench_channel_params)

ActiveRecord::Base.transaction do
@bench_channel.save!
create_first_bench_channel_participant
Expand All @@ -48,7 +38,6 @@ def destroy

def leave_channel
ActiveRecord::Base.transaction do
@channel_participant.update!(left_on: DateTime.current)
@channel_participant.destroy!
InfoMessagesCreatorService.new(@bench_channel.bench_conversation.id).left_channel(@bench_channel.name)
end
Expand All @@ -61,13 +50,17 @@ def joined_channels

private

def search_channel_params
params.permit(:query, :filter, :hide_my_channels, :sort_by, :page)
end

def bench_channel_params
params.require(:bench_channel).permit(:name, :description, :is_private)
end

def create_first_bench_channel_participant
BenchConversation.create!(conversationable_type: 'BenchChannel', conversationable_id: @bench_channel.id)
@bench_channel.channel_participants.create!(bench_channel_id: @bench_channel.id, profile_id: current_profile.id)
BenchConversation.create!(conversationable: @bench_channel)
@bench_channel.channel_participants.create!(profile_id: current_profile.id)
end

def set_bench_channel
Expand All @@ -85,48 +78,8 @@ def bench_channel_cannot_be_public_again
render json: { success: false, error: t('.failure') }, status: :bad_request
end

def sort_bench_channels
sort_methods = ActiveSupport::HashWithIndifferentAccess.new({
'newest' => -> { sort_by_bench_channels('created_at', true) },
'oldest' => -> { sort_by_bench_channels('created_at', false) },
'most_participants' => -> { sort_by_participants(true) },
'fewest_participants' => -> { sort_by_participants(false) },
'a_to_z' => -> { sort_by_bench_channels('name', false) },
'z_to_a' => -> { sort_by_bench_channels('name', true) }
})
raise t('.failure') unless sort_methods.key?(params[:sort_by])

sort_methods[params[:sort_by]].call
end

def filter_bench_channels
return if params[:filter].blank?

@bench_channels = case params[:filter]
when 'private'
@bench_channels.get_private_channels
when 'public'
@bench_channels.get_public_channels
end
end

def hide_profile_bench_channels
@bench_channels = @bench_channels.hide_participated_channels(current_profile.bench_channel_ids) if params[:hide_my_channels].eql?('true')

@bench_channels = BenchChannel.reject_unjoined_privated_channels(@bench_channels)
end

def paginate_bench_channels
@pagy, @bench_channels = pagination_for_bench_channels(@bench_channels, params[:page] || 1)
end

def sort_by_bench_channels(sort_by_param, reverse)
@bench_channels = @bench_channels.order(sort_by_param => (reverse ? :desc : :asc))
end

def sort_by_participants(desc)
order_keyword = desc ? 'DESC' : 'ASC'
@bench_channels = @bench_channels.left_joins(:channel_participants).group(:id).order("count(channel_participants) #{order_keyword}")
@pagy, @bench_channels = pagination_for_bench_channels(@bench_channels, search_channel_params[:page] || 1)
end

def authenticate_channel
Expand Down
25 changes: 4 additions & 21 deletions app/controllers/api/v1/channel_participants_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Api::V1::ChannelParticipantsController < Api::ApiController
before_action :set_bench_channel, only: %i[index create destroy mute_unmute_channel invite_outsider]
before_action :set_bench_channel, only: %i[index create destroy mute_unmute_channel]
before_action :set_channel_paticipant, only: %i[destroy mute_unmute_channel]
before_action :check_profile_ids, :pluck_name_of_participants, only: %i[create]
before_action :set_and_authorize_channel, only: %i[join_public_channel]
Expand All @@ -20,7 +20,7 @@ def index
def create
ActiveRecord::Base.transaction do
params[:profile_ids].each do |profile_id|
ChannelParticipant.create!(bench_channel_id: @bench_channel.id, profile_id: profile_id, permission: true)
@bench_channel.channel_participants.create!(profile_id: profile_id, permission: true)
end
InfoMessagesCreatorService.new(@bench_channel.bench_conversation.id).add_members_in_channel(@users_joined, params[:profile_ids][0])
end
Expand All @@ -30,14 +30,13 @@ def create
def destroy
ActiveRecord::Base.transaction do
@channel_participant.destroy!
create_user_left_message_in_channel
InfoMessagesCreatorService.new(@channel.bench_conversation.id).left_channel(@bench_channel.name)
end

render json: { success: true, message: t('.success') }, status: :ok
end

def join_public_channel
@channel_participant = ChannelParticipant.new(bench_channel_id: @bench_channel.id, profile_id: current_profile.id, permission: true)
@channel_participant = @bench_channel.channel_participants.new(profile_id: current_profile.id, permission: true)
ActiveRecord::Base.transaction do
@channel_participant.save!
InfoMessagesCreatorService.new(@bench_channel.bench_conversation.id).join_public_channel
Expand All @@ -50,12 +49,6 @@ def mute_unmute_channel
render json: { success: true, message: t('.success') }, status: :ok
end

def invite_outsider
@token = Token.new.generate
ChannelMailer.send_email(params[:email], @bench_channel, @token).deliver!
render json: { success: true, message: t('.success'), company_name: @bench_channel.workspace.company_name }, status: :ok
end

private

def set_bench_channel
Expand Down Expand Up @@ -84,14 +77,4 @@ def set_and_authorize_channel
def authorize_channel_participant
authorize! :destroy, @channel_participant
end

def create_user_left_message_in_channel
ConversationMessage.create!(
content: %({"blocks":[{"type":"section","text":{"type":"mrkdwn","text":"#{I18n.t('application.services.left_message')} #{@channel.name}"}}]}),
is_threaded: false,
bench_conversation_id: @channel.bench_conversation_id,
sender_id: @channel_participant.profile_id,
is_info: true
)
end
end
76 changes: 27 additions & 49 deletions app/controllers/api/v1/conversation_messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,15 @@ class Api::V1::ConversationMessagesController < Api::ApiController
before_action :authenticate, only: %i[bench_channel_messages group_messages]
after_action :marked_chat_read, only: %i[bench_channel_messages profile_messages group_messages]

def sent_message
@pagy, @sent_messages = pagination_for_sent_messages(params[:page])
end

def reactions_and_mentions
@messages = mentioned_messages + current_profile.conversation_messages.messages_with_other_reactions(current_profile)
end

def create
if params[:scheduled_at].blank?
@message = @bench_conversation.conversation_messages.new(conversation_messages_params)
authorize! :create, @message

ActiveRecord::Base.transaction do
@message.save!

if params[:profile_list].present?
params[:profile_list].each do |profile_id|
@message.mentions.create!(mentionable_type: 'Profile', mentionable_id: profile_id)
end
end
end

@message.save!
create_mention if params[:profile_list].present?
render json: { success: true, message: t('.success') }, status: :ok
else
@schedule_message = @bench_conversation.schedule_messages.new(schedule_messages_params)
authorize! :create, @schedule_message
@schedule_message.save!
create_schedule_message
end
end

Expand All @@ -50,16 +31,24 @@ def update

def destroy
if delete_parent_message?
delete_reply_and_parent_message
@message.delete_reply_and_parent
elsif soft_delete_message?
soft_delete_message
@message.soft_delete
else
@message.destroy!
end

render json: { success: true, message: t('.success') }, status: :ok
end

def sent_message
@pagy, @sent_messages = pagination_for_sent_messages(params[:page])
end

def reactions_and_mentions
@messages = mentioned_messages + current_profile.conversation_messages.messages_with_other_reactions(current_profile)
end

def recent_files
@messages = current_profile.conversation_messages.includes(:profile, :reactions).with_attached_message_attachments
end
Expand Down Expand Up @@ -88,15 +77,10 @@ def threads

def profile_messages
@conversation = BenchConversation.profile_to_profile_conversation(current_profile.id, @receiver.id)
create_conversation if @conversation.blank?
create_direct_messages
create_direct_message_users
paginate_messages
end

def create_conversation
@conversation = BenchConversation.create(conversationable_type: 'Profile', conversationable_id: @receiver.id, sender_id: current_profile.id)
end

def unread_messages
str = REDIS.get("unreadMessages#{current_workspace.id}#{current_profile.id}")
@previous_unread_messages_details = str.nil? ? {} : JSON.parse(str)
Expand Down Expand Up @@ -162,34 +146,28 @@ def delete_parent_message?
@message.parent_message&.content.eql?(t('.delete_text')) && @message.parent_message.replies.count.eql?(1)
end

def delete_reply_and_parent_message
ActiveRecord::Base.transaction do
@message.pin&.destroy!
@message.destroy!
@message.parent_message.destroy!
end
end

def soft_delete_message?
@message.parent_message_id.blank? && @message.replies.count.positive?
end

def soft_delete_message
ActiveRecord::Base.transaction do
@message.pin&.destroy!
@message.reactions&.delete_all
@message.saved_items&.delete_all
@message.message_attachments&.delete_all
@message.update!(content: t('.delete_text'))
end
end

def create_direct_messages
def create_direct_message_users
current_profile.direct_message_users.find_or_create_by!(receiver_id: @receiver.id)
@receiver.direct_message_users.find_or_create_by!(receiver_id: current_profile.id)
end

def mentioned_messages
ConversationMessage.where(id: current_profile.mentions.pluck(:conversation_message_id)).where.not(sender_id: current_profile.id)
end

def create_mention
params[:profile_list].each do |profile_id|
@message.mentions.create!(mentionable_type: 'Profile', mentionable_id: profile_id)
end
end

def create_schedule_message
@schedule_message = @bench_conversation.schedule_messages.new(schedule_messages_params)
authorize! :create, @schedule_message
@schedule_message.save!
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/direct_message_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Api::V1::DirectMessageUsersController < Api::ApiController
before_action :set_recent_message_users, only: %i[recent_direct_messages]

def index
@profiles = DirectMessageUser.dm_profiles(@direct_message_users_ids)
@profiles = DirectMessageUser.dm_list_profiles(@direct_message_users_ids)
end

def destroy
Expand Down
Loading