Module: Timeline::NotificationHelper::InstanceMethods

Defined in:
lib/timeline/notification_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_activity_to_subscribed_user(followers, activity_item) ⇒ Object



22
23
24
25
26
27
# File 'lib/timeline/notification_helper.rb', line 22

def add_activity_to_subscribed_user(followers, activity_item)
  followers.each do |follower|
    add_to_redis "user:id:#{follower.id}:notification", activity_item
    trim_notification "user:id:#{follower.id}:notification"
  end
end

#add_mentions(activity_item) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/timeline/notification_helper.rb', line 37

def add_mentions(activity_item)
  return unless @mentionable
  @mentionable.each do |mention|
    if user = @actor.class.where("coalesce(display_name, login) = ?",mention)
      add_activity_to_subscribed_user(user, activity_item)
    end
  end
end

#add_to_redis(list, activity_item) ⇒ Object



29
30
31
# File 'lib/timeline/notification_helper.rb', line 29

def add_to_redis(list, activity_item)
  Timeline.redis.lpush list, Timeline.encode(activity_item)
end

#get_unread_notification(user, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/timeline/notification_helper.rb', line 53

def get_unread_notification(user, options= {})
  result = {}
  Timeline.redis.lrange("user:id:#{user.id}:notification", options[:start] || 0, options[:end] || 10).each_with_index do |item, index|
    data = Timeline.decode(item)
    result.merge!(index => data) unless data["read"]
  end
  result
end

#set_as_read_notification(user, read, options = {}) ⇒ Object



46
47
48
49
50
51
# File 'lib/timeline/notification_helper.rb', line 46

def set_as_read_notification(user, read, options= {})
  notifications = get_unread_notification(user, options)
  notifications.each do |index, notification|
    Timeline.redis.lset("user:id:#{user.id}:notification",index, Timeline.encode(reset_read_activity(notification, read)))
  end
end

#track_notification(name, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/timeline/notification_helper.rb', line 9

def track_notification(name, options={})
  @name = name
  @actor = options[:actor]
  @object = options[:object]
  @target = options[:target]
  @followers = set_follower(options[:followers])
  @mentionable = options[:mentionable]
  @read = options[:read] || false

  add_activity_to_subscribed_user(@followers,notification_activity) if @followers.present?
  add_mentions(notification_activity)
end

#trim_notification(list) ⇒ Object



33
34
35
# File 'lib/timeline/notification_helper.rb', line 33

def trim_notification(list)
  Timeline.redis.ltrim list, 0, 29
end