diff options
author | rhenium <re4k@re4k.info> | 2013-05-19 11:38:38 +0900 |
---|---|---|
committer | rhenium <re4k@re4k.info> | 2013-05-19 11:38:38 +0900 |
commit | b49ad1708874c273b5f6bf07c625ea2a7701825a (patch) | |
tree | c54776825c5be7149c31047a7fd7edf2bcc1bd36 /app | |
parent | c93b516fdb0a2f826bde15aaefe3a66508639111 (diff) | |
download | aclog-b49ad1708874c273b5f6bf07c625ea2a7701825a.tar.gz |
rewrite notification
Diffstat (limited to 'app')
-rw-r--r-- | app/models/notification.rb | 36 | ||||
-rw-r--r-- | app/models/tweet.rb | 7 |
2 files changed, 36 insertions, 7 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 |