diff options
author | rhenium <rhenium@rhe.jp> | 2015-02-02 08:33:10 +0900 |
---|---|---|
committer | rhenium <rhenium@rhe.jp> | 2015-02-02 08:33:10 +0900 |
commit | 368918a11d185f31879265a493773c9f7c20c25a (patch) | |
tree | bd1d4ad8f04501b35998bcb3ed2aa05979055986 | |
parent | b2700774e306da9311ea5c11a874a49e7aaa91f9 (diff) | |
download | aclog-368918a11d185f31879265a493773c9f7c20c25a.tar.gz |
notification: don't miss notifying even if 50th favorite was from protected users
* cache last favorite_count
-rw-r--r-- | app/models/notification.rb | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/app/models/notification.rb b/app/models/notification.rb index 075b320..435a07c 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,9 +1,12 @@ class Notification # Notifies the count of favovorites for the tweet with tweeting a reply from notification account. # Notification will be send only when the count reached the specified number in settings.yml. + # THIS METHOD IS NOT THREAD SAFE # # @param [Hash, Tweet] hash_or_tweet the target tweet. def self.try_notify_favorites(hash_or_tweet) + return unless Settings.notification.enabled + if hash_or_tweet.is_a?(Tweet) hash_or_tweet = hash_or_tweet.attributes end @@ -11,15 +14,26 @@ class Notification user_id = hash_or_tweet[:user_id] count = hash_or_tweet[:favorites_count] - if Settings.notification.enabled && Settings.notification.favorites.include?(count) - Rails.cache.fetch("notification/tweets/#{ id }/favorites/#{ count }") do - account = Account.includes(:user).where(users: { id: user_id }).first - if account && account.active? && account.notification_enabled? - notify(account.user, "#{ count }favs!", id) - true - end + notify_favs = -> c do + account = Account.includes(:user).where(users: { id: user_id }).first + if account && account.active? && account.notification_enabled? + notify(account.user, "#{ c }favs!", id) + end + end + + last_count = Rails.cache.read("notification/tweets/#{ id }/favorites_count") + if last_count + t_count = Settings.notification.favorites.select {|m| last_count < m && m <= count }.last + if t_count + notify_favs.(t_count) + end + else + if Settings.notification.favorites.include?(count) + notify_favs.(count) end end + + Rails.cache.write("notification/tweets/#{ id }/favorites_count", [last_count || 0, count].max) end private |