From c011a988a82722f580dc779c168fbb321c8611d1 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 31 Aug 2020 17:26:08 +0200 Subject: [PATCH] Fix relay outbox endpoint Signed-off-by: Thomas Citharel --- lib/federation/activity_pub/activity_pub.ex | 16 +++++++++++++--- lib/mobilizon/actors/actor.ex | 1 + lib/mobilizon/discussions/discussions.ex | 10 ++++++++++ lib/mobilizon/events/events.ex | 10 ++++++++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/federation/activity_pub/activity_pub.ex b/lib/federation/activity_pub/activity_pub.ex index d6447d88..e7b985ac 100644 --- a/lib/federation/activity_pub/activity_pub.ex +++ b/lib/federation/activity_pub/activity_pub.ex @@ -769,12 +769,22 @@ defmodule Mobilizon.Federation.ActivityPub do Return all public activities (events & comments) for an actor """ @spec fetch_public_activities_for_actor(Actor.t(), integer(), integer()) :: map() - def fetch_public_activities_for_actor(%Actor{} = actor, page \\ 1, limit \\ 10) do + def fetch_public_activities_for_actor(%Actor{id: actor_id} = actor, page \\ 1, limit \\ 10) do + %Actor{id: relay_actor_id} = Relay.get_actor() + %Page{total: total_events, elements: events} = - Events.list_public_events_for_actor(actor, page, limit) + if actor_id == relay_actor_id do + Events.list_public_local_events(page, limit) + else + Events.list_public_events_for_actor(actor, page, limit) + end %Page{total: total_comments, elements: comments} = - Discussions.list_public_comments_for_actor(actor, page, limit) + if actor_id == relay_actor_id do + Discussions.list_local_comments(page, limit) + else + Discussions.list_public_comments_for_actor(actor, page, limit) + end event_activities = Enum.map(events, &event_to_activity/1) comment_activities = Enum.map(comments, &comment_to_activity/1) diff --git a/lib/mobilizon/actors/actor.ex b/lib/mobilizon/actors/actor.ex index 72549c22..d59154fa 100644 --- a/lib/mobilizon/actors/actor.ex +++ b/lib/mobilizon/actors/actor.ex @@ -394,6 +394,7 @@ defmodule Mobilizon.Actors.Actor do "keys" => Crypto.generate_rsa_2048_private_key(), "preferred_username" => "relay", "domain" => nil, + "visibility" => :public, "type" => :Application } diff --git a/lib/mobilizon/discussions/discussions.ex b/lib/mobilizon/discussions/discussions.ex index cfc79d7e..cd6e7f52 100644 --- a/lib/mobilizon/discussions/discussions.ex +++ b/lib/mobilizon/discussions/discussions.ex @@ -239,6 +239,16 @@ defmodule Mobilizon.Discussions do Repo.all(from(c in Comment, where: c.visibility == ^:public)) end + @spec list_local_comments(integer | nil, integer | nil) :: Page.t() + def list_local_comments(page \\ nil, limit \\ nil) do + Comment + |> where([c], c.visibility == ^:public) + |> where([c], is_nil(c.deleted_at)) + |> where([c], is_nil(c.discussion_id)) + |> preload_for_comment() + |> Page.build_page(page, limit) + end + @doc """ Returns the list of public comments for the actor. """ diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 6a5248ce..a6f27cb5 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -375,6 +375,16 @@ defmodule Mobilizon.Events do |> Repo.stream() end + @spec list_public_local_events(integer | nil, integer | nil) :: Page.t() + def list_public_local_events(page \\ nil, limit \\ nil) do + Event + |> filter_public_visibility() + |> filter_draft() + |> filter_local() + |> preload_for_event() + |> Page.build_page(page, limit) + end + @doc """ Returns the list of events with the same tags. """