From bafd51e0c835eb86952347127c9b4b748cd40712 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Sun, 24 Aug 2014 01:56:45 +0200 Subject: [PATCH] Moved to support generic objects for recent viewed. --- .../_application_controller.js.coffee | 12 +++++++ .../controllers/organization_zoom.js.coffee | 4 +++ .../app/controllers/ticket_zoom.js.coffee | 8 +++-- .../app/controllers/user_zoom.js.coffee | 3 ++ app/controllers/application_controller.rb | 4 --- app/controllers/recent_viewed_controller.rb | 33 +++++++++++++++++-- app/controllers/tickets_controller.rb | 5 --- app/models/recent_view.rb | 6 ++-- config/routes/recent_viewed.rb | 3 +- test/unit/recent_view_test.rb | 8 ++--- test/unit/session_basic_test.rb | 4 +-- 11 files changed, 65 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/app/controllers/_application_controller.js.coffee b/app/assets/javascripts/app/controllers/_application_controller.js.coffee index 839b34075..215e4c301 100644 --- a/app/assets/javascripts/app/controllers/_application_controller.js.coffee +++ b/app/assets/javascripts/app/controllers/_application_controller.js.coffee @@ -446,6 +446,18 @@ class App.Controller extends Spine.Controller else fetch(params) + recentView: (object, o_id) => + params = + object: object + o_id: o_id + App.Ajax.request( + id: "recent_view_#{object}_#{o_id}" + type: 'POST' + url: @Config.get('api_path') + '/recent_viewed' + data: JSON.stringify(params) + processData: true + ) + ws_send: (data) -> App.Event.trigger( 'ws:send', JSON.stringify(data) ) diff --git a/app/assets/javascripts/app/controllers/organization_zoom.js.coffee b/app/assets/javascripts/app/controllers/organization_zoom.js.coffee index 027f370a7..86eb0a0be 100644 --- a/app/assets/javascripts/app/controllers/organization_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/organization_zoom.js.coffee @@ -34,6 +34,10 @@ class App.OrganizationZoom extends App.Controller render: (organization) => + if !@doNotLog + @doNotLog = 1 + @recentView( 'Organization', @organization_id ) + @html App.view('organization_zoom')( organization: organization ) diff --git a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee index 2e508a65c..7006e03ef 100644 --- a/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/ticket_zoom.js.coffee @@ -11,7 +11,6 @@ class App.TicketZoom extends App.Controller @ticket_id = params.ticket_id @article_id = params.article_id @signature = undefined - @doNotLog = params['doNotLog'] || 0 @key = 'ticket::' + @ticket_id cache = App.Store.get( @key ) @@ -66,7 +65,7 @@ class App.TicketZoom extends App.Controller @ajax( id: 'ticket_zoom_' + ticket_id type: 'GET' - url: @apiPath + '/ticket_full/' + ticket_id + '?do_not_log=' + @doNotLog + url: @apiPath + '/ticket_full/' + ticket_id processData: true success: (data, status, xhr) => @@ -101,7 +100,10 @@ class App.TicketZoom extends App.Controller # remove task App.TaskManager.remove( @task_key ) ) - @doNotLog = 1 + + if !@doNotLog + @doNotLog = 1 + @recentView( 'Ticket', ticket_id ) load: (data, force) => diff --git a/app/assets/javascripts/app/controllers/user_zoom.js.coffee b/app/assets/javascripts/app/controllers/user_zoom.js.coffee index a63ba95ff..f807f6739 100644 --- a/app/assets/javascripts/app/controllers/user_zoom.js.coffee +++ b/app/assets/javascripts/app/controllers/user_zoom.js.coffee @@ -34,6 +34,9 @@ class App.UserZoom extends App.Controller render: (user) => + if !@doNotLog + @doNotLog = 1 + @recentView( 'User', @user_id ) @html App.view('user_zoom')( user: user diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 848319c87..1213a3894 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -223,10 +223,6 @@ class ApplicationController < ActionController::Base return false end - def log_view (object) - RecentView.log( object, current_user ) - end - def config_frontend # config diff --git a/app/controllers/recent_viewed_controller.rb b/app/controllers/recent_viewed_controller.rb index 1f0e67594..9d4cc285a 100644 --- a/app/controllers/recent_viewed_controller.rb +++ b/app/controllers/recent_viewed_controller.rb @@ -14,15 +14,42 @@ Response: } Test: -curl http://localhost/api/v1/recent_viewed.json -v -u #{login}:#{password} -H "Content-Type: application/json" -X GET +curl http://localhost/api/v1/recent_viewed -v -u #{login}:#{password} -H "Content-Type: application/json" -X GET =end - def recent_viewed + def index recent_viewed = RecentView.list_fulldata( current_user, 10 ) # return result render :json => recent_viewed end -end +=begin + +Resource: +POST /api/v1/recent_viewed + +Payload: +{ + "object": "Ticket", + "o_id": 123, +} + +Response: +{} + +Test: +curl http://localhost/api/v1/recent_viewed -v -u #{login}:#{password} -H "Content-Type: application/json" -X POST -d '{"object": "Ticket","o_id": 123}' + +=end + + def create + + RecentView.log( params[:object], params[:o_id], current_user ) + + # return result + render :json => { :message => 'ok' } + end + +end \ No newline at end of file diff --git a/app/controllers/tickets_controller.rb b/app/controllers/tickets_controller.rb index 05c2e7837..eb5fd344a 100644 --- a/app/controllers/tickets_controller.rb +++ b/app/controllers/tickets_controller.rb @@ -239,11 +239,6 @@ class TicketsController < ApplicationController ticket = Ticket.find( params[:id] ) return if !ticket_permission( ticket ) - # log object as viewed - if !params[:do_not_log] || params[:do_not_log].to_i == 0 - log_view( ticket ) - end - # get signature signature = {} if ticket.group.signature diff --git a/app/models/recent_view.rb b/app/models/recent_view.rb index 963c441a9..ee7de2c4e 100644 --- a/app/models/recent_view.rb +++ b/app/models/recent_view.rb @@ -3,14 +3,14 @@ class RecentView < ApplicationModel belongs_to :object_lookup, :class_name => 'ObjectLookup' - def self.log( object, user ) + def self.log( object, o_id, user ) # lookups - object_lookup_id = ObjectLookup.by_name( object.class.to_s ) + object_lookup_id = ObjectLookup.by_name( object ) # create entry record = { - :o_id => object.id, + :o_id => o_id, :object_lookup_id => object_lookup_id.to_i, :created_by_id => user.id, } diff --git a/config/routes/recent_viewed.rb b/config/routes/recent_viewed.rb index ed388e9dd..7b584d5a4 100644 --- a/config/routes/recent_viewed.rb +++ b/config/routes/recent_viewed.rb @@ -1,5 +1,6 @@ Zammad::Application.routes.draw do api_path = Rails.configuration.api_path - match api_path + '/recent_viewed', :to => 'recent_viewed#recent_viewed', :via => :get + match api_path + '/recent_viewed', :to => 'recent_viewed#index', :via => :get + match api_path + '/recent_viewed', :to => 'recent_viewed#create', :via => :post end \ No newline at end of file diff --git a/test/unit/recent_view_test.rb b/test/unit/recent_view_test.rb index bcf5a10a5..299ada8db 100644 --- a/test/unit/recent_view_test.rb +++ b/test/unit/recent_view_test.rb @@ -29,13 +29,13 @@ class RecentViewTest < ActiveSupport::TestCase RecentView.user_log_destroy(user1) - RecentView.log( ticket1, user1 ) + RecentView.log( ticket1.class.to_s, ticket1.id, user1 ) sleep 1 - RecentView.log( ticket2, user1 ) + RecentView.log( ticket2.class.to_s, ticket2.id,, user1 ) sleep 1 - RecentView.log( ticket1, user1 ) + RecentView.log( ticket1.class.to_s, ticket1.id, user1 ) sleep 1 - RecentView.log( ticket1, user1 ) + RecentView.log( ticket1.class.to_s, ticket1.id, user1 ) list = RecentView.list( user1 ) assert( list[0]['o_id'], ticket1.id ) diff --git a/test/unit/session_basic_test.rb b/test/unit/session_basic_test.rb index 1875c20f7..571fbce32 100644 --- a/test/unit/session_basic_test.rb +++ b/test/unit/session_basic_test.rb @@ -261,7 +261,7 @@ class SessionBasicTest < ActiveSupport::TestCase user = User.lookup(:id => 1) ticket = Ticket.all.last - RecentView.log( ticket, user ) + RecentView.log( ticket.class.to_s, ticket.id user ) recent_viewed_client1 = Sessions::Backend::RecentViewed.new(user, false, '123-1') # get as stream @@ -278,7 +278,7 @@ class SessionBasicTest < ActiveSupport::TestCase result1 = recent_viewed_client1.push assert( !result1, "check recent_viewed - recall 2" ) - RecentView.log( ticket, user ) + RecentView.log( ticket.class.to_s, ticket.id, user ) sleep 20