activity_stream.rb 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. class Sessions::Backend::ActivityStream
  2. def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 25)
  3. @user = user
  4. @client = client
  5. @client_id = client_id
  6. @ttl = ttl
  7. @asset_lookup = asset_lookup
  8. @last_change = nil
  9. end
  10. def load
  11. # get whole collection
  12. activity_stream = @user.activity_stream(25)
  13. if activity_stream && !activity_stream.first
  14. return
  15. end
  16. if activity_stream && activity_stream.first && activity_stream.first['created_at'] == @last_change
  17. return
  18. end
  19. # update last changed
  20. if activity_stream && activity_stream.first
  21. @last_change = activity_stream.first['created_at']
  22. end
  23. @user.activity_stream(25, true)
  24. end
  25. def client_key
  26. "as::load::#{self.class}::#{@user.id}::#{@client_id}"
  27. end
  28. def push
  29. # check timeout
  30. timeout = Sessions::CacheIn.get(client_key)
  31. return if timeout
  32. # set new timeout
  33. Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
  34. data = load
  35. return if !data || data.empty?
  36. if !@client
  37. return {
  38. event: 'activity_stream_rebuild',
  39. collection: 'activity_stream',
  40. data: data,
  41. }
  42. end
  43. @client.log "push activity_stream #{data.first.class} for user #{@user.id}"
  44. @client.send(
  45. event: 'activity_stream_rebuild',
  46. collection: 'activity_stream',
  47. data: data,
  48. )
  49. end
  50. end