1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
require "msgpack/rpc/transport/unix"
class Account < ActiveRecord::Base
ACTIVE = 0; DEACTIVATED = 1
belongs_to :user
scope :active, -> { where(status: Account::ACTIVE) }
def self.create_or_update(hash)
account = where(user_id: hash[:user_id]).first_or_initialize
account.oauth_token = hash[:oauth_token]
account.oauth_token_secret = hash[:oauth_token_secret]
account.consumer_version = hash[:consumer_version]
account.status = Account::ACTIVE
account.save if account.changed?
account
end
def self.set_of_collector(collector_id)
self.active.where("id % ? = ?", Settings.collector.count, collector_id)
end
def notification?; self.notification end
def private?; self.private end
def active?; self.status == Account::ACTIVE end
def update_settings!(params)
self.notification = !!params[:notification]
self.private = !!params[:private]
self.save! if self.changed?
self
end
def deactivate!
self.status = Account::DEACTIVATED
self.save! if self.changed?
update_connection
end
def update_connection
transport = MessagePack::RPC::UNIXTransport.new
client = MessagePack::RPC::Client.new(transport, Rails.root.join("tmp", "sockets", "receiver.sock").to_s)
if self.status == Account::ACTIVE
client.call(:register, Marshal.dump(self))
elsif self.status == Account::DEACTIVATED
client.call(:unregister, Marshal.dump(self))
end
rescue Errno::ECONNREFUSED, Errno::ENOENT
Rails.logger.error($!)
end
def client
Twitter::Client.new(consumer_key: Settings.collector.consumer[consumer_version].key,
consumer_secret: Settings.collector.consumer[consumer_version].secret,
oauth_token: oauth_token,
oauth_token_secret: oauth_token_secret)
end
def import_favorites(id)
result = client.status_activity(id)
# favs ユーザー一覧回収
Favorite.from_tweet_object(result)
# favs ユーザー回収
client.users(result.favoriters).each do |u|
User.from_user_object(u)
end
# rts 回収・RTのステータスIDを取得する必要がある
client.retweets(id, count: 100).each do |status|
Retweet.from_tweet_object(status)
end
end
def following?(target_user_id)
api_friendship?(user_id, target_user_id)
end
def followed_by?(source_user_id)
api_friendship?(source_user_id, user_id)
end
private
def api_friendship?(source_user_id, target_user_id)
return nil unless source_user_id && source_user_id.is_a?(Integer)
return nil unless target_user_id && target_user_id.is_a?(Integer)
Rails.cache.fetch("friendship/#{source_user_id}-#{target_user_id}", expires_in: 3.days) do
client.friendship?(source_user_id, target_user_id) rescue nil
end
end
end
|