Skip to content

Commit

Permalink
Add support for reversible suspensions through ActivityPub (mastodon#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored and umonaca committed Nov 8, 2020
1 parent a85cccb commit 201f281
Show file tree
Hide file tree
Showing 47 changed files with 1,045 additions and 200 deletions.
4 changes: 4 additions & 0 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ def username_param
params[:username]
end

def skip_temporary_suspension_response?
request.format == :json
end

def rss_url
if tag_requested?
short_account_tag_url(@account, params[:tag], format: 'rss')
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/activitypub/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class ActivityPub::BaseController < Api::BaseController
def set_cache_headers
response.headers['Vary'] = 'Signature' if authorized_fetch_mode?
end

def skip_temporary_suspension_response?
false
end
end
4 changes: 4 additions & 0 deletions app/controllers/activitypub/inboxes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def account_required?
params[:account_username].present?
end

def skip_temporary_suspension_response?
true
end

def body
return @body if defined?(@body)

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/activitypub/replies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def set_status
end

def set_replies
@replies = only_other_accounts? ? Status.where.not(account_id: @account.id) : @account.statuses
@replies = only_other_accounts? ? Status.where.not(account_id: @account.id).joins(:account).merge(Account.without_suspended) : @account.statuses
@replies = @replies.where(in_reply_to_id: @status.id, visibility: [:public, :unlisted])
@replies = @replies.paginate_by_min_id(DESCENDANTS_LIMIT, params[:min_id])
end
Expand Down
20 changes: 19 additions & 1 deletion app/controllers/concerns/account_owned_concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ def check_account_approval
end

def check_account_suspension
expires_in(3.minutes, public: true) && gone if @account.suspended?
if @account.suspended_permanently?
permanent_suspension_response
elsif @account.suspended? && !skip_temporary_suspension_response?
temporary_suspension_response
end
end

def skip_temporary_suspension_response?
false
end

def permanent_suspension_response
expires_in(3.minutes, public: true)
gone
end

def temporary_suspension_response
expires_in(3.minutes, public: true)
forbidden
end
end
12 changes: 10 additions & 2 deletions app/controllers/follower_accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def page_url(page)
account_followers_url(@account, page: page) unless page.nil?
end

def next_page_url
page_url(follows.next_page) if follows.respond_to?(:next_page)
end

def prev_page_url
page_url(follows.prev_page) if follows.respond_to?(:prev_page)
end

def collection_presenter
if page_requested?
ActivityPub::CollectionPresenter.new(
Expand All @@ -60,8 +68,8 @@ def collection_presenter
size: @account.followers_count,
items: follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.account) },
part_of: account_followers_url(@account),
next: page_url(follows.next_page),
prev: page_url(follows.prev_page)
next: next_page_url,
prev: prev_page_url
)
else
ActivityPub::CollectionPresenter.new(
Expand Down
12 changes: 10 additions & 2 deletions app/controllers/following_accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def page_url(page)
account_following_index_url(@account, page: page) unless page.nil?
end

def next_page_url
page_url(follows.next_page) if follows.respond_to?(:next_page)
end

def prev_page_url
page_url(follows.prev_page) if follows.respond_to?(:prev_page)
end

def collection_presenter
if page_requested?
ActivityPub::CollectionPresenter.new(
Expand All @@ -60,8 +68,8 @@ def collection_presenter
size: @account.following_count,
items: follows.map { |f| ActivityPub::TagManager.instance.uri_for(f.target_account) },
part_of: account_following_index_url(@account),
next: page_url(follows.next_page),
prev: page_url(follows.prev_page)
next: next_page_url,
prev: prev_page_url
)
else
ActivityPub::CollectionPresenter.new(
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/settings/deletes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def challenge_passed?
end

def destroy_account!
current_account.suspend!
current_account.suspend!(origin: :local)
AccountDeletionWorker.perform_async(current_user.account_id)
sign_out
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/well_known/webfinger_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def resource_param
end

def check_account_suspension
expires_in(3.minutes, public: true) && gone if @account.suspended?
expires_in(3.minutes, public: true) && gone if @account.suspended_permanently?
end

def bad_request
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/styles/mastodon/widgets.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
margin-bottom: 10px;
box-shadow: 0 0 15px rgba($base-shadow-color, 0.2);

&:last-child {
margin-bottom: 0;
}

&__img {
width: 100%;
position: relative;
Expand Down
1 change: 1 addition & 0 deletions app/lib/activitypub/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base
voters_count: { 'toot' => 'http://joinmastodon.org/ns#', 'votersCount' => 'toot:votersCount' },
quoteUrl: { 'quoteUrl' => 'as:quoteUrl' },
olm: { 'toot' => 'http://joinmastodon.org/ns#', 'Device' => 'toot:Device', 'Ed25519Signature' => 'toot:Ed25519Signature', 'Ed25519Key' => 'toot:Ed25519Key', 'Curve25519Key' => 'toot:Curve25519Key', 'EncryptedMessage' => 'toot:EncryptedMessage', 'publicKeyBase64' => 'toot:publicKeyBase64', 'deviceId' => 'toot:deviceId', 'claim' => { '@type' => '@id', '@id' => 'toot:claim' }, 'fingerprintKey' => { '@type' => '@id', '@id' => 'toot:fingerprintKey' }, 'identityKey' => { '@type' => '@id', '@id' => 'toot:identityKey' }, 'devices' => { '@type' => '@id', '@id' => 'toot:devices' }, 'messageFranking' => 'toot:messageFranking', 'messageType' => 'toot:messageType', 'cipherText' => 'toot:cipherText' },
suspended: { 'toot' => 'http://joinmastodon.org/ns#', 'suspended' => 'toot:suspended' },
}.freeze

def self.default_key_transform
Expand Down
4 changes: 4 additions & 0 deletions app/lib/webfinger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Webfinger
class Error < StandardError; end
class GoneError < Error; end
class RedirectError < StandardError; end

class Response
def initialize(body)
Expand Down Expand Up @@ -47,6 +49,8 @@ def body_from_webfinger(url = standard_url, use_fallback = true)
res.body_with_limit
elsif res.code == 404 && use_fallback
body_from_host_meta
elsif res.code == 410
raise Webfinger::GoneError, "#{@uri} is gone from the server"
else
raise Webfinger::Error, "Request for #{@uri} returned HTTP #{res.code}"
end
Expand Down
16 changes: 13 additions & 3 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
# header_storage_schema_version :integer
# devices_url :string
# sensitized_at :datetime
# suspension_origin :integer
#

class Account < ApplicationRecord
Expand All @@ -73,6 +74,7 @@ class Account < ApplicationRecord
}.freeze

enum protocol: [:ostatus, :activitypub]
enum suspension_origin: [:local, :remote], _prefix: true

validates :username, presence: true
validates_with UniqueUsernameValidator, if: -> { will_save_change_to_username? }
Expand Down Expand Up @@ -223,17 +225,25 @@ def suspended?
suspended_at.present?
end

def suspend!(date = Time.now.utc)
def suspended_permanently?
suspended? && deletion_request.nil?
end

def suspended_temporarily?
suspended? && deletion_request.present?
end

def suspend!(date: Time.now.utc, origin: :local)
transaction do
create_deletion_request!
update!(suspended_at: date)
update!(suspended_at: date, suspension_origin: origin)
end
end

def unsuspend!
transaction do
deletion_request&.destroy!
update!(suspended_at: nil)
update!(suspended_at: nil, suspension_origin: nil)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/admin/account_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def handle_silence!
def handle_suspend!
authorize(target_account, :suspend?)
log_action(:suspend, target_account)
target_account.suspend!
target_account.suspend!(origin: :local)
end

def text_for_warning
Expand Down
4 changes: 2 additions & 2 deletions app/policies/account_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ def suspend?
end

def destroy?
record.suspended? && record.deletion_request.present? && admin?
record.suspended_temporarily? && admin?
end

def unsuspend?
staff?
staff? && record.suspension_origin_local?
end

def sensitive?
Expand Down
33 changes: 23 additions & 10 deletions app/serializers/activitypub/actor_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer

context_extensions :manually_approves_followers, :featured, :also_known_as,
:moved_to, :property_value, :identity_proof,
:discoverable, :olm
:discoverable, :olm, :suspended

attributes :id, :type, :following, :followers,
:inbox, :outbox, :featured, :featured_tags,
Expand All @@ -23,6 +23,7 @@ class ActivityPub::ActorSerializer < ActivityPub::Serializer
attribute :devices, unless: :instance_actor?
attribute :moved_to, if: :moved?
attribute :also_known_as, if: :also_known_as?
attribute :suspended, if: :suspended?

class EndpointsSerializer < ActivityPub::Serializer
include RoutingHelper
Expand All @@ -39,7 +40,7 @@ def shared_inbox
has_one :icon, serializer: ActivityPub::ImageSerializer, if: :avatar_exists?
has_one :image, serializer: ActivityPub::ImageSerializer, if: :header_exists?

delegate :moved?, :instance_actor?, to: :object
delegate :suspended?, :instance_actor?, to: :object

def id
object.instance_actor? ? instance_actor_url : account_url(object)
Expand Down Expand Up @@ -93,12 +94,16 @@ def preferred_username
object.username
end

def discoverable
object.suspended? ? false : (object.discoverable || false)
end

def name
object.display_name
object.suspended? ? '' : object.display_name
end

def summary
Formatter.instance.simplified_format(object)
object.suspended? ? '' : Formatter.instance.simplified_format(object)
end

def icon
Expand All @@ -113,36 +118,44 @@ def public_key
object
end

def suspended
object.suspended?
end

def url
object.instance_actor? ? about_more_url(instance_actor: true) : short_account_url(object)
end

def avatar_exists?
object.avatar?
!object.suspended? && object.avatar?
end

def header_exists?
object.header?
!object.suspended? && object.header?
end

def manually_approves_followers
object.locked
object.suspended? ? false : object.locked
end

def virtual_tags
object.emojis + object.tags
object.suspended? ? [] : (object.emojis + object.tags)
end

def virtual_attachments
object.fields + object.identity_proofs.active
object.suspended? ? [] : (object.fields + object.identity_proofs.active)
end

def moved_to
ActivityPub::TagManager.instance.uri_for(object.moved_to_account)
end

def moved?
!object.suspended? && object.moved?
end

def also_known_as?
!object.also_known_as.empty?
!object.suspended? && !object.also_known_as.empty?
end

class CustomEmojiSerializer < ActivityPub::EmojiSerializer
Expand Down
Loading

0 comments on commit 201f281

Please sign in to comment.