activity_stream.rb 1.5 KB

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