activity_stream.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Sessions::Backend::ActivityStream < Sessions::Backend::Base
  3. attr_writer :user
  4. def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 25) # rubocop:disable Lint/MissingSuper
  5. @user = user
  6. @client = client
  7. @client_id = client_id
  8. @ttl = ttl
  9. @asset_lookup = asset_lookup
  10. @last_change = nil
  11. end
  12. def load
  13. # get whole collection
  14. activity_stream = @user.activity_stream(25)
  15. if activity_stream && !activity_stream.first
  16. return
  17. end
  18. if activity_stream&.first && activity_stream.first['created_at'] == @last_change
  19. return
  20. end
  21. # update last changed
  22. if activity_stream&.first
  23. @last_change = activity_stream.first['created_at']
  24. end
  25. assets = {}
  26. item_ids = []
  27. activity_stream.each do |item|
  28. begin
  29. assets = item.assets(assets)
  30. rescue ActiveRecord::RecordNotFound
  31. next
  32. end
  33. item_ids.push item.id
  34. end
  35. {
  36. record_ids: item_ids,
  37. assets: assets,
  38. }
  39. end
  40. def push
  41. return if !to_run?
  42. @time_now = Time.zone.now.to_i
  43. data = load
  44. return if data.blank?
  45. if !@client
  46. return {
  47. event: 'activity_stream_rebuild',
  48. collection: 'activity_stream',
  49. data: data,
  50. }
  51. end
  52. @client.log "push activity_stream #{data.first.class} for user #{@user.id}"
  53. @client.send(
  54. event: 'activity_stream_rebuild',
  55. collection: 'activity_stream',
  56. data: data,
  57. )
  58. end
  59. end