aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorre4k <re4k@re4k.info>2013-03-28 00:48:32 +0900
committerre4k <re4k@re4k.info>2013-03-28 00:48:32 +0900
commitb4972590475eb595bc88f6ee47125fe8ca6e1fb2 (patch)
tree52b5e3b230953d60dc9bbfbedec335826bd471b1 /app
parent9ed0b4d86906a47ddb90270032079fd9cec360bb (diff)
downloadaclog-b4972590475eb595bc88f6ee47125fe8ca6e1fb2.tar.gz
Fix errors
Add some JSON APIs
Diffstat (limited to 'app')
-rw-r--r--app/controllers/application_controller.rb65
-rw-r--r--app/controllers/users_controller.rb43
-rw-r--r--app/models/user.rb15
-rw-r--r--app/views/shared/_tweet.html.haml8
-rw-r--r--app/views/shared/_user.json.jbuilder4
5 files changed, 95 insertions, 40 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index ddd3b3b..c65c21c 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -1,8 +1,14 @@
class ApplicationController < ActionController::Base
protect_from_forgery
- before_filter :get_include_user
+ before_filter :set_format, :get_include_user, :get_include_user_stats
after_filter :set_content_type
+ def set_format
+ unless request.format == :json || request.format == :html
+ request.format = :html
+ end
+ end
+
def set_content_type
if request.format == :html
response.content_type = "application/xhtml+xml"
@@ -10,11 +16,13 @@ class ApplicationController < ActionController::Base
end
def get_include_user
- case params[:include_user]
- when /^t/
+ @include_user ||= get_bool(params[:include_user])
+ end
+
+ def get_include_user_stats
+ if @include_user_stats ||= get_bool(params[:include_user_stats])
@include_user = true
end
- @include_user ||= false
end
def render_tweets(a = nil, &blk)
@@ -24,22 +32,47 @@ class ApplicationController < ActionController::Base
end
def page
- if params[:page]
- i = params[:page].to_i
- if i > 0
- ret = i
- end
+ get_int(params[:page], 1) do |i|
+ i > 0
end
- ret || 1
end
def count
- if params[:count]
- i = params[:count].to_i
- if (1..100) === i
- ret = i
- end
+ get_int(params[:count], 10) do |i|
+ (1..100) === i
+ end
+ end
+
+ def order
+ case params[:order]
+ when /^fav/
+ :favorite
+ when /^re?t/
+ :retweet
+ else
+ :default
+ end
+ end
+
+ def all
+ get_bool(params[:all])
+ end
+
+ private
+ def get_bool(str)
+ if /^(t.*|1)$/ =~ str
+ true
+ else
+ false
+ end
+ end
+
+ def get_int(str, default, &blk)
+ i = Integer(str) rescue default
+ if blk.call(i)
+ i
+ else
+ default
end
- ret || Settings.page_per
end
end
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index c8f7df3..d899703 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -5,10 +5,10 @@ class UsersController < ApplicationController
def best
@title = "@#{@user.screen_name}'s Best Tweets"
render_tweets do
- case params[:order]
- when /^fav/
+ case order
+ when :favorite
@user.tweets.reacted.order_by_favorites
- when /^re?t/
+ when :retweet
@user.tweets.reacted.order_by_retweets
else
@user.tweets.reacted.order_by_reactions
@@ -19,10 +19,10 @@ class UsersController < ApplicationController
def recent
@title = "@#{@user.screen_name}'s Recent Best Tweets"
render_tweets do
- case params[:order]
- when /^fav/
+ case order
+ when :favorite
@user.tweets.recent.reacted.order_by_favorites
- when /^re?t/
+ when :retweet
@user.tweets.recent.reacted.order_by_retweets
else
@user.tweets.recent.reacted.order_by_reactions
@@ -35,8 +35,7 @@ class UsersController < ApplicationController
@title = "@#{@user.screen_name}'s Newest Tweets"
render_tweets do
- case params[:all]
- when /^(t|true|1)$/
+ if all
@user.tweets.order_by_id
else
@user.tweets.reacted.order_by_id
@@ -64,11 +63,14 @@ class UsersController < ApplicationController
@title = "@#{@user.screen_name} (#{@user.name})'s Profile"
- @twitter_user = account.twitter_user
respond_to do |format|
- format.html
- format.json
+ format.html do
+ @twitter_user = account.twitter_user
+ end
+ format.json do
+ @include_user_stats = true
+ end
end
end
@@ -118,17 +120,24 @@ class UsersController < ApplicationController
def show
tweet_id = params[:id].to_i
- items = Tweet.where(:id => tweet_id)
+ @items = Tweet.where(:id => tweet_id).page
- item = items.first
+ item = @items.first
raise Aclog::Exceptions::TweetNotFound unless item
- @user = items.first.user
+ @user = item.user
helpers = ApplicationController.helpers
@title = "\"#{helpers.strip_tags(helpers.format_tweet_text(item.text))[0...30]}\" from @#{@user.screen_name}"
@title_b = "@#{@user.screen_name}'s Tweet"
- render_tweets(items)
+ respond_to do |format|
+ format.html do
+ render "shared/tweets"
+ end
+ format.json do
+ render "shared/_tweet", :locals => {:item => item}
+ end
+ end
end
private
@@ -145,8 +154,6 @@ class UsersController < ApplicationController
.limit(100)
.inject(Hash.new(0)){|hash, tweet| pr.call(tweet).each{|event| hash[event.user_id] += 1}; hash}
.sort_by{|id, count| -count}
- .take(50)
- .map{|user, count| [User.cached(user), count]}
render "shared/users"
end
@@ -165,8 +172,6 @@ class UsersController < ApplicationController
.map{|e| Tweet.cached(e.tweet_id)}
.inject(Hash.new(0)){|hash, tweet| hash[tweet.user_id] += 1; hash}
.sort_by{|user_id, count| -count}
- .take(50)
- .map{|user_id, count| [User.cached(user_id), count]}
render "shared/users"
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 46acd47..874f8d6 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -26,4 +26,19 @@ class User < ActiveRecord::Base
def profile_image_url_original
profile_image_url.sub(/_normal((\.(png|jpeg|gif))?)/, "\\1")
end
+
+ def stats
+ return @stats_cache if @stats_cache
+
+ hash = tweets.inject(Hash.new(0)) do |hash, m|
+ hash[:favorited_count] += m.favorites_count
+ hash[:retweeted_count] += m.retweets_count
+ hash
+ end
+ hash[:favorites_count] = favorites.count
+ hash[:retweets_count] = retweets.count
+ hash[:tweets_count] = tweets.count
+
+ @stats_cache = hash
+ end
end
diff --git a/app/views/shared/_tweet.html.haml b/app/views/shared/_tweet.html.haml
index 1b75951..d98fdbb 100644
--- a/app/views/shared/_tweet.html.haml
+++ b/app/views/shared/_tweet.html.haml
@@ -15,18 +15,18 @@
%span.twitter_bird
= link_to image_tag("bird_gray_16.png", :alt => "Twitter"), twitter_status_url(item), :target => "_blank"
%span.created_at
- = link_to format_tweet_created_at(item.tweeted_at), :controller => "i", :action => "show", :id => item.id
+ = link_to format_tweet_created_at(item.tweeted_at), :controller => "users", :action => "show", :id => item.id
%span.source
= raw format_source_text(item.source)
.stats
- - [["favs", item.favorites.order("id")], ["retweets", item.retweets.order("id")]].select{|m| m[1].size > 0}.each do |type, actions|
+ - [["favs", item.favorites.order("id")], ["retweets", item.retweets.order("id")]].select{|m| m[1].count > 0}.each do |type, actions|
%dl.dl-horizontal
%dt
- %span.count= actions.size
+ %span.count= actions.count
%span.type= type
%dd
%ul.inline
- - actions.take(params[:controller] == "i" && params[:action] == "show" ? actions.size : 20).each do |a| |
+ - actions.take(params[:controller] == "users" && params[:action] == "show" ? actions.count : 20).each do |a| |
- m = a.user || User.new
%li
- if m.screen_name
diff --git a/app/views/shared/_user.json.jbuilder b/app/views/shared/_user.json.jbuilder
index a0447c0..1bac0ad 100644
--- a/app/views/shared/_user.json.jbuilder
+++ b/app/views/shared/_user.json.jbuilder
@@ -1,2 +1,4 @@
json.(user, :id, :screen_name, :name, :profile_image_url)
-
+if @include_user_stats && user.registered?
+ json.stats user.stats
+end