Class: Api::V1::ChatsController

Inherits:
ApiController
  • Object
show all
Defined in:
lib/templates/chat/chats_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



17
18
19
20
21
# File 'lib/templates/chat/chats_controller.rb', line 17

def create
  chat = Chat.new(name: chat_name)
  chat.users << params[:users].each { |user| User.find(user) }
  chat.save!
end

#indexObject



4
5
6
# File 'lib/templates/chat/chats_controller.rb', line 4

def index
  @chats = Chat.joins(:participants).where('participants.user' => current_user)
end

#showObject



8
9
10
11
12
13
14
15
# File 'lib/templates/chat/chats_controller.rb', line 8

def show
  chat = Chat.find(params[:id])
  if !chat || chat.messages.empty?
    return render json: { messages: {} }, status: :ok
  end
  chat.mark_user_seen(current_user)
  get_messages(chat, params[:page])
end

#visitObject



23
24
25
26
27
# File 'lib/templates/chat/chats_controller.rb', line 23

def visit
  chat = Chat.find(params[:chat_id])
  participant = Participant.find_by(user: current_user, chat: chat)
  participant.touch(:last_connection)
end