diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | config/config.exs | 4 | ||||
-rw-r--r-- | docs/config.md | 5 | ||||
-rw-r--r-- | lib/mix/tasks/pleroma/user.ex | 34 | ||||
-rw-r--r-- | lib/pleroma/config/deprecation_warnings.ex | 10 | ||||
-rw-r--r-- | lib/pleroma/html.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/mime.ex | 10 | ||||
-rw-r--r-- | lib/pleroma/upload.ex | 4 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex | 44 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/mrf/tag_policy.ex | 139 | ||||
-rw-r--r-- | lib/pleroma/web/activity_pub/transmogrifier.ex | 2 | ||||
-rw-r--r-- | lib/pleroma/web/mastodon_api/views/status_view.ex | 21 | ||||
-rw-r--r-- | test/web/twitter_api/util_controller_test.exs | 1 |
13 files changed, 259 insertions, 18 deletions
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 diff --git a/config/config.exs b/config/config.exs index d0d53a64a..8b42a5351 100644 --- a/config/config.exs +++ b/config/config.exs @@ -227,7 +227,9 @@ config :pleroma, :mrf_rejectnonpublic, allow_followersonly: false, allow_direct: false -config :pleroma, :mrf_hellthread, threshold: 10 +config :pleroma, :mrf_hellthread, + delist_threshold: 5, + reject_threshold: 10 config :pleroma, :mrf_simple, media_removal: [], diff --git a/docs/config.md b/docs/config.md index c14746d93..e042d216f 100644 --- a/docs/config.md +++ b/docs/config.md @@ -17,7 +17,7 @@ Note: `strip_exif` has been replaced by `Pleroma.Upload.Filter.Mogrify`. ## Pleroma.Upload.Filter.Mogrify -* `args`: List of actions for the `mogrify` command like `"strip"` or `["strip", {"impode", "1"}]`. +* `args`: List of actions for the `mogrify` command like `"strip"` or `["strip", "auto-orient", {"impode", "1"}]`. ## Pleroma.Upload.Filter.Dedupe @@ -148,7 +148,8 @@ This section is used to configure Pleroma-FE, unless ``:managed_config`` in ``:i * `allow_direct`: whether to allow direct messages ## :mrf_hellthread -* `threshold`: Number of mentioned users after which the message gets discarded as spam +* `delist_threshold`: Number of mentioned users after which the message gets delisted (the message can still be seen, but it will not show up in public timelines and mentioned users won't get notifications about it). Set to 0 to disable. +* `reject_threshold`: Number of mentioned users after which the messaged gets rejected. Set to 0 to disable. ## :media_proxy * `enabled`: Enables proxying of remote media to the instance’s proxy 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() diff --git a/lib/pleroma/config/deprecation_warnings.ex b/lib/pleroma/config/deprecation_warnings.ex index dc50682ee..7451fd0a7 100644 --- a/lib/pleroma/config/deprecation_warnings.ex +++ b/lib/pleroma/config/deprecation_warnings.ex @@ -14,7 +14,17 @@ defmodule Pleroma.Config.DeprecationWarnings do end end + def check_hellthread_threshold do + if Pleroma.Config.get([:mrf_hellthread, :threshold]) do + Logger.warn(""" + !!!DEPRECATION WARNING!!! + You are using the old configuration mechanism for the hellthread filter. Please check config.md. + """) + end + end + def warn do check_frontend_config_mechanism() + check_hellthread_threshold() end end 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}" diff --git a/lib/pleroma/mime.ex b/lib/pleroma/mime.ex index 84fb536e0..36771533f 100644 --- a/lib/pleroma/mime.ex +++ b/lib/pleroma/mime.ex @@ -102,10 +102,18 @@ defmodule Pleroma.MIME do "audio/ogg" end - defp check_mime_type(<<0x52, 0x49, 0x46, 0x46, _::binary>>) do + defp check_mime_type(<<"RIFF", _::binary-size(4), "WAVE", _::binary>>) do "audio/wav" end + defp check_mime_type(<<"RIFF", _::binary-size(4), "WEBP", _::binary>>) do + "image/webp" + end + + defp check_mime_type(<<"RIFF", _::binary-size(4), "AVI.", _::binary>>) do + "video/avi" + end + defp check_mime_type(_) do @default end diff --git a/lib/pleroma/upload.ex b/lib/pleroma/upload.ex index 0a19e737b..ce2a1b696 100644 --- a/lib/pleroma/upload.ex +++ b/lib/pleroma/upload.ex @@ -124,10 +124,10 @@ defmodule Pleroma.Upload do :pleroma, Pleroma.Upload, [filters: [Pleroma.Upload.Filter.Mogrify]] - :pleroma, Pleroma.Upload.Filter.Mogrify, args: "strip" + :pleroma, Pleroma.Upload.Filter.Mogrify, args: ["strip", "auto-orient"] """) - Pleroma.Config.put([Pleroma.Upload.Filter.Mogrify], args: "strip") + Pleroma.Config.put([Pleroma.Upload.Filter.Mogrify], args: ["strip", "auto-orient"]) Map.put(opts, :filters, opts.filters ++ [Pleroma.Upload.Filter.Mogrify]) else opts diff --git a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex index a3f516ae7..4c6e612b2 100644 --- a/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex @@ -3,20 +3,46 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do + alias Pleroma.User @behaviour Pleroma.Web.ActivityPub.MRF + defp delist_message(message) do + follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address + + message + |> Map.put("to", [follower_collection]) + |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"]) + end + @impl true - def filter(%{"type" => "Create"} = object) do - threshold = Pleroma.Config.get([:mrf_hellthread, :threshold]) - recipients = (object["to"] || []) ++ (object["cc"] || []) - - if length(recipients) > threshold do - {:reject, nil} - else - {:ok, object} + def filter(%{"type" => "Create"} = message) do + delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold]) + + reject_threshold = + Pleroma.Config.get( + [:mrf_hellthread, :reject_threshold], + Pleroma.Config.get([:mrf_hellthread, :threshold]) + ) + + recipients = (message["to"] || []) ++ (message["cc"] || []) + + cond do + length(recipients) > reject_threshold and reject_threshold > 0 -> + {:reject, nil} + + length(recipients) > delist_threshold and delist_threshold > 0 -> + if Enum.member?(message["to"], "https://www.w3.org/ns/activitystreams#Public") or + Enum.member?(message["cc"], "https://www.w3.org/ns/activitystreams#Public") do + {:ok, delist_message(message)} + else + {:ok, message} + end + + true -> + {:ok, message} end end @impl true - def filter(object), do: {:ok, object} + def filter(message), do: {:ok, message} end 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..b242e44e6 --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/tag_policy.ex @@ -0,0 +1,139 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# 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( + "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"] + + 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 + {: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") + + 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 + {:ok, message} + 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 + 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 + + @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 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. diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index d5b7e68c7..c0e289ef8 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -182,8 +182,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do end def render("card.json", %{rich_media: rich_media, page_url: page_url}) do - page_url = rich_media[:url] || page_url - page_url_data = URI.parse(page_url) + page_url_data = + if rich_media[:url] != nil do + URI.merge(URI.parse(page_url), URI.parse(rich_media[:url])) + else + page_url + end + + page_url = page_url_data |> to_string + + image_url = + 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 %{ @@ -191,7 +206,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do provider_name: site_name, provider_url: page_url_data.scheme <> "://" <> page_url_data.host, url: page_url, - image: rich_media[:image] |> MediaProxy.url(), + image: image_url |> MediaProxy.url(), title: rich_media[:title], description: rich_media[:description], pleroma: %{ 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 |