From 4c479926869f6c020c4ef1e22a90d3c17d90bd69 Mon Sep 17 00:00:00 2001 From: floatingghost Date: Sat, 23 Jul 2022 18:58:45 +0000 Subject: [PATCH 1/4] bugfix/follow-state (#104) Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/104 --- lib/mix/tasks/pleroma/user.ex | 32 ++++++++++++++++++++++++++++ lib/pleroma/user.ex | 8 ++++++- test/pleroma/web/common_api_test.exs | 16 +++++++++++--- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index f8c6d5781..8972d4cfb 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -487,6 +487,38 @@ defmodule Mix.Tasks.Pleroma.User do |> Stream.run() end + def run(["fix_follow_state", local_user, remote_user]) do + start_pleroma() + + with {:local, %User{} = local} <- {:local, User.get_by_nickname(local_user)}, + {:remote, %User{} = remote} <- {:remote, User.get_by_nickname(remote_user)}, + {:follow_data, %{data: %{"state" => request_state}}} <- + {:follow_data, Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(local, remote)} do + calculated_state = User.following?(local, remote) + + shell_info( + "Request state is #{request_state}, vs calculated state of following=#{calculated_state}" + ) + + if calculated_state == false && request_state == "accept" do + shell_info("Discrepancy found, fixing") + Pleroma.Web.CommonAPI.reject_follow_request(local, remote) + shell_info("Relationship fixed") + else + shell_info("No discrepancy found") + end + else + {:local, _} -> + shell_error("No local user #{local_user}") + + {:remote, _} -> + shell_error("No remote user #{remote_user}") + + {:follow_data, _} -> + shell_error("No follow data for #{local_user} and #{remote_user}") + end + end + defp set_moderator(user, value) do {:ok, user} = user diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index d66283632..9a56261a7 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1448,13 +1448,19 @@ defmodule Pleroma.User do blocker end - # clear any requested follows as well + # clear any requested follows from both sides as well blocked = case CommonAPI.reject_follow_request(blocked, blocker) do {:ok, %User{} = updated_blocked} -> updated_blocked nil -> blocked end + blocker = + case CommonAPI.reject_follow_request(blocker, blocked) do + {:ok, %User{} = updated_blocker} -> updated_blocker + nil -> blocker + end + unsubscribe(blocked, blocker) unfollowing_blocked = Config.get([:activitypub, :unfollow_blocked], true) diff --git a/test/pleroma/web/common_api_test.exs b/test/pleroma/web/common_api_test.exs index 09b09fb14..fa751bf60 100644 --- a/test/pleroma/web/common_api_test.exs +++ b/test/pleroma/web/common_api_test.exs @@ -61,9 +61,11 @@ defmodule Pleroma.Web.CommonAPITest do describe "blocking" do setup do blocker = insert(:user) - blocked = insert(:user) - User.follow(blocker, blocked) - User.follow(blocked, blocker) + blocked = insert(:user, local: false) + CommonAPI.follow(blocker, blocked) + CommonAPI.follow(blocked, blocker) + CommonAPI.accept_follow_request(blocker, blocked) + CommonAPI.accept_follow_request(blocked, blocked) %{blocker: blocker, blocked: blocked} end @@ -72,6 +74,9 @@ defmodule Pleroma.Web.CommonAPITest do with_mock Pleroma.Web.Federator, publish: fn _ -> nil end do + assert User.get_follow_state(blocker, blocked) == :follow_accept + refute is_nil(Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(blocker, blocked)) + assert {:ok, block} = CommonAPI.block(blocker, blocked) assert block.local @@ -79,6 +84,11 @@ defmodule Pleroma.Web.CommonAPITest do refute User.following?(blocker, blocked) refute User.following?(blocked, blocker) + refute User.get_follow_state(blocker, blocked) + + assert %{data: %{"state" => "reject"}} = + Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(blocker, blocked) + assert called(Pleroma.Web.Federator.publish(block)) end end From 8e94cbcac7828f3db153794ffb2ae9c5f4741629 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sat, 23 Jul 2022 20:00:38 +0100 Subject: [PATCH 2/4] bump version for release --- mix.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mix.exs b/mix.exs index 1e1c06b7b..61521f72e 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Pleroma.Mixfile do def project do [ app: :pleroma, - version: version("3.0.0"), + version: version("3.0.1"), elixir: "~> 1.9", elixirc_paths: elixirc_paths(Mix.env()), compilers: [:phoenix, :gettext] ++ Mix.compilers(), From c4e9c4bc95c5012a6d33fa4a69d613c672fd130d Mon Sep 17 00:00:00 2001 From: floatingghost Date: Sun, 24 Jul 2022 16:42:43 +0000 Subject: [PATCH 3/4] extend custom runtime system (#108) Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/108 --- CHANGELOG.md | 5 ++++- docs/docs/configuration/cheatsheet.md | 17 ++++++++++++++++- lib/pleroma/utils.ex | 15 ++++++++++++++- test/fixtures/runtime_modules/first.ex | 2 ++ test/fixtures/runtime_modules/nope.exs | 1 + test/fixtures/runtime_modules/subdir/second.ex | 2 ++ test/pleroma/utils_test.exs | 7 +++++++ 7 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 test/fixtures/runtime_modules/first.ex create mode 100644 test/fixtures/runtime_modules/nope.exs create mode 100644 test/fixtures/runtime_modules/subdir/second.ex diff --git a/CHANGELOG.md b/CHANGELOG.md index 22644b03c..d6dd4dd4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [Unreleased] +### Added +- extended runtime module support, see config cheatsheet + ### Fixed - Updated mastoFE path, for the newer version @@ -18,7 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - `/api/v1/notifications/dismiss` - `/api/v1/search` - `/api/v1/statuses/{id}/card` -- LDAP authenticator +- LDAP authenticator (use the akkoma-contrib-authenticator-ldap runtime module) - Chats, they were half-baked. Just use PMs. - Prometheus, it causes massive slowdown diff --git a/docs/docs/configuration/cheatsheet.md b/docs/docs/configuration/cheatsheet.md index fdbfb1a3e..7b0934fcf 100644 --- a/docs/docs/configuration/cheatsheet.md +++ b/docs/docs/configuration/cheatsheet.md @@ -1012,7 +1012,22 @@ config :pleroma, Pleroma.Formatter, ## Custom Runtime Modules (`:modules`) -* `runtime_dir`: A path to custom Elixir modules (such as MRF policies). +* `runtime_dir`: A path to custom Elixir modules, such as MRF policies or + custom authenticators. These modules will be loaded on boot, and can be + contained in subdirectories. It is advised to use version-controlled + subdirectories to make management of them a bit easier. Note that only + files with the extension `.ex` will be loaded. + +```elixir +config :pleroma, :modules, runtime_dir: "instance/modules" +``` + +### Adding a module + +```bash +cd instance/modules/ +git clone +``` ## :configurable_from_database diff --git a/lib/pleroma/utils.ex b/lib/pleroma/utils.ex index a446d3ae6..bd9939c9f 100644 --- a/lib/pleroma/utils.ex +++ b/lib/pleroma/utils.ex @@ -14,10 +14,23 @@ defmodule Pleroma.Utils do @repo_timeout Pleroma.Config.get([Pleroma.Repo, :timeout], 15_000) def compile_dir(dir) when is_binary(dir) do + dir + |> elixir_files() + |> Kernel.ParallelCompiler.compile() + end + + defp elixir_files(dir) when is_binary(dir) do dir |> File.ls!() |> Enum.map(&Path.join(dir, &1)) - |> Kernel.ParallelCompiler.compile() + |> Enum.flat_map(fn path -> + if File.dir?(path) do + elixir_files(path) + else + [path] + end + end) + |> Enum.filter(fn path -> String.ends_with?(path, ".ex") end) end @doc """ diff --git a/test/fixtures/runtime_modules/first.ex b/test/fixtures/runtime_modules/first.ex new file mode 100644 index 000000000..2b663a1b0 --- /dev/null +++ b/test/fixtures/runtime_modules/first.ex @@ -0,0 +1,2 @@ +defmodule DynamicModule.First do +end diff --git a/test/fixtures/runtime_modules/nope.exs b/test/fixtures/runtime_modules/nope.exs new file mode 100644 index 000000000..8fe676af6 --- /dev/null +++ b/test/fixtures/runtime_modules/nope.exs @@ -0,0 +1 @@ +def thisisnotloaded diff --git a/test/fixtures/runtime_modules/subdir/second.ex b/test/fixtures/runtime_modules/subdir/second.ex new file mode 100644 index 000000000..22dc046c6 --- /dev/null +++ b/test/fixtures/runtime_modules/subdir/second.ex @@ -0,0 +1,2 @@ +defmodule DynamicModule.Second do +end diff --git a/test/pleroma/utils_test.exs b/test/pleroma/utils_test.exs index c593a9490..e21573679 100644 --- a/test/pleroma/utils_test.exs +++ b/test/pleroma/utils_test.exs @@ -12,4 +12,11 @@ defmodule Pleroma.UtilsTest do File.rm_rf(path) end end + + describe "compile_dir/1" do + test "recursively compiles directories" do + {:ok, [DynamicModule.First, DynamicModule.Second], []} = + Pleroma.Utils.compile_dir("test/fixtures/runtime_modules") + end + end end From 516d1555584f218b0798ed67acec39c126967102 Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sun, 24 Jul 2022 17:56:48 +0100 Subject: [PATCH 4/4] open up functions in user --- lib/pleroma/user.ex | 6 +++--- mix.exs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 9a56261a7..275ad9506 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -730,12 +730,12 @@ defmodule Pleroma.User do end end - defp put_ap_id(changeset) do + def put_ap_id(changeset) do ap_id = ap_id(%User{nickname: get_field(changeset, :nickname)}) put_change(changeset, :ap_id, ap_id) end - defp put_following_and_follower_and_featured_address(changeset) do + def put_following_and_follower_and_featured_address(changeset) do user = %User{nickname: get_field(changeset, :nickname)} followers = ap_followers(user) following = ap_following(user) @@ -2041,7 +2041,7 @@ defmodule Pleroma.User do |> Enum.map(&String.downcase/1) end - defp local_nickname_regex do + def local_nickname_regex do if Config.get([:instance, :extended_nickname_format]) do @extended_local_nickname_regex else diff --git a/mix.exs b/mix.exs index 61521f72e..ff54c79b4 100644 --- a/mix.exs +++ b/mix.exs @@ -34,7 +34,7 @@ defmodule Pleroma.Mixfile do releases: [ pleroma: [ include_executables_for: [:unix], - applications: [ex_syslogger: :load, syslog: :load], + applications: [ex_syslogger: :load, syslog: :load, eldap: :transient], steps: [:assemble, &put_otp_version/1, ©_files/1, ©_nginx_config/1], config_providers: [{Pleroma.Config.ReleaseRuntimeProvider, []}] ]