diff --git a/app/controllers/apis_controller.rb b/app/controllers/apis_controller.rb index 71d61c938..93d008f1a 100644 --- a/app/controllers/apis_controller.rb +++ b/app/controllers/apis_controller.rb @@ -1,5 +1,5 @@ class ApisController < ApplicationController #We should start with this versioned, V0ApisController BEES - before_filter :authenticate_user!, :only => [:home_timeline, :user_timeline] + before_filter :authenticate_user!, :only => [:home_timeline] respond_to :json #posts @@ -11,13 +11,15 @@ class ApisController < ApplicationController #We should start with this versione end end - def user_timeline #No public timeline for a user? - R + def user_timeline set_defaults - person_id = params[:user_id] || current_user.person.guid # I wouldn't put implicit params in anything meant to be programatically accessed - R - - if person = Person.where(:guid => person_id).first - timeline = current_user.posts_from(person) + if person = Person.where(:guid => params[:user_id]).first + if user_signed_in? + timeline = current_user.posts_from(person) + else + timeline = StatusMessage.where(:public => true, :author_id => person.id).includes(:photos).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC") + end respond_with timeline do |format| format.json{ render :json => timeline.to_json(:format => :twitter) } end @@ -58,7 +60,7 @@ class ApisController < ApplicationController #We should start with this versione person = Person.where(:diaspora_handle => params[:screen_name]).first end - if person + if person && !person.remote? respond_with person do |format| format.json{ render :json => person.to_json(:format => :twitter) } end diff --git a/config/routes.rb b/config/routes.rb index 0f4a8c567..a96ccba33 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -82,7 +82,7 @@ Diaspora::Application.routes.draw do match 'receive/users/:guid', :to => 'publics#receive' match 'hub', :to => 'publics#hub' - scope '/api' do + scope '/api/v0' do match '/statuses/public_timeline', :to => 'apis#public_timeline' match '/statuses/home_timeline', :to => 'apis#home_timeline' match '/statuses/show/:guid', :to => 'apis#statuses' @@ -94,7 +94,6 @@ Diaspora::Application.routes.draw do match '/tags_posts/:tag', :to => 'apis#tag_posts' match '/tags_people/:tag', :to => 'apis#tag_people' - match '/person/:diaspora_handle', :to => 'apis#people' end match'localize', :to => "localize#show" diff --git a/spec/controllers/apis_controller_spec.rb b/spec/controllers/apis_controller_spec.rb index 60beb845f..3b101b34e 100644 --- a/spec/controllers/apis_controller_spec.rb +++ b/spec/controllers/apis_controller_spec.rb @@ -92,58 +92,68 @@ describe ApisController do 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 + context 'unauthenticated' do + it 'shows public posts' do + get :user_timeline, :format => :json, :user_id => @status_message1.author.guid + posts = JSON.parse(response.body) + posts.first['id'].should == @status_message1.guid + posts.length.should == 1 end - - it 'shows alice' do + it 'does not show non-public posts' do get :user_timeline, :format => :json, :user_id => alice.person.guid - 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.guid - 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 + posts = JSON.parse(response.body) + posts.should be_empty end end + context 'authenticated' do + context 'with bob logged in' do + before do + @user = bob + authenticate + end - context 'with alice logged in' do - before do - @user = alice - authenticate + it 'shows alice' do + get :user_timeline, :format => :json, :user_id => alice.person.guid + 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.guid + 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, :user_id => bob.person.guid + p = JSON.parse(response.body) + p.length.should == 0 + end end - it 'shows alice' do - get :user_timeline, :format => :json, :user_id => alice.person.guid - p = JSON.parse(response.body) + context 'with alice logged in' do + before do + @user = alice + authenticate + end - p.length.should == 1 - p[0]['id'].should == @message1.guid - end + it 'shows alice' do + get :user_timeline, :format => :json, :user_id => alice.person.guid + p = JSON.parse(response.body) - it 'shows eve' do - get :user_timeline, :format => :json, :user_id => eve.person.guid - p = JSON.parse(response.body) - p.length.should == 0 + p.length.should == 1 + p[0]['id'].should == @message1.guid + end + + it 'shows eve' do + get :user_timeline, :format => :json, :user_id => eve.person.guid + p = JSON.parse(response.body) + p.length.should == 0 + end end end end @@ -176,12 +186,12 @@ describe ApisController do describe '#users' do it 'succeeds' do - get :users, :user_id => @person.guid, :format => :json + get :users, :user_id => alice.person.guid, :format => :json p = JSON.parse(response.body) - p['id'].should == @person.guid - p['name'].should == @person.name - p['screen_name'].should == @person.diaspora_handle - p['profile_image_url'].should == @person.profile.image_url(:thumb_small) + p['id'].should == alice.person.guid + p['name'].should == alice.person.name + p['screen_name'].should == alice.person.diaspora_handle + p['profile_image_url'].should == alice.person.profile.image_url(:thumb_small) p['created_at'].should_not be_nil end end @@ -210,5 +220,4 @@ describe ApisController do p['id'].should == @person.guid end end - end