aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrhenium <re4k@re4k.info>2013-05-19 11:38:38 +0900
committerrhenium <re4k@re4k.info>2013-05-19 11:38:38 +0900
commitb49ad1708874c273b5f6bf07c625ea2a7701825a (patch)
treec54776825c5be7149c31047a7fd7edf2bcc1bd36
parentc93b516fdb0a2f826bde15aaefe3a66508639111 (diff)
downloadaclog-b49ad1708874c273b5f6bf07c625ea2a7701825a.tar.gz
rewrite notification
-rw-r--r--app/models/notification.rb36
-rw-r--r--app/models/tweet.rb7
-rw-r--r--config/settings.yml.example2
-rw-r--r--lib/aclog/notification.rb25
-rw-r--r--lib/aclog/receiver/collector_connection.rb2
5 files changed, 38 insertions, 34 deletions
diff --git a/app/models/notification.rb b/app/models/notification.rb
index dd15afb..111dd40 100644
--- a/app/models/notification.rb
+++ b/app/models/notification.rb
@@ -1,2 +1,38 @@
class Notification
+ def self.notify_favorite(tweet)
+ tweet.reload
+ if [50, 100, 250, 500, 1000].include?(tweet.favorites_count)
+ reply_favs(tweet, tweet.favorites_count) if tweet.user.registered?
+ end
+ end
+
+ def self.reply_favs(tweet, count)
+ url = Rails.application.routes.url_helpers.tweet_url(host: Settings.base_url, id: tweet.id)
+ tweet("@#{tweet.user.screen_name} #{count}favs! #{url}", tweet.id)
+ end
+
+ private
+ def self.tweet(text, reply_to = 0)
+ cur = Rails.cache.read("notification_account") || 0
+ if Settings.notification.token[cur]
+ begin
+ client = Twitter::Client.new(consumer_key: Settings.notification.consumer.key,
+ consumer_secret: Settings.notification.consumer.secret,
+ oauth_token: Settings.notification.token[cur].token,
+ oauth_token_secret: Settings.notification.token[cur].secret)
+
+
+ client.update(text, in_reply_to_status_id: reply_to)
+ rescue Exception
+ cur += 1
+ doretry = true
+ Rails.logger.error($!)
+ end
+ Rails.cache.write("notification_account", cur, expires_in: 15.minutes)
+ tweet(text, reply_to) if doretry
+ else
+ # wait for expiring notification_account cache
+ #cur = 0
+ end
+ end
end
diff --git a/app/models/tweet.rb b/app/models/tweet.rb
index e76759c..5978d26 100644
--- a/app/models/tweet.rb
+++ b/app/models/tweet.rb
@@ -33,13 +33,6 @@ class Tweet < ActiveRecord::Base
joins("INNER JOIN (#{un}) m ON m.tweet_id = tweets.id")
}
- # will be moved
- def notify_favorite
- if [50, 100, 250, 500, 1000].include? favorites.count
- Aclog::Notification.reply_favs(self, favorites.count)
- end
- end
-
def self.delete_from_id(id)
return {} if id.is_a?(Array) && id.size == 0
begin
diff --git a/config/settings.yml.example b/config/settings.yml.example
index 076a907..0fd8ddd 100644
--- a/config/settings.yml.example
+++ b/config/settings.yml.example
@@ -12,7 +12,7 @@ notification:
secret: "consumer secret"
token:
- token: "access token of notification account"
- - secret: "access token secret"
+ secret: "access token secret"
listen_port: 42106
secret_key: "secret key to authorize workers"
diff --git a/lib/aclog/notification.rb b/lib/aclog/notification.rb
deleted file mode 100644
index 0404f4c..0000000
--- a/lib/aclog/notification.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-# -*- coding: utf-8 -*-
-module Aclog
- module Notification
- def self.reply_favs(tweet, count)
- reply_tweet(tweet.user, "#{count}favs!", tweet)
- end
-
- private
- def self.reply_tweet(user, text, tweet)
- @@account ||= Twitter::Client.new(consumer_key: Settings.notification.consumer.key,
- consumer_secret: Settings.notification.consumer.secret,
- oauth_token: Settings.notification.token[0].token,
- oauth_token_secret: Settings.notification.token[0].secret)
-
- url = Rails.application.routes.url_helpers.tweet_url(host: Settings.base_url, id: tweet.id)
-
- begin
- @@account.update("@#{user.screen_name} #{text} #{url}", :in_reply_to_status_id => tweet.id)
- rescue Exception
- Rails.logger.error($!)
- Rails.logger.error($@)
- end
- end
- end
-end
diff --git a/lib/aclog/receiver/collector_connection.rb b/lib/aclog/receiver/collector_connection.rb
index ca37d55..2d82a58 100644
--- a/lib/aclog/receiver/collector_connection.rb
+++ b/lib/aclog/receiver/collector_connection.rb
@@ -161,7 +161,7 @@ module Aclog
f = Favorite.from_hash(:tweet_id => msg["tweet_id"],
:user_id => msg["user_id"])
if t = Tweet.find_by(id: msg["tweet_id"])
- t.notify_favorite
+ Notification.notify_favorite(t)
end
end
end