forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix embed dropdown menu item for unauthenticated users (mastodon#25964)
- Loading branch information
1 parent
068da55
commit 9e60485
Showing
10 changed files
with
194 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::Web::EmbedsController < Api::Web::BaseController | ||
before_action :require_user! | ||
include Authorization | ||
|
||
def create | ||
status = StatusFinder.new(params[:url]).status | ||
before_action :set_status | ||
|
||
return not_found if status.hidden? | ||
def show | ||
return not_found if @status.hidden? | ||
|
||
render json: status, serializer: OEmbedSerializer, width: 400 | ||
rescue ActiveRecord::RecordNotFound | ||
oembed = FetchOEmbedService.new.call(params[:url]) | ||
if @status.local? | ||
render json: @status, serializer: OEmbedSerializer, width: 400 | ||
else | ||
return not_found unless user_signed_in? | ||
|
||
return not_found if oembed.nil? | ||
url = ActivityPub::TagManager.instance.url_for(@status) | ||
oembed = FetchOEmbedService.new.call(url) | ||
return not_found if oembed.nil? | ||
|
||
begin | ||
oembed[:html] = Sanitize.fragment(oembed[:html], Sanitize::Config::MASTODON_OEMBED) | ||
rescue ArgumentError | ||
return not_found | ||
begin | ||
oembed[:html] = Sanitize.fragment(oembed[:html], Sanitize::Config::MASTODON_OEMBED) | ||
rescue ArgumentError | ||
return not_found | ||
end | ||
|
||
render json: oembed | ||
end | ||
end | ||
|
||
render json: oembed | ||
def set_status | ||
@status = Status.find(params[:id]) | ||
authorize @status, :show? | ||
rescue Mastodon::NotPermittedError | ||
not_found | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe '/api/web/embed' do | ||
subject { get "/api/web/embeds/#{id}", headers: headers } | ||
|
||
context 'when accessed anonymously' do | ||
let(:headers) { {} } | ||
|
||
context 'when the requested status is local' do | ||
let(:id) { status.id } | ||
|
||
context 'when the requested status is public' do | ||
let(:status) { Fabricate(:status, visibility: :public) } | ||
|
||
it 'returns JSON with an html attribute' do | ||
subject | ||
|
||
expect(response).to have_http_status(200) | ||
expect(body_as_json[:html]).to be_present | ||
end | ||
end | ||
|
||
context 'when the requested status is private' do | ||
let(:status) { Fabricate(:status, visibility: :private) } | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end | ||
|
||
context 'when the requested status is remote' do | ||
let(:remote_account) { Fabricate(:account, domain: 'example.com') } | ||
let(:status) { Fabricate(:status, visibility: :public, account: remote_account, url: 'https://example.com/statuses/1') } | ||
let(:id) { status.id } | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
|
||
context 'when the requested status does not exist' do | ||
let(:id) { -1 } | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end | ||
|
||
context 'with an API token' do | ||
let(:user) { Fabricate(:user) } | ||
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'read') } | ||
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } | ||
|
||
context 'when the requested status is local' do | ||
let(:id) { status.id } | ||
|
||
context 'when the requested status is public' do | ||
let(:status) { Fabricate(:status, visibility: :public) } | ||
|
||
it 'returns JSON with an html attribute' do | ||
subject | ||
|
||
expect(response).to have_http_status(200) | ||
expect(body_as_json[:html]).to be_present | ||
end | ||
|
||
context 'when the requesting user is blocked' do | ||
before do | ||
status.account.block!(user.account) | ||
end | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end | ||
|
||
context 'when the requested status is private' do | ||
let(:status) { Fabricate(:status, visibility: :private) } | ||
|
||
before do | ||
user.account.follow!(status.account) | ||
end | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end | ||
|
||
context 'when the requested status is remote' do | ||
let(:remote_account) { Fabricate(:account, domain: 'example.com') } | ||
let(:status) { Fabricate(:status, visibility: :public, account: remote_account, url: 'https://example.com/statuses/1') } | ||
let(:id) { status.id } | ||
|
||
let(:service_instance) { instance_double(FetchOEmbedService) } | ||
|
||
before do | ||
allow(FetchOEmbedService).to receive(:new) { service_instance } | ||
allow(service_instance).to receive(:call) { call_result } | ||
end | ||
|
||
context 'when the requesting user is blocked' do | ||
before do | ||
status.account.block!(user.account) | ||
end | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
|
||
context 'when successfully fetching OEmbed' do | ||
let(:call_result) { { html: 'ok' } } | ||
|
||
it 'returns JSON with an html attribute' do | ||
subject | ||
|
||
expect(response).to have_http_status(200) | ||
expect(body_as_json[:html]).to be_present | ||
end | ||
end | ||
|
||
context 'when failing to fetch OEmbed' do | ||
let(:call_result) { nil } | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end | ||
|
||
context 'when the requested status does not exist' do | ||
let(:id) { -1 } | ||
|
||
it 'returns http not found' do | ||
subject | ||
|
||
expect(response).to have_http_status(404) | ||
end | ||
end | ||
end | ||
end |