diff --git a/app/controllers/apis_controller.rb b/app/controllers/apis_controller.rb index 49ffbc1b4..71d61c938 100644 --- a/app/controllers/apis_controller.rb +++ b/app/controllers/apis_controller.rb @@ -1,4 +1,4 @@ -class ApisController < ApplicationController +class ApisController < ApplicationController #We should start with this versioned, V0ApisController BEES before_filter :authenticate_user!, :only => [:home_timeline, :user_timeline] respond_to :json @@ -11,19 +11,18 @@ class ApisController < ApplicationController end end - def user_timeline + def user_timeline #No public timeline for a user? - R set_defaults - person_id = params[:user_id] || current_user.person.id + 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.find(person_id) + if person = Person.where(:guid => person_id).first timeline = current_user.posts_from(person) + respond_with timeline do |format| + format.json{ render :json => timeline.to_json(:format => :twitter) } + end else - timeline = [] - end - - respond_with timeline do |format| - format.json{ render :json => timeline.to_json(:format => :twitter) } + render :json => {:status => 'failed', :reason => 'user not found'}, :status => 404 end end @@ -54,7 +53,7 @@ class ApisController < ApplicationController #people def users if params[:user_id] - person = Person.where(:id => params[:user_id]).first + person = Person.where(:guid => params[:user_id]).first elsif params[:screen_name] person = Person.where(:diaspora_handle => params[:screen_name]).first end @@ -98,20 +97,20 @@ class ApisController < ApplicationController posts = StatusMessage.where(:public => true, :pending => false) posts = posts.tagged_with(params[:tag]) posts = posts.includes(:comments, :photos).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC") - render :json => posts.as_json + render :json => posts.as_json(:format => :twitter) end def tag_people set_defaults profiles = Profile.tagged_with(params[:tag]).where(:searchable => true).select('profiles.id, profiles.person_id') people = Person.where(:id => profiles.map{|p| p.person_id}).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC") - render :json => people.as_json + render :json => people.as_json(:format => :twitter) end protected def set_defaults params[:per_page] ||= 20 - params[:order] ||= 'created_at' + params[:order] = 'created_at' unless ['created_at', 'updated_at'].include?(params[:order]) params[:page] ||= 1 end end diff --git a/app/models/person.rb b/app/models/person.rb index 673c204cc..3614f5e2f 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -189,11 +189,12 @@ class Person < ActiveRecord::Base opts ||= {} if(opts[:format] == :twitter) { - :id => self.id, + :id => self.guid, :screen_name => self.diaspora_handle, :name => self.name, :created_at => self.created_at, - :profile_image_url => self.profile.image_url(:thumb_small) + :profile_image_url => self.profile.image_url(:thumb_small), + :profile => self.profile.as_json(opts) } else super(:include => [:profile], :except => [:mongo_id, :owner_id, :serialized_public_key]) diff --git a/app/models/post.rb b/app/models/post.rb index 627496a68..9c47b6deb 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -47,7 +47,7 @@ class Post < ActiveRecord::Base def as_json(opts={}) { :post => { - :id => self.id, + :id => self.guid, :author => self.author.as_json, } } diff --git a/app/models/status_message.rb b/app/models/status_message.rb index 661d70ce0..8c72aa03d 100644 --- a/app/models/status_message.rb +++ b/app/models/status_message.rb @@ -96,17 +96,6 @@ class StatusMessage < Post identifiers.empty? ? [] : Person.where(:diaspora_handle => identifiers) end - def as_json(opts={}) - {:guid => self.guid, - :id => self.id, - :author => self.author.as_json, - :photos => self.photos.as_json, - :created_at => self.created_at, - :updated_at => self.updated_at, - :raw_message => self.raw_message, - :text => self.text, } - end - def to_activity <<-XML @@ -135,7 +124,7 @@ class StatusMessage < Post }, :source => 'diaspora', :created_at => self.created_at, - :user => self.author.to_json(opts) + :user => self.author.as_json(opts) } else super(opts) diff --git a/spec/controllers/apis_controller_spec.rb b/spec/controllers/apis_controller_spec.rb index 54aaae0c8..60beb845f 100644 --- a/spec/controllers/apis_controller_spec.rb +++ b/spec/controllers/apis_controller_spec.rb @@ -1,18 +1,11 @@ require 'spec_helper' -class ApisController - include ActionController::UrlFor - include ActionController::Testing - include Rails.application.routes.url_helpers - include ActionController::Compatibility -end - describe ApisController do before(:all) do - @status_message1 = Factory(:status_message, :text => '#bobby #flay #sux', :public => true) + @status_message1 = Factory(:status_message, :text => '#bobby #flay #sux', :public => true, :updated_at => Time.now + 20) - @status_message2 = Factory(:status_message, :public => true) - @status_message3 = Factory(:status_message) + @status_message2 = Factory(:status_message, :text => '#aobby', :public => true, :created_at => Time.now + 10) + @status_message3 = Factory(:status_message, :created_at => Time.now + 15) @person = Factory(:person, :profile => Factory.build(:profile,:first_name => 'bobby', :searchable => true, :tag_string => '#zord')) @person.profile.save end @@ -21,15 +14,25 @@ describe ApisController do it 'returns all of the public posts' do get :public_timeline, :format => :json @posts = JSON.parse(response.body) + @posts.map{|p| p['id']}.should == [@status_message2.guid, @status_message1.guid] @posts.count.should == 2 end it 'accepts an order paramater' do - pending + get :public_timeline, :format => :json, :order => 'updated_at' + @posts = JSON.parse(response.body) + @posts.map{|p| p['id']}.should == [@status_message1.guid, @status_message2.guid] end - it 'accpets a page paramater' do - pending + it 'does not allow arbitrary orders' do + get :public_timeline, :format => :json, :order => 'text' + @posts = JSON.parse(response.body) + @posts.map{|p| p['id']}.should == [@status_message2.guid, @status_message1.guid] + end + + it 'accepts a page paramater' do + get :public_timeline, :format => :json, :per_page=> 1, :page => 2 + JSON.parse(response.body).first['id'].should == @status_message1.guid end it 'accepts a per_page param' do @@ -101,7 +104,7 @@ describe ApisController do end it 'shows alice' do - get :user_timeline, :format => :json, :user_id => alice.person.id + get :user_timeline, :format => :json, :user_id => alice.person.guid p = JSON.parse(response.body) p.length.should == 1 @@ -109,7 +112,7 @@ describe ApisController do end it 'shows eve' do - get :user_timeline, :format => :json, :user_id => eve.person.id + get :user_timeline, :format => :json, :user_id => eve.person.guid p = JSON.parse(response.body) p.length.should == 1 @@ -130,7 +133,7 @@ describe ApisController do end it 'shows alice' do - get :user_timeline, :format => :json, :user_id => alice.person.id + get :user_timeline, :format => :json, :user_id => alice.person.guid p = JSON.parse(response.body) p.length.should == 1 @@ -138,7 +141,7 @@ describe ApisController do end it 'shows eve' do - get :user_timeline, :format => :json, :user_id => eve.person.id + get :user_timeline, :format => :json, :user_id => eve.person.guid p = JSON.parse(response.body) p.length.should == 0 end @@ -161,7 +164,7 @@ describe ApisController do p['text'].should == @status_message1.formatted_message(:plain_text => true) p['entities'].class.should == Hash p['source'].should == 'diaspora' - p['user'].should == @status_message1.author.to_json(:format => :twitter) + p['user'].should == JSON.parse(@status_message1.author.to_json(:format => :twitter)) p['created_at'].should_not be_nil end @@ -173,9 +176,9 @@ describe ApisController do describe '#users' do it 'succeeds' do - get :users, :user_id => @person.id, :format => :json + get :users, :user_id => @person.guid, :format => :json p = JSON.parse(response.body) - p['id'].should == @person.id + 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) @@ -193,19 +196,18 @@ describe ApisController do describe '#tag_posts' do it 'succeeds' do - pending get :tag_posts, :tag => 'flay' p = JSON.parse(response.body).first p['id'].should == @status_message1.guid + p['user']['id'].should == @status_message1.author.guid end end describe '#tag_people' do it 'succeeds' do - pending get :tag_people, :tag => 'zord' p = JSON.parse(response.body).first - p['person']['id'].should == @person.id + p['id'].should == @person.guid end end