aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrhenium <rhenium@rhe.jp>2014-06-05 06:51:56 +0900
committerrhenium <rhenium@rhe.jp>2014-06-05 06:51:56 +0900
commit4dae72b60ef7cc65b0c5ed66a47630134f4fa696 (patch)
tree56785050d5272324ed725ef6f4b52de2cdcb315b
parentcacb927220c3a33d3e10963d7cb6eaabbd5ff6fa (diff)
downloadaclog-4dae72b60ef7cc65b0c5ed66a47630134f4fa696.tar.gz
collector: don't defer notification
-rw-r--r--app/models/notification.rb31
-rw-r--r--lib/collector/event_queue.rb12
2 files changed, 20 insertions, 23 deletions
diff --git a/app/models/notification.rb b/app/models/notification.rb
index 5fc15ed..c02c5bb 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -8,9 +8,9 @@ class Notification
count = hash_or_tweet[:favorites_count]
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?
- Rails.cache.fetch("notification/tweets/#{ id }/favorites/#{ count }") do
+ 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?
notify(account.user, "#{ count }favs!", id)
true
end
@@ -27,16 +27,12 @@ class Notification
def self.tweet(text, reply_to = 0)
defer do
begin
- cur = 0
- while cur < Settings.notification.accounts.size
+ Settings.notification.accounts.each do |hash|
begin
- client(cur).update(text, in_reply_to_status_id: reply_to)
+ client(hash).update(text, in_reply_to_status_id: reply_to)
+ break
rescue Twitter::Error::Forbidden => e
- if e.message = "User is over daily status update limit."
- cur += 1
- else
- raise e
- end
+ raise e unless e.message = "User is over daily status update limit."
end
end
rescue => e
@@ -45,12 +41,13 @@ class Notification
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)
+ def self.client(acc)
+ @_client ||= {}
+ @_client[acc] ||=
+ Twitter::REST::Client.new(consumer_key: Settings.notification.consumer.key,
+ consumer_secret: Settings.notification.consumer.secret,
+ access_token: acc.token,
+ access_token_secret: acc.secret)
end
def self.defer(&blk)
diff --git a/lib/collector/event_queue.rb b/lib/collector/event_queue.rb
index e3e17c7..02dba68 100644
--- a/lib/collector/event_queue.rb
+++ b/lib/collector/event_queue.rb
@@ -27,6 +27,12 @@ module Collector
Tweet.destroy_bulk_from_json(queue_delete)
Retweet.delete_bulk_from_json(queue_delete)
end
+
+ queue_favorite.each do |event|
+ 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
def push_user(user)
@@ -43,12 +49,6 @@ 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