1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Filter do
15 belongs_to(:user, User, type: Pleroma.FlakeId)
16 field(:filter_id, :integer)
17 field(:hide, :boolean, default: false)
18 field(:whole_word, :boolean, default: true)
19 field(:phrase, :string)
20 field(:context, {:array, :string})
21 field(:expires_at, :utc_datetime)
26 def get(id, %{id: user_id} = _user) do
30 where: f.filter_id == ^id,
31 where: f.user_id == ^user_id
37 def get_filters(%User{id: user_id} = _user) do
41 where: f.user_id == ^user_id,
48 def create(%Pleroma.Filter{user_id: user_id, filter_id: nil} = filter) do
49 # If filter_id wasn't given, use the max filter_id for this user plus 1.
50 # XXX This could result in a race condition if a user tries to add two
51 # different filters for their account from two different clients at the
52 # same time, but that should be unlikely.
57 where: f.user_id == ^user_id,
58 select: max(f.filter_id)
62 case Repo.one(max_id_query) do
63 # Start allocating from 1
72 |> Map.put(:filter_id, filter_id)
76 def create(%Pleroma.Filter{} = filter) do
80 def delete(%Pleroma.Filter{id: filter_key} = filter) when is_number(filter_key) do
84 def delete(%Pleroma.Filter{id: filter_key} = filter) when is_nil(filter_key) do
85 %Pleroma.Filter{id: id} = get(filter.filter_id, %{id: filter.user_id})
92 def update(%Pleroma.Filter{} = filter) do
93 destination = Map.from_struct(filter)
95 Pleroma.Filter.get(filter.filter_id, %{id: filter.user_id})
96 |> cast(destination, [:phrase, :context, :hide, :expires_at, :whole_word])