From 877fe577f0a203401127ee1ce6dba2c7b2947f0d Mon Sep 17 00:00:00 2001
From: Joe <820839844@qq.com>
Date: Tue, 29 Nov 2016 00:10:16 +0800
Subject: [PATCH] third version
---
果仁/.idea/workspace.xml | 503 +++++++++---------
果仁/app/controllers/chat_controller.rb | 38 +-
果仁/app/helpers/chat_helper.rb | 49 +-
果仁/app/views/chat/index.html.erb | 91 +++-
果仁/app/views/layouts/_nav.html.erb | 34 +-
果仁/{bin => }/config/application.rb | 0
果仁/{bin => }/config/boot.rb | 0
果仁/{bin => }/config/cable.yml | 0
果仁/{bin => }/config/database.yml | 0
果仁/{bin => }/config/environment.rb | 0
.../config/environments/development.rb | 0
.../{bin => }/config/environments/production.rb | 0
果仁/{bin => }/config/environments/test.rb | 0
.../application_controller_renderer.rb | 0
果仁/{bin => }/config/initializers/assets.rb | 0
.../config/initializers/backtrace_silencers.rb | 0
.../config/initializers/cookies_serializer.rb | 0
.../initializers/filter_parameter_logging.rb | 0
.../config/initializers/inflections.rb | 0
.../{bin => }/config/initializers/mime_types.rb | 0
.../initializers/new_framework_defaults.rb | 0
.../config/initializers/session_store.rb | 0
.../config/initializers/wrap_parameters.rb | 0
果仁/{bin => }/config/locales/en.yml | 0
果仁/{bin => }/config/puma.rb | 0
果仁/{bin => }/config/routes.rb | 3 +-
果仁/{bin => }/config/secrets.yml | 0
果仁/{bin => }/config/spring.rb | 0
28 files changed, 392 insertions(+), 326 deletions(-)
rename 果仁/{bin => }/config/application.rb (100%)
rename 果仁/{bin => }/config/boot.rb (100%)
rename 果仁/{bin => }/config/cable.yml (100%)
rename 果仁/{bin => }/config/database.yml (100%)
rename 果仁/{bin => }/config/environment.rb (100%)
rename 果仁/{bin => }/config/environments/development.rb (100%)
rename 果仁/{bin => }/config/environments/production.rb (100%)
rename 果仁/{bin => }/config/environments/test.rb (100%)
rename 果仁/{bin => }/config/initializers/application_controller_renderer.rb (100%)
rename 果仁/{bin => }/config/initializers/assets.rb (100%)
rename 果仁/{bin => }/config/initializers/backtrace_silencers.rb (100%)
rename 果仁/{bin => }/config/initializers/cookies_serializer.rb (100%)
rename 果仁/{bin => }/config/initializers/filter_parameter_logging.rb (100%)
rename 果仁/{bin => }/config/initializers/inflections.rb (100%)
rename 果仁/{bin => }/config/initializers/mime_types.rb (100%)
rename 果仁/{bin => }/config/initializers/new_framework_defaults.rb (100%)
rename 果仁/{bin => }/config/initializers/session_store.rb (100%)
rename 果仁/{bin => }/config/initializers/wrap_parameters.rb (100%)
rename 果仁/{bin => }/config/locales/en.yml (100%)
rename 果仁/{bin => }/config/puma.rb (100%)
rename 果仁/{bin => }/config/routes.rb (86%)
rename 果仁/{bin => }/config/secrets.yml (100%)
rename 果仁/{bin => }/config/spring.rb (100%)
diff --git a/果仁/.idea/workspace.xml b/果仁/.idea/workspace.xml
index bd0915a..29bfe3b 100644
--- a/果仁/.idea/workspace.xml
+++ b/果仁/.idea/workspace.xml
@@ -3,142 +3,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -151,7 +20,7 @@
-
+
@@ -162,11 +31,51 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
@@ -221,12 +130,12 @@
-
-
-
+
+
+
@@ -244,10 +153,9 @@
-
-
-
-
+
+
+
@@ -274,8 +182,6 @@
-
-
@@ -511,12 +417,12 @@
+
+
-
-
@@ -528,7 +434,7 @@
-
+
@@ -729,13 +635,13 @@
1480129957263
-
-
+
+
-
+
@@ -747,27 +653,26 @@
-
+
-
+
-
-
+
-
-
-
-
-
+
+
+
+
+
@@ -805,68 +710,28 @@
file://$PROJECT_DIR$/app/controllers/chat_controller.rb
- 20
-
-
-
- file://$PROJECT_DIR$/app/controllers/chat_controller.rb
- 6
-
-
-
- file://$PROJECT_DIR$/app/controllers/chat_controller.rb
- 8
-
-
-
- file://$PROJECT_DIR$/app/views/chat/index.html.erb
- 16
-
+ 9
+
-
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
@@ -878,6 +743,7 @@
+
@@ -931,7 +797,6 @@
-
@@ -939,7 +804,6 @@
-
@@ -947,6 +811,7 @@
+
@@ -964,15 +829,41 @@
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -980,11 +871,10 @@
-
-
+
-
-
+
+
@@ -992,7 +882,6 @@
-
@@ -1011,7 +900,13 @@
-
+
+
+
+
+
+
+
@@ -1030,71 +925,157 @@
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
-
-
+
+
diff --git a/果仁/app/controllers/chat_controller.rb b/果仁/app/controllers/chat_controller.rb
index 7d3e1ba..f56095c 100644
--- a/果仁/app/controllers/chat_controller.rb
+++ b/果仁/app/controllers/chat_controller.rb
@@ -9,11 +9,7 @@ class ChatController < ApplicationController
@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)
- @current_chat_user_id = User.find_by(name: @current_chat_user_name).id
- @current_messages = Message.where(send_user: [@user.id, @current_chat_user_id],
- recieve_user: [@user.id, @current_chat_user_id]).order(create_time: :asc)
- messages = process_messages(@current_messages, @user, @current_chat_user)
- p @current_chat_user_id
+ messages = process_messages(@user, @current_chat_user)
render json: messages
# else
# if !@chat_with_users.empty?
@@ -41,20 +37,40 @@ class ChatController < ApplicationController
def notify
@user = current_user
+ # unreaded_lists = unread_msg_users(@uesr)
render json: {unreaded: unread_msg_num(@user)}
end
+
+ def online
+ @user = current_user
+ results = unread_msg_users(@user)
+ if results.empty?
+ results = nil
+ end
+ render json: {users:results}
+ end
+
def query
+ @user = current_user
query_name = params[:username]
- query_user = User.find_by(name:query_name)
- if !query_user.nil?
- query_name = query_user.name
- ok = true
- else
+ all_user_names = params[:all]
+
+ 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.empty?
+ query_name = nil
+ ok = false
+ else
+ query_name = query_user.name
+ ok = true
+ end
end
- render json: {username:query_name,ok:ok}
+ render json: {username: query_name, ok: ok}
+
end
end
diff --git a/果仁/app/helpers/chat_helper.rb b/果仁/app/helpers/chat_helper.rb
index f1a8dc8..d5468f1 100644
--- a/果仁/app/helpers/chat_helper.rb
+++ b/果仁/app/helpers/chat_helper.rb
@@ -4,13 +4,13 @@ module ChatHelper
def get_chat_with_users user
chat_with_users = {}
- user.recieve_messages.where(readed:true).select(:send_user).distinct.each do |chat|
+ 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)
@@ -18,7 +18,7 @@ module ChatHelper
chat_with_users[user_name_sym] = true
end
- user.recieve_messages.where(readed:false).select(:send_user).distinct.each do |chat|
+ 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
@@ -29,16 +29,23 @@ module ChatHelper
end
- def process_messages(messages, user, chat_with)
+ def process_messages(user, chat_with)
results = []
- if !messages.nil?
- messages.each do |message|
- if message.send_user == user.id
- x = {issend: true, send: user.name, recieve: chat_with.name, content: message.content, time: message.create_time}
- else
- x = {issend: false, send: chat_with.name, recieve: user.name, content: message.content, time: message.create_time}
+
+ Message.transaction do
+ messages = Message.lock.where(send_user: [user.id, chat_with.id],
+ recieve_user: [user.id, chat_with.id]).order(create_time: :asc)
+ if !messages.empty?
+ messages.each do |message|
+ if message.send_user == user.id
+ x = {issend: true, send: user.name, recieve: chat_with.name, content: message.content, time: message.create_time}
+ else
+ message.readed = true
+ message.save
+ x = {issend: false, send: chat_with.name, recieve: user.name, content: message.content, time: message.create_time}
+ end
+ results << x
end
- results << x
end
end
results
@@ -49,7 +56,23 @@ module ChatHelper
end
def unread_msg_users user
- user.recieve_messages.where(readed: false).select(:send_user).distinct
+ # unreaded_lists = []
+ # unreaded_users = user.recieve_messages.where(readed: false).select(:send_user).distinct
+ # if !unreaded_users.empty?
+ # unreaded_users do |unreaded_id|
+ # unreaded_lists << User.find(unreaded_id).id
+ # end
+ # end
+ # unreaded_lists
+ return_user_msg = {}
+ results = user.recieve_messages.where(readed: false)
+ if results.count != 0
+ results.each do |result|
+ username = User.find(result.send_user).name
+ return_user_msg[username] = [];
+ return_user_msg[username] <' +
@@ -166,18 +164,28 @@
}
function query_user(name) {
- $.ajax({
- type: 'get',
- data: {username: name},
- dataType: 'json',
- url: '/query',
- success: function (data, textStatus, jqXHR) {
- append_user(data["username"]);
- }
+ var all_user_name = [];
+ $(".user_entry label").each(function () {
+ all_user_name.push($(this).text());
});
+
+ if (all_user_name.indexOf(name) < 0) {
+ console.log(all_user_name,name);
+ $.ajax({
+ type: 'get',
+ data: {username: name, all: all_user_name},
+ dataType: 'json',
+ url: '/query',
+ success: function (data, textStatus, jqXHR) {
+ if (data["ok"] == true) {
+ append_user(data["username"]);
+ $('.user_entry').bind("click", label_click);
+ }
+ }
+ });
+ }
return false;
}
- ;
$("#query_submit").click(function () {
$('#addUser').modal("hide");
@@ -189,6 +197,45 @@
}
});
+ $(document).ready(function () {
+ $('.user_entry').bind("click", label_click);
+ });
+
+
+ function addOnlineLable(uniquename) {
+ var yuandian = '';
+ var results = $('.user_name:contains(' + uniquename + ')');
+ for (var i = 0; i < results.length; i++) {
+ if($(results[i]).text() === uniquename ) {
+ if($(results[i]).parent().find("i").length == 0 ){
+ $(results[i]).parent().append(yuandian);
+ }
+ }
+ }
+ }
+
+ function online() {
+ $.ajax({
+ type: 'get',
+ dataType: 'json',
+ url: '/online',
+ success: function (data, textStatus, jqXHR) {
+ if (data["users"] != null) {
+ for(var key in data["users"]){
+ console.log(key);
+ addOnlineLable(key);
+ }
+ }
+ }
+ });
+ return false;
+ }
+
+
+
+ setInterval('online()', 10000);
+// clearInterval(notify_num_ticker);
+
diff --git a/果仁/app/views/layouts/_nav.html.erb b/果仁/app/views/layouts/_nav.html.erb
index ff62383..14c9351 100644
--- a/果仁/app/views/layouts/_nav.html.erb
+++ b/果仁/app/views/layouts/_nav.html.erb
@@ -49,38 +49,36 @@
\ No newline at end of file
diff --git a/果仁/bin/config/application.rb b/果仁/config/application.rb
similarity index 100%
rename from 果仁/bin/config/application.rb
rename to 果仁/config/application.rb
diff --git a/果仁/bin/config/boot.rb b/果仁/config/boot.rb
similarity index 100%
rename from 果仁/bin/config/boot.rb
rename to 果仁/config/boot.rb
diff --git a/果仁/bin/config/cable.yml b/果仁/config/cable.yml
similarity index 100%
rename from 果仁/bin/config/cable.yml
rename to 果仁/config/cable.yml
diff --git a/果仁/bin/config/database.yml b/果仁/config/database.yml
similarity index 100%
rename from 果仁/bin/config/database.yml
rename to 果仁/config/database.yml
diff --git a/果仁/bin/config/environment.rb b/果仁/config/environment.rb
similarity index 100%
rename from 果仁/bin/config/environment.rb
rename to 果仁/config/environment.rb
diff --git a/果仁/bin/config/environments/development.rb b/果仁/config/environments/development.rb
similarity index 100%
rename from 果仁/bin/config/environments/development.rb
rename to 果仁/config/environments/development.rb
diff --git a/果仁/bin/config/environments/production.rb b/果仁/config/environments/production.rb
similarity index 100%
rename from 果仁/bin/config/environments/production.rb
rename to 果仁/config/environments/production.rb
diff --git a/果仁/bin/config/environments/test.rb b/果仁/config/environments/test.rb
similarity index 100%
rename from 果仁/bin/config/environments/test.rb
rename to 果仁/config/environments/test.rb
diff --git a/果仁/bin/config/initializers/application_controller_renderer.rb b/果仁/config/initializers/application_controller_renderer.rb
similarity index 100%
rename from 果仁/bin/config/initializers/application_controller_renderer.rb
rename to 果仁/config/initializers/application_controller_renderer.rb
diff --git a/果仁/bin/config/initializers/assets.rb b/果仁/config/initializers/assets.rb
similarity index 100%
rename from 果仁/bin/config/initializers/assets.rb
rename to 果仁/config/initializers/assets.rb
diff --git a/果仁/bin/config/initializers/backtrace_silencers.rb b/果仁/config/initializers/backtrace_silencers.rb
similarity index 100%
rename from 果仁/bin/config/initializers/backtrace_silencers.rb
rename to 果仁/config/initializers/backtrace_silencers.rb
diff --git a/果仁/bin/config/initializers/cookies_serializer.rb b/果仁/config/initializers/cookies_serializer.rb
similarity index 100%
rename from 果仁/bin/config/initializers/cookies_serializer.rb
rename to 果仁/config/initializers/cookies_serializer.rb
diff --git a/果仁/bin/config/initializers/filter_parameter_logging.rb b/果仁/config/initializers/filter_parameter_logging.rb
similarity index 100%
rename from 果仁/bin/config/initializers/filter_parameter_logging.rb
rename to 果仁/config/initializers/filter_parameter_logging.rb
diff --git a/果仁/bin/config/initializers/inflections.rb b/果仁/config/initializers/inflections.rb
similarity index 100%
rename from 果仁/bin/config/initializers/inflections.rb
rename to 果仁/config/initializers/inflections.rb
diff --git a/果仁/bin/config/initializers/mime_types.rb b/果仁/config/initializers/mime_types.rb
similarity index 100%
rename from 果仁/bin/config/initializers/mime_types.rb
rename to 果仁/config/initializers/mime_types.rb
diff --git a/果仁/bin/config/initializers/new_framework_defaults.rb b/果仁/config/initializers/new_framework_defaults.rb
similarity index 100%
rename from 果仁/bin/config/initializers/new_framework_defaults.rb
rename to 果仁/config/initializers/new_framework_defaults.rb
diff --git a/果仁/bin/config/initializers/session_store.rb b/果仁/config/initializers/session_store.rb
similarity index 100%
rename from 果仁/bin/config/initializers/session_store.rb
rename to 果仁/config/initializers/session_store.rb
diff --git a/果仁/bin/config/initializers/wrap_parameters.rb b/果仁/config/initializers/wrap_parameters.rb
similarity index 100%
rename from 果仁/bin/config/initializers/wrap_parameters.rb
rename to 果仁/config/initializers/wrap_parameters.rb
diff --git a/果仁/bin/config/locales/en.yml b/果仁/config/locales/en.yml
similarity index 100%
rename from 果仁/bin/config/locales/en.yml
rename to 果仁/config/locales/en.yml
diff --git a/果仁/bin/config/puma.rb b/果仁/config/puma.rb
similarity index 100%
rename from 果仁/bin/config/puma.rb
rename to 果仁/config/puma.rb
diff --git a/果仁/bin/config/routes.rb b/果仁/config/routes.rb
similarity index 86%
rename from 果仁/bin/config/routes.rb
rename to 果仁/config/routes.rb
index ab37a04..9e445d4 100644
--- a/果仁/bin/config/routes.rb
+++ b/果仁/config/routes.rb
@@ -12,8 +12,9 @@ Rails.application.routes.draw do
get '/chat', to: 'chat#index'
post '/newmsg', to: 'chat#new'
- get '/msg', to: 'chat#notify'
+ get '/notify', to: 'chat#notify'
get '/query', to: 'chat#query'
+ get '/online',to: 'chat#online'
resource :users
diff --git a/果仁/bin/config/secrets.yml b/果仁/config/secrets.yml
similarity index 100%
rename from 果仁/bin/config/secrets.yml
rename to 果仁/config/secrets.yml
diff --git a/果仁/bin/config/spring.rb b/果仁/config/spring.rb
similarity index 100%
rename from 果仁/bin/config/spring.rb
rename to 果仁/config/spring.rb