Skip to content
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

Change: 単方向の承認だけでフレンドサーバーが有効になるようにする #74

Merged
merged 10 commits into from
Oct 10, 2023
8 changes: 6 additions & 2 deletions app/controllers/admin/friend_servers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def create
def update
authorize :friend_server, :update?

if @friend.update(resource_params)
if @friend.update(update_resource_params)
redirect_to admin_friend_servers_path
else
render action: :edit
Expand Down Expand Up @@ -79,7 +79,11 @@ def set_friend
end

def resource_params
params.require(:friend_domain).permit(:domain, :inbox_url, :available, :pseudo_relay, :unlocked, :allow_all_posts)
params.require(:friend_domain).permit(:domain, :inbox_url, :available, :pseudo_relay, :delivery_local, :unlocked, :allow_all_posts)
end

def update_resource_params
params.require(:friend_domain).permit(:inbox_url, :available, :pseudo_relay, :delivery_local, :unlocked, :allow_all_posts)
end

def warn_signatures_not_enabled!
Expand Down
2 changes: 1 addition & 1 deletion app/lib/activitypub/activity/accept.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def relay_follow?
end

def accept_follow_for_friend
friend.update!(active_state: :accepted)
friend.update!(active_state: :accepted, passive_state: :idle)
end

def friend
Expand Down
6 changes: 5 additions & 1 deletion app/lib/activitypub/activity/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def find_existing_status
end

def process_status_params
@status_parser = ActivityPub::Parser::StatusParser.new(@json, followers_collection: @account.followers_url, object: @object, account: @account)
@status_parser = ActivityPub::Parser::StatusParser.new(@json, followers_collection: @account.followers_url, object: @object, account: @account, friend_domain: friend_domain?)

@params = {
uri: @status_parser.uri,
Expand Down Expand Up @@ -506,6 +506,10 @@ def free_friend_domain?
FriendDomain.free_receivings.exists?(domain: @account.domain)
end

def friend_domain?
FriendDomain.enabled.find_by(domain: @account.domain)&.accepted?
end

def quote
@quote ||= @object['quote'] || @object['quoteUrl'] || @object['quoteURL'] || @object['_misskey_quote']
end
Expand Down
14 changes: 9 additions & 5 deletions app/lib/activitypub/activity/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ def request_follow_for_friend
already_accepted = false

if friend.present?
already_accepted = friend.they_are_accepted?
friend.update!(passive_state: :pending, passive_follow_activity_id: @json['id'])
already_accepted = friend.accepted?
friend.update!(passive_state: :pending, active_state: :idle, passive_follow_activity_id: @json['id'])
else
@friend = FriendDomain.create!(domain: @account.domain, passive_state: :pending, passive_follow_activity_id: @json['id'])
@friend = FriendDomain.new(domain: @account.domain, passive_state: :pending, passive_follow_activity_id: @json['id'])
@friend.initialize_inbox_url!
@friend.save!
end

if already_accepted || friend.unlocked || Setting.unlocked_friend
if already_accepted || Setting.unlocked_friend
friend.accept!
else

# Notify for admin even if unlocked
notify_staff_about_pending_friend_server! unless already_accepted
else
notify_staff_about_pending_friend_server!
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/lib/activitypub/activity/reject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def relay_follow?
end

def reject_follow_for_friend
friend.update!(active_state: :rejected)
friend.update!(active_state: :rejected, passive_state: :idle)
end

def friend
Expand Down
2 changes: 1 addition & 1 deletion app/lib/activitypub/activity/undo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def undo_follow
end

def remove_follow_from_friend
friend.update!(passive_state: :idle, passive_follow_activity_id: nil)
friend.destroy_without_signal!
end

def friend
Expand Down
5 changes: 3 additions & 2 deletions app/lib/activitypub/parser/status_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def initialize(json, magic_values = {})
@object = magic_values[:object] || json['object'] || json
@magic_values = magic_values
@account = magic_values[:account]
@friend = magic_values[:friend_domain]
end

def uri
Expand Down Expand Up @@ -76,7 +77,7 @@ def sensitive
def visibility
if audience_to.any? { |to| ActivityPub::TagManager.instance.public_collection?(to) }
:public
elsif audience_to.include?('LocalPublic')
elsif audience_to.include?('LocalPublic') && @friend
:public_unlisted
elsif audience_cc.any? { |cc| ActivityPub::TagManager.instance.public_collection?(cc) }
:unlisted
Expand Down Expand Up @@ -200,7 +201,7 @@ def searchability_from_audience
:public
elsif audience_searchable_by.include?('as:Limited')
:limited
elsif audience_searchable_by.include?('LocalPublic')
elsif audience_searchable_by.include?('LocalPublic') && @friend
:public_unlisted
elsif audience_searchable_by.include?(@account.followers_url)
:private
Expand Down
12 changes: 10 additions & 2 deletions app/lib/status_reach_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(status, options = {})
end

def inboxes
(reached_account_inboxes + followers_inboxes + relay_inboxes).uniq
(reached_account_inboxes + followers_inboxes + relay_inboxes + nolocal_friend_inboxes).uniq
end

def inboxes_for_misskey
Expand Down Expand Up @@ -147,7 +147,15 @@ def relay_inboxes

def friend_inboxes
if @status.public_visibility? || @status.public_unlisted_visibility? || (@status.unlisted_visibility? && (@status.public_searchability? || @status.public_unlisted_searchability?))
DeliveryFailureTracker.without_unavailable(FriendDomain.distributables.pluck(:inbox_url))
DeliveryFailureTracker.without_unavailable(FriendDomain.distributables.where(delivery_local: true).pluck(:inbox_url))
else
[]
end
end

def nolocal_friend_inboxes
if @status.public_visibility?
DeliveryFailureTracker.without_unavailable(FriendDomain.distributables.where(delivery_local: false).pluck(:inbox_url))
else
[]
end
Expand Down
43 changes: 30 additions & 13 deletions app/models/friend_domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# passive_follow_activity_id :string
# available :boolean default(TRUE), not null
# pseudo_relay :boolean default(FALSE), not null
# unlocked :boolean default(FALSE), not null
# allow_all_posts :boolean default(TRUE), not null
# created_at :datetime not null
# updated_at :datetime not null
# delivery_local :boolean default(TRUE), not null
#

class FriendDomain < ApplicationRecord
Expand All @@ -27,24 +27,31 @@ class FriendDomain < ApplicationRecord
enum passive_state: { idle: 0, pending: 1, accepted: 2, rejected: 3 }, _prefix: :they_are

scope :by_domain_and_subdomains, ->(domain) { where(domain: Instance.by_domain_and_subdomains(domain).select(:domain)) }
scope :enabled, -> { where(available: true) }
scope :mutuals, -> { enabled.where(active_state: :accepted, passive_state: :accepted) }
scope :distributables, -> { mutuals.where(pseudo_relay: true) }
scope :deliver_locals, -> { enabled.where(active_state: :accepted) }
scope :free_receivings, -> { mutuals.where(allow_all_posts: true) }
scope :enabled, -> { where(active_state: :accepted).or(FriendDomain.where(passive_state: :accepted)).where(available: true) }
scope :distributables, -> { enabled.where(pseudo_relay: true) }
scope :deliver_locals, -> { enabled.where(delivery_local: true) }
scope :free_receivings, -> { enabled.where(allow_all_posts: true) }

before_destroy :ensure_disabled
after_commit :set_default_inbox_url

def mutual?
i_am_accepted? && they_are_accepted?
def accepted?
i_am_accepted? || they_are_accepted?
end

def pending?
!accepted? && (i_am_pending? || they_are_pending?)
end

def idle?
(i_am_idle? || i_am_rejected?) && (they_are_idle? || they_are_rejected?)
end

def follow!
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
payload = Oj.dump(follow_activity(activity_id))

update!(active_state: :pending, active_follow_activity_id: activity_id)
update!(active_state: :pending, passive_state: :idle, active_follow_activity_id: activity_id)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
Expand All @@ -53,7 +60,7 @@ def unfollow!
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
payload = Oj.dump(unfollow_activity(activity_id))

update!(active_state: :idle, active_follow_activity_id: nil)
update!(active_state: :idle, passive_state: :idle, active_follow_activity_id: nil)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
Expand All @@ -64,7 +71,7 @@ def accept!
activity_id = passive_follow_activity_id
payload = Oj.dump(accept_follow_activity(activity_id))

update!(passive_state: :accepted)
update!(passive_state: :accepted, active_state: :idle)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
Expand All @@ -75,11 +82,21 @@ def reject!
activity_id = passive_follow_activity_id
payload = Oj.dump(reject_follow_activity(activity_id))

update!(passive_state: :rejected, passive_follow_activity_id: nil)
update!(passive_state: :rejected, active_state: :idle, passive_follow_activity_id: nil)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end

def destroy_without_signal!
self.active_state = :idle
self.passive_state = :idle
destroy!
end

def initialize_inbox_url!
self.inbox_url = default_inbox_url
end

private

def default_inbox_url
Expand Down Expand Up @@ -154,7 +171,7 @@ def some_local_account
end

def ensure_disabled
delete_for_friend! unless i_am_idle? && they_are_idle?
delete_for_friend! unless id.nil? || (i_am_idle? && they_are_idle?)
end

def set_default_inbox_url
Expand Down
20 changes: 5 additions & 15 deletions app/views/admin/friend_servers/_friend_domain.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
= t 'admin.friend_servers.disabled'
%samp= friend.domain
%td
- if friend.i_am_accepted?
- if friend.accepted?
%span.positive-hint
= fa_icon('check')
= ' '
Expand All @@ -16,21 +16,11 @@
= fa_icon('hourglass')
= ' '
= t 'admin.friend_servers.pending'
- else
%span.negative-hint
= fa_icon('times')
= ' '
= t 'admin.friend_servers.disabled'
%td
- if friend.they_are_accepted?
%span.positive-hint
= fa_icon('check')
= ' '
= t 'admin.friend_servers.enabled'
- elsif friend.they_are_pending?
= fa_icon('hourglass')
= ' '
= t 'admin.friend_servers.pending'
%span.warning-hint
= fa_icon('hourglass')
= ' '
= t 'admin.friend_servers.pending_you'
- else
%span.negative-hint
= fa_icon('times')
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/friend_servers/_friend_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
= f.input :available, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.available')

.fields-group
= f.input :pseudo_relay, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.pseudo_relay'), hint: t('admin.friend_servers.edit.pseudo_relay_hint')
= f.input :delivery_local, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.delivery_local'), hint: t('admin.friend_servers.edit.delivery_local_hint')

.fields-group
= f.input :unlocked, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.unlocked')
= f.input :pseudo_relay, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.pseudo_relay'), hint: t('admin.friend_servers.edit.pseudo_relay_hint')

.fields-group
= f.input :allow_all_posts, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.allow_all_posts'), hint: t('admin.friend_servers.edit.allow_all_posts_hint')
28 changes: 4 additions & 24 deletions app/views/admin/friend_servers/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,14 @@
= render 'friend_fields', f: f, friend: @friend

.fields-group
%h4= t('admin.friend_servers.active_status')
%h4= t('admin.friend_servers.status')
.fields-group
- if @friend.i_am_accepted?
- if @friend.accepted?
%span.positive-hint
= fa_icon('check')
= ' '
= t 'admin.friend_servers.enabled'
- elsif @friend.i_am_pending?
= fa_icon('hourglass')
= ' '
= t 'admin.friend_servers.pending'
- else
%span.negative-hint
= fa_icon('times')
= ' '
= t 'admin.friend_servers.disabled'
.action-buttons
%div
= link_to t('admin.friend_servers.follow'), follow_admin_friend_server_path(@friend), class: 'button', method: :post, data: { confirm: t('admin.accounts.are_you_sure') } if @friend.i_am_idle? || @friend.i_am_rejected?
= link_to t('admin.friend_servers.unfollow'), unfollow_admin_friend_server_path(@friend), class: 'button', method: :post, data: { confirm: t('admin.accounts.are_you_sure') } if @friend.i_am_pending? || @friend.i_am_accepted?

%h4= t('admin.friend_servers.passive_status')
.fields-gtoup
- if @friend.they_are_accepted?
%span.positive-hint
= fa_icon('check')
= ' '
= t 'admin.friend_servers.enabled'
- elsif @friend.they_are_pending?
- elsif @friend.pending?
= fa_icon('hourglass')
= ' '
= t 'admin.friend_servers.pending'
Expand All @@ -45,6 +24,7 @@
= t 'admin.friend_servers.disabled'
.action-buttons
%div
= link_to t('admin.friend_servers.follow'), follow_admin_friend_server_path(@friend), class: 'button', method: :post, data: { confirm: t('admin.accounts.are_you_sure') } if @friend.idle?
= link_to t('admin.friend_servers.accept'), accept_admin_friend_server_path(@friend), class: 'button', method: :post, data: { confirm: t('admin.accounts.are_you_sure') } if @friend.they_are_pending?
= link_to t('admin.friend_servers.reject'), reject_admin_friend_server_path(@friend), class: 'button', method: :post, data: { confirm: t('admin.accounts.are_you_sure') } if @friend.they_are_pending?

Expand Down
3 changes: 1 addition & 2 deletions app/views/admin/friend_servers/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
%thead
%tr
%th= t('admin.friend_servers.domain')
%th= t('admin.friend_servers.active_status')
%th= t('admin.friend_servers.passive_status')
%th= t('admin.friend_servers.status')
%th
%tbody
- @friends.each do |friend|
Expand Down
5 changes: 3 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,6 @@ en:
unsuppress: Restore follow recommendation
friend_servers:
accept: Accept
active_status: My status
add_new: Add and make a new application
delete: Delete
description_html: <strong>フレンドサーバー</strong>とは、お互いのローカル公開・ローカル検索許可の投稿をそのまま交換するシステムです。
Expand All @@ -511,6 +510,8 @@ en:
allow_all_posts: Receive all posts
allow_all_posts_hint: 通常は自分のサーバーの誰もフォローしていないアカウントの投稿は例外を除き受け入れがブロックされます。そのブロックを解除します。スパムが発生した場合など、いつでもブロックを再開できます。
available: Available
delivery_local: Deliver without changing public unlisted visibility and searchability
delivery_local_hint: Public unlisted posts will be added the friend's global timeline
description: フレンドサーバーは、登録と同時に相手方のサーバーへ申請されます。
domain: Domain
inbox_url: Friend server inbox URL
Expand All @@ -521,8 +522,8 @@ en:
edit_friend: Edit
enabled: Enabled
follow: Request
passive_status: Partner status
pending: Pending
pending_you: Your review requested
reject: Reject
save_and_enable: Save and enable
setup: Add and make a new application
Expand Down
Loading