Class: Chats::ReactionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/chats/reactions_controller.rb

Overview

Emoji reactions: tap-to-toggle, one endpoint.

Instance Method Summary collapse

Instance Method Details

#createObject

Toggle: adds or removes (Reaction.toggle! is race-safe through the unique index). The response re-renders the bubble for the actor; the broadcast updates everyone else.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/chats/reactions_controller.rb', line 9

def create
  conversation = find_conversation(params[:conversation_id])
  message = conversation.messages.find(params[:message_id])

  Chats::Reaction.toggle!(
    message: message,
    reactor: chats_current_messager,
    emoji: params.require(:emoji)
  )

  respond_to do |format|
    format.turbo_stream do
      render turbo_stream: turbo_stream.replace(
        message,
        partial: "chats/messages/message",
        locals: { message: message }
      )
    end
    format.html { redirect_to conversation_path(conversation) }
  end
rescue ActiveRecord::RecordInvalid
  head :unprocessable_entity
end