2020-12-03 04:22:51 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::ChatMessagesController < Api::BaseController
|
|
|
|
before_action -> { authorize_if_got_token! :read, :'read:chats' }
|
|
|
|
before_action -> { doorkeeper_authorize! :write, :'write:chats' }
|
|
|
|
|
|
|
|
before_action :require_user!
|
2020-12-19 06:33:33 +00:00
|
|
|
before_action :set_chat_message, only: :destroy
|
2020-12-03 04:22:51 +00:00
|
|
|
|
|
|
|
def create
|
2020-12-16 00:31:30 +00:00
|
|
|
@chat_conversation = ChatConversation.find(chat_params[:chat_conversation_id])
|
|
|
|
@chat = PostChatMessageService.new.call(current_user.account, text: chat_params[:text], chat_conversation: @chat_conversation)
|
2020-12-03 04:22:51 +00:00
|
|
|
render json: @chat, serializer: REST::ChatMessageSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-12-19 06:33:33 +00:00
|
|
|
return not_found if @chatMessage.nil?
|
|
|
|
DeleteChatMessageService.new.call(@chatMessage)
|
|
|
|
render json: @chatMessage, serializer: REST::ChatMessageSerializer
|
2020-12-03 04:22:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def chat_params
|
|
|
|
params.permit(:text, :chat_conversation_id)
|
|
|
|
end
|
|
|
|
|
2020-12-19 06:33:33 +00:00
|
|
|
def set_chat_message
|
|
|
|
@chatMessage = ChatMessage.where(from_account: current_user.account).find(params[:id])
|
|
|
|
end
|
|
|
|
|
2020-12-03 04:22:51 +00:00
|
|
|
end
|