From 16ce129e382cc5fa0cf5d86fc0785457a0dedd76 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Sun, 3 Feb 2019 21:24:09 +0300 Subject: [PATCH 01/19] Split hide_network into hide_followers & hide_followings (fixed) --- lib/pleroma/user/info.ex | 6 +- .../web/activity_pub/views/user_view.ex | 8 +-- .../mastodon_api/mastodon_api_controller.ex | 4 +- .../web/twitter_api/twitter_api_controller.ex | 6 +- .../web/twitter_api/views/user_view.ex | 3 +- .../20190203185340_split_hide_network.exs | 11 ++++ .../activity_pub_controller_test.exs | 8 +-- .../mastodon_api_controller_test.exs | 16 ++--- .../twitter_api_controller_test.exs | 60 +++++++++++++------ test/web/twitter_api/views/user_view_test.exs | 12 ++-- 10 files changed, 89 insertions(+), 45 deletions(-) create mode 100644 priv/repo/migrations/20190203185340_split_hide_network.exs diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index c6c923aac..2186190a0 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -30,7 +30,8 @@ defmodule Pleroma.User.Info do field(:topic, :string, default: nil) field(:hub, :string, default: nil) field(:salmon, :string, default: nil) - field(:hide_network, :boolean, default: false) + field(:hide_followers, :boolean, default: false) + field(:hide_followings, :boolean, default: false) field(:pinned_activities, {:array, :string}, default: []) # Found in the wild @@ -143,7 +144,8 @@ defmodule Pleroma.User.Info do :no_rich_text, :default_scope, :banner, - :hide_network, + :hide_followings, + :hide_followers, :background ]) end diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index dcf681b6d..b9588bee5 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -86,7 +86,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do query = from(user in query, select: [:ap_id]) following = Repo.all(query) - collection(following, "#{user.ap_id}/following", page, !user.info.hide_network) + collection(following, "#{user.ap_id}/following", page, !user.info.hide_followings) |> Map.merge(Utils.make_json_ld_header()) end @@ -99,7 +99,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do "id" => "#{user.ap_id}/following", "type" => "OrderedCollection", "totalItems" => length(following), - "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_network) + "first" => collection(following, "#{user.ap_id}/following", 1, !user.info.hide_followings) } |> Map.merge(Utils.make_json_ld_header()) end @@ -109,7 +109,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do query = from(user in query, select: [:ap_id]) followers = Repo.all(query) - collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_network) + collection(followers, "#{user.ap_id}/followers", page, !user.info.hide_followers) |> Map.merge(Utils.make_json_ld_header()) end @@ -122,7 +122,7 @@ defmodule Pleroma.Web.ActivityPub.UserView do "id" => "#{user.ap_id}/followers", "type" => "OrderedCollection", "totalItems" => length(followers), - "first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_network) + "first" => collection(followers, "#{user.ap_id}/followers", 1, !user.info.hide_followers) } |> Map.merge(Utils.make_json_ld_header()) end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 7f3fbff4a..85769c3d7 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -605,7 +605,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do followers = cond do for_user && user.id == for_user.id -> followers - user.info.hide_network -> [] + user.info.hide_followers -> [] true -> followers end @@ -621,7 +621,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do followers = cond do for_user && user.id == for_user.id -> followers - user.info.hide_network -> [] + user.info.hide_followings -> [] true -> followers end diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 3064d61ea..5e3fe9352 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -503,7 +503,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do followers = cond do for_user && user.id == for_user.id -> followers - user.info.hide_network -> [] + user.info.hide_followers -> [] true -> followers end @@ -523,7 +523,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do friends = cond do for_user && user.id == for_user.id -> friends - user.info.hide_network -> [] + user.info.hide_followings -> [] true -> friends end @@ -618,7 +618,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do defp build_info_cng(user, params) do info_params = - ["no_rich_text", "locked", "hide_network"] + ["no_rich_text", "locked", "hide_followers", "hide_followings"] |> Enum.reduce(%{}, fn key, res -> if value = params[key] do Map.put(res, key, value == "true") diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index 15682db8f..cd7c4349c 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -108,7 +108,8 @@ defmodule Pleroma.Web.TwitterAPI.UserView do "locked" => user.info.locked, "default_scope" => user.info.default_scope, "no_rich_text" => user.info.no_rich_text, - "hide_network" => user.info.hide_network, + "hide_followers" => user.info.hide_followers, + "hide_followings" => user.info.hide_followings, "fields" => fields, # Pleroma extension diff --git a/priv/repo/migrations/20190203185340_split_hide_network.exs b/priv/repo/migrations/20190203185340_split_hide_network.exs new file mode 100644 index 000000000..9cc362d17 --- /dev/null +++ b/priv/repo/migrations/20190203185340_split_hide_network.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.SplitHideNetwork do + use Ecto.Migration + + def up do + execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network')") + execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network')") + end + + def down do + end +end diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index d3dd160dd..0a0103793 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -386,9 +386,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do assert result["first"]["orderedItems"] == [user.ap_id] end - test "it returns returns empty if the user has 'hide_network' set", %{conn: conn} do + test "it returns returns empty if the user has 'hide_followers' set", %{conn: conn} do user = insert(:user) - user_two = insert(:user, %{info: %{hide_network: true}}) + user_two = insert(:user, %{info: %{hide_followers: true}}) User.follow(user, user_two) result = @@ -441,8 +441,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do assert result["first"]["orderedItems"] == [user_two.ap_id] end - test "it returns returns empty if the user has 'hide_network' set", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns returns empty if the user has 'hide_followings' set", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) user_two = insert(:user) User.follow(user, user_two) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 8528d4f64..d4e2acae3 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -1101,9 +1101,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert id == to_string(user.id) end - test "getting followers, hide_network", %{conn: conn} do + test "getting followers, hide_followers", %{conn: conn} do user = insert(:user) - other_user = insert(:user, %{info: %{hide_network: true}}) + other_user = insert(:user, %{info: %{hide_followers: true}}) {:ok, _user} = User.follow(user, other_user) conn = @@ -1113,9 +1113,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert [] == json_response(conn, 200) end - test "getting followers, hide_network, same user requesting", %{conn: conn} do + test "getting followers, hide_followers, same user requesting", %{conn: conn} do user = insert(:user) - other_user = insert(:user, %{info: %{hide_network: true}}) + other_user = insert(:user, %{info: %{hide_followers: true}}) {:ok, _user} = User.follow(user, other_user) conn = @@ -1139,8 +1139,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert id == to_string(other_user.id) end - test "getting following, hide_network", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "getting following, hide_followings", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) other_user = insert(:user) {:ok, user} = User.follow(user, other_user) @@ -1151,8 +1151,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do assert [] == json_response(conn, 200) end - test "getting following, hide_network, same user requesting", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "getting following, hide_followings, same user requesting", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) other_user = insert(:user) {:ok, user} = User.follow(user, other_user) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 863abd10f..6777354c3 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1132,8 +1132,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do ) end - test "it returns empty for a hidden network", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns empty when hide_followers is set to true", %{conn: conn} do + user = insert(:user, %{info: %{hide_followers: true}}) follower_one = insert(:user) follower_two = insert(:user) not_follower = insert(:user) @@ -1150,10 +1150,11 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert [] == response end - test "it returns the followers for a hidden network if requested by the user themselves", %{ - conn: conn - } do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns the followers when hide_followers is set to true if requested by the user themselves", + %{ + conn: conn + } do + user = insert(:user, %{info: %{hide_followers: true}}) follower_one = insert(:user) follower_two = insert(:user) _not_follower = insert(:user) @@ -1256,8 +1257,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do ) end - test "it returns empty for a hidden network", %{conn: conn} do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns empty when hide_followings is set to true", %{conn: conn} do + user = insert(:user, %{info: %{hide_followings: true}}) followed_one = insert(:user) followed_two = insert(:user) not_followed = insert(:user) @@ -1273,10 +1274,11 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert [] == json_response(conn, 200) end - test "it returns friends for a hidden network if the user themselves request it", %{ - conn: conn - } do - user = insert(:user, %{info: %{hide_network: true}}) + test "it returns friends when hide_followings is set to true if the user themselves request it", + %{ + conn: conn + } do + user = insert(:user, %{info: %{hide_followings: true}}) followed_one = insert(:user) followed_two = insert(:user) _not_followed = insert(:user) @@ -1364,27 +1366,51 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end - test "it sets and un-sets hide_network", %{conn: conn} do + test "it sets and un-sets hide_followings", %{conn: conn} do user = insert(:user) conn |> assign(:user, user) |> post("/api/account/update_profile.json", %{ - "hide_network" => "true" + "hide_followings" => "true" }) user = Repo.get!(User, user.id) - assert user.info.hide_network == true + assert user.info.hide_followings == true conn = conn |> assign(:user, user) |> post("/api/account/update_profile.json", %{ - "hide_network" => "false" + "hide_followings" => "false" }) user = Repo.get!(User, user.id) - assert user.info.hide_network == false + assert user.info.hide_followings == false + assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) + end + + test "it sets and un-sets hide_followers", %{conn: conn} do + user = insert(:user) + + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "hide_followers" => "true" + }) + + user = Repo.get!(User, user.id) + assert user.info.hide_followers == true + + conn = + conn + |> assign(:user, user) + |> post("/api/account/update_profile.json", %{ + "hide_followers" => "false" + }) + + user = Repo.get!(User, user.id) + assert user.info.hide_followers == false assert json_response(conn, 200) == UserView.render("user.json", %{user: user, for: user}) end diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index daf18c1c5..0885afaec 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -100,7 +100,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, @@ -147,7 +148,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, @@ -195,7 +197,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, @@ -257,7 +260,8 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do "locked" => false, "default_scope" => "public", "no_rich_text" => false, - "hide_network" => false, + "hide_followings" => false, + "hide_followers" => false, "fields" => [], "pleroma" => %{ "confirmation_pending" => false, From 93e136d70b181fa271c2b4a5decd258f1287b1fa Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 02:33:11 +0000 Subject: [PATCH 02/19] 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 @@ defmodule Mix.Tasks.Pleroma.User 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 03/19] 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 04/19] 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do {: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 05/19] 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy 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 06/19] 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy 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 07/19] 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do 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 08/19] 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do 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 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do 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 From cd6606b7f69d1604a08cb31fa539cbd549591e23 Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Mon, 4 Feb 2019 22:23:09 +0300 Subject: [PATCH 09/19] Initialize hide_network with false --- priv/repo/migrations/20190203185340_split_hide_network.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/priv/repo/migrations/20190203185340_split_hide_network.exs b/priv/repo/migrations/20190203185340_split_hide_network.exs index 9cc362d17..fd6fd940a 100644 --- a/priv/repo/migrations/20190203185340_split_hide_network.exs +++ b/priv/repo/migrations/20190203185340_split_hide_network.exs @@ -2,6 +2,7 @@ defmodule Pleroma.Repo.Migrations.SplitHideNetwork do use Ecto.Migration def up do + execute("UPDATE users SET info = '{\"hide_network\": false}'::jsonb WHERE NOT(info::jsonb ? 'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network')") end From f3c8b02d65f6abc8263c73c10c9028606bfe6894 Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 4 Feb 2019 23:47:29 +0100 Subject: [PATCH 10/19] Massage index until it actually does the stuff we want. Also makes the index a lot smoler. --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- .../20190204200237_add_correct_dm_index.exs | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20190204200237_add_correct_dm_index.exs diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 4635e7fcd..b33912721 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -521,7 +521,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do defp restrict_actor(query, _), do: query defp restrict_type(query, %{"type" => type}) when is_binary(type) do - restrict_type(query, %{"type" => [type]}) + from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type)) end defp restrict_type(query, %{"type" => type}) do diff --git a/priv/repo/migrations/20190204200237_add_correct_dm_index.exs b/priv/repo/migrations/20190204200237_add_correct_dm_index.exs new file mode 100644 index 000000000..558732cd2 --- /dev/null +++ b/priv/repo/migrations/20190204200237_add_correct_dm_index.exs @@ -0,0 +1,30 @@ +defmodule Pleroma.Repo.Migrations.AddCorrectDMIndex do + use Ecto.Migration + @disable_ddl_transaction true + + def up do + drop_if_exists( + index(:activities, ["activity_visibility(actor, recipients, data)"], + name: :activities_visibility_index + ) + ) + + create( + index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC NULLS LAST"], + name: :activities_visibility_index, + concurrently: true, + where: "data->>'type' = 'Create'" + ) + ) + end + + def down do + drop( + index(:activities, ["activity_visibility(actor, recipients, data)", "id DESC"], + name: :activities_visibility_index, + concurrently: true, + where: "data->>'type' = 'Create'" + ) + ) + end +end From db1165f70f76b5892c4f3a2861db90549e2291a6 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Mon, 4 Feb 2019 22:58:29 +0000 Subject: [PATCH 11/19] activitypub: c2s: add /api/ap/whoami endpoint for andstatus --- lib/pleroma/web/activity_pub/activity_pub_controller.ex | 8 ++++++++ lib/pleroma/web/router.ex | 1 + 2 files changed, 9 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex index 4dea6ab83..2cdf132e2 100644 --- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex +++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex @@ -198,6 +198,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do end end + def whoami(%{assigns: %{user: %User{} = user}} = conn, _params) do + conn + |> put_resp_header("content-type", "application/activity+json") + |> json(UserView.render("user.json", %{user: user})) + end + + def whoami(_conn, _params), do: {:error, :not_found} + def read_inbox(%{assigns: %{user: user}} = conn, %{"nickname" => nickname} = params) do if nickname == user.nickname do conn diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index c6b4d37ab..7f606ac40 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -454,6 +454,7 @@ defmodule Pleroma.Web.Router do scope "/", Pleroma.Web.ActivityPub do pipe_through([:activitypub_client]) + get("/api/ap/whoami", ActivityPubController, :whoami) get("/users/:nickname/inbox", ActivityPubController, :read_inbox) post("/users/:nickname/outbox", ActivityPubController, :update_outbox) end From e71ab5a10fd19c563ff1627ded58c79ae084f016 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 5 Feb 2019 00:32:49 +0000 Subject: [PATCH 12/19] activitypub: transmogrifier: fix bare tags --- lib/pleroma/web/activity_pub/transmogrifier.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index 43725c3db..7151efdeb 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -313,6 +313,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do |> Map.put("tag", combined) end + def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag]) + def fix_tag(object), do: object # content map usually only has one language so this will do for now. From a2bb5d890d95062e146a4ec6f5923d77ac44a1b9 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 5 Feb 2019 05:06:17 +0000 Subject: [PATCH 13/19] html: don't attempt to parse nil content --- lib/pleroma/html.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/pleroma/html.ex b/lib/pleroma/html.ex index bf5daa948..b4a4742ee 100644 --- a/lib/pleroma/html.ex +++ b/lib/pleroma/html.ex @@ -59,6 +59,8 @@ defmodule Pleroma.HTML do end) end + def extract_first_external_url(_, nil), do: {:error, "No content"} + def extract_first_external_url(object, content) do key = "URL|#{object.id}" From 47eb557617ce1e74781a7d232fcfd92d2b32264a Mon Sep 17 00:00:00 2001 From: "witches.live" Date: Tue, 5 Feb 2019 07:03:54 +0000 Subject: [PATCH 14/19] ARGLE GARBLE Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 72fe2ce43..04c61ede7 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ erl_crash.dump # secrets files as long as you replace their contents by environment # variables. /config/*.secret.exs +/config/generated_config.exs # Database setup file, some may forget to delete it /config/setup_db.psql From c48aba3c2a3e594e5d68e8b4aaa31db1823384ad Mon Sep 17 00:00:00 2001 From: Maxim Filippov Date: Tue, 5 Feb 2019 10:22:31 +0300 Subject: [PATCH 15/19] Fix SQL --- priv/repo/migrations/20190203185340_split_hide_network.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/priv/repo/migrations/20190203185340_split_hide_network.exs b/priv/repo/migrations/20190203185340_split_hide_network.exs index fd6fd940a..9c44e8aff 100644 --- a/priv/repo/migrations/20190203185340_split_hide_network.exs +++ b/priv/repo/migrations/20190203185340_split_hide_network.exs @@ -2,7 +2,7 @@ defmodule Pleroma.Repo.Migrations.SplitHideNetwork do use Ecto.Migration def up do - execute("UPDATE users SET info = '{\"hide_network\": false}'::jsonb WHERE NOT(info::jsonb ? 'hide_network')") + execute("UPDATE users SET info = jsonb_set(info, '{hide_network}'::text[], 'false'::jsonb) WHERE NOT(info::jsonb ? 'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followings}'::text[], info->'hide_network')") execute("UPDATE users SET info = jsonb_set(info, '{hide_followers}'::text[], info->'hide_network')") end From 1d94b67e409010e4da20b4d325813390d9d8bb73 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 5 Feb 2019 18:30:27 +0000 Subject: [PATCH 16/19] mastodon api: fix rendering of cards without image URLs (closes #597) --- lib/pleroma/web/mastodon_api/views/status_view.ex | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index d1b11d4f1..c0e289ef8 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -192,8 +192,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do page_url = page_url_data |> to_string image_url = - URI.merge(page_url_data, URI.parse(rich_media[:image])) - |> to_string + if rich_media[:image] != nil do + URI.merge(page_url_data, URI.parse(rich_media[:image])) + |> to_string + else + nil + end site_name = rich_media[:site_name] || page_url_data.host From c4d317ccb679f7fef11983b9629249e047bec4db Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 5 Feb 2019 18:49:02 +0000 Subject: [PATCH 17/19] test: twitterapi: fix the test breakage for real --- test/web/twitter_api/util_controller_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/web/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index dc9bad369..8e152ecd4 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -36,6 +36,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do describe "GET /api/statusnet/config.json" do test "it returns the managed config", %{conn: conn} do Pleroma.Config.put([:instance, :managed_config], false) + Pleroma.Config.put([:fe, :theme], "rei-ayanami-towel") response = conn From 73e6a1f1dd9bfe9ebf95308292ecb9a2dd67abe4 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 5 Feb 2019 20:04:39 +0000 Subject: [PATCH 18/19] test: twitterapi: fix another possible test failure case --- test/web/twitter_api/util_controller_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/web/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index 8e152ecd4..007d7d8e6 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -36,7 +36,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do describe "GET /api/statusnet/config.json" do test "it returns the managed config", %{conn: conn} do Pleroma.Config.put([:instance, :managed_config], false) - Pleroma.Config.put([:fe, :theme], "rei-ayanami-towel") + Pleroma.Config.put([:fe], theme: "rei-ayanami-towel") response = conn From d83dbd9070e8af0ec6c36c1bbd11c417510fa7e3 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Tue, 5 Feb 2019 20:50:57 +0000 Subject: [PATCH 19/19] rich media: parser: reject any data which cannot be explicitly encoded into JSON --- lib/pleroma/web/rich_media/parser.ex | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lib/pleroma/web/rich_media/parser.ex b/lib/pleroma/web/rich_media/parser.ex index 32dec9887..38f1cdeec 100644 --- a/lib/pleroma/web/rich_media/parser.ex +++ b/lib/pleroma/web/rich_media/parser.ex @@ -54,22 +54,12 @@ defmodule Pleroma.Web.RichMedia.Parser do {:error, "Found metadata was invalid or incomplete: #{inspect(data)}"} end - defp string_is_valid_unicode(data) when is_binary(data) do - data - |> :unicode.characters_to_binary() - |> clean_string() - end - - defp string_is_valid_unicode(data), do: {:ok, data} - - defp clean_string({:error, _, _}), do: {:error, "Invalid data"} - defp clean_string(data), do: {:ok, data} - defp clean_parsed_data(data) do data - |> Enum.reject(fn {_, val} -> - case string_is_valid_unicode(val) do - {:ok, _} -> false + |> Enum.reject(fn {key, val} -> + with {:ok, _} <- Jason.encode(%{key => val}) do + false + else _ -> true end end)