diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2015-06-19 11:43:35 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2015-06-19 11:43:35 +0900 |
commit | 25bbdb048c2a66bc0ef4eae2edb4ef406e023079 (patch) | |
tree | 633420885566dc4f49a31f7a1af9e983c464e527 | |
parent | a1274269eb9074d7ddecccbb810d6bc1d5f24439 (diff) | |
download | aclog-25bbdb048c2a66bc0ef4eae2edb4ef406e023079.tar.gz |
BackgroundJob: refactor TweetReponseNotificaitonJob
-rw-r--r-- | app/jobs/tweet_response_notification_job.rb | 57 | ||||
-rw-r--r-- | lib/collector/event_queue.rb | 10 |
2 files changed, 26 insertions, 41 deletions
diff --git a/app/jobs/tweet_response_notification_job.rb b/app/jobs/tweet_response_notification_job.rb index 08b7f71..1fa9c06 100644 --- a/app/jobs/tweet_response_notification_job.rb +++ b/app/jobs/tweet_response_notification_job.rb @@ -9,47 +9,38 @@ class TweetResponseNotificationJob < ActiveJob::Base def perform(tweet) return unless Settings.notification.enabled - notify_favs = -> c do - account = Account.includes(:user).where(users: { id: tweet.user_id }).first - if account && account.active? && account.notification_enabled? - notify(account.user, "#{ c }favs!", tweet.id) - end - end - last_count = Rails.cache.read("notification/tweets/#{ tweet.id }/favorites_count") + Rails.cache.write("notification/tweets/#{ tweet.id }/favorites_count", [last_count || 0, tweet.favorites_count].max) + if last_count t_count = Settings.notification.favorites.select {|m| last_count < m && m <= tweet.favorites_count }.last - if t_count - notify_favs.(t_count) - end else - if Settings.notification.favorites.include?(tweet.favorites_count) - notify_favs.(tweet.favorites_count) - end + t_count = Settings.notification.favorites.include?(tweet.favorites_count) || tweet.favorites_count end - Rails.cache.write("notification/tweets/#{ tweet.id }/favorites_count", [last_count || 0, tweet.favorites_count].max) + if t_count + notify(tweet, "#{ t_count }favs!") + end end private - def 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) + def notify(tweet, text) + user = tweet.user + account = user.account + + if account && account.active? && account.notification_enabled? + url = Rails.application.routes.url_helpers.tweet_url(host: Settings.base_url, id: tweet.id) + post("@#{ user.screen_name } #{ text } #{ url }", tweet.id) + end end - def tweet(text, reply_to = 0) - defer do + def post(text, reply_to = 0) + Settings.notification.accounts.each do |hash| begin - Settings.notification.accounts.each do |hash| - begin - client(hash).update(text, in_reply_to_status_id: reply_to) - break - rescue Twitter::Error::Forbidden => e - raise e unless e.message = "User is over daily status update limit." - end - end - rescue => e - Rails.logger.error("NOTIFICATION: #{ e.class }: #{ e.message }") + client(hash).update(text, in_reply_to_status_id: reply_to) + break + rescue Twitter::Error::Forbidden => e + raise e unless e.message = "User is over daily status update limit." end end end @@ -62,12 +53,4 @@ class TweetResponseNotificationJob < ActiveJob::Base access_token: acc.token, access_token_secret: acc.secret) end - - def defer(&blk) - if EM.reactor_running? - EM.defer &blk - else - Thread.new &blk - end - end end diff --git a/lib/collector/event_queue.rb b/lib/collector/event_queue.rb index 9860961..2686cdd 100644 --- a/lib/collector/event_queue.rb +++ b/lib/collector/event_queue.rb @@ -37,10 +37,12 @@ module Collector Retweet.delete_bulk_from_json(deletes) end - tweet_ids = favorites.map {|f| f[:target_object][:id] } - if tweet_ids.size > 0 - Tweet.where(id: tweet_ids).each do |tweet| - TweetResponseNotificationJob.perform_later(tweet) + if Settings.notification.enabled? + tweet_ids = favorites.map {|f| f[:target_object][:id] } + if tweet_ids.size > 0 + Tweet.where(id: tweet_ids).each do |tweet| + TweetResponseNotificationJob.perform_later(tweet) + end end end |