Skip to content

Commit

Permalink
Add: #198 contextのAPIについてwith_references対応 (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode authored Nov 1, 2023
1 parent cc938d7 commit 99511ee
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/api/v1/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def context
loaded_descendants = cache_collection(descendants_results, Status)
loaded_references = cache_collection(references_results, Status)

unless params[:with_reference]
loaded_ancestors = (loaded_ancestors + loaded_references).uniq(&:id)
loaded_references = []
end

@context = Context.new(ancestors: loaded_ancestors, descendants: loaded_descendants, references: loaded_references)
statuses = [@status] + @context.ancestors + @context.descendants + @context.references

Expand Down
2 changes: 1 addition & 1 deletion app/javascript/mastodon/actions/statuses.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export function fetchContext(id) {
return (dispatch, getState) => {
dispatch(fetchContextRequest(id));

api(getState).get(`/api/v1/statuses/${id}/context`).then(response => {
api(getState).get(`/api/v1/statuses/${id}/context?with_reference=1`).then(response => {
dispatch(importFetchedStatuses(response.data.ancestors.concat(response.data.descendants).concat(response.data.references)));
dispatch(fetchContextSuccess(id, response.data.ancestors, response.data.descendants, response.data.references));

Expand Down
63 changes: 63 additions & 0 deletions spec/controllers/api/v1/statuses_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,69 @@
end
end

context 'with reference' do
let(:status) { Fabricate(:status, account: user.account) }
let(:scopes) { 'read:statuses' }
let(:referred) { Fabricate(:status) }
let(:referred_private) { Fabricate(:status, visibility: :private) }
let(:referred_private_following) { Fabricate(:status, visibility: :private) }

before do
user.account.follow!(referred_private_following.account)
Fabricate(:status_reference, status: status, target_status: referred)
Fabricate(:status_reference, status: status, target_status: referred_private)
Fabricate(:status_reference, status: status, target_status: referred_private_following)
end

it 'returns http success' do
get :context, params: { id: status.id }
expect(response).to have_http_status(200)
end

it 'returns empty references' do
get :context, params: { id: status.id }
status_ids = body_as_json[:references].map { |ref| ref[:id].to_i }

expect(status_ids).to eq []
end

it 'contains referred status' do
get :context, params: { id: status.id }
status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }

expect(status_ids).to include referred.id
expect(status_ids).to include referred_private_following.id
end

it 'does not contain private status' do
get :context, params: { id: status.id }
status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }

expect(status_ids).to_not include referred_private.id
end

context 'when with_reference is enabled' do
it 'returns http success' do
get :context, params: { id: status.id, with_reference: true }
expect(response).to have_http_status(200)
end

it 'returns empty ancestors' do
get :context, params: { id: status.id, with_reference: true }
status_ids = body_as_json[:ancestors].map { |ref| ref[:id].to_i }

expect(status_ids).to eq []
end

it 'contains referred status' do
get :context, params: { id: status.id, with_reference: true }
status_ids = body_as_json[:references].map { |ref| ref[:id].to_i }

expect(status_ids).to include referred.id
end
end
end

describe 'POST #create' do
let(:scopes) { 'write:statuses' }

Expand Down

0 comments on commit 99511ee

Please sign in to comment.