Add experimental basic quote post authoring (#35355)
This commit is contained in:
@@ -116,6 +116,20 @@ class ActivityPub::Activity
|
||||
fetch_remote_original_status
|
||||
end
|
||||
|
||||
def quote_from_request_json(json)
|
||||
quoted_status_uri = value_or_id(json['object'])
|
||||
quoting_status_uri = value_or_id(json['instrument'])
|
||||
return if quoting_status_uri.nil? || quoted_status_uri.nil?
|
||||
|
||||
quoting_status = status_from_uri(quoting_status_uri)
|
||||
return unless quoting_status.present? && quoting_status.quote.present?
|
||||
|
||||
quoted_status = status_from_uri(quoted_status_uri)
|
||||
return unless quoted_status.present? && quoted_status.account == @account && quoting_status.quote.quoted_status == quoted_status
|
||||
|
||||
quoting_status.quote
|
||||
end
|
||||
|
||||
def dereference_object!
|
||||
return unless @object.is_a?(String)
|
||||
|
||||
@@ -143,6 +157,10 @@ class ActivityPub::Activity
|
||||
@follow_request_from_object ||= FollowRequest.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
|
||||
end
|
||||
|
||||
def quote_request_from_object
|
||||
@quote_request_from_object ||= Quote.find_by(quoted_account: @account, activity_uri: object_uri) unless object_uri.nil?
|
||||
end
|
||||
|
||||
def follow_from_object
|
||||
@follow_from_object ||= ::Follow.find_by(target_account: @account, uri: object_uri) unless object_uri.nil?
|
||||
end
|
||||
|
||||
@@ -4,10 +4,13 @@ class ActivityPub::Activity::Accept < ActivityPub::Activity
|
||||
def perform
|
||||
return accept_follow_for_relay if relay_follow?
|
||||
return accept_follow!(follow_request_from_object) unless follow_request_from_object.nil?
|
||||
return accept_quote!(quote_request_from_object) unless quote_request_from_object.nil?
|
||||
|
||||
case @object['type']
|
||||
when 'Follow'
|
||||
accept_embedded_follow
|
||||
when 'QuoteRequest'
|
||||
accept_embedded_quote_request
|
||||
end
|
||||
end
|
||||
|
||||
@@ -31,6 +34,29 @@ class ActivityPub::Activity::Accept < ActivityPub::Activity
|
||||
RemoteAccountRefreshWorker.perform_async(request.target_account_id) if is_first_follow
|
||||
end
|
||||
|
||||
def accept_embedded_quote_request
|
||||
approval_uri = value_or_id(first_of_value(@json['result']))
|
||||
return if approval_uri.nil?
|
||||
|
||||
quote = quote_from_request_json(@object)
|
||||
return unless quote.present? && quote.status.local?
|
||||
|
||||
accept_quote!(quote)
|
||||
end
|
||||
|
||||
def accept_quote!(quote)
|
||||
approval_uri = value_or_id(first_of_value(@json['result']))
|
||||
return if unsupported_uri_scheme?(approval_uri) || quote.quoted_account != @account || !quote.status.local?
|
||||
|
||||
# NOTE: we are not going through `ActivityPub::VerifyQuoteService` as the `Accept` is as authoritative
|
||||
# as the stamp, but this means we are not checking the stamp, which may lead to inconsistencies
|
||||
# in case of an implementation bug
|
||||
quote.update!(state: :accepted, approval_uri: approval_uri)
|
||||
|
||||
DistributionWorker.perform_async(quote.status_id, { 'update' => true })
|
||||
ActivityPub::StatusUpdateDistributionWorker.perform_async(quote.status_id, { 'updated_at' => Time.now.utc.iso8601 })
|
||||
end
|
||||
|
||||
def accept_follow_for_relay
|
||||
relay.update!(state: :accepted)
|
||||
end
|
||||
|
||||
@@ -5,10 +5,13 @@ class ActivityPub::Activity::Reject < ActivityPub::Activity
|
||||
return reject_follow_for_relay if relay_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?
|
||||
return reject_quote!(quote_request_from_object) unless quote_request_from_object.nil?
|
||||
|
||||
case @object['type']
|
||||
when 'Follow'
|
||||
reject_embedded_follow
|
||||
when 'QuoteRequest'
|
||||
reject_embedded_quote_request
|
||||
end
|
||||
end
|
||||
|
||||
@@ -29,6 +32,20 @@ class ActivityPub::Activity::Reject < ActivityPub::Activity
|
||||
relay.update!(state: :rejected)
|
||||
end
|
||||
|
||||
def reject_embedded_quote_request
|
||||
quote = quote_from_request_json(@object)
|
||||
return unless quote.present? && quote.status.local?
|
||||
|
||||
reject_quote!(quoting_status.quote)
|
||||
end
|
||||
|
||||
def reject_quote!(quote)
|
||||
return unless quote.quoted_account == @account && quote.status.local?
|
||||
|
||||
# TODO: broadcast an update?
|
||||
quote.reject!
|
||||
end
|
||||
|
||||
def relay
|
||||
@relay ||= Relay.find_by(follow_activity_id: object_uri) unless object_uri.nil?
|
||||
end
|
||||
|
||||
@@ -12,9 +12,7 @@ module ActivityPub::CaseTransform
|
||||
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
|
||||
when Symbol then camel_lower(value.to_s).to_sym
|
||||
when String
|
||||
camel_lower_cache[value] ||= if value.start_with?('_:')
|
||||
"_:#{value.delete_prefix('_:').underscore.camelize(:lower)}"
|
||||
elsif LanguagesHelper::ISO_639_1_REGIONAL.key?(value.to_sym)
|
||||
camel_lower_cache[value] ||= if value.start_with?('_misskey') || LanguagesHelper::ISO_639_1_REGIONAL.key?(value.to_sym)
|
||||
value
|
||||
else
|
||||
value.underscore.camelize(:lower)
|
||||
|
||||
Reference in New Issue
Block a user