2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# 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
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.User
|
2018-08-13 20:27:28 -06:00
|
|
|
|
|
|
|
schema "filters" do
|
2019-09-18 08:54:31 -06:00
|
|
|
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, :utc_datetime)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(id, %{id: user_id} = _user) do
|
|
|
|
query =
|
|
|
|
from(
|
|
|
|
f in Pleroma.Filter,
|
|
|
|
where: f.filter_id == ^id,
|
|
|
|
where: f.user_id == ^user_id
|
|
|
|
)
|
|
|
|
|
|
|
|
Repo.one(query)
|
|
|
|
end
|
|
|
|
|
2019-11-16 14:54:13 -07:00
|
|
|
def get_active(query) do
|
|
|
|
from(f in query, where: is_nil(f.expires_at) or f.expires_at > ^NaiveDateTime.utc_now())
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_irreversible(query) do
|
|
|
|
from(f in query, where: f.hide)
|
|
|
|
end
|
|
|
|
|
2020-02-26 06:59:07 -07:00
|
|
|
def get_filters(query \\ __MODULE__, %User{id: user_id}) do
|
2018-08-13 20:27:28 -06:00
|
|
|
query =
|
|
|
|
from(
|
2019-11-16 14:54:13 -07:00
|
|
|
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
|
|
|
|
|
2018-10-11 23:19:43 -06:00
|
|
|
def create(%Pleroma.Filter{user_id: user_id, filter_id: nil} = filter) do
|
|
|
|
# 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 Pleroma.Filter,
|
|
|
|
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
|
|
|
|
|
|
|
|
filter
|
|
|
|
|> Map.put(:filter_id, filter_id)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2018-08-13 20:27:28 -06:00
|
|
|
def create(%Pleroma.Filter{} = filter) do
|
|
|
|
Repo.insert(filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(%Pleroma.Filter{id: filter_key} = filter) when is_number(filter_key) do
|
|
|
|
Repo.delete(filter)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(%Pleroma.Filter{id: filter_key} = filter) when is_nil(filter_key) do
|
|
|
|
%Pleroma.Filter{id: id} = get(filter.filter_id, %{id: filter.user_id})
|
|
|
|
|
|
|
|
filter
|
|
|
|
|> Map.put(:id, id)
|
|
|
|
|> Repo.delete()
|
|
|
|
end
|
|
|
|
|
2020-04-14 08:36:32 -06:00
|
|
|
def update(%Pleroma.Filter{} = filter, params) do
|
|
|
|
filter
|
|
|
|
|> cast(params, [:phrase, :context, :hide, :expires_at, :whole_word])
|
|
|
|
|> validate_required([:phrase, :context])
|
2018-08-13 20:27:28 -06:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-11-16 14:54:13 -07:00
|
|
|
|
|
|
|
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)
|
2019-11-16 14:54:13 -07:00
|
|
|
|> 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
|