From 93e136d70b181fa271c2b4a5decd258f1287b1fa Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 02:33:11 +0000 Subject: [PATCH 1/7] mix: add user tag/untag task --- lib/mix/tasks/pleroma/user.ex | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index c311d48e0..ffc45fd03 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -52,6 +52,14 @@ defmodule Mix.Tasks.Pleroma.User do - `--locked`/`--no-locked` - whether the user's account is locked - `--moderator`/`--no-moderator` - whether the user is a moderator - `--admin`/`--no-admin` - whether the user is an admin + + ## Add tags to a user. + + mix pleroma.user tag NICKNAME TAGS + + ## Delete tags from a user. + + mix pleroma.user untag NICKNAME TAGS """ def run(["new", nickname, email | rest]) do {options, [], []} = @@ -249,6 +257,32 @@ def run(["set", nickname | rest]) do end end + def run(["tag", nickname | tags]) do + Common.start_pleroma() + + with %User{} = user <- User.get_by_nickname(nickname) do + user = user |> User.tag(tags) + + Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}") + else + _ -> + Mix.shell().error("Could not change user tags for #{nickname}") + end + end + + def run(["untag", nickname | tags]) do + Common.start_pleroma() + + with %User{} = user <- User.get_by_nickname(nickname) do + user = user |> User.untag(tags) + + Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}") + else + _ -> + Mix.shell().error("Could not change user tags for #{nickname}") + end + end + def run(["invite"]) do Common.start_pleroma() From 88e32a32ce6a23de12a431c57a6db8251b0e323a Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 02:35:46 +0000 Subject: [PATCH 2/7] mrf: add initial MRF.TagPolicy engine --- .../web/activity_pub/mrf/tag_policy.ex | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 lib/pleroma/web/activity_pub/mrf/tag_policy.ex diff --git a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex new file mode 100644 index 000000000..4d6dc9c9e --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -0,0 +1,54 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do + alias Pleroma.User + @behaviour Pleroma.Web.ActivityPub.MRF + + defp get_tags(%User{tags: tags}) when is_list(tags), do: tags + defp get_tags(_), do: [] + + defp process_tag( + "mrf_tag:media-force-nsfw", + %{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message + ) + when length(child_attachment) > 0 do + tags = (object["tag"] || []) ++ ["nsfw"] + + object = + object + |> Map.put("tags", tags) + |> Map.put("sensitive", true) + + message = Map.put(message, "object", object) + + {:ok, message} + end + + defp process_tag( + "mrf_tag:media-strip", + %{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message + ) + when length(child_attachment) > 0 do + object = Map.delete(object, "attachment") + message = Map.put(message, "object", object) + + {:ok, message} + end + + defp process_tag(_, message), do: {:ok, message} + + @impl true + def filter(%{"actor" => actor} = message) do + User.get_cached_by_ap_id(actor) + |> get_tags() + |> Enum.reduce({:ok, message}, fn + tag, {:ok, message} -> + process_tag(tag, message) + + _, error -> + error + end) + end +end From 084bb8ccd546410c77cf37e1a9850b83e3782e81 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 15:55:20 +0000 Subject: [PATCH 3/7] activitypub: mrf: tag policy: implement force-unlisted and sandbox --- .../web/activity_pub/mrf/tag_policy.ex | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex index 4d6dc9c9e..e05663371 100644 --- a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -37,6 +37,54 @@ defp process_tag( {:ok, message} end + defp process_tag( + "mrf_tag:force-unlisted", + %{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message + ) do + user = User.get_cached_by_ap_id(actor) + + if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") do + to = + List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address] + + cc = + List.delete(cc, user.follower_address) ++ ["https://www.w3.org/ns/activitystreams#Public"] + + message = + message + |> Map.put("to", to) + |> Map.put("cc", cc) + + {:ok, message} + else + {:ok, message} + end + end + + defp process_tag( + "mrf_tag:sandbox", + %{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message + ) do + user = User.get_cached_by_ap_id(actor) + + if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") or + Enum.member?(cc, "https://www.w3.org/ns/activitystreams#Public") do + to = + List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address] + + cc = List.delete(cc, "https://www.w3.org/ns/activitystreams#Public") + + message = + message + |> Map.put("to", to) + |> Map.put("cc", cc) + + {:ok, message} + else + {:ok, message} + end + end + defp process_tag(_, message), do: {:ok, message} @impl true From 9a69f08e86ce79a36dfe3d1a6f4c20b0a8a0f3c6 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 17:03:34 +0000 Subject: [PATCH 4/7] activitypub: mrf: tag policy: add support for processing follow requests --- lib/pleroma/web/activity_pub/mrf/tag_policy.ex | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex index e05663371..2af36616f 100644 --- a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -88,7 +88,20 @@ defp process_tag( defp process_tag(_, message), do: {:ok, message} @impl true - def filter(%{"actor" => actor} = message) do + def filter(%{"object" => target_actor, "type" => "Follow"} = message) do + User.get_cached_by_ap_id(target_actor) + |> get_tags() + |> Enum.reduce({:ok, message}, fn + tag, {:ok, message} -> + process_tag(tag, message) + + _, error -> + error + end) + end + + @impl true + def filter(%{"actor" => actor, "type" => "Create"} = message) do User.get_cached_by_ap_id(actor) |> get_tags() |> Enum.reduce({:ok, message}, fn @@ -99,4 +112,7 @@ def filter(%{"actor" => actor} = message) do error end) end + + @impl true + def filter(message), do: {:ok, message} end From ff2c28fd6d58b0985e8d59dfbe4ee0d52544e8b3 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 17:06:37 +0000 Subject: [PATCH 5/7] activitypub: mrf: tag policy: refactor the filtering hook a bit --- .../web/activity_pub/mrf/tag_policy.ex | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex index 2af36616f..dd3129707 100644 --- a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -87,21 +87,7 @@ defp process_tag( defp process_tag(_, message), do: {:ok, message} - @impl true - def filter(%{"object" => target_actor, "type" => "Follow"} = message) do - User.get_cached_by_ap_id(target_actor) - |> get_tags() - |> Enum.reduce({:ok, message}, fn - tag, {:ok, message} -> - process_tag(tag, message) - - _, error -> - error - end) - end - - @impl true - def filter(%{"actor" => actor, "type" => "Create"} = message) do + def filter_message(actor, message) do User.get_cached_by_ap_id(actor) |> get_tags() |> Enum.reduce({:ok, message}, fn @@ -113,6 +99,14 @@ def filter(%{"actor" => actor, "type" => "Create"} = message) do end) end + @impl true + def filter(%{"object" => target_actor, "type" => "Follow"} = message), + do: filter_message(target_actor, message) + + @impl true + def filter(%{"actor" => actor, "type" => "Create"} = message), + do: filter_message(actor, message) + @impl true def filter(message), do: {:ok, message} end From 64a3993425a062842a6affc7a6faa81c194c5e2b Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 17:48:48 +0000 Subject: [PATCH 6/7] activitypub: mrf: tag policy: add support for subscription control --- lib/pleroma/web/activity_pub/mrf/tag_policy.ex | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex index dd3129707..901a0f2b0 100644 --- a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -85,6 +85,21 @@ defp process_tag( end end + defp process_tag( + "mrf_tag:disable-remote-subscription", + %{"type" => "Follow", "actor" => actor} = message + ) do + user = User.get_cached_by_ap_id(actor) + + if user.local == true do + {:ok, message} + else + {:reject, nil} + end + end + + defp process_tag("mrf_tag:disable-any-subscription", %{"type" => "Follow"}), do: {:reject, nil} + defp process_tag(_, message), do: {:ok, message} def filter_message(actor, message) do From 7d110be1195dad6f96c8e41ee233daf4563994e3 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 19:03:25 +0000 Subject: [PATCH 7/7] activitypub: mrf: tag policy: fix force-unlisted and sandbox actions --- lib/pleroma/web/activity_pub/mrf/tag_policy.ex | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex index 901a0f2b0..b242e44e6 100644 --- a/lib/pleroma/web/activity_pub/mrf/tag_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -50,10 +50,16 @@ defp process_tag( cc = List.delete(cc, user.follower_address) ++ ["https://www.w3.org/ns/activitystreams#Public"] + object = + message["object"] + |> Map.put("to", to) + |> Map.put("cc", cc) + message = message |> Map.put("to", to) |> Map.put("cc", cc) + |> Map.put("object", object) {:ok, message} else @@ -74,10 +80,16 @@ defp process_tag( cc = List.delete(cc, "https://www.w3.org/ns/activitystreams#Public") + object = + message["object"] + |> Map.put("to", to) + |> Map.put("cc", cc) + message = message |> Map.put("to", to) |> Map.put("cc", cc) + |> Map.put("object", object) {:ok, message} else