diff options
author | rhenium <rhenium@rhe.jp> | 2014-07-18 06:06:10 +0900 |
---|---|---|
committer | rhenium <rhenium@rhe.jp> | 2014-07-18 06:06:10 +0900 |
commit | 2e0b9ea57de549ca452fb557d3dc684a03d70aa4 (patch) | |
tree | b940b5418fa96d005f414814390d8abfa05d221d /app/models | |
parent | 057b07f072b02dc22dae4b82aed5c4e9483f5b10 (diff) | |
download | aclog-2e0b9ea57de549ca452fb557d3dc684a03d70aa4.tar.gz |
refactor models
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/account.rb | 41 | ||||
-rw-r--r-- | app/models/tweet.rb | 45 | ||||
-rw-r--r-- | app/models/user.rb | 23 |
3 files changed, 51 insertions, 58 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index bb6b9bd..d3c23ab 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -7,33 +7,26 @@ class Account < ActiveRecord::Base scope :for_node, ->(block_number) { active.where("id % ? = ?", Settings.collector.nodes_count, block_number) } def notification_enabled?; notification_enabled end + def deactivate!; self.inactive! end + + class << self + def register(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.status = :active + account.save! if account.changed? + end - 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.status = :active - account.save if account.changed? - account - end - - def self.random - self.active.order("RAND()").first - end - - def deactivate! - self.inactive! - - WorkerManager.update_account(self) - rescue Aclog::Exceptions::WorkerConnectionError + def random + active.order("RAND()").first + end end def verify_token! - begin - client.user - rescue - self.revoked! - end + client.user + rescue + self.revoked! end def client @@ -44,7 +37,7 @@ class Account < ActiveRecord::Base end def following?(target_id) - target_id = target_id.id if target_id.is_a? User + target_id = target_id.id if target_id.is_a?(User) friends.member? target_id end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index b5cf06e..76cf56c 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -37,26 +37,20 @@ class Tweet < ActiveRecord::Base } class << self - def initialize_from_json(json, ignore_relation: false) - tweet = self.new(id: json[:id], - text: extract_entities(json), - source: json[:source], - tweeted_at: Time.parse(json[:created_at]), - in_reply_to_id: json[:in_reply_to_status_id], - favorites_count: json[:favorite_count], - retweets_count: json[:retweet_count], - reactions_count: json[:favorite_count] + json[:retweet_count]) - if ignore_relation - tweet.user_id = json[:user][:id] - else - tweet.user = User.initialize_from_json(json[:user]) - end - - tweet + def build_from_json(json) + self.new(id: json[:id], + text: extract_entities(json), + source: json[:source], + tweeted_at: Time.parse(json[:created_at]), + user_id: json[:user][:id], + in_reply_to_id: json[:in_reply_to_status_id], + favorites_count: json[:favorite_count], + retweets_count: json[:retweet_count], + reactions_count: json[:favorite_count] + json[:retweet_count]) end def create_bulk_from_json(array) - objects = array.map {|json| self.initialize_from_json(json, ignore_relation: true) } + objects = array.map {|json| build_from_json(json) } self.import(objects, on_duplicate_key_update: [:favorites_count, :retweets_count, :reactions_count]) end @@ -72,13 +66,15 @@ class Tweet < ActiveRecord::Base st = client.status(id) self.create_bulk_from_json([st.attrs]) + User.create_or_update_from_json(st.attrs[:user]) + tweet = self.find(st.id) tweet.update(text: extract_entities(st.attrs), source: st.attrs[:source], in_reply_to_id: (tweet.in_reply_to_id || st.attrs[:in_reply_to_status_id])) nt = tweet - nt = self.create_bulk_from_json([client.status(nt.in_reply_to_id)]) while !nt.in_reply_to && nt.in_reply_to_id + nt = self.build_from_json(client.status(nt.in_reply_to_id).attrs).save! while nt.in_reply_to_id && !nt.in_reply_to tweet.reload end @@ -89,9 +85,8 @@ class Tweet < ActiveRecord::Base escape_text = -> str do str.gsub(/#(\d+)/) { strings[$1.to_i] } - .gsub("%", "\\%") + .gsub(/(_|%)/) {|x| "\\" + x } .gsub("*", "%") - .gsub("_", "\\_") .gsub("?", "_") end @@ -146,7 +141,7 @@ class Tweet < ActiveRecord::Base end def twitter_url - "https://twitter.com/#{self.user.screen_name}/status/#{self.id}" + "https://twitter.com/#{user.screen_name}/status/#{self.id}" end def reply_ancestors(max_level = Float::INFINITY) @@ -154,11 +149,11 @@ class Tweet < ActiveRecord::Base node = self level = 0 - while node.in_reply_to && level < max_level + while level < max_level && node.in_reply_to nodes << (node = node.in_reply_to) level += 1 end - nodes + nodes.reverse end def reply_descendants(max_level = Float::INFINITY) @@ -166,10 +161,10 @@ class Tweet < ActiveRecord::Base c_nodes = [self] level = 0 - while c_nodes.size > 0 && level < max_level + while level < max_level && c_nodes.size > 0 nodes.concat(c_nodes.map! {|node| node.replies }.flatten!) level += 1 end - nodes.sort_by {|t| t.id } + nodes.sort_by(&:id) end end diff --git a/app/models/user.rb b/app/models/user.rb index 40936e8..e008619 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,16 +18,21 @@ class User < ActiveRecord::Base key && where(key => value).order(updated_at: :desc).first || raise(ActiveRecord::RecordNotFound, "Couldn't find User with #{key}=#{value}") end - def create_or_update_bulk_from_json(array) - objects = array.map do |json| - self.new(id: json[:id], - screen_name: json[:screen_name], - name: json[:name], - profile_image_url: json[:profile_image_url_https] || json[:profile_image_url], - protected: json[:protected]) - end + def build_from_json(json) + self.new(id: json[:id], + screen_name: json[:screen_name], + name: json[:name], + profile_image_url: json[:profile_image_url_https] || json[:profile_image_url], + protected: json[:protected]) + end - self.import(objects, on_duplicate_key_update: [:screen_name, :name, :profile_image_url, :protected]) + def create_or_update_from_json(json) + import([build_from_json(json)], on_duplicate_key_update: [:screen_name, :name, :profile_image_url, :protected]) + end + + def create_or_update_bulk_from_json(array) + objects = array.map {|json| build_from_json(json) } + import(objects, on_duplicate_key_update: [:screen_name, :name, :profile_image_url, :protected]) end end |