• Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 13 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Activity feeds backed by Redis

ActivityFeed

Activity feeds backed by Redis. Activity feeds may also be referred to as timelines or news feeds.

Compatibility

The gem has been built and tested under Ruby 1.8.7 and Ruby 1.9.3.

Installation

gem install activity_feed

or:

gem 'activity_feed'

Make sure your redis server is running! Redis configuration is outside the scope of this README, but check out the Redis documentation.

Configuration

Basic configuration options

require 'activity_feed'

ActivityFeed.configure do |configuration|
  configuration.redis = Redis.new(:host => '127.0.0.1', :port => 6379)
  configuration.namespace = 'activity_feed'
  configuration.aggregate = false
  configuration.aggregate_key = 'aggregate'
  configuration.page_size = 25
end
  • redis: The Redis connection instance to be used.
  • namespace: Namespace to isolate ActivityFeed data in Redis.
  • aggregate: Determines whether or not, by default, various calls will pull from the aggregate activity feed for a user.
  • aggregate_key: Further isolates the aggregate ActivityFeed data.
  • page_size: Number of activity feed items to be retrieved per-page.

Advanced configuration options

  • items_loader: ActivityFeed supports loading items from your ORM (e.g. ActiveRecord) or your ODM (e.g. Mongoid) when a page for a user's activity feed is requested. This option should be set to a Proc that will be called passing the item IDs as its only argument.

NOTE: The following examples developing an activity feed with Mongoid using Mongoid 3.x.

For example:

Assume you have defined a class for storing your activity feed items in Mongoid as follows:

require 'mongoid'

module ActivityFeed
  module Mongoid
    class Item
      include ::Mongoid::Document
      include ::Mongoid::Timestamps

      field :user_id, type: String
      validates_presence_of :user_id

      field :nickname, type: String
      field :type, type: String
      field :title, type: String
      field :text, type: String
      field :url, type: String
      field :icon, type: String
      field :sticky, type: Boolean

      index { user_id: 1 }

      after_save :update_item_in_activity_feed
      after_destroy :remove_item_from_activity_feed

      private

      def update_item_in_activity_feed
        ActivityFeed.update_item(self.user_id, self.id, self.updated_at.to_i)
      end

      def remove_item_from_activity_feed
        ActivityFeed.remove_item(self.user_id, self.id)
      end
    end
  end
end

You would add the following option where you are configuring ActivityFeed as follows:

ActivityFeed.items_loader = Proc.new { |ids| ActivityFeed::Mongoid::Item.where(:id.in => ids).order_by(updated_at: :desc).to_a }

If you need to handle any exceptions when loading activity feed items, please do this in the Proc.

Usage

Developing an Activity Feed for an Individual

Below is a complete example using Mongoid as our persistent storage for activity feed items. The example uses callbacks to update and remove items from the activity feed. As this example uses the updated_at time of the item, updated items will "bubble up" to the top of the activity feed.

# Configure Mongoid
require 'mongoid'

Mongoid.load!("/path/to/your/mongoid.yml", :production)

# Create a class for activity feed items
module ActivityFeed
  module Mongoid
    class Item
      include ::Mongoid::Document
      include ::Mongoid::Timestamps

      field :user_id, type: String
      validates_presence_of :user_id

      field :nickname, type: String
      field :type, type: String
      field :title, type: String
      field :text, type: String
      field :url, type: String
      field :icon, type: String
      field :sticky, type: Boolean

      index { user_id: 1 }

      after_save :update_item_in_activity_feed
      after_destroy :remove_item_from_activity_feed

      private

      def update_item_in_activity_feed
        ActivityFeed.update_item(self.user_id, self.id, self.updated_at.to_i)
      end

      def remove_item_from_activity_feed
        ActivityFeed.remove_item(self.user_id, self.id)
      end
    end
  end
end

# Configure ActivityFeed
require 'activity_feed'

ActivityFeed.configure do |configuration|
  configuration.redis = Redis.new(:host => '127.0.0.1', :port => 6379)
  configuration.namespace = 'activity_feed'
  configuration.aggregate = false
  configuration.aggregate_key = 'aggregate'
  configuration.page_size = 25
  configuration.items_loader = Proc.new { |ids| ActivityFeed::Mongoid::Item.where(:id.in => ids).order_by(updated_at: :desc).to_a }
end

# Create a couple of activity feed items
activity_item_1 = ActivityFeed::Mongoid::Item.create(
  :user_id => 'david',
  :nickname => 'David Czarnecki',
  :type => 'some_activity',
  :title => 'Great activity',
  :text => 'This is text for the activity feed item',
  :url => 'http://url.com'
)

activity_item_2 = ActivityFeed::Mongoid::Item.create(
  :user_id => 'david',
  :nickname => 'David Czarnecki',
  :type => 'some_activity',
  :title => 'Another great activity',
  :text => 'This is some other text for the activity feed item',
  :url => 'http://url.com'
)

# Pull up the activity feed
feed = ActivityFeed.feed('david', 1)
 => [#<ActivityFeed::Mongoid::Item _id: 4fe0ce26421aa91fc2000004, _type: nil, created_at: 2012-06-19 19:08:22 UTC, updated_at: 2012-06-19 19:08:22 UTC, user_id: "david", nickname: "David Czarnecki", type: "some_activity", title: "Another great activity", text: "This is some other text for the activity feed item", url: "http://url.com", icon: nil, sticky: nil>, #<ActivityFeed::Mongoid::Item _id: 4fe0ce26421aa91fc2000003, _type: nil, created_at: 2012-06-19 19:08:22 UTC, updated_at: 2012-06-19 19:08:22 UTC, user_id: "david", nickname: "David Czarnecki", type: "some_activity", title: "Great activity", text: "This is text for the activity feed item", url: "http://url.com", icon: nil, sticky: nil>]

# Update an actitivity feed item
activity_item_1.text = 'Updated some text for the activity feed item'
activity_item_1.save

# Pull up the activity feed item and notice that the item you updated has "bubbled up" to the top of the feed
feed = ActivityFeed.feed('david', 1)
 => [#<ActivityFeed::Mongoid::Item _id: 4fe0ce26421aa91fc2000003, _type: nil, created_at: 2012-06-19 19:08:22 UTC, updated_at: 2012-06-19 19:11:27 UTC, user_id: "david", nickname: "David Czarnecki", type: "some_activity", title: "Great activity", text: "Updated some text for the activity feed item", url: "http://url.com", icon: nil, sticky: nil>, #<ActivityFeed::Mongoid::Item _id: 4fe0ce26421aa91fc2000004, _type: nil, created_at: 2012-06-19 19:08:22 UTC, updated_at: 2012-06-19 19:08:22 UTC, user_id: "david", nickname: "David Czarnecki", type: "some_activity", title: "Another great activity", text: "This is some other text for the activity feed item", url: "http://url.com", icon: nil, sticky: nil>]

Developing an Aggregate Activity Feed for an Individual

# Configure Mongoid
require 'mongoid'

Mongoid.load!("/path/to/your/mongoid.yml", :production)

# Create a class for activity feed items
module ActivityFeed
  module Mongoid
    class Item
      include ::Mongoid::Document
      include ::Mongoid::Timestamps

      field :user_id, type: String
      validates_presence_of :user_id

      field :text, type: String

      after_save :update_item_in_activity_feed
      after_destroy :remove_item_from_activity_feed

      private

      def update_item_in_activity_feed
        ActivityFeed.update_item(self.user_id, self.id, self.updated_at.to_i)
      end

      def remove_item_from_activity_feed
        ActivityFeed.remove_item(self.user_id, self.id)
      end
    end
  end
end

# Configure ActivityFeed
require 'activity_feed'

ActivityFeed.configure do |configuration|
  configuration.redis = Redis.new(:host => '127.0.0.1', :port => 6379)
  configuration.namespace = 'activity_feed'
  configuration.aggregate = true
  configuration.aggregate_key = 'aggregate'
  configuration.page_size = 25
  configuration.items_loader = Proc.new { |ids| ActivityFeed::Mongoid::Item.where(:id.in => ids).order_by(updated_at: :desc).to_a }
end

# Create activity feed items for a couple of users and aggregate the activity feed items from the second user in the first user's activity feed
1.upto(5) do |index|
  ActivityFeed::Mongoid::Item.create(
    :user_id => 'david',
    :text => "This is from david's activity feed"
  )

  sleep(1) # Sleep a little so we make sure to have unique timestamps between activity feed items

  another_item = ActivityFeed::Mongoid::Item.create(
    :user_id => 'unknown',
    :text => "This is from unknown's activity feed"
  )

  sleep(1)

  ActivityFeed.aggregate_item('david', another_item.id, another_item.updated_at.to_i)
end

# Pull up the aggregate activity feed
pp feed = ActivityFeed.feed('david', 1, true)
  [#<ActivityFeed::Mongoid::Item _id: 4fe289248bb895b79500000a, _type: nil, created_at: 2012-06-21 02:38:28 UTC, updated_at: 2012-06-21 02:38:28 UTC, user_id: "unknown", text: "This is from unknown's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe289238bb895b795000009, _type: nil, created_at: 2012-06-21 02:38:27 UTC, updated_at: 2012-06-21 02:38:27 UTC, user_id: "david", text: "This is from david's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe289228bb895b795000008, _type: nil, created_at: 2012-06-21 02:38:26 UTC, updated_at: 2012-06-21 02:38:26 UTC, user_id: "unknown", text: "This is from unknown's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe289218bb895b795000007, _type: nil, created_at: 2012-06-21 02:38:25 UTC, updated_at: 2012-06-21 02:38:25 UTC, user_id: "david", text: "This is from david's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe289208bb895b795000006, _type: nil, created_at: 2012-06-21 02:38:24 UTC, updated_at: 2012-06-21 02:38:24 UTC, user_id: "unknown", text: "This is from unknown's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe2891f8bb895b795000005, _type: nil, created_at: 2012-06-21 02:38:23 UTC, updated_at: 2012-06-21 02:38:23 UTC, user_id: "david", text: "This is from david's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe2891e8bb895b795000004, _type: nil, created_at: 2012-06-21 02:38:22 UTC, updated_at: 2012-06-21 02:38:22 UTC, user_id: "unknown", text: "This is from unknown's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe2891d8bb895b795000003, _type: nil, created_at: 2012-06-21 02:38:21 UTC, updated_at: 2012-06-21 02:38:21 UTC, user_id: "david", text: "This is from david's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe2891c8bb895b795000002, _type: nil, created_at: 2012-06-21 02:38:20 UTC, updated_at: 2012-06-21 02:38:20 UTC, user_id: "unknown", text: "This is from unknown's activity feed">,
   #<ActivityFeed::Mongoid::Item _id: 4fe2891b8bb895b795000001, _type: nil, created_at: 2012-06-21 02:38:19 UTC, updated_at: 2012-06-21 02:38:19 UTC, user_id: "david", text: "This is from david's activity feed">]

ActivityFeed Caveats

ActivityFeed.remove_item can ONLY remove items from a single user's activity feed. If you allow activity feed items to be deleted from a user's activity feed, you will need to propagate that delete out to all the other feeds in which that activity feed item may have been aggregated.

ActivityFeed method summary

# Item-related

ActivityFeed.update_item(user_id, item_id, timestamp, aggregate = ActivityFeed.aggregate)
ActivityFeed.add_item(user_id, item_id, timestamp, aggregate = ActivityFeed.aggregate)

ActivityFeed.aggregate_item(user_id, item_id, timestamp)
ActivityFeed.remove_item(user_id, item_id)
ActivityFeed.check_item?(user_id, item_id, aggregate = ActivityFeed.aggregate)

# Feed-related

ActivityFeed.feed(user_id, page, aggregate = ActivityFeed.aggregate)
ActivityFeed.for(user_id, page, aggregate = ActivityFeed.aggregate)

ActivityFeed.full_feed(user_id, aggregate = ActivityFeed.aggregate)

ActivityFeed.feed_between_timestamps(user_id, starting_timestamp, ending_timestamp, aggregate = ActivityFeed.aggregate)
ActivityFeed.between(user_id, starting_timestamp, ending_timestamp, aggregate = ActivityFeed.aggregate)

ActivityFeed.total_pages_in_feed(user_id, aggregate = ActivityFeed.aggregate, page_size = ActivityFeed.page_size)
ActivityFeed.total_pages(user_id, aggregate = ActivityFeed.aggregate, page_size = ActivityFeed.page_size)

ActivityFeed.total_items_in_feed(user_id, aggregate = ActivityFeed.aggregate)
ActivityFeed.total_items(user_id, aggregate = ActivityFeed.aggregate)

ActivityFeed.trim_feed(user_id, starting_timestamp, ending_timestamp, aggregate = ActivityFeed.aggregate)
ActivityFeed.trim(user_id, starting_timestamp, ending_timestamp, aggregate = ActivityFeed.aggregate)
ActivityFeed.trim_to_size(user_id, size, aggregate = ActivityFeed.aggregate)
ActivityFeed.trim_to_(user_id, size, aggregate = ActivityFeed.aggregate)

ActivityFeed.expire_feed(user_id, seconds, aggregate = ActivityFeed.aggregate)
ActivityFeed.expire_feed_in(user_id, seconds, aggregate = ActivityFeed.aggregate)
ActivityFeed.expire_in(user_id, seconds, aggregate = ActivityFeed.aggregate)

ActivityFeed.expire_feed_at(user_id, timestamp, aggregate = ActivityFeed.aggregate)
ActivityFeed.expire_at(user_id, timestamp, aggregate = ActivityFeed.aggregate)

ActivityFeed.remove_feeds(user_id)

Contributing to ActivityFeed

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2011-2017 David Czarnecki. See LICENSE.txt for further details.

More Repositories

1

leaderboard

Leaderboards backed by Redis in Ruby
Ruby
478
star
2

kairos

Python module for time series data in Redis and Mongo
Python
207
star
3

stache

mustache and handlebars template handling in Rails 3.x and Rails 4.x
Ruby
166
star
4

haigha

AMQP Python client
Python
161
star
5

leaderboard-python

Leaderboards backed by Redis in Python
Python
156
star
6

nginx-google-oauth

Lua module to add Google OAuth to nginx
Lua
141
star
7

amico

Relationships (e.g. friendships) backed by Redis
Ruby
112
star
8

confirm-with-reveal

Replacement for window.confirm() using the Reveal modal popup plugin from ZURB Foundation.
CoffeeScript
50
star
9

bracket_tree

Tree-based Bracketing System
Ruby
49
star
10

tassadar

A Starcraft 2 replay parser written in pure Ruby
Ruby
44
star
11

torus

A service implementing the Carbon protocol and storing time series data using kairos
Python
42
star
12

chai

Mocking framework for Python
Python
40
star
13

oembedr

Lightweight, Flexible OEmbed Consumer Library
Ruby
37
star
14

php-leaderboard

Leaderboards backed by Redis in PHP
PHP
25
star
15

leaderboard-coffeescript

Leaderboards backed by Redis in CoffeeScript
CoffeeScript
24
star
16

java-leaderboard

Leaderboards backed by Redis in Java
Java
24
star
17

factory-worker

Factories for NodeJS
JavaScript
23
star
18

bnet_scraper

A Nokogiri-based scraper of Battle.net profiles. Currently this only includes Starcraft2.
Ruby
22
star
19

python-leaderboard

Leaderboards backed by Redis in Python
Python
21
star
20

halo-reach-api

Ruby gem for interacting with the Halo:Reach API
Ruby
15
star
21

soonatra

Sinatra application to show a โ€œComing Soonโ€ page and collect emails.
Ruby
14
star
22

GWFSelect-for-jQuery-UI

Google WebFont selection drop-down widget for jQuery UI.
JavaScript
13
star
23

amico-python

Relationships (e.g. friendships) backed by Redis
Python
11
star
24

php-bracket_tree

Tree-based Bracketing System
PHP
10
star
25

strumbar

Strumbar is a wrapper around ActiveSupport::Notifications with preconfigurations for basic instrumentation to be sent to statsd.
Ruby
9
star
26

scala-leaderboard

Leaderboards backed by Redis in Scala
Scala
7
star
27

vindicia-api

A wrapper for creating queries to the Vindicia CashBox API
Ruby
7
star
28

windbag

Notification System for Rails 3.1+
Ruby
6
star
29

pyevent

Python extension module for Niels Provos' libevent
Python
6
star
30

node-amico

NodeJS port of amico (Relationships (e.g. friendships) backed by Redis) using CoffeeScript.
CoffeeScript
6
star
31

ventilation

Ruby
6
star
32

improved_logging

Adds improved logging capabilities to the ActiveSupport::BufferedLogger class
Ruby
5
star
33

bracketeer

BracketTree Visual Template Creator
Ruby
4
star
34

seed_list

Seed management for tournament brackets
Ruby
3
star
35

tassadar-server

A web service interface to the tassadar Starcraft 2 replay parser
Ruby
3
star
36

leaderboard_factory

Nice little package to help you with leaderboards when you need a lot of them.
Ruby
3
star
37

py-eventsocket

Python
3
star
38

notify-campfire-multi

Notify multiple campfire rooms from a post-commit svn hook
Ruby
2
star
39

silver_spoon

Entitlements in Redis
Ruby
2
star
40

mm_sortable_item

A tiny MongoMapper plugin to provide some basic list functionality.
Ruby
2
star
41

javascripto

Client-side Javascript Application Framework
Ruby
2
star
42

saltstack-sandbox

A Vagrant-based sandbox environment for experimenting with SaltStack
2
star
43

hydra-async-demo

This is a user facing tutorial that will help guide users through the usage of Hydra Studio to support an asynchronous multiplayer game.
C#
2
star
44

read-and-write-if-nil

Pass through the value of a block to a cache key if the value is nil when it's requested
Ruby
1
star
45

ruby-openid-oauth-hybrid

ruby-openid-2.1.2 with added support for the openid-oauth hybrid extention
Ruby
1
star
46

bcms_xml_data_feed

JavaScript
1
star
47

website-middleman

Company public site and blog, rendered to static files by Middleman.
SCSS
1
star
48

gondola

Ruby
1
star
49

seedlings

Simple seed data management
Ruby
1
star
50

bcms_twitter

Twitter integration for BrowserCMS
JavaScript
1
star
51

beta

Beta access restriction library
Ruby
1
star
52

test-runner-benchmark

Benchmarking your tests
Ruby
1
star
53

action-mailer-with-temporary-delivery-method

Send email using ActionMailer but without using the templates or changing your smtp_settings
Ruby
1
star