New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add federation relay support #7998
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class RelaysController < BaseController | ||
before_action :set_relay, except: [:index, :new, :create] | ||
|
||
def index | ||
authorize :relay, :update? | ||
@relays = Relay.all | ||
end | ||
|
||
def new | ||
authorize :relay, :update? | ||
@relay = Relay.new(inbox_url: Relay::PRESET_RELAY) | ||
end | ||
|
||
def create | ||
authorize :relay, :update? | ||
|
||
@relay = Relay.new(resource_params) | ||
|
||
if @relay.save | ||
@relay.enable! | ||
redirect_to admin_relays_path | ||
else | ||
render action: :new | ||
end | ||
end | ||
|
||
def destroy | ||
authorize :relay, :update? | ||
@relay.destroy | ||
redirect_to admin_relays_path | ||
end | ||
|
||
def enable | ||
authorize :relay, :update? | ||
@relay.enable! | ||
redirect_to admin_relays_path | ||
end | ||
|
||
def disable | ||
authorize :relay, :update? | ||
@relay.disable! | ||
redirect_to admin_relays_path | ||
end | ||
|
||
private | ||
|
||
def set_relay | ||
@relay = Relay.find(params[:id]) | ||
end | ||
|
||
def resource_params | ||
params.require(:relay).permit(:inbox_url) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
# == Schema Information | ||
# | ||
# Table name: relays | ||
# | ||
# id :bigint(8) not null, primary key | ||
# inbox_url :string default(""), not null | ||
# enabled :boolean default(FALSE), not null | ||
# follow_activity_id :string | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
|
||
class Relay < ApplicationRecord | ||
PRESET_RELAY = 'https://relay.joinmastodon.org/inbox' | ||
|
||
validates :inbox_url, presence: true, uniqueness: true, url: true, if: :will_save_change_to_inbox_url? | ||
|
||
scope :enabled, -> { where(enabled: true) } | ||
|
||
before_destroy :ensure_disabled | ||
|
||
def enable! | ||
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil) | ||
payload = Oj.dump(follow_activity(activity_id)) | ||
|
||
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url) | ||
update(enabled: true, follow_activity_id: activity_id) | ||
end | ||
|
||
def disable! | ||
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil) | ||
payload = Oj.dump(unfollow_activity(activity_id)) | ||
|
||
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url) | ||
update(enabled: false, follow_activity_id: nil) | ||
end | ||
|
||
private | ||
|
||
def follow_activity(activity_id) | ||
{ | ||
'@context': ActivityPub::TagManager::CONTEXT, | ||
id: activity_id, | ||
type: 'Follow', | ||
actor: ActivityPub::TagManager.instance.uri_for(some_local_account), | ||
object: ActivityPub::TagManager::COLLECTIONS[:public], | ||
} | ||
end | ||
|
||
def unfollow_activity(activity_id) | ||
{ | ||
'@context': ActivityPub::TagManager::CONTEXT, | ||
id: activity_id, | ||
type: 'Undo', | ||
actor: ActivityPub::TagManager.instance.uri_for(some_local_account), | ||
object: { | ||
id: follow_activity_id, | ||
type: 'Follow', | ||
actor: ActivityPub::TagManager.instance.uri_for(some_local_account), | ||
object: ActivityPub::TagManager::COLLECTIONS[:public], | ||
}, | ||
} | ||
end | ||
|
||
def some_local_account | ||
@some_local_account ||= Account.local.find_by(suspended: false) | ||
end | ||
|
||
def ensure_disabled | ||
return unless enabled? | ||
disable! | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class RelayPolicy < ApplicationPolicy | ||
def update? | ||
admin? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
%tr | ||
%td | ||
%samp= relay.inbox_url | ||
%td | ||
- if relay.enabled? | ||
%span.positive-hint | ||
= fa_icon('check') | ||
= ' ' | ||
= t 'admin.relays.enabled' | ||
- else | ||
%span.negative-hint | ||
= fa_icon('times') | ||
= ' ' | ||
= t 'admin.relays.disabled' | ||
%td | ||
- if relay.enabled? | ||
= table_link_to 'power-off', t('admin.relays.disable'), disable_admin_relay_path(relay), method: :post, data: { confirm: t('admin.accounts.are_you_sure') } | ||
- else | ||
= table_link_to 'power-off', t('admin.relays.enable'), enable_admin_relay_path(relay), method: :post, data: { confirm: t('admin.accounts.are_you_sure') } | ||
|
||
= table_link_to 'times', t('admin.relays.delete'), admin_relay_path(relay), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') } |
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,20 @@ | ||
- content_for :page_title do | ||
= t('admin.relays.title') | ||
|
||
.simple_form | ||
%p.hint= t('admin.relays.description_html') | ||
= link_to @relays.empty? ? t('admin.relays.setup') : t('admin.relays.add_new'), new_admin_relay_path, class: 'block-button' | ||
|
||
- unless @relays.empty? | ||
%hr.spacer | ||
|
||
.table-wrapper | ||
%table.table | ||
%thead | ||
%tr | ||
%th= t('admin.relays.inbox_url') | ||
%th= t('admin.relays.status') | ||
%th | ||
%tbody | ||
= render @relays | ||
|
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,13 @@ | ||
- content_for :page_title do | ||
= t('admin.relays.add_new') | ||
|
||
= simple_form_for @relay, url: admin_relays_path do |f| | ||
= render 'shared/error_messages', object: @relay | ||
|
||
.field-group | ||
= f.input :inbox_url, as: :string, wrapper: :with_block_label | ||
|
||
.actions | ||
= f.button :button, t('admin.relays.save_and_enable'), type: :submit | ||
|
||
%p.hint.subtle-hint= t('admin.relays.enable_hint') |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the Follow activity does not contain the actor, it seems? It seems the actor is implied at the relay side based on who signed the message... I'd prefer just implicitly mentioning it in the Follow..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume you meant "explicitly" in the last sentence?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some_local_account
is also confusing. It is already present inReportService
, but we are going to have two activities (Follow
for this case andFlag
forReportService
). I think it is a good time to have account-independentService
if Mastodon is going to have more and more such activities.