added users_profile_image, home_timeline, fixed public_timeline
This commit is contained in:
parent
517f3fd802
commit
5dbbc79f65
3 changed files with 152 additions and 12 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue