Skip to content

Commit

Permalink
Allow private posts with references
Browse files Browse the repository at this point in the history
  • Loading branch information
kmycode committed Sep 19, 2023
1 parent 818bcc0 commit 11300d7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
21 changes: 14 additions & 7 deletions app/serializers/activitypub/note_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class ActivityPub::NoteSerializer < ActivityPub::Serializer
:atom_uri, :in_reply_to_atom_uri,
:conversation, :searchable_by, :limited_scope

attribute :references, if: :not_private_post?

attribute :content
attribute :content_map, if: :language?
attribute :updated, if: :edited?
Expand All @@ -25,6 +23,7 @@ class ActivityPub::NoteSerializer < ActivityPub::Serializer
has_many :virtual_tags, key: :tag

has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local?
has_one :references, serializer: ActivityPub::CollectionSerializer

has_many :poll_options, key: :one_of, if: :poll_and_not_multiple?
has_many :poll_options, key: :any_of, if: :poll_and_multiple?
Expand Down Expand Up @@ -71,7 +70,19 @@ def replies
end

def references
ActivityPub::TagManager.instance.references_uri_for(object)
refs = object.references.reorder(id: :asc).take(5).pluck(:id, :uri)
last_id = refs.last&.first

ActivityPub::CollectionPresenter.new(
type: :unordered,
id: ActivityPub::TagManager.instance.references_uri_for(object),
first: ActivityPub::CollectionPresenter.new(
type: :unordered,
part_of: ActivityPub::TagManager.instance.references_uri_for(object),
items: refs.map(&:second),
next: last_id ? ActivityPub::TagManager.instance.references_uri_for(object, page: true, min_id: last_id) : ActivityPub::TagManager.instance.references_uri_for(object, page: true, only_other_accounts: true)
)
)
end

def language?
Expand Down Expand Up @@ -160,10 +171,6 @@ def local?
object.account.local?
end

def not_private_post?
!object.private_visibility? && !object.direct_visibility? && !object.limited_visibility?
end

def quote?
object.references.count == 1 && object.account.user&.settings&.[]('single_ref_to_quote')
end
Expand Down
28 changes: 27 additions & 1 deletion spec/serializers/activitypub/note_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
let!(:reply_by_account_third) { Fabricate(:status, account: account, thread: parent, visibility: :public) }
let!(:reply_by_account_visibility_direct) { Fabricate(:status, account: account, thread: parent, visibility: :direct) }
let!(:referred) { nil }
let!(:referred2) { nil }
let(:convert_to_quote) { false }

before(:each) do
parent.references << referred if referred.present?
parent.references << referred2 if referred2.present?
account.user&.settings&.[]=('single_ref_to_quote', true) if convert_to_quote
@serialization = ActiveModelSerializers::SerializableResource.new(parent, serializer: described_class, adapter: ActivityPub::Adapter)
end
Expand Down Expand Up @@ -49,8 +51,17 @@
context 'when has quote but no_convert setting' do
let(:referred) { Fabricate(:status) }

it 'has a references collection' do
expect(subject['references']['type']).to eql('Collection')
end

it 'has a references collection with a first Page' do
expect(subject['references']['first']['type']).to eql('CollectionPage')
end

it 'has as reference' do
expect(subject['quoteUri']).to be_nil
expect(subject['references']['first']['items']).to include referred.uri
end
end

Expand All @@ -60,7 +71,22 @@

it 'has as quote' do
expect(subject['quoteUri']).to_not be_nil
expect(subject['_misskey_quote'] == subject['quoteUri']).to be true
expect(subject['quoteUri']).to eq referred.uri
expect(subject['_misskey_quote']).to eq referred.uri
expect(subject['_misskey_content']).to eq referred.text
expect(subject['references']['first']['items']).to include referred.uri
end
end

context 'when has multiple references and convert setting' do
let(:referred) { Fabricate(:status) }
let(:referred2) { Fabricate(:status) }
let(:convert_to_quote) { true }

it 'has as quote' do
expect(subject['quoteUri']).to be_nil
expect(subject['references']['first']['items']).to include referred.uri
expect(subject['references']['first']['items']).to include referred2.uri
end
end
end

0 comments on commit 11300d7

Please sign in to comment.