statuses/ and users/ GET routes
This commit is contained in:
parent
2dca7b4807
commit
517f3fd802
5 changed files with 140 additions and 55 deletions
|
|
@ -1,25 +1,74 @@
|
||||||
class ApisController < ActionController::Metal
|
class ApisController < ApplicationController #ActionController::Metal
|
||||||
include ActionController::Rendering
|
#include ActionController::Rendering
|
||||||
include ActionController::Renderers::All
|
#include ActionController::Renderers::All
|
||||||
|
|
||||||
|
respond_to :json
|
||||||
|
|
||||||
## posts
|
#posts
|
||||||
def posts_index
|
def public_timeline
|
||||||
set_defaults
|
set_defaults
|
||||||
sm = StatusMessage.where(:public => true).includes(:photos, :author => :profile).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
|
timeline = StatusMessage.where(:public => true).includes(:photos, :author => :profile).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
|
||||||
render :json => sm.to_json
|
respond_with timeline do |format|
|
||||||
|
format.json{ render :json => timeline.to_json(:format => :twitter) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def posts
|
def user_timeline
|
||||||
sm = StatusMessage.where(:guid => params[:guid], :public => true).includes(:photos, :author => :profile).first
|
set_defaults
|
||||||
if sm
|
if params[:user_id]
|
||||||
render :json => sm.to_json
|
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
|
||||||
|
end
|
||||||
|
respond_with timeline
|
||||||
|
end
|
||||||
|
|
||||||
|
def statuses
|
||||||
|
status = StatusMessage.where(:guid => params[:guid], :public => true).includes(:photos, :author => :profile).first
|
||||||
|
if status
|
||||||
|
respond_with status do |format|
|
||||||
|
format.json{ render :json => status.to_json(:format => :twitter) }
|
||||||
|
end
|
||||||
else
|
else
|
||||||
render(:nothing => true, :status => 404)
|
render(:nothing => true, :status => 404)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
#people
|
||||||
|
def users
|
||||||
|
if params[:user_id]
|
||||||
|
person = Person.where(:id => params[:user_id]).first
|
||||||
|
elsif params[:screen_name]
|
||||||
|
person = Person.where(:diaspora_handle => params[:screen_name]).first
|
||||||
|
end
|
||||||
|
|
||||||
|
if person
|
||||||
|
respond_with person do |format|
|
||||||
|
format.json{ render :json => person.to_json(:format => :twitter) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
render(:nothing => true, :status => 404)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def users_search
|
||||||
|
set_defaults
|
||||||
|
|
||||||
|
if params[:q]
|
||||||
|
people = Person.public_search(params[:q]).paginate(:page => params[:page], :per_page => params[:per_page], :order => "#{params[:order]} DESC")
|
||||||
|
end
|
||||||
|
|
||||||
|
if people
|
||||||
|
respond_with people do |format|
|
||||||
|
format.json{ render :json => people.to_json(:format => :twitter) }
|
||||||
|
end
|
||||||
|
else
|
||||||
|
render(:nothing => true, :status => 404)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
#tags
|
#tags
|
||||||
#
|
|
||||||
def tag_posts
|
def tag_posts
|
||||||
set_defaults
|
set_defaults
|
||||||
posts = StatusMessage.where(:public => true, :pending => false)
|
posts = StatusMessage.where(:public => true, :pending => false)
|
||||||
|
|
@ -35,26 +84,9 @@ class ApisController < ActionController::Metal
|
||||||
render :json => people.as_json
|
render :json => people.as_json
|
||||||
end
|
end
|
||||||
|
|
||||||
##people
|
|
||||||
def people_index
|
|
||||||
set_defaults
|
|
||||||
people = Person.public_search(params[:q]).paginate(:page => params[:page], :per_page => params[:per_page], :order => "profiles.last_name ASC, profiles.first_name ASC")
|
|
||||||
render :json => people.as_json
|
|
||||||
end
|
|
||||||
|
|
||||||
def people
|
|
||||||
person = Person.where(:diaspora_handle => params[:diaspora_handle]).first
|
|
||||||
if person
|
|
||||||
render :json => person.as_json
|
|
||||||
else
|
|
||||||
render(:nothing => true, :status => 404)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def set_defaults
|
def set_defaults
|
||||||
params[:per_page] ||= 15
|
params[:per_page] ||= 20
|
||||||
params[:order] ||= 'created_at'
|
params[:order] ||= 'created_at'
|
||||||
params[:page] ||= 1
|
params[:page] ||= 1
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -186,7 +186,21 @@ class Person < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_json(opts={})
|
def as_json(opts={})
|
||||||
super(:include => [:profile], :except => [:mongo_id, :owner_id, :serialized_public_key])
|
opts ||= {}
|
||||||
|
if(opts[:format] == :twitter)
|
||||||
|
{
|
||||||
|
:id => self.id,
|
||||||
|
:screen_name => self.diaspora_handle,
|
||||||
|
:name => self.name,
|
||||||
|
:created_at => self.created_at,
|
||||||
|
:profile_image_url => self.profile.image_url(:thumb_small)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
super(:include => [:profile], :except => [:mongo_id, :owner_id, :serialized_public_key])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_twitter(format=:json)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
||||||
|
|
@ -122,6 +122,26 @@ class StatusMessage < Post
|
||||||
XML
|
XML
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def as_json(opts={})
|
||||||
|
opts ||= {}
|
||||||
|
if(opts[:format] == :twitter)
|
||||||
|
{
|
||||||
|
:id => self.guid,
|
||||||
|
:text => self.formatted_message(:plain_text => true),
|
||||||
|
:entities => {
|
||||||
|
:urls => [],
|
||||||
|
:hashtags => self.tag_list,
|
||||||
|
:user_mentions => self.mentioned_people.map{|p| p.diaspora_handle},
|
||||||
|
},
|
||||||
|
:source => 'diaspora',
|
||||||
|
:created_at => self.created_at,
|
||||||
|
:user => self.author.to_json(opts)
|
||||||
|
}
|
||||||
|
else
|
||||||
|
super(opts)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def message_or_photos_present?
|
def message_or_photos_present?
|
||||||
|
|
|
||||||
|
|
@ -82,12 +82,16 @@ Diaspora::Application.routes.draw do
|
||||||
match 'receive/users/:guid', :to => 'publics#receive'
|
match 'receive/users/:guid', :to => 'publics#receive'
|
||||||
match 'hub', :to => 'publics#hub'
|
match 'hub', :to => 'publics#hub'
|
||||||
|
|
||||||
scope '/v-1' do
|
scope '/api' do
|
||||||
match '/posts', :to => 'apis#posts_index'
|
match '/statuses/public_timeline', :to => 'apis#public_timeline'
|
||||||
match '/posts/:guid', :to => 'apis#posts'
|
match '/statuses/show/:guid', :to => 'apis#statuses'
|
||||||
|
|
||||||
|
match '/statuses/user_timeline', :to => 'apis#user_timeline'
|
||||||
|
match '/users/show', :to => 'apis#users'
|
||||||
|
match '/users/search', :to => 'apis#users_search'
|
||||||
|
|
||||||
match '/tags_posts/:tag', :to => 'apis#tag_posts'
|
match '/tags_posts/:tag', :to => 'apis#tag_posts'
|
||||||
match '/tags_people/:tag', :to => 'apis#tag_people'
|
match '/tags_people/:tag', :to => 'apis#tag_people'
|
||||||
match '/people', :to => 'apis#people_index'
|
|
||||||
match '/person/:diaspora_handle', :to => 'apis#people'
|
match '/person/:diaspora_handle', :to => 'apis#people'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,10 +17,10 @@ describe ApisController do
|
||||||
@person.profile.save
|
@person.profile.save
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#posts' do
|
describe '#public_timeline' do
|
||||||
|
|
||||||
it 'returns all of the public posts' do
|
it 'returns all of the public posts' do
|
||||||
get :posts_index, :format => :json
|
get :public_timeline, :format => :json
|
||||||
@posts = JSON.parse(response.body)
|
@posts = JSON.parse(response.body)
|
||||||
@posts.count.should == 2
|
@posts.count.should == 2
|
||||||
end
|
end
|
||||||
|
|
@ -34,50 +34,65 @@ describe ApisController do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'accepts a per_page param' do
|
it 'accepts a per_page param' do
|
||||||
get :posts_index, :format => :json, :per_page=> 1
|
get :public_timeline, :format => :json, :per_page=> 1
|
||||||
JSON.parse(response.body).count.should == 1
|
JSON.parse(response.body).count.should == 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#post' do
|
describe '#statuses' do
|
||||||
it 'returns a post' do
|
it 'returns a post' do
|
||||||
get :posts, :guid => @status_message1.guid, :format => :json
|
get :statuses, :guid => @status_message1.guid, :format => :json
|
||||||
p = JSON.parse(response.body)
|
p = JSON.parse(response.body)
|
||||||
p['guid'].should == @status_message1.guid
|
p['id'].should == @status_message1.guid
|
||||||
|
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['created_at'].should_not be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns a 404 if does not exsist' do
|
it 'returns a 404 if does not exsist' do
|
||||||
get :posts, :guid => 999
|
get :statuses, :guid => 999
|
||||||
response.code.should == '404'
|
response.code.should == '404'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#users' do
|
||||||
|
it 'succeeds' do
|
||||||
|
get :users, :user_id => @person.id, :format => :json
|
||||||
|
p = JSON.parse(response.body)
|
||||||
|
p['id'].should == @person.id
|
||||||
|
p['name'].should == @person.name
|
||||||
|
p['screen_name'].should == @person.diaspora_handle
|
||||||
|
p['profile_image_url'].should == @person.profile.image_url(:thumb_small)
|
||||||
|
p['created_at'].should_not be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe '#users_search' do
|
||||||
|
it 'searches' do
|
||||||
|
get :users_search, :q => @person.name, :format => :json
|
||||||
|
p = JSON.parse(response.body)
|
||||||
|
response.code.should == '200'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#tag_posts' do
|
describe '#tag_posts' do
|
||||||
it 'succeeds' do
|
it 'succeeds' do
|
||||||
|
pending
|
||||||
get :tag_posts, :tag => 'flay'
|
get :tag_posts, :tag => 'flay'
|
||||||
p = JSON.parse(response.body).first
|
p = JSON.parse(response.body).first
|
||||||
p['guid'].should == @status_message1.guid
|
p['id'].should == @status_message1.guid
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#tag_people' do
|
describe '#tag_people' do
|
||||||
it 'succeeds' do
|
it 'succeeds' do
|
||||||
|
pending
|
||||||
get :tag_people, :tag => 'zord'
|
get :tag_people, :tag => 'zord'
|
||||||
p = JSON.parse(response.body).first
|
p = JSON.parse(response.body).first
|
||||||
p['person']['id'].should == @person.id
|
p['person']['id'].should == @person.id
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#people_index' do
|
|
||||||
it 'succeeds' do
|
|
||||||
get :people_index, :q => 'bobby'
|
|
||||||
p = JSON.parse(response.body)
|
|
||||||
p.count.should == 1
|
|
||||||
p.first['person']['id'].should == @person.id
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#people' do
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue