diff options
author | rhenium <rhenium@rhe.jp> | 2014-06-04 06:47:12 +0900 |
---|---|---|
committer | rhenium <rhenium@rhe.jp> | 2014-06-04 06:47:12 +0900 |
commit | 6b29bcc7ebe34b571bc3f0f472ef23fd34667a22 (patch) | |
tree | bbddce7ea9e18c8ba4dedf5aed9d5add8f63879f | |
parent | 9c665f06fa192a3784dd9e88dd09e0705a39f4f1 (diff) | |
download | aclog-6b29bcc7ebe34b571bc3f0f472ef23fd34667a22.tar.gz |
collector: fav notification
-rw-r--r-- | app/models/notification.rb | 76 | ||||
-rw-r--r-- | lib/collector/event_queue.rb | 6 |
2 files changed, 50 insertions, 32 deletions
diff --git a/app/models/notification.rb b/app/models/notification.rb index 4d4ac36..5fc15ed 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,51 +1,63 @@ class Notification - def self.notify_favorites_count(tweet) - return unless Settings.notification.enabled + def self.try_notify_favorites(hash_or_tweet) + if hash_or_tweet.is_a?(Tweet) + hash_or_tweet = hash_or_tweet.attributes + end + id = hash_or_tweet[:id] + user_id = hash_or_tweet[:user_id] + count = hash_or_tweet[:favorites_count] - if Settings.notification.favorites.include?(tweet.reload.favorites_count) - account = Account.where(user_id: tweet.user_id).first + if Settings.notification.enabled && Settings.notification.favorites.include?(count) + account = Account.includes(:user).where(users: { id: user_id }).first if account && account.active? && account.notification? - reply_favs(tweet, tweet.favorites_count) + Rails.cache.fetch("notification/tweets/#{ id }/favorites/#{ count }") do + notify(account.user, "#{ count }favs!", id) + true + end end end end - def self.reply_favs(tweet, count) - url = Rails.application.routes.url_helpers.tweet_url(host: Settings.base_url, id: tweet.id) - tweet("@#{tweet.user.screen_name} #{count}favs! #{url}", tweet.id) + private + def self.notify(user, text, id) + url = Rails.application.routes.url_helpers.tweet_url(host: Settings.base_url, id: id) + tweet("@#{ user.screen_name } #{ text } #{ url }", id) end - private def self.tweet(text, reply_to = 0) - s = proc do - cur = Rails.cache.read("notification_account") || 0 - if Settings.notification.accounts[cur] - begin - client = Twitter::REST::Client.new(consumer_key: Settings.notification.consumer.key, - consumer_secret: Settings.notification.consumer.secret, - access_token: Settings.notification.accounts[cur].token, - access_token_secret: Settings.notification.accounts[cur].secret) - - client.update(text, in_reply_to_status_id: reply_to) - rescue Twitter::Error::AlreadyPosted - # Status is a duplicate. - rescue Exception - cur += 1 - doretry = true - Rails.logger.error($!) + defer do + begin + cur = 0 + while cur < Settings.notification.accounts.size + begin + client(cur).update(text, in_reply_to_status_id: reply_to) + rescue Twitter::Error::Forbidden => e + if e.message = "User is over daily status update limit." + cur += 1 + else + raise e + end + end end - Rails.cache.write("notification_account", cur, expires_in: 15.minutes) - tweet(text, reply_to) if doretry - else - # wait for expiring notification_account cache - #cur = 0 + rescue => e + Rails.logger.error("NOTIFICATION: #{ e.class }: #{ e.message }") end end + end + + def self.client(index) + s = Settings.notification.accounts[index] + Twitter::REST::Client.new(consumer_key: Settings.notification.consumer.key, + consumer_secret: Settings.notification.consumer.secret, + access_token: s.token, + access_token_secret: s.secret) + end + def self.defer(&blk) if EM.reactor_running? - EM.defer &s + EM.defer &blk else - Thread.new &s + Thread.new &blk end end end diff --git a/lib/collector/event_queue.rb b/lib/collector/event_queue.rb index df6a84d..e3e17c7 100644 --- a/lib/collector/event_queue.rb +++ b/lib/collector/event_queue.rb @@ -43,6 +43,12 @@ module Collector push_tweet(event[:target_object]) push_user(event[:source]) @queue_favorite << event + + EM.defer do + Notification.try_notify_favorites(id: event[:target_object][:id], + user_id: event[:target_object][:user][:id], + favorites_count: event[:target_object][:favorite_count]) + end end end |