extend custom runtime system (#108)
authorfloatingghost <hannah@coffee-and-dreams.uk>
Sun, 24 Jul 2022 16:42:43 +0000 (16:42 +0000)
committerfloatingghost <hannah@coffee-and-dreams.uk>
Sun, 24 Jul 2022 16:42:43 +0000 (16:42 +0000)
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/108

CHANGELOG.md
docs/docs/configuration/cheatsheet.md
lib/pleroma/utils.ex
test/fixtures/runtime_modules/first.ex [new file with mode: 0644]
test/fixtures/runtime_modules/nope.exs [new file with mode: 0644]
test/fixtures/runtime_modules/subdir/second.ex [new file with mode: 0644]
test/pleroma/utils_test.exs

index 22644b03c4fd2ad2e358d1ea3e9f3243409c29b6..d6dd4dd4ccb4a022e6385643570544d871030cb7 100644 (file)
@@ -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
 
index fdbfb1a3ea493967936f0c5f478e341db1f4a425..7b0934fcf65afe25f42213f9234303b9c63cb61c 100644 (file)
@@ -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 <MY MODULE>
+```
 
 ## :configurable_from_database
 
index a446d3ae6f32a80d73a3780fe69e073f52aaccce..bd9939c9fa9fe9b8f0625052c728ea69a5829dcf 100644 (file)
@@ -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 (file)
index 0000000..2b663a1
--- /dev/null
@@ -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 (file)
index 0000000..8fe676a
--- /dev/null
@@ -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 (file)
index 0000000..22dc046
--- /dev/null
@@ -0,0 +1,2 @@
+defmodule DynamicModule.Second do
+end
index c593a9490705b28abbb8f3465a042692c9c6d156..e2157367990e0c47e2b663bc23958ae7abbc1f15 100644 (file)
@@ -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