From 5dbbc79f657cf2fb384c1ace5e476e0ee0148a50 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Fri, 25 Mar 2011 12:01:17 -0700 Subject: [PATCH] added users_profile_image, home_timeline, fixed public_timeline --- app/controllers/apis_controller.rb | 42 ++++++-- config/routes.rb | 4 +- spec/controllers/apis_controller_spec.rb | 118 ++++++++++++++++++++++- 3 files changed, 152 insertions(+), 12 deletions(-) diff --git a/app/controllers/apis_controller.rb b/app/controllers/apis_controller.rb index 664c0ce36..49ffbc1b4 100644 --- a/app/controllers/apis_controller.rb +++ b/app/controllers/apis_controller.rb @@ -1,7 +1,5 @@ -class ApisController < ApplicationController #ActionController::Metal - #include ActionController::Rendering - #include ActionController::Renderers::All - +class ApisController < ApplicationController + before_filter :authenticate_user!, :only => [:home_timeline, :user_timeline] respond_to :json #posts @@ -15,12 +13,31 @@ class ApisController < ApplicationController #ActionController::Metal def user_timeline set_defaults - if params[:user_id] - if person = Person.find(params[:user_id]) - timeline = person.posts.where(:type => "StatusMessage", :public => true).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC") - end + + person_id = params[:user_id] || current_user.person.id + + if person = Person.find(person_id) + timeline = current_user.posts_from(person) + else + timeline = [] + end + + respond_with timeline do |format| + format.json{ render :json => timeline.to_json(:format => :twitter) } + end + end + + def home_timeline + set_defaults + + aspect_ids = current_user.aspects.map{|a| a.id} + timeline = StatusMessage.joins(:aspects).where(:pending => false, + :aspects => {:id => aspect_ids}).includes(:comments, :photos, :likes, :dislikes).select('DISTINCT `posts`.*').paginate( + :page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC") + + respond_with timeline do |format| + format.json{ render :json => timeline.to_json(:format => :twitter) } end - respond_with timeline end def statuses @@ -67,6 +84,13 @@ class ApisController < ApplicationController #ActionController::Metal end end + def users_profile_image + if person = Person.where(:diaspora_handle => params[:screen_name]).first + redirect_to person.profile.image_url + else + render(:nothing => true, :status => 404) + end + end #tags def tag_posts diff --git a/config/routes.rb b/config/routes.rb index 248d8000e..0f4a8c567 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -84,11 +84,13 @@ Diaspora::Application.routes.draw do scope '/api' do match '/statuses/public_timeline', :to => 'apis#public_timeline' + match '/statuses/home_timeline', :to => 'apis#home_timeline' match '/statuses/show/:guid', :to => 'apis#statuses' + match '/statuses/user_timeline', :to => 'apis#user_timeline' - match '/statuses/user_timeline', :to => 'apis#user_timeline' match '/users/show', :to => 'apis#users' match '/users/search', :to => 'apis#users_search' + match '/users/profile_image', :to => 'apis#users_profile_image' match '/tags_posts/:tag', :to => 'apis#tag_posts' match '/tags_people/:tag', :to => 'apis#tag_people' diff --git a/spec/controllers/apis_controller_spec.rb b/spec/controllers/apis_controller_spec.rb index 0d7529322..54aaae0c8 100644 --- a/spec/controllers/apis_controller_spec.rb +++ b/spec/controllers/apis_controller_spec.rb @@ -8,7 +8,7 @@ class ApisController end describe ApisController do - before :all do + before(:all) do @status_message1 = Factory(:status_message, :text => '#bobby #flay #sux', :public => true) @status_message2 = Factory(:status_message, :public => true) @@ -18,7 +18,6 @@ describe ApisController do end describe '#public_timeline' do - it 'returns all of the public posts' do get :public_timeline, :format => :json @posts = JSON.parse(response.body) @@ -39,6 +38,121 @@ describe ApisController do end end + context 'protected timelines' do + let(:authenticate){ + sign_in(:user, @user); + @controller.stub(:current_user).and_return(@user) + } + + before do + @message1 = alice.post(:status_message, :text=> "hello", :to => alice.aspects.first) + @message2 = eve.post(:status_message, :text=> "hello", :to => eve.aspects.first) + end + + describe '#home_timeline' do + it 'authenticates' do + get :home_timeline, :format => :json + response.code.should == '401' + end + + it 'shows posts for alice' do + @user = alice + authenticate + + get :home_timeline, :format => :json + p = JSON.parse(response.body) + + p.length.should == 1 + p[0]['id'].should == @message1.guid + end + + it 'shows posts for eve' do + @user = eve + authenticate + + get :home_timeline, :format => :json + p = JSON.parse(response.body) + + p.length.should == 1 + p[0]['id'].should == @message2.guid + end + + it 'shows posts for bob' do + @user = bob + authenticate + + get :home_timeline, :format => :json + p = JSON.parse(response.body) + + p.length.should == 2 + end + end + + describe '#user_timeline' do + it 'authenticates' do + get :home_timeline, :format => :json + response.code.should == '401' + end + + context 'with bob logged in' do + before do + @user = bob + authenticate + end + + it 'shows alice' do + get :user_timeline, :format => :json, :user_id => alice.person.id + p = JSON.parse(response.body) + + p.length.should == 1 + p[0]['id'].should == @message1.guid + end + + it 'shows eve' do + get :user_timeline, :format => :json, :user_id => eve.person.id + p = JSON.parse(response.body) + + p.length.should == 1 + p[0]['id'].should == @message2.guid + end + + it 'shows bob' do + get :user_timeline, :format => :json + p = JSON.parse(response.body) + p.length.should == 0 + end + end + + context 'with alice logged in' do + before do + @user = alice + authenticate + end + + it 'shows alice' do + get :user_timeline, :format => :json, :user_id => alice.person.id + p = JSON.parse(response.body) + + p.length.should == 1 + p[0]['id'].should == @message1.guid + end + + it 'shows eve' do + get :user_timeline, :format => :json, :user_id => eve.person.id + p = JSON.parse(response.body) + p.length.should == 0 + end + end + end + end + + describe '#users_profile_image' do + it 'redirects on success' do + get :users_profile_image, :screen_name => bob.diaspora_handle, :format => :json + response.should redirect_to(bob.person.profile.image_url) + end + end + describe '#statuses' do it 'returns a post' do get :statuses, :guid => @status_message1.guid, :format => :json