From a87df693bfdc4280f1d135dfad236fc5c5d0a44c Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Sat, 17 Oct 2015 03:07:52 +0800 Subject: [PATCH] changes for sockets --- app/controllers/admin/tickets_controller.rb | 8 +++- app/models/ticket.rb | 6 ++- app/views/admin/tickets/show.html.erb | 43 +++++++++++---------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/app/controllers/admin/tickets_controller.rb b/app/controllers/admin/tickets_controller.rb index cc78ddc..1ec146c 100644 --- a/app/controllers/admin/tickets_controller.rb +++ b/app/controllers/admin/tickets_controller.rb @@ -204,6 +204,12 @@ class Admin::TicketsController < OrbitAdminController rescue e end end + url = "/admin/tickets/#{tq.ticket.id.to_s}#response_#{response.id.to_s}" + Fiber.new{ + params[:user_tags].each do |user_id| + WebsocketRails["tag_notifications_for_#{user_id}".to_sym].trigger(:new_notification_recieved, {"ticket_link" => "#{tq.ticket.subject}", "total_ticket_count" => Ticket.get_ticket_unread_count_for_user(user_id), "blink_message" => "#{current_user.name} tagged you!"}) + end + }.resume end render :partial => "response", :object => response else @@ -216,7 +222,7 @@ class Admin::TicketsController < OrbitAdminController if !response.nil? response.read_by << current_user.id.to_s if !response.read_by.include?(current_user.id.to_s) response.save - data = {"success" => true, "user_id" => current_user.id.to_s} + data = {"success" => true, "user_id" => current_user.id.to_s, "total_ticket_count" => Ticket.get_ticket_unread_count_for_user(current_user)} else data = {"success" => false} end diff --git a/app/models/ticket.rb b/app/models/ticket.rb index e1ccff9..91c6c3e 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -47,7 +47,11 @@ class Ticket end def self.get_ticket_unread_count_for_user(user) - tqrs = TicketQueryResponse.where(:user_tags.in => [user.id.to_s], :read_by.nin => [user.id.to_s]) + if user.instance_of?(User) + tqrs = TicketQueryResponse.where(:user_tags.in => [user.id.to_s], :read_by.nin => [user.id.to_s]) + elsif user.instance_of?(String) + tqrs = TicketQueryResponse.where(:user_tags.in => [user], :read_by.nin => [user]) + end unread_tickets = [] tqrs.each do |tqr| unread_tickets << tqr.ticket_query.ticket if !tqr.ticket_query.nil? && !tqr.ticket_query.ticket.nil? && tqr.ticket_query.ticket.status != "closed" diff --git a/app/views/admin/tickets/show.html.erb b/app/views/admin/tickets/show.html.erb index 3dfa37b..a611cf3 100644 --- a/app/views/admin/tickets/show.html.erb +++ b/app/views/admin/tickets/show.html.erb @@ -261,7 +261,7 @@ $(document).ready(function(){ }, placeholder: "Tag Members" }); - + readbyFunction(); }) $(".reopen-back-btn").on("click",function(){ for (instance in CKEDITOR.instances) { @@ -363,28 +363,31 @@ $(document).ready(function(){ } }) + var readbyFunction = function(){ + var responses_seen = $("div[data-user-tags*=" + user_id + "]:in-viewport"); + $.each(responses_seen,function(){ + var el = $(this), + avatar = $("div[data-scroll-id=response_" + el.data("response-id") + "]").find("img[data-avatar-id="+ user_id +"]"); + if(!avatar.hasClass("read")){ + $.ajax({ + url : "/admin/comments/read", + dataType : "json", + type : "post", + data : {"response_id" : el.data("response-id")} + }).done(function(data){ + if(data.success){ + avatar.addClass("read"); + notificationManager.setNotificationNumber(data.total_ticket_count); + } + }) + } + }) + } + var timer = null; $(document).on("scroll",function(){ clearTimeout(timer); - timer = setTimeout(function(){ - var responses_seen = $("div[data-user-tags*=" + user_id + "]:in-viewport"); - $.each(responses_seen,function(){ - var el = $(this), - avatar = $("div[data-scroll-id=response_" + el.data("response-id") + "]").find("img[data-avatar-id="+ user_id +"]"); - if(!avatar.hasClass("read")){ - $.ajax({ - url : "/admin/comments/read", - dataType : "json", - type : "post", - data : {"response_id" : el.data("response-id")} - }).done(function(data){ - if(data.success){ - avatar.addClass("read"); - } - }) - } - }) - },1000); + timer = setTimeout(readbyFunction,1000); }) })();