guoren/app/controllers/chat_controller.rb

101 lines
2.5 KiB
Ruby
Raw Normal View History

2016-12-22 20:00:08 +08:00
class ChatController < ApplicationController
before_action :logged_in_user
include ChatHelper
def index
@user = current_user
@current_chat_user_name = params[:chat_with]
@chat_with_users = get_chat_with_users(@user)
if !@current_chat_user_name.nil?
@current_chat_user = User.find_by(name: @current_chat_user_name)
2016-12-22 20:05:38 +08:00
if !@current_chat_user_name.nil?
messages = process_messages(@user, @current_chat_user)
render json: messages
end
2016-12-22 20:00:08 +08:00
end
end
def new
2016-12-22 20:05:38 +08:00
# 用户发送新的消息
2016-12-22 20:00:08 +08:00
@user = current_user
2016-12-22 20:05:38 +08:00
username = @user.name
userpic = @user.picurl
2016-12-22 20:00:08 +08:00
msg = params[:msg]
current_chat_user_name = params[:chat_with]
current_chat_user = User.find_by(name: current_chat_user_name)
save_time = DateTime.now
2016-12-22 20:05:38 +08:00
msg_db = Message.new(content: msg, send_user: @user.id, recieve_user: current_chat_user.id,
create_time: save_time, readed: false)
2016-12-22 20:00:08 +08:00
if msg_db.save
2016-12-27 10:46:28 +08:00
render json: {username: username, userpic: userpic, content: msg, time: get_strftime(save_time)}
2016-12-22 20:00:08 +08:00
else
2016-12-27 10:46:28 +08:00
render json: {username: username, userpic: userpic, content: "error", time: get_strftime(save_time)}
2016-12-22 20:00:08 +08:00
end
2016-12-22 20:05:38 +08:00
2016-12-22 20:00:08 +08:00
end
def notify
2016-12-22 20:05:38 +08:00
# 获取未读短信数
2016-12-22 20:00:08 +08:00
@user = current_user
render json: {unreaded: unread_msg_num(@user)}
end
def online
2016-12-22 20:05:38 +08:00
# # 获取未读短信和对对应的联系人
2016-12-22 20:00:08 +08:00
@user = current_user
results = unread_msg_users(@user)
if results.empty?
results = nil
end
2016-12-22 20:05:38 +08:00
render json: {users: results}
2016-12-22 20:00:08 +08:00
end
def query
2016-12-22 20:05:38 +08:00
# 添加新的联系人
2016-12-22 20:00:08 +08:00
@user = current_user
query_name = params[:username]
all_user_names = params[:all]
2016-12-22 20:05:38 +08:00
query_pic = String.new()
2016-12-22 20:00:08 +08:00
2016-12-22 20:05:38 +08:00
if all_user_names.nil?
2016-12-22 20:00:08 +08:00
query_user = User.find_by(name: query_name)
2016-12-22 20:05:38 +08:00
if query_user.nil?
2016-12-22 20:00:08 +08:00
query_name = nil
ok = false
else
2017-01-07 11:18:42 +08:00
if query_name == @user.name
query_name = nil
ok = false
else
query_name = query_user.name
query_pic = query_user.picurl
ok = true
end
2016-12-22 20:00:08 +08:00
end
2016-12-22 20:05:38 +08:00
else
if all_user_names.include? query_name or query_name == @user.name
query_name = nil
ok = false
else
query_user = User.find_by(name: query_name)
if query_user.nil?
query_name = nil
ok = false
else
query_name = query_user.name
query_pic = query_user.picurl
ok = true
end
end
2016-12-22 20:00:08 +08:00
end
2016-12-22 20:05:38 +08:00
render json: {username: query_name, userpic: query_pic, ok: ok}
2016-12-22 20:00:08 +08:00
end
end