Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 채팅에 room polling 추가 #133

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions back-chat/app/controllers/chats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ def list
chat_room_id: chat_room.id,
user_id: other_user_id,
user_name: users_info[other_user_id]&.name,
last_message_id: last_message&.id,
chat_room_message: last_message&.content,
chat_room_date: last_message&.timestamp&.strftime("%Y-%m-%d %H:%M")
last_message_id: last_message ? last_message.id : 0,
chat_room_message: last_message ? last_message.content : "",
chat_room_date: last_message ? last_message.timestamp.strftime("%Y-%m-%d %H:%M") : "2000-06-04 00:00"
}
end

sorted_rooms = chat_rooms_data.sort_by { |room| room[:chat_room_date] ? DateTime.parse(room[:chat_room_date]) : DateTime.new(0) }.reverse
max = chat_rooms.maximum(:id)

render_success(data: { rooms: sorted_rooms }, message: "Chat rooms retrieved successfully")
if max.nil?
max = 0
end

render_success(data: { rooms: sorted_rooms, max:max }, message: "Chat rooms retrieved successfully")
end

# 유저간 채팅방을 생성해줌
Expand Down
44 changes: 42 additions & 2 deletions back-chat/app/controllers/chats_polling_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,47 @@ class ChatsPollingController < ApplicationController

before_action :authorize_request

def short_poll
def room_poll
user_id = @decoded[:uuid]
last_room_id = params[:room_id].to_i

timeout = 10.seconds.from_now

loop do
new_rooms = ChatRoom
.where("id > ? AND (user1_uuid = ? OR user2_uuid = ?)", last_room_id, user_id, user_id)

print new_rooms.ids

new_rooms_data = new_rooms.map do |chat_room|
other_user_id = chat_room.user1_uuid == user_id ? chat_room.user2_uuid : chat_room.user1_uuid
last_message = chat_room.messages.order(timestamp: :desc).first

{
chat_room_id: chat_room.id,
user_id: other_user_id,
user_name: User.find_by(user_id: other_user_id)&.name,
last_message_id: last_message ? last_message.id : 0,
chat_room_message: last_message ? last_message.content : "",
chat_room_date: last_message ? last_message.timestamp.strftime("%Y-%m-%d %H:%M") : "2000-06-04 00:00"
}
end

if new_rooms_data.any?
sorted_contents = new_rooms_data.sort_by { |message| message[:id] }
render_success(message: "Polling Successful", data: { rooms: sorted_contents })
return
elsif Time.current >= timeout
render_fail(message: "Chat room timeout", status: :bad_request)
return
else
sleep(2)
end

end
end

def message_poll
user_id = @decoded[:uuid]

chat_room_list = params.require(:list)
Expand Down Expand Up @@ -40,7 +80,7 @@ def short_poll
render_success(message: "Polling Successful", data: { messages: sorted_contents })
return
elsif Time.current >= timeout
render_fail(message: "Chat room timeout", status: :bad_request)
render_fail(message: "Chat Message List timeout", status: :bad_request)
return
else
sleep(2)
Expand Down
3 changes: 2 additions & 1 deletion back-chat/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
get 'api/chat/list' => 'chats#list'
get 'api/chat/join/:chat_id/:message_id' => 'chats#join'
post 'api/chat/connect/:user_id' => 'chats#connect'
post 'api/chat/poll/list' => 'chats_polling#short_poll'
post 'api/chat/poll/room/:room_id' => 'chats_polling#room_poll'
post 'api/chat/poll/message' => 'chats_polling#message_poll'
post 'api/chat/poll/:chat_id/:message_id' => 'chats_polling#detail_poll'
post 'api/chat/:chat_id' => 'chats#send_message'
end