From ba8ba05761eb095f377bf2b7743ab5c2b5881e64 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 14 Jan 2019 16:21:13 +0100 Subject: [PATCH] Add visibility to comments Signed-off-by: Thomas Citharel --- lib/mobilizon/events/comment.ex | 7 +++---- lib/mobilizon/events/events.ex | 16 ++++++++-------- .../controllers/activity_pub_controller.ex | 2 +- lib/service/activity_pub/activity_pub.ex | 4 ++-- .../20190103150805_fix_event_visibility.exs | 9 +++++++++ test/mobilizon/events/events_test.exs | 13 +++++++------ .../controllers/activity_pub_controller_test.exs | 2 +- 7 files changed, 31 insertions(+), 22 deletions(-) diff --git a/lib/mobilizon/events/comment.ex b/lib/mobilizon/events/comment.ex index 33127ccdd..72bca80dd 100644 --- a/lib/mobilizon/events/comment.ex +++ b/lib/mobilizon/events/comment.ex @@ -2,11 +2,10 @@ import EctoEnum defenum(Mobilizon.Events.CommentVisibilityEnum, :comment_visibility_type, [ :public, - :local, - :group, :unlisted, + :private, :moderated, - :private + :invite ]) defmodule Mobilizon.Events.Comment do @@ -25,7 +24,7 @@ defmodule Mobilizon.Events.Comment do field(:text, :string) field(:url, :string) field(:local, :boolean, default: true) - field(:visibility, :integer) + field(:visibility, Mobilizon.Events.CommentVisibilityEnum, default: :public) field(:uuid, Ecto.UUID) belongs_to(:actor, Actor, foreign_key: :actor_id) belongs_to(:attributed_to, Actor, foreign_key: :attributed_to_id) diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index 14c79da9b..68c5f1464 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -19,11 +19,11 @@ defmodule Mobilizon.Events do queryable end - def get_events_for_actor(%Actor{id: actor_id} = _actor, page \\ nil, limit \\ nil) do + def get_public_events_for_actor(%Actor{id: actor_id} = _actor, page \\ nil, limit \\ nil) do query = from( e in Event, - where: e.organizer_actor_id == ^actor_id, + where: e.organizer_actor_id == ^actor_id and e.visibility in [^:public, ^:unlisted], order_by: [desc: :id], preload: [ :organizer_actor, @@ -50,7 +50,7 @@ defmodule Mobilizon.Events do from( e in Event, select: count(e.id), - where: e.local == ^true and e.visibility == ^:public + where: e.local == ^true and e.visibility in [^:public, ^:unlisted] ) ) end @@ -60,7 +60,7 @@ defmodule Mobilizon.Events do from( c in Comment, select: count(c.id), - where: c.local == ^true + where: c.local == ^true and c.visibility in [^:public, ^:unlisted] ) ) end @@ -870,7 +870,7 @@ defmodule Mobilizon.Events do alias Mobilizon.Events.Comment @doc """ - Returns the list of comments. + Returns the list of public comments. ## Examples @@ -879,14 +879,14 @@ defmodule Mobilizon.Events do """ def list_comments do - Repo.all(Comment) + Repo.all(from(c in Comment, where: c.visibility == ^:public)) end - def get_comments_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do + def get_public_comments_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do query = from( c in Comment, - where: c.actor_id == ^actor_id, + where: c.actor_id == ^actor_id and c.visibility in [^:public, ^:unlisted], order_by: [desc: :id], preload: [ :actor, diff --git a/lib/mobilizon_web/controllers/activity_pub_controller.ex b/lib/mobilizon_web/controllers/activity_pub_controller.ex index 0777f8514..c7c6a9636 100644 --- a/lib/mobilizon_web/controllers/activity_pub_controller.ex +++ b/lib/mobilizon_web/controllers/activity_pub_controller.ex @@ -47,7 +47,7 @@ defmodule MobilizonWeb.ActivityPubController do def event(conn, %{"uuid" => uuid}) do with %Event{} = event <- Events.get_event_full_by_uuid(uuid), - true <- event.public do + true <- event.visibility in [:public, :unlisted] do conn |> put_resp_header("content-type", "application/activity+json") |> json(ObjectView.render("event.json", %{event: event |> Utils.make_event_data()})) diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index 1fc8fbcb3..13979a32f 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -474,8 +474,8 @@ defmodule Mobilizon.Service.ActivityPub do def fetch_public_activities_for_actor(%Actor{} = actor, page, limit) do case actor.type do :Person -> - {:ok, events, total_events} = Events.get_events_for_actor(actor, page, limit) - {:ok, comments, total_comments} = Events.get_comments_for_actor(actor, page, limit) + {:ok, events, total_events} = Events.get_public_events_for_actor(actor, page, limit) + {:ok, comments, total_comments} = Events.get_public_comments_for_actor(actor, page, limit) event_activities = Enum.map(events, &event_to_activity/1) diff --git a/priv/repo/migrations/20190103150805_fix_event_visibility.exs b/priv/repo/migrations/20190103150805_fix_event_visibility.exs index 9a7b6ba6b..b08dfba06 100644 --- a/priv/repo/migrations/20190103150805_fix_event_visibility.exs +++ b/priv/repo/migrations/20190103150805_fix_event_visibility.exs @@ -4,6 +4,7 @@ defmodule Mobilizon.Repo.Migrations.FixEventVisibility do def up do Mobilizon.Events.EventVisibilityEnum.create_type Mobilizon.Events.EventStatusEnum.create_type + Mobilizon.Events.CommentVisibilityEnum.create_type alter table(:events) do remove(:public) remove(:status) @@ -11,6 +12,10 @@ defmodule Mobilizon.Repo.Migrations.FixEventVisibility do add(:visibility, Mobilizon.Events.EventVisibilityEnum.type()) add(:status, Mobilizon.Events.EventStatusEnum.type()) end + + alter table(:comments) do + add(:visibility, Mobilizon.Events.CommentVisibilityEnum.type()) + end end def down do @@ -21,7 +26,11 @@ defmodule Mobilizon.Repo.Migrations.FixEventVisibility do add(:public, :boolean, null: false, default: false) add(:status, :integer, null: false, default: 0) end + alter table(:comments) do + remove(:visibility) + end Mobilizon.Events.EventVisibilityEnum.drop_type Mobilizon.Events.EventStatusEnum.drop_type + Mobilizon.Events.CommentVisibilityEnum.drop_type end end diff --git a/test/mobilizon/events/events_test.exs b/test/mobilizon/events/events_test.exs index 8103f0e45..d16e56e50 100644 --- a/test/mobilizon/events/events_test.exs +++ b/test/mobilizon/events/events_test.exs @@ -125,15 +125,15 @@ defmodule Mobilizon.EventsTest do assert %Ecto.Changeset{} = Events.change_event(event) end - test "get_events_for_actor/1", %{actor: actor, event: event} do - assert {:ok, [event_found], 1} = Events.get_events_for_actor(actor) + test "get_public_events_for_actor/1", %{actor: actor, event: event} do + assert {:ok, [event_found], 1} = Events.get_public_events_for_actor(actor) assert event_found.title == event.title end - test "get_events_for_actor/3", %{actor: actor, event: event} do + test "get_public_events_for_actor/3", %{actor: actor, event: event} do event1 = insert(:event, organizer_actor: actor) - with {:ok, events_found, 2} <- Events.get_events_for_actor(actor, 1, 10) do + with {:ok, events_found, 2} <- Events.get_public_events_for_actor(actor, 1, 10) do event_ids = MapSet.new(events_found |> Enum.map(& &1.id)) assert event_ids == MapSet.new([event.id, event1.id]) else @@ -142,10 +142,11 @@ defmodule Mobilizon.EventsTest do end end - test "get_events_for_actor/3 with limited results", %{actor: actor, event: event} do + test "get_public_events_for_actor/3 with limited results", %{actor: actor, event: event} do event1 = insert(:event, organizer_actor: actor) - with {:ok, [%Event{id: event_found_id}], 2} <- Events.get_events_for_actor(actor, 1, 1) do + with {:ok, [%Event{id: event_found_id}], 2} <- + Events.get_public_events_for_actor(actor, 1, 1) do assert event_found_id in [event.id, event1.id] else err -> diff --git a/test/mobilizon_web/controllers/activity_pub_controller_test.exs b/test/mobilizon_web/controllers/activity_pub_controller_test.exs index 4038a0432..3dfa42d95 100644 --- a/test/mobilizon_web/controllers/activity_pub_controller_test.exs +++ b/test/mobilizon_web/controllers/activity_pub_controller_test.exs @@ -42,7 +42,7 @@ defmodule MobilizonWeb.ActivityPubControllerTest do end test "it returns 404 for non-public events", %{conn: conn} do - event = insert(:event, public: false) + event = insert(:event, visibility: :private) conn = conn