akkoma/lib/pleroma/filter.ex

227 lines
6 KiB
Elixir
Raw Permalink Normal View History

# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2018-08-13 20:27:28 -06:00
defmodule Pleroma.Filter do
use Ecto.Schema
2019-02-09 08:16:26 -07:00
import Ecto.Changeset
import Ecto.Query
alias Pleroma.Repo
alias Pleroma.User
2018-08-13 20:27:28 -06:00
@type t() :: %__MODULE__{}
@type format() :: :postgres | :re
2018-08-13 20:27:28 -06:00
schema "filters" do
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
2018-08-13 20:27:28 -06:00
field(:filter_id, :integer)
field(:hide, :boolean, default: false)
field(:whole_word, :boolean, default: true)
field(:phrase, :string)
field(:context, {:array, :string})
field(:expires_at, :naive_datetime)
2018-08-13 20:27:28 -06:00
timestamps()
end
@spec get(integer() | String.t(), User.t()) :: t() | nil
2018-08-13 20:27:28 -06:00
def get(id, %{id: user_id} = _user) do
query =
from(
f in __MODULE__,
2018-08-13 20:27:28 -06:00
where: f.filter_id == ^id,
where: f.user_id == ^user_id
)
Repo.one(query)
end
@spec get_active(Ecto.Query.t() | module()) :: Ecto.Query.t()
def get_active(query) do
from(f in query, where: is_nil(f.expires_at) or f.expires_at > ^NaiveDateTime.utc_now())
end
@spec get_irreversible(Ecto.Query.t()) :: Ecto.Query.t()
def get_irreversible(query) do
from(f in query, where: f.hide)
end
@spec get_filters(Ecto.Query.t() | module(), User.t()) :: [t()]
def get_filters(query \\ __MODULE__, %User{id: user_id}) do
2018-08-13 20:27:28 -06:00
query =
from(
f in query,
2019-05-15 10:23:01 -06:00
where: f.user_id == ^user_id,
order_by: [desc: :id]
2018-08-13 20:27:28 -06:00
)
Repo.all(query)
end
@spec create(map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
def create(attrs \\ %{}) do
Repo.transaction(fn -> create_with_expiration(attrs) end)
end
defp create_with_expiration(attrs) do
with {:ok, filter} <- do_create(attrs),
{:ok, _} <- maybe_add_expiration_job(filter) do
filter
else
{:error, error} -> Repo.rollback(error)
end
end
defp do_create(attrs) do
%__MODULE__{}
|> cast(attrs, [:phrase, :context, :hide, :expires_at, :whole_word, :user_id, :filter_id])
|> maybe_add_filter_id()
|> validate_required([:phrase, :context, :user_id, :filter_id])
|> maybe_add_expires_at(attrs)
|> Repo.insert()
end
defp maybe_add_filter_id(%{changes: %{filter_id: _}} = changeset), do: changeset
defp maybe_add_filter_id(%{changes: %{user_id: user_id}} = changeset) do
2018-10-11 23:19:43 -06:00
# If filter_id wasn't given, use the max filter_id for this user plus 1.
2018-12-09 02:12:48 -07:00
# XXX This could result in a race condition if a user tries to add two
# different filters for their account from two different clients at the
# same time, but that should be unlikely.
2018-10-11 23:19:43 -06:00
max_id_query =
from(
f in __MODULE__,
2018-10-11 23:19:43 -06:00
where: f.user_id == ^user_id,
select: max(f.filter_id)
)
filter_id =
case Repo.one(max_id_query) do
# Start allocating from 1
nil ->
1
max_id ->
max_id + 1
end
change(changeset, filter_id: filter_id)
end
# don't override expires_at, if passed expires_at and expires_in
defp maybe_add_expires_at(%{changes: %{expires_at: %NaiveDateTime{} = _}} = changeset, _) do
changeset
2018-10-11 23:19:43 -06:00
end
defp maybe_add_expires_at(changeset, %{expires_in: expires_in})
when is_integer(expires_in) and expires_in > 0 do
expires_at =
NaiveDateTime.utc_now()
|> NaiveDateTime.add(expires_in)
|> NaiveDateTime.truncate(:second)
change(changeset, expires_at: expires_at)
2018-08-13 20:27:28 -06:00
end
defp maybe_add_expires_at(changeset, %{expires_in: nil}) do
change(changeset, expires_at: nil)
2018-08-13 20:27:28 -06:00
end
defp maybe_add_expires_at(changeset, _), do: changeset
2018-08-13 20:27:28 -06:00
defp maybe_add_expiration_job(%{expires_at: %NaiveDateTime{} = expires_at} = filter) do
Pleroma.Workers.PurgeExpiredFilter.enqueue(%{
filter_id: filter.id,
expires_at: DateTime.from_naive!(expires_at, "Etc/UTC")
})
2018-08-13 20:27:28 -06:00
end
defp maybe_add_expiration_job(_), do: {:ok, nil}
@spec delete(t()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
def delete(%__MODULE__{} = filter) do
Repo.transaction(fn -> delete_with_expiration(filter) end)
end
defp delete_with_expiration(filter) do
with {:ok, _} <- maybe_delete_old_expiration_job(filter, nil),
{:ok, filter} <- Repo.delete(filter) do
filter
else
{:error, error} -> Repo.rollback(error)
end
end
@spec update(t(), map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
def update(%__MODULE__{} = filter, params) do
Repo.transaction(fn -> update_with_expiration(filter, params) end)
end
defp update_with_expiration(filter, params) do
with {:ok, updated} <- do_update(filter, params),
{:ok, _} <- maybe_delete_old_expiration_job(filter, updated),
{:ok, _} <-
maybe_add_expiration_job(updated) do
updated
else
{:error, error} -> Repo.rollback(error)
end
end
defp do_update(filter, params) do
2020-04-14 08:36:32 -06:00
filter
|> cast(params, [:phrase, :context, :hide, :expires_at, :whole_word])
|> validate_required([:phrase, :context])
|> maybe_add_expires_at(params)
2018-08-13 20:27:28 -06:00
|> Repo.update()
end
defp maybe_delete_old_expiration_job(%{expires_at: nil}, _), do: {:ok, nil}
defp maybe_delete_old_expiration_job(%{expires_at: expires_at}, %{expires_at: expires_at}) do
{:ok, nil}
end
defp maybe_delete_old_expiration_job(%{id: id}, _) do
with %Oban.Job{} = job <- Pleroma.Workers.PurgeExpiredFilter.get_expiration(id) do
Repo.delete(job)
else
nil -> {:ok, nil}
end
end
@spec compose_regex(User.t() | [t()], format()) :: String.t() | Regex.t() | nil
def compose_regex(user_or_filters, format \\ :postgres)
def compose_regex(%User{} = user, format) do
__MODULE__
|> get_active()
|> get_irreversible()
2020-02-26 07:50:56 -07:00
|> get_filters(user)
|> compose_regex(format)
end
def compose_regex([_ | _] = filters, format) do
phrases =
filters
|> Enum.map(& &1.phrase)
|> Enum.join("|")
case format do
:postgres ->
"\\y(#{phrases})\\y"
:re ->
~r/\b#{phrases}\b/i
_ ->
nil
end
end
def compose_regex(_, _), do: nil
2018-08-13 20:27:28 -06:00
end