2014-07-13 18:52:32 +00:00
|
|
|
class Sessions::Backend::ActivityStream
|
|
|
|
|
2015-02-25 22:07:53 +00:00
|
|
|
def initialize( user, client = nil, client_id = nil, ttl = 30 )
|
|
|
|
@user = user
|
|
|
|
@client = client
|
|
|
|
@client_id = client_id
|
|
|
|
@ttl = ttl
|
|
|
|
@last_change = nil
|
2014-07-13 18:52:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def load
|
|
|
|
|
|
|
|
# get whole collection
|
|
|
|
activity_stream = @user.activity_stream( 25 )
|
|
|
|
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
|
|
|
|
@last_change = activity_stream.first['created_at']
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
@user.activity_stream( 25, true )
|
|
|
|
end
|
|
|
|
|
|
|
|
def client_key
|
|
|
|
"as::load::#{ self.class.to_s }::#{ @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
|
|
|
|
timeout = Sessions::CacheIn.get( self.client_key )
|
|
|
|
return if timeout
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
# set new timeout
|
2015-02-25 22:07:53 +00:00
|
|
|
Sessions::CacheIn.set( self.client_key, true, { :expires_in => @ttl.seconds } )
|
2013-09-21 22:40:19 +00:00
|
|
|
|
2014-07-13 18:52:32 +00:00
|
|
|
data = self.load
|
|
|
|
|
|
|
|
return if !data||data.empty?
|
|
|
|
|
|
|
|
if !@client
|
|
|
|
return {
|
2013-09-21 22:40:19 +00:00
|
|
|
:event => 'activity_stream_rebuild',
|
2014-07-13 18:52:32 +00:00
|
|
|
:collection => 'activity_stream',
|
|
|
|
:data => data,
|
|
|
|
}
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
2014-07-13 18:52:32 +00:00
|
|
|
|
|
|
|
@client.log 'notify', "push activity_stream #{ data.first.class.to_s } for user #{ @user.id }"
|
|
|
|
@client.send({
|
|
|
|
:event => 'activity_stream_rebuild',
|
|
|
|
:collection => 'activity_stream',
|
|
|
|
:data => data,
|
|
|
|
})
|
2013-09-21 22:40:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|