diff options
author | re4k <re4k@re4k.info> | 2013-05-04 18:09:28 +0900 |
---|---|---|
committer | re4k <re4k@re4k.info> | 2013-05-04 18:09:28 +0900 |
commit | 31e9e320cdc5aa1b26ebdda3da4db7f067667fa5 (patch) | |
tree | e60cf2095f93cb772ed277c293f8f7d119df547f | |
parent | 2844d9159d6417f8c8d55e818c2ed6a4e30ea405 (diff) | |
download | aclog-31e9e320cdc5aa1b26ebdda3da4db7f067667fa5.tar.gz |
refactor, fix errors
25 files changed, 127 insertions, 156 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index e1cac88..0a8217a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,9 +7,9 @@ class ApplicationController < ActionController::Base protected def _get_user(id, screen_name) if id - User.find_by(id: id) + User.find(id: id) rescue raise Aclog::Exceptions::UserNotFound elsif screen_name - User.find_by(screen_name: screen_name) + User.find_by(screen_name: screen_name) || (raise Aclog::Exceptions::UserNotFound) end end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index aa9a433..cb22e75 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- class TweetsController < ApplicationController before_filter :set_user_limit @@ -7,8 +8,8 @@ class TweetsController < ApplicationController tweet_required @user = @tweet.user - text = ApplicationController.helpers.format_tweet_text(@tweet.text)[0...30] @caption = "#{@user.screen_name}'s Tweet" + text = ApplicationController.helpers.format_tweet_text(@tweet.text) @title = "\"#{text}\" from #{@user.screen_name}" end @@ -89,14 +90,10 @@ class TweetsController < ApplicationController private def render(*args) - if args.empty? - if params[:action] == "show" - super - else - super "shared/tweets" - end - else + if lookup_context.exists?(params[:action], params[:controller]) super(*args) + else + super("_tweets") end end @@ -120,17 +117,18 @@ class TweetsController < ApplicationController end def set_user_limit - if params[:limit] - if params[:limit].to_i == -1 + if params[:action] == "show" + if params[:full] == "true" @user_limit = nil else - @user_limit = params[:limit].to_i + @user_limit = 100 end else @user_limit = 20 - if request.format == :html && params[:action] == "show" - @user_limit = 100 - end + end + + if request.format == :json + @user_limit = nil end end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3448967..e7328c3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -2,21 +2,22 @@ class UsersController < ApplicationController def info user_required @caption = "Profile" - @stats = @user.stats(true) + @user_stats = @user.stats + @user_twitter = @user.account.client.user if request.format == :html end def discovered_by user_required - @usermap = @user.count_discovered_by + @result = @user.count_discovered_by.take(50) @caption = "Discovered By" - render "shared/user_ranking" + render "_user_ranking" end - def discovered_of + def discovered_users user_required - @usermap = @user.count_discovered_of - @caption = "Discovered Of" - render "shared/user_ranking" + @result = @user.count_discovered_users.take(50) + @caption = "Discovered Users" + render "_user_ranking" end private diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0c54ec4..ae07f0c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -30,8 +30,6 @@ module ApplicationHelper $& end end - ret.gsub!(/\r\n|\r|\n/, "<br />") - return ret end alias format_source_text format_tweet_text diff --git a/app/helpers/tweets_helper.rb b/app/helpers/tweets_helper.rb index 6b7d657..0d9f8b7 100644 --- a/app/helpers/tweets_helper.rb +++ b/app/helpers/tweets_helper.rb @@ -1,2 +1,6 @@ module TweetsHelper + def user_truncated?(tweet) + tr = @user_limit || Float::INFINITY + tr < tweet.favorites_count || tr < tweet.retweets_count + end end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 046ba96..6cce829 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -74,7 +74,6 @@ class Tweet < ActiveRecord::Base ret = limit(count) - if params[:page] ret.page(params[:page].to_i, count) else diff --git a/app/models/user.rb b/app/models/user.rb index d230efb..c4804bc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -50,7 +50,7 @@ class User < ActiveRecord::Base profile_image_url.sub(/_normal((?:\.(?:png|jpeg|gif))?)/, "\\1") end - def stats(include_stats_api = false) + def stats @stats_cache ||= begin raise Aclog::Exceptions::UserNotRegistered unless account @@ -60,35 +60,22 @@ class User < ActiveRecord::Base favorited_count: 0, retweeted_count: 0} - if include_stats_api - twitter_user = account.client.user - if twitter_user - h = { - favorites_count: twitter_user.favourites_count, - listed_count: twitter_user.listed_count, - followers_count: twitter_user.followers_count, - tweets_count: twitter_user.statuses_count, - friends_count: twitter_user.friends_count, - bio: twitter_user.description - } - end - hash[:stats_api] = h - end - tweets.inject(hash) do |hash, m| hash[:favorited_count] += m.favorites_count hash[:retweeted_count] += m.retweets_count hash end + + OpenStruct.new(hash) end end def count_discovered_by - count_by(Favorite).merge(count_by(Retweet)) {|_, fav, rt| fav + rt }.sort_by {|user_id, count| -count }.map {|user_id, count| [User.find(user_id), count] }.take(50) + merge_count_user(count_by(Favorite), count_by(Retweet)) end - def count_discovered_of - count_to(Favorite).merge(count_to(Retweet)) {|_, fav, rt| fav + rt }.sort_by {|user_id, count| -count }.map {|user_id, count| [User.find(user_id), count] }.take(50) + def count_discovered_users + merge_count_user(count_to(Favorite), count_to(Retweet)) end private @@ -101,4 +88,15 @@ class User < ActiveRecord::Base actions = Tweet.joins("INNER JOIN (#{klass.where(user: self).order("id DESC").limit(500).to_sql}) m ON tweets.id = m.tweet_id") actions.inject(Hash.new(0)) {|hash, obj| hash[obj.user_id] += 1; hash } end + + def merge_count_user(*args) + ret = {} + args.map.each_with_index do |o, i| + o.each do |user_id, count| + ret[user_id] ||= Array.new(args.size, 0) + ret[user_id][i] = count + end + end + ret.map(&:flatten).sort_by {|user_id, favorites_count, retweets_count| -(favorites_count + retweets_count) } + end end diff --git a/app/views/errors/error.json.jbuilder b/app/views/errors/error.json.jbuilder index 87e5f2c..41e8ea2 100644 --- a/app/views/errors/error.json.jbuilder +++ b/app/views/errors/error.json.jbuilder @@ -20,5 +20,5 @@ json.error do |json| json.message "Internal Error (Unknown)" end end - json.exception @exception.class.to_s + # json.exception @exception.class.to_s end diff --git a/app/views/shared/partial/_tweet.json.jbuilder b/app/views/shared/partial/_tweet.json.jbuilder deleted file mode 100644 index db7eed2..0000000 --- a/app/views/shared/partial/_tweet.json.jbuilder +++ /dev/null @@ -1,24 +0,0 @@ -json.(tweet, :id, :text, :source, :tweeted_at, :favorites_count, :retweets_count) - -json.user do - json.id tweet.user_id - if include_user? - json.partial! "shared/partial/user", user: tweet.user - end -end - -render_actions = -> name, data, render_id do - json.__send__(name, data.includes(:user).limit(user_limit).load) do |action| - json.id action.id if render_id - json.user do - json.id action.user_id - if include_user? - json.partial! "shared/partial/user", user: action.user - end - end - end -end - -render_actions.call(:favorites, tweet.favorites, false) -render_actions.call(:retweets, tweet.retweets, true) - diff --git a/app/views/shared/sidebar/_users.html.haml b/app/views/shared/sidebar/_users.html.haml index 8ef51f4..ab24d19 100644 --- a/app/views/shared/sidebar/_users.html.haml +++ b/app/views/shared/sidebar/_users.html.haml @@ -7,16 +7,16 @@ %ul.records %li %span favorited - %span.data= @user.stats[:favorited_count] + %span.data= @user.stats.favorited_count %li %span retweeted - %span.data= @user.stats[:retweeted_count] + %span.data= @user.stats.retweeted_count %li %span avg. fav - %span.data= ((@user.stats[:favorited_count] + 0.0) / @user.stats[:tweets_count]).round(2) + %span.data= ((@user.stats.favorited_count + 0.0) / @user.stats.tweets_count).round(2) %li %span joined - %span.data= format_days_ago(@user.created_at) + %span.data= format_days_ago(@user.account.created_at) - else .alert.alert-info = "@#{@user.screen_name} has never signed in to aclog" @@ -34,4 +34,4 @@ %li = link_to "discovered by", user_discovered_by_path(@user.screen_name) %li - = link_to "discovered of", user_discovered_of_path(@user.screen_name) + = link_to "discovered users", user_discovered_users_path(@user.screen_name) diff --git a/app/views/shared/tweets.json.jbuilder b/app/views/shared/tweets.json.jbuilder deleted file mode 100644 index a1776b7..0000000 --- a/app/views/shared/tweets.json.jbuilder +++ /dev/null @@ -1,4 +0,0 @@ -json.array! @tweets do |json, tweet| - json.partial! "shared/partial/tweet", tweet: tweet -end - diff --git a/app/views/shared/user_ranking.html.haml b/app/views/shared/user_ranking.html.haml deleted file mode 100644 index 37ba110..0000000 --- a/app/views/shared/user_ranking.html.haml +++ /dev/null @@ -1,20 +0,0 @@ -.users - %ul.inline - - @usermap.each do |user, count| - %li - - if user - .avatar - = link_to user_best_path(user.screen_name) do - = image_tag user.profile_image_url, alt: user.screen_name, title: user.name - .data - .count - - if params[:action] == "discovered_by" - = link_to count, user_discovered_by_user_path(@user.screen_name, user.screen_name) - - elsif params[:action] == "discovered_of" - = link_to count, user_discovered_by_user_path(user.screen_name, @user.screen_name) - - else - - raise Exception - - else - .avatar= image_tag "missing_profile_image.png", alt: "Missing User: #{user_id}", title: "Missing User: #{user_id}" - .data - .count= count diff --git a/app/views/shared/user_ranking.json.jbuilder b/app/views/shared/user_ranking.json.jbuilder deleted file mode 100644 index 6888263..0000000 --- a/app/views/shared/user_ranking.json.jbuilder +++ /dev/null @@ -1,9 +0,0 @@ -json.array! @usermap do |json, u| - json.count u[1] - json.user do |json| - json.id u[0] - if @include_user - json.partial! "shared/partial/user", user: User.find(u[0]) - end - end -end diff --git a/app/views/shared/partial/_tweet.html.haml b/app/views/tweets/_tweet.html.haml index e8e26d9..6546a47 100644 --- a/app/views/shared/partial/_tweet.html.haml +++ b/app/views/tweets/_tweet.html.haml @@ -15,30 +15,30 @@ %span.nam= link_to tweet.user.name, user_best_path(tweet.user.screen_name) %span.screen_name= link_to tweet.user.screen_name, user_best_path(tweet.user.screen_name) .text{class: tweet.stolen_tweet ? "copied" : nil} - = raw format_tweet_text(tweet.text) + = simple_format(format_tweet_text(tweet.text)) .meta.clearfix %span.twitter_bird = link_to image_tag("bird_gray_16.png", alt: "Twitter"), twitter_status_url(tweet), target: "_blank" %span.created_at = link_to format_time(tweet.tweeted_at), tweet_path(tweet.id) - - if params[:action] == "show" + - if params[:action] == "show" && user_truncated?(tweet) %span.full - = link_to "full", params.merge(limit: -1) + = link_to "full", params.merge(full: true) - if tweet.stolen_tweet %span.copied = link_to "original", tweet_path(tweet.original.id) %span.source = raw format_source_text(tweet.source) .stats - - [["favs", tweet.favoriters], ["retweets", tweet.retweeters]].each do |type, actions| - - if actions.count > 0 + - [["favs", tweet.favorites_count, tweet.favoriters], ["retweets", tweet.retweets_count, tweet.retweeters]].each do |title, count, users| + - if count > 0 %dl.dl-horizontal %dt - %span.count= actions.count - %span.type= type + %span.count= count + %span.type= title %dd %ul.inline - - actions.limit(user_limit).each do |m| + - users.limit(@user_limit).each do |m| %li = link_to user_best_path(m.screen_name) do = image_tag m.profile_image_url, alt: m.screen_name, title: m.name diff --git a/app/views/tweets/_tweet.json.jbuilder b/app/views/tweets/_tweet.json.jbuilder new file mode 100644 index 0000000..4619da1 --- /dev/null +++ b/app/views/tweets/_tweet.json.jbuilder @@ -0,0 +1,7 @@ +json.(tweet, :id, :favorites_count, :retweets_count) + +json.user_id tweet.user_id + +json.favoriters tweet.favoriters.limit(@user_limit).pluck(:user_id) +json.retweeters tweet.retweeters.limit(@user_limit).pluck(:user_id) + diff --git a/app/views/shared/tweets.html.haml b/app/views/tweets/_tweets.html.haml index 8bae139..83b0e81 100644 --- a/app/views/shared/tweets.html.haml +++ b/app/views/tweets/_tweets.html.haml @@ -1,7 +1,7 @@ - if params[:action] == "search" = render partial: "shared/partial/search" .tweets - = render partial: "shared/partial/tweet", collection: @tweets, as: :tweet + = render partial: "tweet", collection: @tweets.includes(:user).includes(:stolen_tweet), as: :tweet .loading = image_tag "loading.gif", alt: "loading...", title: nil .pagination diff --git a/app/views/tweets/_tweets.json.jbuilder b/app/views/tweets/_tweets.json.jbuilder new file mode 100644 index 0000000..f3acf04 --- /dev/null +++ b/app/views/tweets/_tweets.json.jbuilder @@ -0,0 +1,4 @@ +json.array! @tweets do |json, tweet| + json.partial! "tweet", tweet: tweet +end + diff --git a/app/views/tweets/show.html.haml b/app/views/tweets/show.html.haml index 0cc1298..65fcf3c 100644 --- a/app/views/tweets/show.html.haml +++ b/app/views/tweets/show.html.haml @@ -1,2 +1,2 @@ .tweets - = render partial: "shared/partial/tweet", locals: {tweet: @tweet} + = render partial: "tweet", locals: {tweet: @tweet} diff --git a/app/views/tweets/show.json.jbuilder b/app/views/tweets/show.json.jbuilder new file mode 100644 index 0000000..c92aaa0 --- /dev/null +++ b/app/views/tweets/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "tweet", tweet: @tweet diff --git a/app/views/users/_user_ranking.html.haml b/app/views/users/_user_ranking.html.haml new file mode 100644 index 0000000..0e57f2c --- /dev/null +++ b/app/views/users/_user_ranking.html.haml @@ -0,0 +1,16 @@ +.users + %ul.inline + - @result.each do |user_id, favorites_count, retweets_count| + - target = User.find(user_id) + %li + .avatar + = link_to user_best_path(target.screen_name) do + = image_tag target.profile_image_url, alt: target.screen_name, title: target.name + .data + .count + - if params[:action] == "discovered_by" + = link_to favorites_count + retweets_count, user_discovered_by_user_path(@user.screen_name, target.screen_name) + - elsif params[:action] == "discovered_users" + = link_to favorites_count + retweets_count, user_discovered_by_user_path(target.screen_name, @user.screen_name) + - else + - raise Exception diff --git a/app/views/users/_user_ranking.json.jbuilder b/app/views/users/_user_ranking.json.jbuilder new file mode 100644 index 0000000..e544fc6 --- /dev/null +++ b/app/views/users/_user_ranking.json.jbuilder @@ -0,0 +1,5 @@ +json.array! @result do |user_id, favorites_count, retweets_count| + json.user_id user_id + json.favorites_count favorites_count + json.retweets_count retweets_count +end diff --git a/app/views/users/info.html.haml b/app/views/users/info.html.haml index d55ed13..f622323 100644 --- a/app/views/users/info.html.haml +++ b/app/views/users/info.html.haml @@ -3,27 +3,27 @@ %p profile %dl.dl-horizontal %dt Username - %dd= @user.screen_name + %dd= @user_twitter.screen_name %dt Name - %dd= raw @user.name + %dd= raw @user_twitter.name %dt tweets - %dd= @stats[:stats_api][:tweets_count] + %dd= @user_twitter.statuses_count %dt Following - %dd= @stats[:stats_api][:friends_count] + %dd= @user_twitter.friends_count %dt Followers - %dd= @stats[:stats_api][:followers_count] + %dd= @user_twitter.followers_count %dt Favorites - %dd= @stats[:stats_api][:favorites_count] + %dd= @user_twitter.favourites_count %dt Listed - %dd= @stats[:stats_api][:listed_count] + %dd= @user_twitter.listed_count %dt Bio - %dd= raw @stats[:stats_api][:bio] + %dd= raw @user_twitter.description - if @user.registered? %p records %dl.dl-horizontal %dt Favorited - %dd= @stats[:favorited_count] + %dd= @user.stats.favorited_count %dt Retweeted - %dd= @stats[:retweeted_count] + %dd= @user.stats.retweeted_count %dt Since %dd= @user.account.created_at diff --git a/app/views/users/info.json.jbuilder b/app/views/users/info.json.jbuilder index 6969d81..a1274a0 100644 --- a/app/views/users/info.json.jbuilder +++ b/app/views/users/info.json.jbuilder @@ -1 +1,2 @@ -json.partial! "shared/partial/user", user: @user +json.id @user.id +json.(@user.stats, :favorited_count, :retweeted_count) diff --git a/config/routes.rb b/config/routes.rb index 31836a4..bc15c9d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,22 +37,10 @@ Aclog::Application.routes.draw do get "/:controller/:action" end - # User pages. - scope ":screen_name", controller: "users", constraints: constraints do - get "/info", action: "info", as: "user_info" - get "/discovered_by", action: "discovered_by", as: "user_discovered_by" - get "/discovered_of", action: "discovered_of", as: "user_discovered_of" - end - scope ":screen_name", controller: "tweets", constraints: constraints do - get "/(:page)", action: "best", as: "user_best" - get "/favorited(/:page)", action: "favorited", as: "user_favorited" - get "/retweeted(/:page)", action: "retweeted", as: "user_retweeted" - get "/recent(/:page)", action: "recent", as: "user_recent" - get "/timeline", action: "timeline", as: "user_timeline" - get "/discoveries", action: "discoveries", as: "user_discoveries" - get "/discovered_by/:screen_name_b", action: "discovered_by", as: "user_discovered_by_user" - end - + # deprecated API + get "/users/best" => "tweets#best" + get "/users/timeline" => "tweets#timeline" + get "/users/discovered" => "tweets#discoveries" # Favstar redirects scope "users/:screen_name", constraints: constraints do @@ -72,9 +60,21 @@ Aclog::Application.routes.draw do get "/given_to/:screen_name_b" => redirect("/%{screen_name}/given_favorites_to/%{screen_name_b}") end - # deprecated API - get "/users/best" => "tweets#best" - get "/users/recent" => "tweets#recent" - get "/users/timeline" => "tweets#timeline" - get "/users/discovered" => "tweets#discoveries" + # User pages. + scope ":screen_name", controller: "users", constraints: constraints do + get "/info", action: "info", as: "user_info" + get "/discovered_by", action: "discovered_by", as: "user_discovered_by" + get "/discovered_users", action: "discovered_users", as: "user_discovered_users" + end + scope ":screen_name", controller: "tweets", constraints: constraints do + get "/(:page)", action: "best", as: "user_best" + get "/favorited(/:page)", action: "favorited", as: "user_favorited" + get "/retweeted(/:page)", action: "retweeted", as: "user_retweeted" + get "/recent(/:page)", action: "recent", as: "user_recent" + get "/timeline", action: "timeline", as: "user_timeline" + get "/discoveries", action: "discoveries", as: "user_discoveries" + get "/discovered_by/:screen_name_b", action: "discovered_by", as: "user_discovered_by_user" + end + + end diff --git a/lib/receiver/worker.rb b/lib/receiver/worker.rb index baf0afe..19ee4af 100644 --- a/lib/receiver/worker.rb +++ b/lib/receiver/worker.rb @@ -155,7 +155,7 @@ class Receiver::Worker < DaemonSpawn::Base Rails.logger.debug("Received Tweet(#{@worker_number}): #{msg["id"]}") unless @@saved_tweets.include?(msg["id"]) @@saved_tweets << msg["id"] - if @@saved_tweets.size > 10000 + if @@saved_tweets.size > 100000 Rails.logger.debug("Tweet id dropped from cache: #{@@saved_tweets.shift}") end @@ -214,7 +214,7 @@ class Receiver::Worker < DaemonSpawn::Base def receive_data(data) @pac.feed_each(data) do |msg| - p msg + Rails.logger.debug(msg.to_s) unless msg["type"] Rails.logger.error("Unknown message") send_object({:type => "fatal", :message => "Unknown message"}) @@ -259,8 +259,6 @@ class Receiver::Worker < DaemonSpawn::Base EM.stop end Signal.trap(:INT, &stop) - Signal.trap(:QUIT, &stop) - Signal.trap(:TERM, &stop) end end @@ -268,5 +266,3 @@ class Receiver::Worker < DaemonSpawn::Base end end - - |