Skip to content
Extraits de code Groupes Projets
Valider 2ec45317 rédigé par Steffen van Bergerem's avatar Steffen van Bergerem Validation de Benjamin Neff
Parcourir les fichiers

Add new JSON endpoint for reshares

parent 636c69c0
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -17,4 +17,16 @@ class ResharesController < ApplicationController
render :nothing => true, :status => 422
end
end
def index
@reshares = target.reshares.includes(author: :profile)
render json: @reshares.as_api_response(:backbone)
end
private
def target
@target ||= current_user.find_visible_shareable_by_id(Post, params[:post_id]) ||
raise(ActiveRecord::RecordNotFound.new)
end
end
......@@ -21,6 +21,14 @@ class Reshare < Post
self.root.update_reshares_counter if self.root.present?
end
acts_as_api
api_accessible :backbone do |t|
t.add :id
t.add :guid
t.add :author
t.add :created_at
end
def root_diaspora_id
root.try(:author).try(:diaspora_handle)
end
......
......@@ -38,6 +38,7 @@ Diaspora::Application.routes.draw do
resources :poll_participations, only: :create
resources :likes, only: %i(create destroy index)
resources :comments, only: %i(new create destroy index)
resources :reshares, only: :index
end
get 'p/:id' => 'posts#show', :as => 'short_post'
......
......@@ -64,4 +64,44 @@ describe ResharesController, :type => :controller do
end
end
end
describe "#index" do
context "with a private post" do
before do
@alices_aspect = alice.aspects.where(name: "generic").first
@post = alice.post(:status_message, text: "hey", to: @alices_aspect.id)
end
it "returns a 404 for a post not visible to the user" do
sign_in(eve, scope: :user)
expect {
get :index, post_id: @post.id, format: :json
}.to raise_error(ActiveRecord::RecordNotFound)
end
it "returns an empty array for a post visible to the user" do
sign_in(bob, scope: :user)
get :index, post_id: @post.id, format: :json
expect(JSON.parse(response.body)).to eq([])
end
end
context "with a public post" do
before do
sign_in(alice, scope: :user)
@post = alice.post(:status_message, text: "hey", public: true)
end
it "returns an array of reshares for a post" do
bob.reshare!(@post)
get :index, post_id: @post.id, format: :json
expect(JSON.parse(response.body).map {|h| h["id"] }).to eq(@post.reshares.map(&:id))
end
it "returns an empty array for a post with no likes" do
get :index, post_id: @post.id, format: :json
expect(JSON.parse(response.body)).to eq([])
end
end
end
end
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter