2014-07-13 18:52:32 +00:00
|
|
|
class Sessions::Backend::ActivityStream
|
|
|
|
|
2016-03-03 08:56:13 +00:00
|
|
|
def initialize(user, asset_lookup, client = nil, client_id = nil, ttl = 25)
|
|
|
|
@user = user
|
|
|
|
@client = client
|
|
|
|
@client_id = client_id
|
|
|
|
@ttl = ttl
|
|
|
|
@asset_lookup = asset_lookup
|
|
|
|
@last_change = nil
|
2014-07-13 18:52:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
|
|
|
|
|
|
|
# get whole collection
|
2016-03-01 14:26:46 +00:00
|
|
|
activity_stream = @user.activity_stream(25)
|
2014-07-13 18:52:32 +00:00
|
|
|
if activity_stream && !activity_stream.first
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if activity_stream && activity_stream.first && activity_stream.first['created_at'] == @last_change
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# update last changed
|
|
|
|
if activity_stream && activity_stream.first
|
2015-04-30 17:14:51 +00:00
|
|
|
@last_change = activity_stream.first['created_at']
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2016-03-01 14:26:46 +00:00
|
|
|
@user.activity_stream(25, true)
|
2014-07-13 18:52:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def client_key
|
2015-07-03 15:18:01 +00:00
|
|
|
"as::load::#{self.class}::#{@user.id}::#{@client_id}"
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
def push
|
|
|
|
|
|
|
|
# check timeout
|
2016-03-01 14:26:46 +00:00
|
|
|
timeout = Sessions::CacheIn.get(client_key)
|
2014-07-13 18:52:32 +00:00
|
|
|
return if timeout
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# set new timeout
|
2016-03-01 14:26:46 +00:00
|
|
|
Sessions::CacheIn.set(client_key, true, { expires_in: @ttl.seconds })
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
data = load
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2015-04-27 14:41:03 +00:00
|
|
|
return if !data || data.empty?
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
if !@client
|
|
|
|
return {
|
2015-04-27 13:42:53 +00:00
|
|
|
event: 'activity_stream_rebuild',
|
|
|
|
collection: 'activity_stream',
|
|
|
|
data: data,
|
2014-07-13 18:52:32 +00:00
|
|
|
}
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
|
2015-07-03 15:18:01 +00:00
|
|
|
@client.log "push activity_stream #{data.first.class} for user #{@user.id}"
|
2015-04-27 15:21:17 +00:00
|
|
|
@client.send(
|
2015-04-27 13:42:53 +00:00
|
|
|
event: 'activity_stream_rebuild',
|
|
|
|
collection: 'activity_stream',
|
|
|
|
data: data,
|
2015-04-27 15:21:17 +00:00
|
|
|
)
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|