From 521fd5e6597c8c71ebf4e3e9bb277480c6e23f76 Mon Sep 17 00:00:00 2001 From: rhenium Date: Sun, 29 Jun 2014 08:06:37 +0900 Subject: collector: change account state to revoked when recieved unauthorized --- app/models/account.rb | 8 ++++++++ lib/collector/event_queue.rb | 11 +++++++++++ lib/collector/node_connection.rb | 1 + 3 files changed, 20 insertions(+) diff --git a/app/models/account.rb b/app/models/account.rb index c7b6d90..bb6b9bd 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -28,6 +28,14 @@ class Account < ActiveRecord::Base rescue Aclog::Exceptions::WorkerConnectionError end + def verify_token! + begin + client.user + rescue + self.revoked! + end + end + def client @_client ||= Twitter::REST::Client.new(consumer_key: Settings.consumer.key, consumer_secret: Settings.consumer.secret, diff --git a/lib/collector/event_queue.rb b/lib/collector/event_queue.rb index 02dba68..bc61373 100644 --- a/lib/collector/event_queue.rb +++ b/lib/collector/event_queue.rb @@ -7,6 +7,7 @@ module Collector @queue_retweet = [] @queue_unfavorite = [] @queue_delete = [] + @queue_unauthorized = [] end def flush @@ -16,6 +17,7 @@ module Collector queue_retweet = @queue_retweet; @queue_retweet = [] queue_unfavorite = @queue_unfavorite; @queue_unfavorite = [] queue_delete = @queue_delete; @queue_delete = [] + queue_unauthorized = @queue_unauthorized; @queue_unauthorized = [] User.create_or_update_bulk_from_json(queue_user.values) Tweet.create_bulk_from_json(queue_tweet.values) @@ -33,6 +35,11 @@ module Collector user_id: event[:target_object][:user][:id], favorites_count: event[:target_object][:favorite_count]) end + + queue_unauthorized.each do |a| + account = Account.find(a[:id]) + account.verify_token! + end end def push_user(user) @@ -74,6 +81,10 @@ module Collector end end + def push_unauthorized(unauthorized) + @queue_unauthorized << unauthorized + end + private def caching(type, unique_key) @_cache ||= {} diff --git a/lib/collector/node_connection.rb b/lib/collector/node_connection.rb index 83cde04..f3da06f 100644 --- a/lib/collector/node_connection.rb +++ b/lib/collector/node_connection.rb @@ -70,6 +70,7 @@ module Collector case msg[:title] when "unauthorized" log(:info, "Received unauthorized: ##{msg[:id]}/#{msg[:user_id]}") + @queue.push_unauthorized(msg) when "tweet" log(:debug, "Received tweet: #{msg[:id]}") @queue.push_tweet(msg) -- cgit v1.2.3