2016-12-22 20:00:08 +08:00
|
|
|
module ChatHelper
|
|
|
|
|
|
|
|
|
|
|
|
def get_chat_with_users user
|
2016-12-26 11:00:51 +08:00
|
|
|
# 获得与当前用户有聊天记录的用户
|
2016-12-22 20:00:08 +08:00
|
|
|
chat_with_users = {}
|
|
|
|
|
|
|
|
user.recieve_messages.where(readed: true).select(:send_user).distinct.each do |chat|
|
|
|
|
temp_user_id = chat.send_user
|
|
|
|
find_user = User.find(temp_user_id)
|
|
|
|
user_name_sym = find_user.name.to_sym
|
|
|
|
chat_with_users[user_name_sym] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
user.send_messages.select(:recieve_user).distinct.each do |chat|
|
|
|
|
temp_user_id = chat.recieve_user
|
|
|
|
find_user = User.find(temp_user_id)
|
|
|
|
user_name_sym = find_user.name.to_sym
|
|
|
|
chat_with_users[user_name_sym] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
user.recieve_messages.where(readed: false).select(:send_user).distinct.each do |chat|
|
|
|
|
temp_user_id = chat.send_user
|
|
|
|
find_user = User.find(temp_user_id)
|
|
|
|
user_name_sym = find_user.name.to_sym
|
|
|
|
chat_with_users[user_name_sym] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
return chat_with_users
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_messages(user, chat_with)
|
2016-12-26 11:00:51 +08:00
|
|
|
# 处理用户对话(消息)
|
2016-12-22 20:00:08 +08:00
|
|
|
results = []
|
|
|
|
|
|
|
|
Message.transaction do
|
|
|
|
messages = Message.lock.where(send_user: [user.id, chat_with.id],
|
2016-12-22 20:05:38 +08:00
|
|
|
recieve_user: [user.id, chat_with.id])
|
2016-12-26 11:00:51 +08:00
|
|
|
|
2016-12-22 20:05:38 +08:00
|
|
|
if messages.length >= 1
|
|
|
|
messages = messages.order(create_time: :asc)
|
2016-12-22 20:00:08 +08:00
|
|
|
messages.each do |message|
|
|
|
|
if message.send_user == user.id
|
2016-12-26 11:00:51 +08:00
|
|
|
x = {issend: true, name: user.name, userpic: user.picurl,
|
2016-12-27 10:46:28 +08:00
|
|
|
content: message.content, time: get_strftime(message.create_time)}
|
2016-12-22 20:00:08 +08:00
|
|
|
else
|
|
|
|
message.readed = true
|
|
|
|
message.save
|
2016-12-26 11:00:51 +08:00
|
|
|
x = {issend: false, name: chat_with.name, userpic: chat_with.picurl,
|
2016-12-27 10:46:28 +08:00
|
|
|
content: message.content, time: get_strftime(message.create_time)}
|
2016-12-22 20:00:08 +08:00
|
|
|
end
|
|
|
|
results << x
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
results
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_msg_num user
|
2016-12-26 11:00:51 +08:00
|
|
|
# 未读消息数目
|
2016-12-22 20:00:08 +08:00
|
|
|
user.recieve_messages.where(readed: false).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_msg_users user
|
2016-12-26 11:00:51 +08:00
|
|
|
# 未读消息的用户
|
2016-12-22 20:00:08 +08:00
|
|
|
return_user_msg = {}
|
|
|
|
results = user.recieve_messages.where(readed: false)
|
2016-12-22 20:05:38 +08:00
|
|
|
if results.length != 0
|
2016-12-22 20:00:08 +08:00
|
|
|
results.each do |result|
|
|
|
|
username = User.find(result.send_user).name
|
|
|
|
return_user_msg[username] = [];
|
|
|
|
return_user_msg[username] <<result.content;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return_user_msg
|
|
|
|
end
|
|
|
|
end
|