Browse Source

Improved fetching activity stream, online notifications and recent viewed items. Improved REST API with full and expaned attributes.

Martin Edenhofer 7 years ago
parent
commit
70847f2b41

+ 22 - 1
app/assets/javascripts/app/controllers/_application_controller_generic.coffee

@@ -1171,7 +1171,7 @@ class App.CollectionController extends App.Controller
 
 class App.ObserverController extends App.Controller
   model: 'Ticket'
-  template: 'ticket_zoom/title'
+  template: 'tba'
   globalRerender: true
 
   ###
@@ -1251,3 +1251,24 @@ class App.ObserverController extends App.Controller
     #console.trace()
     @log 'debug', 'release', @object_id, @model, @subscribeId
     App[@model].unsubscribe(@subscribeId)
+
+class App.ObserverActionRow extends App.ObserverController
+  constructor: ->
+    super
+
+  render: (object) =>
+    return if _.isEmpty(object)
+    actions = @actions()
+    @html App.view('generic/actions')(
+      items: actions
+      type:  @type
+    )
+
+    for item in actions
+      do (item) =>
+        @$("[data-type=\"#{item.name}\"]").on(
+          'click'
+          (e) ->
+            e.preventDefault()
+            item.callback(object)
+        )

+ 4 - 3
app/assets/javascripts/app/controllers/_dashboard/activity_stream.coffee

@@ -27,7 +27,7 @@ class App.DashboardActivityStream extends App.CollectionController
       @ajax(
         id:    'dashoard_activity_stream'
         type:  'GET'
-        url:   "#{@apiPath}/activity_stream"
+        url:   "#{@apiPath}/activity_stream?full=true"
         data:
           limit: @limit || 8
         processData: true
@@ -37,8 +37,9 @@ class App.DashboardActivityStream extends App.CollectionController
 
   load: (data) =>
     App.SessionStorage.set('activity_stream', data)
-    @items = data.activity_stream
+    App.ActivityStream.refresh([], clear: true)
     App.Collection.loadAssets(data.assets)
+    @items = App.ActivityStream.search(sortBy: 'created_at', order: 'DESC')
     @collectionSync(@items)
 
   itemGet: (key) =>
@@ -49,7 +50,7 @@ class App.DashboardActivityStream extends App.CollectionController
     # nothing
 
   itemsAll: =>
-    @items
+    @items || []
 
   onRenderEnd: =>
     return if _.isEmpty(@items)

+ 2 - 3
app/assets/javascripts/app/controllers/navigation.coffee

@@ -448,10 +448,9 @@ class App.Navigation extends App.ControllerWidgetPermanent
     @Config.set('NavBarRight', NavBarRight)
 
   fetchRecentView: =>
-    load = (data) =>
-      App.RecentView.refresh(data.stream, clear: true)
+    load = =>
       @renderPersonal()
-    App.RecentView.fetchFull(load)
+    App.RecentView.fetchFull(load, clear: true)
 
   toggleNotifications: (e) ->
     e.stopPropagation()

+ 20 - 27
app/assets/javascripts/app/controllers/organization_profile.coffee

@@ -73,50 +73,43 @@ class App.OrganizationProfile extends App.Controller
   currentPosition: =>
     @$('.profile').scrollTop()
 
-class ActionRow extends App.ObserverController
+class ActionRow extends App.ObserverActionRow
   model: 'Organization'
   observe:
     member_ids: true
 
-  render: (organization) =>
-
-    # start action controller
-    showHistory = =>
-      new App.OrganizationHistory(
-        organization_id: organization.id
-        container: @el.closest('.content')
-      )
+  showHistory: (organization) =>
+    new App.OrganizationHistory(
+      organization_id: organization.id
+      container: @el.closest('.content')
+    )
 
-    editOrganization = =>
-      new App.ControllerGenericEdit(
-        id: organization.id
-        genericObject: 'Organization'
-        screen: 'edit'
-        pageData:
-          title: 'Organizations'
-          object: 'Organization'
-          objects: 'Organizations'
-        container: @el.closest('.content')
-      )
+  editOrganization: (organization) =>
+    new App.ControllerGenericEdit(
+      id: organization.id
+      genericObject: 'Organization'
+      screen: 'edit'
+      pageData:
+        title: 'Organizations'
+        object: 'Organization'
+        objects: 'Organizations'
+      container: @el.closest('.content')
+    )
 
+  actions: =>
     actions = [
       {
         name:     'edit'
         title:    'Edit'
-        callback: editOrganization
+        callback: @editOrganization
       }
       {
         name:     'history'
         title:    'History'
-        callback: showHistory
+        callback: @showHistory
       }
     ]
 
-    new App.ActionRow(
-      el:    @el
-      items: actions
-    )
-
 class Object extends App.ObserverController
   model: 'Organization'
   observeNot:

+ 2 - 1
app/assets/javascripts/app/controllers/ticket_overview.coffee

@@ -574,11 +574,12 @@ class App.TicketOverview extends App.Controller
       @activeFocus = 'nav'
     )
 
-    @bind 'overview:fetch', =>
+    @bind('overview:fetch', =>
       return if !@view
       update = =>
         App.OverviewListCollection.fetch(@view)
       @delay(update, 2800, 'overview:fetch')
+    )
 
   renderBatchOverlay: (elLocal) =>
     if elLocal

+ 24 - 31
app/assets/javascripts/app/controllers/user_profile.coffee

@@ -80,58 +80,51 @@ class App.UserProfile extends App.Controller
   currentPosition: =>
     @$('.profile').scrollTop()
 
-class ActionRow extends App.ObserverController
+class ActionRow extends App.ObserverActionRow
   model: 'User'
   observe:
     organization_id: true
 
-  render: (user) =>
-
-    # start action controller
-    showHistory = =>
-      new App.UserHistory(
-        user_id: user.id
-        container: @el.closest('.content')
-      )
+  showHistory: (user) =>
+    new App.UserHistory(
+      user_id: user.id
+      container: @el.closest('.content')
+    )
 
-    editUser = =>
-      new App.ControllerGenericEdit(
-        id: user.id
-        genericObject: 'User'
-        screen: 'edit'
-        pageData:
-          title: 'Users'
-          object: 'User'
-          objects: 'Users'
-        container: @el.closest('.content')
-      )
+  editUser: (user) =>
+    new App.ControllerGenericEdit(
+      id: user.id
+      genericObject: 'User'
+      screen: 'edit'
+      pageData:
+        title: 'Users'
+        object: 'User'
+        objects: 'Users'
+      container: @el.closest('.content')
+    )
 
-    newTicket = =>
-      @navigate("ticket/create/customer/#{user.id}")
+  newTicket: (user) =>
+    @navigate("ticket/create/customer/#{user.id}")
 
-    actions = [
+  actions: =>
+    [
       {
         name:     'edit'
         title:    'Edit'
-        callback: editUser
+        callback: @editUser
       }
       {
         name:     'history'
         title:    'History'
-        callback: showHistory
+        callback: @showHistory
       }
       {
         name:     'ticket'
         title:    'New Ticket'
-        callback: newTicket
+        callback: @newTicket
       }
     ]
 
-    new App.ActionRow(
-      el:    @el
-      items: actions
-    )
-
 class Object extends App.ObserverController
   model: 'User'
   observeNot:

+ 8 - 6
app/assets/javascripts/app/controllers/widget/online_notification.coffee

@@ -20,16 +20,17 @@ class App.OnlineNotificationWidget extends App.Controller
     super
 
     # at runtime if a online notifiction has changed
-    @bind 'OnlineNotification::changed', =>
+    @bind('OnlineNotification::changed', =>
       @delay(
         => @fetch()
         2200
         'online-notification-changed'
       )
+    )
 
     # after new websocket connection has been established
     @ignoreInitLogin = false
-    @bind 'ws:login', =>
+    @bind('ws:login', =>
       if @ignoreInitLogin
         @delay(
           => @fetch()
@@ -37,15 +38,17 @@ class App.OnlineNotificationWidget extends App.Controller
           'online-notification-changed'
         )
       @ignoreInitLogin = true
+    )
 
     # rebuild widget on auth
-    @bind 'auth', (user) =>
+    @bind('auth', (user) =>
       if !user
         @counterUpdate(0)
         return
       if !@access()
         @counterUpdate(0)
         return
+    )
 
     $(window).on 'click.notifications', @hide
 
@@ -150,10 +153,9 @@ class App.OnlineNotificationWidget extends App.Controller
     )
 
   fetch: =>
-    load = (data) =>
+    load = =>
       @fetchedData = true
-      App.OnlineNotification.refresh(data.stream, clear: true)
-    App.OnlineNotification.fetchFull(load)
+    App.OnlineNotification.fetchFull(load, clear: true)
 
   toggle: =>
     if @shown

+ 34 - 19
app/assets/javascripts/app/models/_application_model.coffee

@@ -273,11 +273,11 @@ set new attributes of model (remove already available attributes)
     # subscribe and reload data / fetch new data if triggered
     subscribeId = undefined
     if bind
-      subscribeId = App[ @className ].subscribeItem(id, callback)
+      subscribeId = App[@className].subscribeItem(id, callback)
 
     # execute if object already exists
-    if !force && App[ @className ].exists(id)
-      data = App[ @className ].find(id)
+    if !force && App[@className].exists(id)
+      data = App[@className].find(id)
       data = @_fillUp(data)
       if callback
         callback(data, 'full')
@@ -312,17 +312,18 @@ set new attributes of model (remove already available attributes)
 
           # find / load object
           else
-            App[ @className ].refresh(data)
+            App[@className].refresh(data)
 
           # execute callbacks
           if @FULL_CALLBACK[ data.id ]
             for key, callback of @FULL_CALLBACK[ data.id ]
-              callback( @_fillUp( App[ @className ].find(data.id) ) )
+              callback( @_fillUp( App[@className].find(data.id) ) )
               delete @FULL_CALLBACK[ data.id ][ key ]
             if _.isEmpty @FULL_CALLBACK[ data.id ]
               delete @FULL_CALLBACK[ data.id ]
 
-        error: (xhr, statusText, error) ->
+        error: (xhr, statusText, error) =>
+          @FULL_FETCH[ data.id ] = false
           App.Log.error('Model', statusText, error, url)
       )
     subscribeId
@@ -455,8 +456,8 @@ set new attributes of model (remove already available attributes)
             items = [items]
           App.Log.debug('Model', "local change #{@className}", items)
           for item in items
-            for key, callback of App[ @className ].SUBSCRIPTION_ITEM[ item.id ]
-              callback(App[ @className ]._fillUp(item), 'change')
+            for key, callback of App[@className].SUBSCRIPTION_ITEM[ item.id ]
+              callback(App[@className]._fillUp(item), 'change')
       )
       @bind(
         'destroy'
@@ -467,8 +468,8 @@ set new attributes of model (remove already available attributes)
             items = [items]
           App.Log.debug('Model', "local destroy #{@className}", items)
           for item in items
-            for key, callback of App[ @className ].SUBSCRIPTION_ITEM[ item.id ]
-              callback(App[ @className ]._fillUp(item), 'destroy')
+            for key, callback of App[@className].SUBSCRIPTION_ITEM[ item.id ]
+              callback(App[@className]._fillUp(item), 'destroy')
       )
 
       @changeTable = {}
@@ -481,7 +482,7 @@ set new attributes of model (remove already available attributes)
             items = [items]
           App.Log.debug('Model', "local refresh #{@className}", items)
           for item in items
-            for key, callback of App[ @className ].SUBSCRIPTION_ITEM[ item.id ]
+            for key, callback of App[@className].SUBSCRIPTION_ITEM[ item.id ]
 
               # only trigger callbacks if object has changed
               if !@changeTable[key] || @changeTable[key] < item.updated_at
@@ -498,8 +499,8 @@ set new attributes of model (remove already available attributes)
           App.Log.debug('Model', "server change on #{@className}.find(#{item.id}) #{item.updated_at}")
           callback = =>
             genericObject = undefined
-            if App[ @className ].exists(item.id)
-              genericObject = App[ @className ].find(item.id)
+            if App[@className].exists(item.id)
+              genericObject = App[@className].find(item.id)
             if !genericObject || new Date(item.updated_at) > new Date(genericObject.updated_at)
               App.Log.debug('Model', "request #{@className}.find(#{item.id}) from server")
               @full(item.id, false, true)
@@ -512,8 +513,8 @@ set new attributes of model (remove already available attributes)
         events
         (item) =>
           return if !@SUBSCRIPTION_ITEM || !@SUBSCRIPTION_ITEM[ item.id ]
-          return if !App[ @className ].exists(item.id)
-          genericObject = App[ @className ].find(item.id)
+          return if !App[@className].exists(item.id)
+          genericObject = App[@className].find(item.id)
           App.Log.debug('Model', "server delete on #{@className}.find(#{item.id}) #{item.updated_at}")
           callback = ->
             genericObject.trigger('destroy', genericObject)
@@ -565,16 +566,30 @@ set new attributes of model (remove already available attributes)
   @fetchFull: (callback, params = {}) ->
     url = "#{@url}/?full=true"
     App.Log.debug('Model', "fetchFull collection #{@className}", url)
+
+    # request already active, queue callback
+    queueManagerName = "#{@className}::fetchFull"
     if params.force is false && App[@className].count() isnt 0
       if callback
-        callback(App[@className].all())
+        localCallback = =>
+          callback(App[@className].all(), 'full')
+        App.QueueManager.add(queueManagerName, localCallback)
+        App.QueueManager.run(queueManagerName)
       return
 
+    if callback
+      localCallback = =>
+        callback(App[@className].all())
+      App.QueueManager.add(queueManagerName, localCallback)
+
+    return if @fetchFullActive is true
+    @fetchFullActive = true
     App.Ajax.request(
       type:  'GET'
       url:   url
       processData: true,
       success: (data, status, xhr) =>
+        @fetchFullActive = false
 
         App.Log.debug('Model', "got fetchFull collection #{@className}", data)
 
@@ -594,10 +609,10 @@ set new attributes of model (remove already available attributes)
         else
           App[@className].refresh(data)
 
-        if callback
-          callback(data)
+        App.QueueManager.run(queueManagerName)
 
-      error: (xhr, statusText, error) ->
+      error: (xhr, statusText, error) =>
+        @fetchFullActive = false
         App.Log.error('Model', statusText, error, url)
     )
 

+ 4 - 0
app/assets/javascripts/app/models/activity_stream.coffee

@@ -0,0 +1,4 @@
+class App.ActivityStream extends App.Model
+  @configure 'ActivityStream', 'name'
+  @extend Spine.Model.Ajax
+  @url: @apiPath + '/activity_steams'

+ 29 - 3
app/controllers/activity_stream_controller.rb

@@ -5,10 +5,36 @@ class ActivityStreamController < ApplicationController
 
   # GET /api/v1/activity_stream
   def show
-    activity_stream = current_user.activity_stream(params[:limit], true)
+    activity_stream = current_user.activity_stream(params[:limit])
 
-    # return result
-    render json: activity_stream
+    if response_expand?
+      list = []
+      activity_stream.each do |item|
+        list.push item.attributes_with_association_names
+      end
+      render json: list, status: :ok
+      return
+    end
+
+    if response_full?
+      assets = {}
+      item_ids = []
+      activity_stream.each do |item|
+        item_ids.push item.id
+        assets = item.assets(assets)
+      end
+      render json: {
+        record_ids: item_ids,
+        assets: assets,
+      }, status: :ok
+      return
+    end
+
+    all = []
+    activity_stream.each do |item|
+      all.push item.attributes_with_association_ids
+    end
+    render json: all, status: :ok
   end
 
 end

Some files were not shown because too many files changed in this diff