Class: Stay::Api::V1::MessagesController
- Inherits:
-
BaseApiController
- Object
- BaseApiController
- Stay::Api::V1::MessagesController
- Defined in:
- app/controllers/stay/api/v1/messages_controller.rb
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/controllers/stay/api/v1/messages_controller.rb', line 63 def create begin sender_id = @chat.sender_id receiver_id = @chat.receiver_id = @chat..new() .sender_id = current_devise_api_user.id .chat = @chat .event_for= Stay::Message.event_fors["both"] if .sender_id == sender_id .receiver_id = receiver_id elsif .sender_id == receiver_id .receiver_id = sender_id else return render json: { error: "Invalid sender or receiver", success: false }, status: :unprocessable_entity end return render json: { error: "You cannot send a message to yourself", success: false }, status: :unprocessable_entity if .sender_id == .receiver_id if .save ActionCable.server.broadcast "ChatChannel", render json: { data: "Message Created", chat: ActiveModelSerializers::SerializableResource.new(@chat, serializer: ChatSerializer, scope: { current_user: current_devise_api_user }), message: ActiveModelSerializers::SerializableResource.new(, each_serializer: MessageSerializer), success: true }, status: :created else render json: { error: .errors..join(", "), success: false }, status: :unprocessable_entity end rescue ActiveRecord::RecordNotFound => e render json: { error: "Chat not found: #{e.}", success: false }, status: :not_found rescue StandardError => e render json: { error: e., success: false }, status: :internal_server_error end end |
#index ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'app/controllers/stay/api/v1/messages_controller.rb', line 7 def index begin page = params[:page].to_i > 0 ? params[:page].to_i : 1 per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : 20 cumulative_limit = page * per_page @chat. .where("sender_id = :user_id OR receiver_id = :user_id", user_id: current_devise_api_user.id) .where(read_at: nil) .update_all(read_at: DateTime.now) total_count = @chat..count total_pages = (total_count.to_f / per_page).ceil = @chat. .order(created_at: :desc) .limit(cumulative_limit) = .select("DATE(created_at) as message_date, stay_messages.*") .group_by { || .created_at.to_date } = .transform_keys(&:to_s).map do |date, msgs| { date: formatted_date(date.to_date), messages: ActiveModelSerializers::SerializableResource.new(msgs, each_serializer: MessageSerializer) } end serialized_chat = ActiveModelSerializers::SerializableResource.new(@chat, serializer: ChatSerializer, scope: { current_user: current_devise_api_user }) render json: { data: "Messages Found", chat: serialized_chat, message: , success: true, meta: { total_pages: total_pages, current_page: page, next_page: page < total_pages ? page + 1 : nil, prev_page: page > 1 ? page - 1 : nil, total_count: total_count } }, status: :ok rescue StandardError => e render json: { error: e., success: false }, status: :internal_server_error end end |
#mark_as_read ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 |
# File 'app/controllers/stay/api/v1/messages_controller.rb', line 101 def mark_as_read if @message.receiver_id == current_devise_api_user.id @chat..where(receiver_id: current_devise_api_user.id).update_all(read_at: DateTime.now) render json: { message: "Message marked as read", success: true }, status: :ok elsif @message.sender_id == current_devise_api_user.id @chat..where(sender_id: current_devise_api_user.id).update_all(read_at: DateTime.now) render json: { message: "Message marked as read", success: true }, status: :ok else render json: { error: "You are not authorized to read this message" }, status: :unauthorized end end |
#new ⇒ Object
56 57 58 59 60 61 |
# File 'app/controllers/stay/api/v1/messages_controller.rb', line 56 def new @message = @chat..new render json: { data: "Message form loaded", message: @message, success: true }, status: :ok rescue StandardError => e render json: { error: e., success: false }, status: :internal_server_error end |