[KeywordPolicy] Implement the /list and /add routes
This commit is contained in:
parent
bd6dbfe758
commit
fd149754dd
|
@ -60,8 +60,47 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
|
|||
|> put_in(["object", "summary"], summary)}
|
||||
end
|
||||
|
||||
def save_keyword_policy(keyword_policy) do
|
||||
Pleroma.Config.put(:mrf_keyword, keyword_policy)
|
||||
def save_keyword_policy(keyword_policy=%{"federated_timeline_removal" => ftr,
|
||||
"reject" => reject,
|
||||
"replace" => replace}) do
|
||||
with true <- Enum.all?(ftr, &String.valid?(&1)),
|
||||
true <- Enum.all?(reject, &String.valid?(&1)),
|
||||
true <- Enum.all?(Map.keys(replace), &String.valid?(&1)),
|
||||
true <- Enum.all?(Map.values(replace), &String.valid?(&1)) do
|
||||
Pleroma.Config.put(:mrf_keyword, keyword_policy)
|
||||
:ok
|
||||
else
|
||||
false -> {:error, "All elements must be valid strings"}
|
||||
end
|
||||
end
|
||||
|
||||
def list_keyword_policy(), do: Pleroma.Config.get(:mrf_keyword)
|
||||
|
||||
def nodeinfo_keyword_policy() do
|
||||
Pleroma.Config.get(:mrf_keyword, [])
|
||||
|> Enum.map(fn {key, value} ->
|
||||
{key,
|
||||
Enum.map(value, fn
|
||||
{pattern, replacement} ->
|
||||
%{
|
||||
"pattern" =>
|
||||
if not is_binary(pattern) do
|
||||
inspect(pattern)
|
||||
else
|
||||
pattern
|
||||
end,
|
||||
"replacement" => replacement
|
||||
}
|
||||
|
||||
pattern ->
|
||||
if not is_binary(pattern) do
|
||||
inspect(pattern)
|
||||
else
|
||||
pattern
|
||||
end
|
||||
end)}
|
||||
end)
|
||||
|> Enum.into(%{})
|
||||
end
|
||||
|
||||
@impl true
|
||||
|
|
|
@ -6,6 +6,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|
|||
use Pleroma.Web, :controller
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.ActivityPub.Relay
|
||||
alias Pleroma.Web.ActivityPub.MRF.KeywordPolicy
|
||||
|
||||
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
|
||||
|
||||
|
@ -183,10 +184,34 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
|
|||
|> json(token.token)
|
||||
end
|
||||
|
||||
@doc "List this instance's keyword policy"
|
||||
def list_keyword_policy(conn, _params) do
|
||||
mrf_keyword = KeywordPolicy.list_keyword_policy()
|
||||
|
||||
conn
|
||||
|> put_status(200)
|
||||
|> json(mrf_keyword)
|
||||
end
|
||||
|
||||
@doc "Add another MRF Keyword Policy rule"
|
||||
def add_keyword_policy(conn, %{"keyword_policy" => %{"pattern" => pattern, "replacement" => replacement}}) when is_binary(pattern) and
|
||||
is_binary(replacement) do
|
||||
|
||||
def add_keyword_policy(conn, %{"policy" => policy}) do
|
||||
result = policy
|
||||
|> Poison.decode! #FIXME: Better handling of invalid JSON
|
||||
|> IO.inspect()
|
||||
|> KeywordPolicy.save_keyword_policy()
|
||||
|
||||
case result do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_status(201)
|
||||
|> json(%{status: "success", message: "New keyword policy created"})
|
||||
{:error, msg} ->
|
||||
conn
|
||||
|> put_status(422)
|
||||
|> json(%{status: "error", message: msg})
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue