1
0
Fork 0
forked from gitea/nas

Add: フレンドサーバー (#61)

* Fix mastodon version

* テーブル作成

* Wip: フレンドサーバーフォローの承認を受信

* Wip: フレンド申請拒否を受信

* Wip: フォローリクエストを受理

* Wip: 相手からのフォロー・アンフォローを受理

* 普通のフォローとフレンドサーバーのフォローを区別するテストを追加

* ドメインブロックによるフォロー拒否

* ドメインブロックしたあと、申請中のフォロリクを取り下げる処理

* スタブに条件を追加

* Wip: 相手からのDelete信号に対応

* DB定義が消えていたので修正

* Wip: ローカル公開投稿をフレンドに送信する処理など

* Wip: 未収載+誰でもの投稿をフレンドに送る設定

* Wip: ローカル公開をそのまま送信する設定を考慮

* Fix test

* Wip: 他サーバーからのローカル公開投稿の受け入れ

* Wip: Web画面作成

* Fix test

* Wip: ローカル公開を連合TLに流す

* Wip: フレンドサーバーの削除ボタン

* Wip: メール通知や設定のテストなど

* Wip: 翻訳を作成

* Fix: 却下されたあとフォローボタンが表示されない問題

* Wip: 編集できない問題

* 有効にしていないフレンドサーバーをリストで無効表示
This commit is contained in:
KMY(雪あすか) 2023-10-09 11:51:15 +09:00 committed by GitHub
parent acb29e5b11
commit 87e858a202
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
66 changed files with 1638 additions and 51 deletions

View file

@ -89,17 +89,17 @@ module Admin
def update_params
params.require(:domain_block).permit(:severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_send_not_public_searchability, :reject_send_public_unlisted, :reject_send_dissubscribable, :reject_send_media, :reject_send_sensitive, :reject_hashtag,
:reject_straight_follow, :reject_new_follow, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
:reject_straight_follow, :reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
end
def resource_params
params.require(:domain_block).permit(:domain, :severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_send_not_public_searchability, :reject_send_public_unlisted, :reject_send_dissubscribable, :reject_send_media, :reject_send_sensitive, :reject_hashtag,
:reject_straight_follow, :reject_new_follow, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
:reject_straight_follow, :reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
end
def form_domain_block_batch_params
params.require(:form_domain_block_batch).permit(domain_blocks_attributes: [:enabled, :domain, :severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_send_not_public_searchability, :reject_send_public_unlisted, :reject_send_dissubscribable, :reject_send_media,
:reject_send_sensitive, :reject_hashtag, :reject_straight_follow, :reject_new_follow, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous])
:reject_send_sensitive, :reject_hashtag, :reject_straight_follow, :reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous])
end
def action_from_button

View file

@ -0,0 +1,89 @@
# frozen_string_literal: true
module Admin
class FriendServersController < BaseController
before_action :set_friend, except: [:index, :new, :create]
before_action :warn_signatures_not_enabled!, only: [:new, :edit, :create, :follow, :unfollow, :accept, :reject]
def index
authorize :friend_server, :update?
@friends = FriendDomain.all
end
def new
authorize :friend_server, :update?
@friend = FriendDomain.new
end
def edit
authorize :friend_server, :update?
end
def create
authorize :friend_server, :update?
@friend = FriendDomain.new(resource_params)
if @friend.save
@friend.follow!
redirect_to admin_friend_servers_path
else
render action: :new
end
end
def update
authorize :friend_server, :update?
if @friend.update(resource_params)
redirect_to admin_friend_servers_path
else
render action: :edit
end
end
def destroy
authorize :friend_server, :update?
@friend.destroy
redirect_to admin_friend_servers_path
end
def follow
authorize :friend_server, :update?
@friend.follow!
render action: :edit
end
def unfollow
authorize :friend_server, :update?
@friend.unfollow!
render action: :edit
end
def accept
authorize :friend_server, :update?
@friend.accept!
render action: :edit
end
def reject
authorize :friend_server, :update?
@friend.reject!
render action: :edit
end
private
def set_friend
@friend = FriendDomain.find(params[:id])
end
def resource_params
params.require(:friend_domain).permit(:domain, :inbox_url, :available, :pseudo_relay, :unlocked, :allow_all_posts)
end
def warn_signatures_not_enabled!
flash.now[:error] = I18n.t('admin.relays.signatures_not_enabled') if authorized_fetch_mode?
end
end
end

View file

@ -70,7 +70,7 @@ class Api::V1::Admin::DomainBlocksController < Api::BaseController
def domain_block_params
params.permit(:severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_reports, :reject_send_not_public_searchability, :reject_send_public_unlisted, :reject_send_dissubscribable, :reject_send_media, :reject_send_sensitive, :reject_hashtag, :reject_straight_follow,
:reject_new_follow, :detect_invalid_subscription, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
:reject_new_follow, :reject_friend, :detect_invalid_subscription, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
end
def insert_pagination_headers
@ -103,6 +103,6 @@ class Api::V1::Admin::DomainBlocksController < Api::BaseController
def resource_params
params.permit(:domain, :severity, :reject_media, :reject_favourite, :reject_reply, :reject_reply_exclude_followers, :reject_send_not_public_searchability, :reject_send_public_unlisted, :reject_send_dissubscribable, :reject_send_media, :reject_send_sensitive, :reject_hashtag, :reject_straight_follow,
:reject_new_follow, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
:reject_new_follow, :reject_friend, :detect_invalid_subscription, :reject_reports, :private_comment, :public_comment, :obfuscate, :hidden, :hidden_anonymous)
end
end

View file

@ -3,6 +3,7 @@
class ActivityPub::Activity::Accept < ActivityPub::Activity
def perform
return accept_follow_for_relay if relay_follow?
return accept_follow_for_friend if friend_follow?
return accept_follow!(follow_request_from_object) unless follow_request_from_object.nil?
case @object['type']
@ -43,6 +44,18 @@ class ActivityPub::Activity::Accept < ActivityPub::Activity
relay.present?
end
def accept_follow_for_friend
friend.update!(active_state: :accepted)
end
def friend
@friend ||= FriendDomain.find_by(domain: @account.domain, active_follow_activity_id: object_uri, active_state: [:pending, :accepted]) if @account.domain.present?
end
def friend_follow?
friend.present?
end
def target_uri
@target_uri ||= value_or_id(@object['actor'])
end

View file

@ -447,7 +447,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
def related_to_local_activity?
fetch? || followed_by_local_accounts? || requested_through_relay? ||
responds_to_followed_account? || addresses_local_accounts? || quote_local?
responds_to_followed_account? || addresses_local_accounts? || quote_local? || free_friend_domain?
end
def responds_to_followed_account?
@ -502,6 +502,10 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
end
end
def free_friend_domain?
FriendDomain.free_receivings.exists?(domain: @account.domain)
end
def quote
@quote ||= @object['quote'] || @object['quoteUrl'] || @object['quoteURL'] || @object['_misskey_quote']
end

View file

@ -4,6 +4,8 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
def perform
if @account.uri == object_uri
delete_person
elsif object_uri == ActivityPub::TagManager::COLLECTIONS[:public]
delete_friend
else
delete_note
end
@ -42,6 +44,11 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity
end
end
def delete_friend
friend = FriendDomain.find_by(domain: @account.domain)
friend&.destroy
end
def forwarder
@forwarder ||= ActivityPub::Forwarder.new(@account, @json, @status)
end

View file

@ -4,6 +4,8 @@ class ActivityPub::Activity::Follow < ActivityPub::Activity
include Payloadable
def perform
return request_follow_for_friend if friend_follow?
target_account = account_from_uri(object_uri)
return if target_account.nil? || !target_account.local? || delete_arrived_first?(@json['id'])
@ -43,6 +45,36 @@ class ActivityPub::Activity::Follow < ActivityPub::Activity
ActivityPub::DeliveryWorker.perform_async(json, target_account.id, @account.inbox_url)
end
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'])
else
@friend = FriendDomain.create!(domain: @account.domain, passive_state: :pending, passive_follow_activity_id: @json['id'])
end
if already_accepted || friend.unlocked || Setting.unlocked_friend
friend.accept!
else
# Notify for admin even if unlocked
notify_staff_about_pending_friend_server!
end
end
def friend
@friend ||= FriendDomain.find_by(domain: @account.domain) if @account.domain.present?
end
def friend_follow?
@json['object'] == ActivityPub::TagManager::COLLECTIONS[:public] && !block_friend?
end
def block_friend?
@block_friend ||= DomainBlock.reject_friend?(@account.domain) || DomainBlock.blocked?(@account.domain)
end
def block_straight_follow?
@block_straight_follow ||= DomainBlock.reject_straight_follow?(@account.domain)
end
@ -73,4 +105,12 @@ class ActivityPub::Activity::Follow < ActivityPub::Activity
def instance_info
@instance_info ||= InstanceInfo.find_by(domain: @account.domain)
end
def notify_staff_about_pending_friend_server!
User.those_who_can(:manage_federation).includes(:account).find_each do |u|
next unless u.allows_pending_friend_server_emails?
AdminMailer.with(recipient: u.account).new_pending_friend_server(friend).deliver_later
end
end
end

View file

@ -3,6 +3,7 @@
class ActivityPub::Activity::Reject < ActivityPub::Activity
def perform
return reject_follow_for_relay if relay_follow?
return reject_follow_for_friend if friend_follow?
return follow_request_from_object.reject! unless follow_request_from_object.nil?
return UnfollowService.new.call(follow_from_object.account, @account) unless follow_from_object.nil?
@ -37,6 +38,18 @@ class ActivityPub::Activity::Reject < ActivityPub::Activity
relay.present?
end
def reject_follow_for_friend
friend.update!(active_state: :rejected)
end
def friend
@friend ||= FriendDomain.find_by(domain: @account.domain, active_follow_activity_id: object_uri, active_state: [:pending, :accepted]) if @account.domain.present?
end
def friend_follow?
friend.present?
end
def target_uri
@target_uri ||= value_or_id(@object['actor'])
end

View file

@ -87,6 +87,8 @@ class ActivityPub::Activity::Undo < ActivityPub::Activity
end
def undo_follow
return remove_follow_from_friend if friend_follow?
target_account = account_from_uri(target_uri)
return if target_account.nil? || !target_account.local?
@ -100,6 +102,18 @@ class ActivityPub::Activity::Undo < ActivityPub::Activity
end
end
def remove_follow_from_friend
friend.update!(passive_state: :idle, passive_follow_activity_id: nil)
end
def friend
@friend ||= FriendDomain.find_by(domain: @account.domain) if @account.domain.present? && @object['object'] == ActivityPub::TagManager::COLLECTIONS[:public]
end
def friend_follow?
friend.present?
end
def undo_like_original
status = status_from_uri(target_uri)

View file

@ -76,6 +76,8 @@ class ActivityPub::Parser::StatusParser
def visibility
if audience_to.any? { |to| ActivityPub::TagManager.instance.public_collection?(to) }
:public
elsif audience_to.include?('LocalPublic')
:public_unlisted
elsif audience_cc.any? { |cc| ActivityPub::TagManager.instance.public_collection?(cc) }
:unlisted
elsif audience_to.include?('as:LoginOnly') || audience_to.include?('LoginUser')
@ -198,6 +200,8 @@ class ActivityPub::Parser::StatusParser
:public
elsif audience_searchable_by.include?('as:Limited')
:limited
elsif audience_searchable_by.include?('LocalPublic')
:public_unlisted
elsif audience_searchable_by.include?(@account.followers_url)
:private
else

View file

@ -126,6 +126,12 @@ class ActivityPub::TagManager
end
end
def to_for_friend(status)
to = to(status)
to << 'LocalPublic' if status.public_unlisted_visibility?
to
end
# Secondary audience of a status
# Public statuses go out to followers as well
# Unlisted statuses go to the public as well
@ -147,7 +153,7 @@ class ActivityPub::TagManager
end
def cc_for_misskey(status)
if (status.account.user&.setting_reject_unlisted_subscription && status.visibility == 'unlisted') || (status.account.user&.setting_reject_public_unlisted_subscription && status.visibility == 'public_unlisted')
if (status.account.user&.setting_reject_unlisted_subscription && status.unlisted_visibility?) || (status.account.user&.setting_reject_public_unlisted_subscription && status.public_unlisted_visibility?)
cc = cc_private_visibility(status)
cc << uri_for(status.reblog.account) if status.reblog?
return cc
@ -251,6 +257,12 @@ class ActivityPub::TagManager
searchable_by.concat(mentions_uris(status)).compact
end
def searchable_by_for_friend(status)
searchable = searchable_by(status)
searchable << 'LocalPublic' if status.compute_searchability_local == 'public_unlisted'
searchable
end
def account_searchable_by(account)
case account.compute_searchability_activitypub
when 'public'

View file

@ -21,6 +21,10 @@ class StatusReachFinder
end
end
def inboxes_for_friend
(reached_account_inboxes_for_friend + followers_inboxes_for_friend + friend_inboxes).uniq
end
private
def reached_account_inboxes
@ -32,7 +36,7 @@ class StatusReachFinder
elsif @status.limited_visibility?
Account.where(id: mentioned_account_ids).where.not(domain: banned_domains).inboxes
else
Account.where(id: reached_account_ids).where.not(domain: banned_domains).inboxes
Account.where(id: reached_account_ids).where.not(domain: banned_domains + friend_domains).inboxes
end
end
@ -42,7 +46,17 @@ class StatusReachFinder
elsif @status.limited_visibility?
Account.where(id: mentioned_account_ids).where(domain: banned_domains_for_misskey).inboxes
else
Account.where(id: reached_account_ids).where(domain: banned_domains_for_misskey).inboxes
Account.where(id: reached_account_ids).where(domain: banned_domains_for_misskey - friend_domains).inboxes
end
end
def reached_account_inboxes_for_friend
if @status.reblog?
[]
elsif @status.limited_visibility?
Account.where(id: mentioned_account_ids).where.not(domain: banned_domains).inboxes
else
Account.where(id: reached_account_ids, domain: friend_domains).where.not(domain: banned_domains - friend_domains).inboxes
end
end
@ -95,21 +109,31 @@ class StatusReachFinder
def followers_inboxes
if @status.in_reply_to_local_account? && distributable?
@status.account.followers.or(@status.thread.account.followers.not_domain_blocked_by_account(@status.account)).where.not(domain: banned_domains).inboxes
@status.account.followers.or(@status.thread.account.followers.not_domain_blocked_by_account(@status.account)).where.not(domain: banned_domains + friend_domains).inboxes
elsif @status.direct_visibility? || @status.limited_visibility?
[]
else
@status.account.followers.where.not(domain: banned_domains).inboxes
@status.account.followers.where.not(domain: banned_domains + friend_domains).inboxes
end
end
def followers_inboxes_for_misskey
if @status.in_reply_to_local_account? && distributable?
@status.account.followers.or(@status.thread.account.followers.not_domain_blocked_by_account(@status.account)).where(domain: banned_domains_for_misskey).inboxes
@status.account.followers.or(@status.thread.account.followers.not_domain_blocked_by_account(@status.account)).where(domain: banned_domains_for_misskey - friend_domains).inboxes
elsif @status.direct_visibility? || @status.limited_visibility?
[]
else
@status.account.followers.where(domain: banned_domains_for_misskey).inboxes
@status.account.followers.where(domain: banned_domains_for_misskey - friend_domains).inboxes
end
end
def followers_inboxes_for_friend
if @status.in_reply_to_local_account? && distributable?
@status.account.followers.or(@status.thread.account.followers.not_domain_blocked_by_account(@status.account)).where(domain: friend_domains).inboxes
elsif @status.direct_visibility? || @status.limited_visibility?
[]
else
@status.account.followers.where(domain: friend_domains).inboxes
end
end
@ -121,6 +145,14 @@ class StatusReachFinder
end
end
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))
else
[]
end
end
def distributable?
@status.public_visibility? || @status.unlisted_visibility? || @status.public_unlisted_visibility?
end
@ -129,6 +161,13 @@ class StatusReachFinder
@options[:unsafe]
end
def friend_domains
return @friend_domains if defined?(@friend_domains)
@friend_domains = FriendDomain.deliver_locals.pluck(:domain)
@friend_domains -= UnavailableDomain.where(domain: @friend_domains).pluck(:domain)
end
def banned_domains
return @banned_domains if @banned_domains

View file

@ -35,6 +35,14 @@ class AdminMailer < ApplicationMailer
end
end
def new_pending_friend_server(friend_server)
@friend = friend_server
locale_for_account(@me) do
mail subject: default_i18n_subject(instance: @instance, domain: @friend.domain)
end
end
def new_trends(links, tags, statuses)
@links = links
@tags = tags

View file

@ -263,6 +263,10 @@ module HasUserSettings
settings['notification_emails.pending_account']
end
def allows_pending_friend_server_emails?
settings['notification_emails.pending_friend_server']
end
def allows_appeal_emails?
settings['notification_emails.appeal']
end

View file

@ -28,6 +28,7 @@
# hidden_anonymous :boolean default(FALSE), not null
# detect_invalid_subscription :boolean default(FALSE), not null
# reject_reply_exclude_followers :boolean default(FALSE), not null
# reject_friend :boolean default(FALSE), not null
#
class DomainBlock < ApplicationRecord
@ -44,7 +45,16 @@ class DomainBlock < ApplicationRecord
scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) }
scope :with_user_facing_limitations, -> { where(hidden: false) }
scope :with_limitations, -> { where(severity: [:silence, :suspend]).or(where(reject_media: true)).or(where(reject_favourite: true)).or(where(reject_reply: true)).or(where(reject_reply_exclude_followers: true)).or(where(reject_new_follow: true)).or(where(reject_straight_follow: true)) }
scope :with_limitations, lambda {
where(severity: [:silence, :suspend])
.or(where(reject_media: true))
.or(where(reject_favourite: true))
.or(where(reject_reply: true))
.or(where(reject_reply_exclude_followers: true))
.or(where(reject_new_follow: true))
.or(where(reject_straight_follow: true))
.or(where(reject_friend: true))
}
scope :by_severity, -> { order(Arel.sql('(CASE severity WHEN 0 THEN 1 WHEN 1 THEN 2 WHEN 2 THEN 0 END), domain')) }
def to_log_human_identifier
@ -68,6 +78,7 @@ class DomainBlock < ApplicationRecord
reject_hashtag? ? :reject_hashtag : nil,
reject_straight_follow? ? :reject_straight_follow : nil,
reject_new_follow? ? :reject_new_follow : nil,
reject_friend? ? :reject_friend : nil,
detect_invalid_subscription? ? :detect_invalid_subscription : nil,
reject_reports? ? :reject_reports : nil].reject { |policy| policy == :noop || policy.nil? }
end
@ -110,6 +121,10 @@ class DomainBlock < ApplicationRecord
!!rule_for(domain)&.reject_new_follow?
end
def reject_friend?(domain)
!!rule_for(domain)&.reject_friend?
end
def detect_invalid_subscription?(domain)
!!rule_for(domain)&.detect_invalid_subscription?
end

View file

@ -48,6 +48,7 @@ class Form::AdminSettings
enable_emoji_reaction
check_lts_version_only
enable_public_unlisted_visibility
unlocked_friend
).freeze
INTEGER_KEYS = %i(
@ -76,6 +77,7 @@ class Form::AdminSettings
enable_emoji_reaction
check_lts_version_only
enable_public_unlisted_visibility
unlocked_friend
).freeze
UPLOAD_KEYS = %i(

163
app/models/friend_domain.rb Normal file
View file

@ -0,0 +1,163 @@
# frozen_string_literal: true
# == Schema Information
#
# Table name: friend_domains
#
# id :bigint(8) not null, primary key
# domain :string default(""), not null
# inbox_url :string default(""), not null
# active_state :integer default("idle"), not null
# passive_state :integer default("idle"), not null
# active_follow_activity_id :string
# 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
#
class FriendDomain < ApplicationRecord
validates :domain, presence: true, uniqueness: true, if: :will_save_change_to_domain?
validates :inbox_url, presence: true, uniqueness: true, if: :will_save_change_to_inbox_url?
enum active_state: { idle: 0, pending: 1, accepted: 2, rejected: 3 }, _prefix: :i_am
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) }
before_destroy :ensure_disabled
after_commit :set_default_inbox_url
def mutual?
i_am_accepted? && they_are_accepted?
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)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
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)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
def accept!
return if they_are_idle?
activity_id = passive_follow_activity_id
payload = Oj.dump(accept_follow_activity(activity_id))
update!(passive_state: :accepted)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
def reject!
return if they_are_idle?
activity_id = passive_follow_activity_id
payload = Oj.dump(reject_follow_activity(activity_id))
update!(passive_state: :rejected, passive_follow_activity_id: nil)
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
private
def default_inbox_url
"https://#{domain}/inbox"
end
def delete_for_friend!
activity_id = ActivityPub::TagManager.instance.generate_uri_for(nil)
payload = Oj.dump(delete_follow_activity(activity_id))
DeliveryFailureTracker.reset!(inbox_url)
ActivityPub::DeliveryWorker.perform_async(payload, some_local_account.id, inbox_url)
end
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: active_follow_activity_id,
type: 'Follow',
actor: ActivityPub::TagManager.instance.uri_for(some_local_account),
object: ActivityPub::TagManager::COLLECTIONS[:public],
},
}
end
def accept_follow_activity(activity_id)
{
'@context': ActivityPub::TagManager::CONTEXT,
id: "#{activity_id}#accepts/friends",
type: 'Accept',
actor: ActivityPub::TagManager.instance.uri_for(some_local_account),
object: activity_id,
}
end
def reject_follow_activity(activity_id)
{
'@context': ActivityPub::TagManager::CONTEXT,
id: "#{activity_id}#rejects/friends",
type: 'Reject',
actor: ActivityPub::TagManager.instance.uri_for(some_local_account),
object: activity_id,
}
end
def delete_follow_activity(activity_id)
{
'@context': ActivityPub::TagManager::CONTEXT,
id: "#{activity_id}#delete/friends",
type: 'Delete',
actor: ActivityPub::TagManager.instance.uri_for(some_local_account),
object: ActivityPub::TagManager::COLLECTIONS[:public],
}
end
def some_local_account
@some_local_account ||= Account.representative
end
def ensure_disabled
delete_for_friend! unless i_am_idle? && they_are_idle?
end
def set_default_inbox_url
self.inbox_url = default_inbox_url if inbox_url.blank?
end
end

View file

@ -20,6 +20,7 @@ class Instance < ApplicationRecord
belongs_to :domain_allow
belongs_to :unavailable_domain # skipcq: RB-RL1031
belongs_to :instance_info
belongs_to :friend_domain
end
scope :searchable, -> { where.not(domain: DomainBlock.select(:domain)) }

View file

@ -19,7 +19,7 @@ class PublicFeed
# @param [Integer] min_id
# @return [Array<Status>]
def get(limit, max_id = nil, since_id = nil, min_id = nil)
scope = local_only? ? public_scope : global_timeline_only_scope
scope = public_scope
scope.merge!(without_replies_scope) unless with_replies?
scope.merge!(without_reblogs_scope) unless with_reblogs?
@ -70,10 +70,6 @@ class PublicFeed
Status.with_public_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
end
def global_timeline_only_scope
Status.with_global_timeline_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
end
def public_search_scope
Status.with_public_search_visibility.joins(:account).merge(Account.without_suspended.without_silenced)
end

View file

@ -130,7 +130,6 @@ class Status < ApplicationRecord
scope :without_reblogs, -> { where(statuses: { reblog_of_id: nil }) }
scope :with_public_visibility, -> { where(visibility: [:public, :public_unlisted, :login]) }
scope :with_public_search_visibility, -> { merge(where(visibility: [:public, :public_unlisted, :login]).or(Status.where(searchability: [:public, :public_unlisted]))) }
scope :with_global_timeline_visibility, -> { where(visibility: [:public, :login]) }
scope :tagged_with, ->(tag_ids) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag_ids }) }
scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced_at: nil }) }
scope :including_silenced_accounts, -> { left_outer_joins(:account).where.not(accounts: { silenced_at: nil }) }

View file

@ -80,6 +80,7 @@ class UserSettings
setting :follow_request, default: true
setting :report, default: true
setting :pending_account, default: true
setting :pending_friend_server, default: true
setting :trends, default: true
setting :appeal, default: true
setting :software_updates, default: 'critical', in: %w(none critical patch all)

View file

@ -0,0 +1,7 @@
# frozen_string_literal: true
class FriendServerPolicy < ApplicationPolicy
def update?
role.can?(:manage_federation)
end
end

View file

@ -4,13 +4,13 @@ class ActivityPub::ActivityPresenter < ActiveModelSerializers::Model
attributes :id, :type, :actor, :published, :to, :cc, :virtual_object
class << self
def from_status(status, use_bearcap: true, allow_inlining: true, for_misskey: false)
def from_status(status, use_bearcap: true, allow_inlining: true, for_misskey: false, for_friend: false)
new.tap do |presenter|
presenter.id = ActivityPub::TagManager.instance.activity_uri_for(status)
presenter.type = status.reblog? ? 'Announce' : 'Create'
presenter.actor = ActivityPub::TagManager.instance.uri_for(status.account)
presenter.published = status.created_at
presenter.to = ActivityPub::TagManager.instance.to(status)
presenter.to = for_friend ? ActivityPub::TagManager.instance.to_for_friend(status) : ActivityPub::TagManager.instance.to(status)
presenter.cc = for_misskey ? ActivityPub::TagManager.instance.cc_for_misskey(status) : ActivityPub::TagManager.instance.cc(status)
presenter.virtual_object = begin

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
class ActivityPub::ActivityForFriendSerializer < ActivityPub::Serializer
def self.serializer_for(model, options)
case model.class.name
when 'Status'
ActivityPub::NoteForFriendSerializer
when 'DeliverToDeviceService::EncryptedMessage'
ActivityPub::EncryptedMessageSerializer
else
super
end
end
attributes :id, :type, :actor, :published, :to, :cc
has_one :virtual_object, key: :object
def published
object.published.iso8601
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
class ActivityPub::NoteForFriendSerializer < ActivityPub::NoteSerializer
def to
ActivityPub::TagManager.instance.to_for_friend(object)
end
def searchable_by
ActivityPub::TagManager.instance.searchable_by_for_friend(object)
end
end

View file

@ -24,6 +24,9 @@ class BlockDomainService < BaseService
silence_accounts!
elsif domain_block.suspend?
suspend_accounts!
remove_friends!
elsif domain_block.reject_friend?
remove_friends!
end
DomainClearMediaWorker.perform_async(domain_block.id) if domain_block.reject_media?
@ -41,6 +44,10 @@ class BlockDomainService < BaseService
end
end
def remove_friends!
blocked_friends.find_each(&:destroy)
end
def blocked_domain
domain_block.domain
end
@ -48,4 +55,8 @@ class BlockDomainService < BaseService
def blocked_domain_accounts
Account.by_domain_and_subdomains(blocked_domain)
end
def blocked_friends
@blocked_friends ||= FriendDomain.by_domain_and_subdomains(blocked_domain)
end
end

View file

@ -21,9 +21,6 @@ class FanOutOnWriteService < BaseService
if broadcastable?
fan_out_to_public_recipients!
fan_out_to_public_streams!
elsif broadcastable_unlisted?
fan_out_to_public_recipients!
fan_out_to_public_unlisted_streams!
elsif broadcastable_unlisted2?
fan_out_to_unlisted_streams!
end
@ -75,11 +72,6 @@ class FanOutOnWriteService < BaseService
broadcast_to_public_streams!
end
def fan_out_to_public_unlisted_streams!
broadcast_to_hashtag_streams!
broadcast_to_public_unlisted_streams!
end
def fan_out_to_unlisted_streams!
broadcast_to_hashtag_streams!
end
@ -176,16 +168,6 @@ class FanOutOnWriteService < BaseService
end
end
def broadcast_to_public_unlisted_streams!
return if @status.reply? && @status.in_reply_to_account_id != @account.id
redis.publish(@status.local? ? 'timeline:public:local' : 'timeline:public:remote', anonymous_payload)
if @status.with_media?
redis.publish(@status.local? ? 'timeline:public:local:media' : 'timeline:public:remote:media', anonymous_payload)
end
end
def deliver_to_conversation!
AccountConversation.add_status(@account, @status) unless update?
end
@ -210,11 +192,7 @@ class FanOutOnWriteService < BaseService
end
def broadcastable?
(@status.public_visibility? || @status.login_visibility?) && !@status.reblog? && !@account.silenced?
end
def broadcastable_unlisted?
@status.public_unlisted_visibility? && !@status.reblog? && !@account.silenced?
(@status.public_visibility? || @status.public_unlisted_visibility? || @status.login_visibility?) && !@status.reblog? && !@account.silenced?
end
def broadcastable_unlisted2?

View file

@ -47,6 +47,9 @@
.fields-group
= f.input :reject_new_follow, as: :boolean, wrapper: :with_label, label: I18n.t('admin.domain_blocks.reject_new_follow'), hint: I18n.t('admin.domain_blocks.reject_new_follow_hint')
.fields-group
= f.input :reject_friend, as: :boolean, wrapper: :with_label, label: I18n.t('admin.domain_blocks.reject_friend'), hint: I18n.t('admin.domain_blocks.reject_friend_hint')
.fields-group
= f.input :detect_invalid_subscription, as: :boolean, wrapper: :with_label, label: I18n.t('admin.domain_blocks.detect_invalid_subscription'), hint: I18n.t('admin.domain_blocks.detect_invalid_subscription_hint')

View file

@ -47,6 +47,9 @@
.fields-group
= f.input :reject_new_follow, as: :boolean, wrapper: :with_label, label: I18n.t('admin.domain_blocks.reject_new_follow'), hint: I18n.t('admin.domain_blocks.reject_new_follow_hint')
.fields-group
= f.input :reject_friend, as: :boolean, wrapper: :with_label, label: I18n.t('admin.domain_blocks.reject_friend'), hint: I18n.t('admin.domain_blocks.reject_friend_hint')
.fields-group
= f.input :detect_invalid_subscription, as: :boolean, wrapper: :with_label, label: I18n.t('admin.domain_blocks.detect_invalid_subscription'), hint: I18n.t('admin.domain_blocks.detect_invalid_subscription_hint')

View file

@ -0,0 +1,41 @@
%tr
%td
- unless friend.available
%span.negative-hint
= fa_icon('times')
= ' '
= t 'admin.friend_servers.disabled'
%samp= friend.domain
%td
- if friend.i_am_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'
%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'
- else
%span.negative-hint
= fa_icon('times')
= ' '
= t 'admin.friend_servers.disabled'
%td
= table_link_to 'pencil', t('admin.friend_servers.edit_friend'), edit_admin_friend_server_path(friend)
= table_link_to 'times', t('admin.friend_servers.delete'), admin_friend_server_path(friend), method: :delete, data: { confirm: t('admin.accounts.are_you_sure') }

View file

@ -0,0 +1,20 @@
%p= t 'admin.friend_servers.edit.description'
%hr.spacer/
.fields-group
= f.input :domain, as: :string, wrapper: :with_label, required: true, disabled: !friend.id.nil?, label: t('admin.friend_servers.edit.domain')
.fields-group
= f.input :inbox_url, as: :string, wrapper: :with_label, label: t('admin.friend_servers.edit.inbox_url'), hint: t('admin.friend_servers.edit.inbox_url_hint')
.fields-group
= 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')
.fields-group
= f.input :unlocked, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.unlocked')
.fields-group
= f.input :allow_all_posts, as: :boolean, wrapper: :with_label, label: t('admin.friend_servers.edit.allow_all_posts')

View file

@ -0,0 +1,52 @@
- content_for :page_title do
= t('admin.friend_servers.edit_friend')
= simple_form_for @friend, url: admin_friend_server_path(@friend), method: :put do |f|
= render 'shared/error_messages', object: @friend
= render 'friend_fields', f: f, friend: @friend
.fields-group
%h4= t('admin.friend_servers.active_status')
.fields-group
- if @friend.i_am_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?
= 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.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?
.actions
= f.button :button, t('generic.save_changes'), type: :submit

View file

@ -0,0 +1,21 @@
- content_for :page_title do
= t('admin.friend_servers.title')
.simple_form
%p.hint= t('admin.friend_servers.description_html')
= link_to @friends.empty? ? t('admin.friend_servers.setup') : t('admin.friend_servers.add_new'), new_admin_friend_server_path, class: 'block-button'
- unless @friends.empty?
%hr.spacer
.table-wrapper
%table.table
%thead
%tr
%th= t('admin.friend_servers.domain')
%th= t('admin.friend_servers.active_status')
%th= t('admin.friend_servers.passive_status')
%th
%tbody
- @friends.each do |friend|
= render 'friend_domain', friend: friend

View file

@ -0,0 +1,9 @@
- content_for :page_title do
= t('admin.friend_servers.add_new')
= simple_form_for @friend, url: admin_friend_servers_path do |f|
= render 'shared/error_messages', object: @friend
= render 'friend_fields', f: f, friend: @friend
.actions
= f.button :button, t('admin.friend_servers.save_and_enable'), type: :submit

View file

@ -45,6 +45,11 @@
.fields-group
= f.input :enable_public_unlisted_visibility, as: :boolean, wrapper: :with_label, kmyblue: true, hint: false
%h4= t('admin.settings.discovery.friend_servers')
.fields-group
= f.input :unlocked_friend, as: :boolean, wrapper: :with_label, kmyblue: true, hint: false
%h4= t('admin.settings.discovery.publish_statistics')
.fields-group

View file

@ -0,0 +1,5 @@
<%= raw t('application_mailer.salutation', name: display_name(@me)) %>
<%= raw t('admin_mailer.new_pending_friend_server.body', domain: @friend.domain) %>
<%= raw t('application_mailer.view')%> <%= admin_friend_servers_url %>

View file

@ -22,13 +22,14 @@
.fields-group
= ff.input :always_send_emails, wrapper: :with_label, label: I18n.t('simple_form.labels.defaults.setting_always_send_emails'), hint: I18n.t('simple_form.hints.defaults.setting_always_send_emails')
- if current_user.can?(:manage_reports, :manage_appeals, :manage_users, :manage_taxonomies) || (SoftwareUpdate.check_enabled? && current_user.can?(:view_devops))
- if current_user.can?(:manage_reports, :manage_appeals, :manage_users, :manage_taxonomies, :manage_federation) || (SoftwareUpdate.check_enabled? && current_user.can?(:view_devops))
%h4= t 'notifications.administration_emails'
.fields-group
= ff.input :'notification_emails.report', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.report') if current_user.can?(:manage_reports)
= ff.input :'notification_emails.appeal', as: :boolean, wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.appeal') if current_user.can?(:manage_appeals)
= ff.input :'notification_emails.pending_account', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.pending_account') if current_user.can?(:manage_users)
= ff.input :'notification_emails.pending_friend_server', as: :boolean, wrapper: :with_label, kmyblue: true, label: I18n.t('simple_form.labels.notification_emails.pending_friend_server') if current_user.can?(:manage_federation)
= ff.input :'notification_emails.trends', wrapper: :with_label, label: I18n.t('simple_form.labels.notification_emails.trending_tag') if current_user.can?(:manage_taxonomies)
- if SoftwareUpdate.check_enabled? && current_user.can?(:view_devops)

View file

@ -22,6 +22,10 @@ class ActivityPub::DistributionWorker < ActivityPub::RawDistributionWorker
@inboxes_for_misskey ||= status_reach_finder.inboxes_for_misskey
end
def inboxes_for_friend
@inboxes_for_friend ||= status_reach_finder.inboxes_for_friend
end
def status_reach_finder
@status_reach_finder ||= StatusReachFinder.new(@status)
end
@ -34,6 +38,10 @@ class ActivityPub::DistributionWorker < ActivityPub::RawDistributionWorker
@payload_for_misskey ||= Oj.dump(serialize_payload(activity_for_misskey, ActivityPub::ActivityForMisskeySerializer, signer: @account))
end
def payload_for_friend
@payload_for_friend ||= Oj.dump(serialize_payload(activity_for_friend, ActivityPub::ActivityForFriendSerializer, signer: @account))
end
def activity
ActivityPub::ActivityPresenter.from_status(@status)
end
@ -42,6 +50,10 @@ class ActivityPub::DistributionWorker < ActivityPub::RawDistributionWorker
ActivityPub::ActivityPresenter.from_status(@status, for_misskey: true)
end
def activity_for_friend
ActivityPub::ActivityPresenter.from_status(@status, for_friend: true)
end
def options
{ 'synchronize_followers' => @status.private_visibility? }
end

View file

@ -29,6 +29,12 @@ class ActivityPub::RawDistributionWorker
end
end
unless inboxes_for_friend.empty?
ActivityPub::DeliveryWorker.push_bulk(inboxes_for_friend, limit: 1_000) do |inbox_url|
[payload_for_friend, source_account_id, inbox_url, options]
end
end
return if inboxes.empty?
ActivityPub::DeliveryWorker.push_bulk(inboxes, limit: 1_000) do |inbox_url|
@ -44,6 +50,10 @@ class ActivityPub::RawDistributionWorker
payload
end
def payload_for_friend
payload
end
def source_account_id
@account.id
end
@ -56,6 +66,10 @@ class ActivityPub::RawDistributionWorker
[]
end
def inboxes_for_friend
[]
end
def options
{}
end