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
47 def create(%Pleroma.Filter{user_id: user_id, filter_id: nil} = filter) do
48 # If filter_id wasn't given, use the max filter_id for this user plus 1.
49 # XXX This could result in a race condition if a user tries to add two
50 # different filters for their account from two different clients at the
51 # same time, but that should be unlikely.
56 where: f.user_id == ^user_id,
57 select: max(f.filter_id)
61 case Repo.one(max_id_query) do
62 # Start allocating from 1
71 |> Map.put(:filter_id, filter_id)
75 def create(%Pleroma.Filter{} = filter) do
79 def delete(%Pleroma.Filter{id: filter_key} = filter) when is_number(filter_key) do
83 def delete(%Pleroma.Filter{id: filter_key} = filter) when is_nil(filter_key) do
84 %Pleroma.Filter{id: id} = get(filter.filter_id, %{id: filter.user_id})
91 def update(%Pleroma.Filter{} = filter) do
92 destination = Map.from_struct(filter)
94 Pleroma.Filter.get(filter.filter_id, %{id: filter.user_id})
95 |> cast(destination, [:phrase, :context, :hide, :expires_at, :whole_word])