diff --git a/app/controllers/private_messages_controller.rb b/app/controllers/private_messages_controller.rb index 6a8ac1e3a..97f5db79f 100644 --- a/app/controllers/private_messages_controller.rb +++ b/app/controllers/private_messages_controller.rb @@ -6,14 +6,25 @@ class PrivateMessagesController < ApplicationController def index #用户更新私信为全部已读 + if User.current == @user || User.current.admin? + @onclick_time = User.current.onclick_time.onclick_time + User.current.onclick_time.update_attribute(:onclick_time, Time.now) + jours = PrivateMessage.find_by_sql("SELECT ui.* FROM (SELECT * FROM private_messages WHERE STATUS != 2 AND user_id = #{@user.id} ORDER BY id DESC) ui GROUP BY ui.target_id ORDER BY ui.send_time DESC") + + @unred_all_messages = 0 + @unred_private_messages = @user.unread_private_messages + # jours = PrivateMessage.without_deleted.where("user_id = ?", @user.id).order("created_at desc") + @limit = 20 + @jours_count = jours.count + @jours_all_pages = Paginator.new @jours_count, @limit, params['page'] || 1 + @offset ||= @jours_all_pages.offset + @jours_alls = paginateHelper jours, @limit + render :layout=>'base_user_message' + else + render_403 + end + - jours = PrivateMessage.without_deleted.where("user_id = ?", @user.id).order("created_at desc") - @limit = 20 - @jours_count = jours.count - @jours_all_pages = Paginator.new @jours_count, @limit, params['page'] || 1 - @offset ||= @jours_all_pages.offset - @jours_alls = paginateHelper jours, @limit - render :layout=>'base_user_message' end def new @@ -53,7 +64,7 @@ class PrivateMessagesController < ApplicationController respond_to do |format| format.js format.html{ - redirect_to user_private_message_path(current_user,@sender_message.id) + redirect_to private_messages_detail_user_private_messages_path(@user, target_ids: user_id) } end else @@ -66,29 +77,47 @@ class PrivateMessagesController < ApplicationController end end - def show - @jour_messages = @user.journals_for_messages.where(m_parent_id: @jour.id) - - render :layout=>'base_user_message' - end - def private_messages_detail - @user_message_details = @user.private_messages.where(target_id: params[:target_ids]) - @contact_user = User.find_by_id(params[:target_ids]) - render :layout=>'base_user_message' + if User.current == @user || User.current.admin? + @unred_private_messages = 0 + @unred_all_messages = 0 + @target_user = User.where(:id => params[:target_ids]).includes(:user_extensions).first + if @target_user.present? + target_user_messages = @user.private_messages.where(:target_id => params[:target_ids], :status => [0, 1]) + @message_list = target_user_messages.order("send_time asc") + target_user_messages.where(status: 0).update_all(status: 1) + @onclick_time = User.current.onclick_time.onclick_time + User.current.onclick_time.update_attribute(:onclick_time, Time.now) + @all_users = params[:all_user] || "0" + user_message_details = PrivateMessage.find_by_sql("SELECT ui.* FROM (SELECT * FROM private_messages WHERE STATUS != 2 AND user_id = #{@user.id} ORDER BY id DESC) ui GROUP BY ui.target_id ORDER BY ui.send_time DESC") + user_message_details_count = user_message_details.size + if user_message_details_count > 10 && @all_users == "0" + user_message_details = user_message_details.first(10) + end + @user_message_details = user_message_details + render :layout=>'base_user_message' + else + render_404 + end + else + render_403 + end end def destroy - + @message.update_attribute(:status, 2) end def get_recent_users - @recent_users = @user.recent_contacts.without_deleted.distinct.order("created_at desc").limit(10).includes(:user_extension) + @recent_users = User.where(:id => @user.private_messages.without_deleted.order("created_at desc").map(&:target_id)).includes(:user_extensions).limit(10) + render "get_recent_users", layout: false end - def search_users - all_search_users = User.where("LOWER(concat(lastname, ifnull(firstname, ''), login)) LIKE ?", "#{params[:user_name].strip.to_s.downcase}") + def message_search_users + @search_users = User.where("id != #{@user.id} and LOWER(concat(lastname, firstname)) LIKE '%#{params[:receiver_name].strip.to_s.downcase}%'").includes(:user_extensions) + # @search_users = User.where("LOWER(concat(lastname, ifnull(firstname, ''), login)) LIKE ?", "#{params[:receiver_name].strip.to_s.downcase}") + render "message_search_users", layout: false end @@ -99,7 +128,7 @@ class PrivateMessagesController < ApplicationController end def set_message - @jour = JournalsForMessage.find_by_id(params[:id]) + @message = PrivateMessage.find_by_id(params[:id]) end def set_type_and_count diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 49d47b441..1237be7bc 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -517,6 +517,7 @@ class UsersController < ApplicationController @message_alls = user_all_messages.where(message_type: %w(SystemMessage ContestMessage OrgMessage)) end @unred_all_messages = @user.count_new_message(@msg_type) + @unred_private_messages = 0 @message_alls = @message_alls.order("created_at desc") #点击进入tab后,全部更新为已读,全部页面除外 diff --git a/app/models/user.rb b/app/models/user.rb index 53208c515..9cc47f3f1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -354,7 +354,7 @@ class User < Principal end def get_user_contact_messages(user_id) - private_messages.where(user_id: user_id).size + private_messages.where(target_id: user_id).size end #判断是否可以点击项目或issue @@ -512,6 +512,11 @@ class User < Principal messages_count = course_count + contest_count + forge_count + user_feedback_count + user_memo_count + system_messages_count + at_count + org_count + applied_count + blog_message_count end + #未读私信数量 + def unread_private_messages + private_messages.where(status: 0).size + end + # 查询指派给我的缺陷记录 def issue_status_update self.status_updates diff --git a/app/views/layouts/base_user_message.html.erb b/app/views/layouts/base_user_message.html.erb index 1cfc28803..76edbe39d 100644 --- a/app/views/layouts/base_user_message.html.erb +++ b/app/views/layouts/base_user_message.html.erb @@ -5,6 +5,16 @@