-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfeed_manager.rb
64 lines (54 loc) · 1.77 KB
/
feed_manager.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
module StreamRails
# Class used to manage feeds
class FeedManager
attr_reader :client
def initialize(client, opts = {})
@client = client
@user_feed = opts[:user_feed]
@news_feeds = opts[:news_feeds]
@notification_feed = opts[:notification_feed]
end
def get_user_feed(user_id)
@client.feed(@user_feed, user_id)
end
def get_news_feeds(user_id)
Hash[@news_feeds.map { |feed_name, feed_slug| [feed_name, get_feed(feed_slug, user_id)] }]
end
def get_notification_feed(user_id)
@client.feed(@notification_feed, user_id)
end
def get_feed(feed_type, user_id)
@client.feed(feed_type, user_id)
end
def follow_user(user_id, target_id)
return unless StreamRails.enabled?
target_feed = get_user_feed(target_id)
@news_feeds.each do |_, feed|
news_feed = get_feed(feed, user_id)
news_feed.follow(target_feed.slug, target_feed.user_id)
end
end
def unfollow_user(user_id, target_id)
return unless StreamRails.enabled?
target_feed = get_user_feed(target_id)
@news_feeds.each do |_, feed|
news_feed = get_feed(feed, user_id)
news_feed.unfollow(target_feed.slug, target_feed.user_id)
end
end
def get_owner_feed(instance)
get_feed(instance.activity_owner_feed, instance.activity_owner_id)
end
def created_activity(instance)
return unless StreamRails.enabled? && instance.activity_should_sync?
activity = instance.create_activity
feed = get_owner_feed(instance)
feed.add_activity(activity)
end
def destroyed_activity(instance)
return unless StreamRails.enabled?
feed = get_owner_feed(instance)
feed.remove(instance.activity_foreign_id, true)
end
end
end