Skip to content

Commit

Permalink
Add full mentions system and keys. Update docs a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
John Metta committed Mar 15, 2012
1 parent 095c683 commit fca5bca
Show file tree
Hide file tree
Showing 8 changed files with 265 additions and 113 deletions.
19 changes: 18 additions & 1 deletion README.md
Expand Up @@ -99,7 +99,7 @@ Followers are notified by an arbitrary string key that can literally be whatever
to

```
<namespace>:<activity_scope>:<streamable_object_id> <time> <activity_id>
<namespace>:<class.name.tableize.singularize>:<streamable_object_id>:<activity_scope> <time> <activity_id>
```

where ```streamable_object_id``` is the value of ```activity_attr```, both of which are set in the configuration options.
Expand All @@ -116,6 +116,23 @@ where ```follower_key``` is the key used above, and activity_id is the id of the

The following system allows for time sorted organization of activities, and ability to see all "viewers" of an activity.

### Mentions

Unless ```:ignore_mentions``` is provided to the acts_as_stream method, mentioned objects can be notified if they were "discussed" in activity. The key
pattern for this is

```
<namespace>:<class.name.tableize.singularize>:<streamable_object_id>:<mentions_scope> <time> <activity_id>
```

and the list by id is
```
<namespace>:<activity_scope>:<mentions_key>:<activity_id> <mentioned_key>
```

The attribute ```mentions_scope``` is provided in the ActsAsStream configuration and defaults to ```:mentions```. This can
be overwritten by sending :mentions_scope as an attribute to acts_as_stream.

## Configuration

## Future Plans
Expand Down
7 changes: 6 additions & 1 deletion lib/acts_as_stream/configuration.rb
Expand Up @@ -9,7 +9,8 @@ module Configuration
:activity_scope,
:activity_attr,
:page_size,
:activity_incr
:activity_incr,
:mentions_scope

def configure
yield self
Expand Down Expand Up @@ -45,5 +46,9 @@ def activity_incr
@activity_incr
end

def mentions_scope
@mentions_scope ||= :mentions
end

end
end
28 changes: 28 additions & 0 deletions lib/acts_as_stream/connector.rb
Expand Up @@ -20,6 +20,7 @@ def deregister_activity! ident
ActsAsStream.redis.zrem "#{base_key}:sorted", ident
end
deregister_followers! ident
deregister_mentioned! ident
end

def register_followers! options = {}
Expand Down Expand Up @@ -48,6 +49,33 @@ def deregister_followers! activity_id = nil
end
end

def register_mentions! options = {}
options.assert_valid_keys(:mentioned_keys, :activity_id)
return if options[:mentioned_keys].nil? or options[:activity_id].nil?
raise ":mentioned_keys must be an array of keys" if not options[:mentioned_keys].is_a? Array
ActsAsStream.redis.multi do
options[:mentioned_keys].each do |key|
ActsAsStream.redis.zadd key, Time.now.to_f, options[:activity_id]
ActsAsStream.redis.lpush "#{base_key}:mentions:#{options[:activity_id]}", key
end
end
end

def deregister_mentioned! activity_id = nil
return if activity_id.nil?
mentioned_key = "#{base_key}:mentions:#{activity_id}"
return if mentioned_key.nil?
len = ActsAsStream.redis.llen mentioned_key
mentioned = ActsAsStream.redis.lrange mentioned_key, 0, len
return if mentioned.nil?
ActsAsStream.redis.multi do
mentioned.each do |f|
ActsAsStream.redis.zrem f, activity_id
end
ActsAsStream.redis.del "#{base_key}:mentions:#{activity_id}"
end
end

def get_activity_for follower_key, options = {}
options = {:page => 1, :page_size => ActsAsStream.page_size}.merge options
options[:page] = 1 if options[:page] < 1
Expand Down
18 changes: 16 additions & 2 deletions lib/acts_as_stream/streamable_object.rb
Expand Up @@ -8,12 +8,16 @@ def self.included base
module ClassMethods

def acts_as_stream options = {}
cattr_accessor :activity_scope, :activity_attr, :activity_key_base, :followers_attr
cattr_accessor :activity_scope, :activity_attr, :activity_key_base, :followers_attr, :mentions_scope
self.activity_scope = options[:activity_scope] || ActsAsStream.activity_scope
self.mentions_scope = options[:mentions_scope] || ActsAsStream.mentions_scope
self.activity_attr = options[:activity_attr] || ActsAsStream.activity_attr
self.activity_key_base = "#{ActsAsStream.namespace}:#{self.activity_scope}"
self.followers_attr = options[:followers_attr] || :all_followers
send :include, ActsAsStream::StreamableObject::InstanceMethods
unless options[:ignore_mentions]
send :include, ActsAsStream::StreamableObject::MentionsMethods
end
end
end

Expand All @@ -31,10 +35,10 @@ def activity_key
def following_key
"#{ActsAsStream.namespace}:#{self.class.name.tableize.singularize}:#{streamable_object_id}:#{activity_scope}"
end

def register_activity! package
act_id = ActsAsStream.register_new_activity! package
self.register_followers! act_id
act_id
end

def delete_activity act_id
Expand Down Expand Up @@ -75,5 +79,15 @@ def activity_count
end

end

module MentionsMethods
def register_mentions!(options = {})
options[:mentioned_keys] = [options[:mentioned_keys]] unless options[:mentioned_keys].is_a?(Array)
ActsAsStream.register_mentions! :mentioned_keys => options[:mentioned_keys], :activity_id => options[:activity_id]
end
def mentions_key
"#{ActsAsStream.namespace}:#{self.class.name.tableize.singularize}:#{streamable_object_id}:#{mentions_scope}"
end
end
end
end

0 comments on commit fca5bca

Please sign in to comment.