diff options
author | Rhenium <rhenium@rhe.jp> | 2014-01-20 13:02:15 +0900 |
---|---|---|
committer | Rhenium <rhenium@rhe.jp> | 2014-01-20 13:02:15 +0900 |
commit | d282b7ef38f3cf42e8d9e4fd104228d0bf95f856 (patch) | |
tree | c182289010ed08e16fe8ebdeb25d219999ec2dea | |
parent | 2ad52ebb033394cb721f55a90af977adfb596a16 (diff) | |
download | aclog-d282b7ef38f3cf42e8d9e4fd104228d0bf95f856.tar.gz |
update models
-rw-r--r-- | app/models/account.rb | 39 | ||||
-rw-r--r-- | app/models/favorite.rb | 23 | ||||
-rw-r--r-- | app/models/retweet.rb | 21 | ||||
-rw-r--r-- | app/models/settings.rb | 1 | ||||
-rw-r--r-- | app/models/tweet.rb | 22 | ||||
-rw-r--r-- | app/models/user.rb | 39 |
6 files changed, 58 insertions, 87 deletions
diff --git a/app/models/account.rb b/app/models/account.rb index 66c1523..91fd337 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -4,11 +4,14 @@ class Account < ActiveRecord::Base ACTIVE = 0; INACTIVE = 1 belongs_to :user - scope :active, -> { where(status: Account::ACTIVE) } + alias notification? notification + alias private? private + def active?; status == Account::ACTIVE end + def self.create_or_update(hash) - account = self.where(user_id: hash[:user_id]).first_or_initialize + 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 = Account::ACTIVE @@ -20,17 +23,6 @@ class Account < ActiveRecord::Base 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::INACTIVE self.save! if self.changed? @@ -43,7 +35,7 @@ class Account < ActiveRecord::Base client = MessagePack::RPC::Client.new(transport, Rails.root.join("tmp", "sockets", "receiver.sock").to_s) client.call(:register, Marshal.dump(self)) rescue Errno::ECONNREFUSED, Errno::ENOENT - Rails.logger.error "Account#update_connection: couldn't connect to the receiver" + logger.error("Account#update_connection: couldn't connect to the receiver") end def client @@ -58,22 +50,21 @@ class Account < ActiveRecord::Base Tweet.from_twitter_object(obj) end - def following?(target_user_id) - api_friendship?(user_id, target_user_id) + def following?(target_id) + api_friendship?(self.user_id, target_id) end - def followed_by?(source_user_id) - api_friendship?(source_user_id, user_id) + def followed_by?(source_id) + api_friendship?(source_id, self.user_id) end private - def api_friendship?(source_user_id, target_user_id) - return nil unless source_user_id.is_a?(Integer) - return nil unless target_user_id.is_a?(Integer) + def api_friendship?(source_id, target_id) + return nil unless source_id.is_a?(Integer) + return nil unless target_id.is_a?(Integer) - Rails.cache.fetch("friendship/#{source_user_id}-#{target_user_id}", expires_in: 3.days) do - self.client.friendship?(source_user_id, target_user_id) rescue nil + Rails.cache.fetch("friendship/#{source_id}-#{target_id}", expires_in: 3.days) do + client.friendship?(source_id, target_id) rescue nil end end end - diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 7d0f1a1..6e3d197 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -10,21 +10,16 @@ class Favorite < ActiveRecord::Base Tweet.update_counters(self.tweet_id, favorites_count: -1, reactions_count: -1) end - def self.from_receiver(msg) - transaction do - t = Tweet.from_receiver(msg["target_object"]) - u = User.from_receiver(msg["source"]) - f = t.favorites.new(user: u) - f.save_ignore! - logger.debug("Created Favorite: #{msg["source"]["id"]} => #{msg["target_object"]["id"]}") - return f + def self.from_json(json) + tweet = Tweet.from_json(json[:target_object]) + user = User.from_json(json[:source]) + favorite = Favorite.new(tweet: tweet, user: user) + if favorite.save + logger.debug("Successfully created a favorite: #{favorite.id}") + else + logger.debug("Failed to create a favorite: #{favorite}") end - rescue => e - logger.error("Unknown error while inserting favorite: #{e.class}: #{e.message}/#{e.backtrace.join("\n")}") - return nil - end - def self.delete_from_receiver(msg) - where(tweet_id: msg["target_object"]["id"], user_id: msg["source"]["id"]).destroy_all + favorite end end diff --git a/app/models/retweet.rb b/app/models/retweet.rb index 521bd3c..a2baf51 100644 --- a/app/models/retweet.rb +++ b/app/models/retweet.rb @@ -10,17 +10,16 @@ class Retweet < ActiveRecord::Base Tweet.update_counters(self.tweet_id, retweets_count: -1, reactions_count: -1) end - def self.from_receiver(msg) - transaction do - t = Tweet.from_receiver(msg["retweeted_status"]) - u = User.from_receiver(msg["user"]) - r = t.retweets.new(id: msg["id"], user: u) - r.save_ignore! - logger.debug("Created Retweet: #{msg["id"]}: #{msg["user"]["id"]} => #{msg["retweeted_status"]["id"]}") - return r + def self.from_json(json) + tweet = Tweet.from_json(json[:retweeted_status]) + user = User.from_json(json[:user]) + retweet = Retweet.new(tweet: tweet, user: user) + if retweet.save + logger.debug("Successfully created a retweet: #{retweet.id}") + else + logger.debug("Failed to create a retweet: #{retweet}") end - rescue => e - logger.error("Unknown error while inserting retweet: #{e.class}: #{e.message}/#{e.backtrace.join("\n")}") - return nil + + retweet end end diff --git a/app/models/settings.rb b/app/models/settings.rb index e9ded95..b3cf53b 100644 --- a/app/models/settings.rb +++ b/app/models/settings.rb @@ -2,4 +2,3 @@ class Settings < Settingslogic namespace Rails.env source Rails.root.join("config", "settings.yml") end - diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 3e5017b..b400c74 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -42,18 +42,18 @@ class Tweet < ActiveRecord::Base end end - def self.from_json(msg) - find_by(id: msg[:id]) || begin - user = User.from_json(msg[:user]) - create!(id: msg[:id], - text: extract_entities(msg), - source: msg[:source], - tweeted_at: msg[:created_at], - in_reply_to_id: msg[:in_reply_to_status_id], + def self.from_json(json) + find_by(id: json[:id]) || begin + user = User.from_json(json[:user]) + create!(id: json[:id], + text: extract_entities(json), + source: json[:source], + tweeted_at: json[:created_at], + in_reply_to_id: json[:in_reply_to_status_id], user: user) end rescue ActiveRecord::RecordNotUnique - logger.debug("Duplicate Tweet: #{msg[:id]}") + logger.debug("Duplicate Tweet: #{json[:id]}") rescue => e logger.error("Unknown error while inserting tweet: #{e.class}: #{e.message}/#{e.backtrace.join("\n")}") end @@ -78,7 +78,7 @@ class Tweet < ActiveRecord::Base end parse_condition = ->(scoped, token) do - p positive = !token.slice!(/^[-!]/) + positive = !token.slice!(/^[-!]/) where_args = case token when /^(?:user|from):([A-Za-z0-9_]{1,20})$/ @@ -107,7 +107,7 @@ class Tweet < ActiveRecord::Base private def self.extract_entities(json) - entity_values = json[:entities].values.sort_by {|v| v[:indices].first } + entity_values = json[:entities].values.sort_by {|v| v[:indices].first }.flatten result = "" last_index = entity_values.inject(0) do |last_index, entity| diff --git a/app/models/user.rb b/app/models/user.rb index f41c68c..c73872d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -39,36 +39,23 @@ class User < ActiveRecord::Base raise ActiveRecord::RecordNotFound, "Couldn't find User without any parameter" end - def self.from_receiver(msg) - user = where(id: msg["id"]).first_or_initialize - att = user.attributes.dup - - user.screen_name = msg["screen_name"] - user.name = msg["name"] - user.profile_image_url = msg["profile_image_url"] - user.protected = msg["protected"] - - if att["screen_name"] == user.screen_name && - att["name"] == user.name && - att["profile_image_url"] == user.profile_image_url && - att["protected"] == user.protected? - logger.debug("User not changed: #{user.id}") + def self.from_json(json) + user = where(id: json[:id]).first_or_initialize + orig = user.attributes.dup + + user.screen_name = json[:screen_name] + user.name = json[:name] + user.profile_image_url = json[:profile_image_url] + user.protected = json[:protected] + + if user.attributes == orig + logger.debug("User was not updated: #{user.id}" else user.save! - logger.debug("User saved: #{user.id}") + logger.debug("Successfully saved an user: #{user.id}") end - return user - rescue - logger.error("Unknown error while inserting user: #{$!}/#{$@}") - end - - def self.from_user_object(user_object) - from_receiver("id" => user_object.id, - "screen_name" => user_object.screen_name, - "name" => user_object.name, - "profile_image_url" => user_object.profile_image_url_https, - "protected" => user_object.protected) + user end def protected? |