class Api::V1::ChatsController

Public Instance Methods

create() click to toggle source
# 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
index() click to toggle source
# File lib/templates/chat/chats_controller.rb, line 4
def index
  @chats = Chat.joins(:participants).where('participants.user' => current_user)
end
show() click to toggle source
# 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
visit() click to toggle source
# 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

Private Instance Methods

chat_name() click to toggle source
# File lib/templates/chat/chats_controller.rb, line 36
def chat_name
  Chat.find_or_create_by_name(infer_chat_type)
end
get_messages(chat, page) click to toggle source
# File lib/templates/chat/chats_controller.rb, line 31
def get_messages(chat, page)
  @messages = chat.messages.order_by_date.page(page)
  @messages = @messages.sort_by(&:created_at)
end
infer_chat_type() click to toggle source
# File lib/templates/chat/chats_controller.rb, line 40
def infer_chat_type
  room = params[:room]
  room.present ? room : personal_chat
end
personal_chat() click to toggle source
# File lib/templates/chat/chats_controller.rb, line 45
def personal_chat
  username = params[:user]
  current_user_name = current_user.username
  if (current_user_name <=> username).negative?
    "#{current_user_name}_#{username}"
  else
    "#{username}_#{current_user_name}"
  end
end