filter.ex 2.3 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

Haelwenn's avatar
Haelwenn committed
5
6
7
defmodule Pleroma.Filter do
  use Ecto.Schema
  import Ecto.{Changeset, Query}
Maksim's avatar
Maksim committed
8
  alias Pleroma.{User, Repo}
Haelwenn's avatar
Haelwenn committed
9
10

  schema "filters" do
Maksim's avatar
Maksim committed
11
    belongs_to(:user, User)
Haelwenn's avatar
Haelwenn committed
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    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

Maksim's avatar
Maksim committed
33
  def get_filters(%User{id: user_id} = _user) do
Haelwenn's avatar
Haelwenn committed
34
35
36
37
38
39
40
41
42
    query =
      from(
        f in Pleroma.Filter,
        where: f.user_id == ^user_id
      )

    Repo.all(query)
  end

Lee Starnes's avatar
Lee Starnes committed
43
44
  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.
Maksim's avatar
Maksim committed
45
46
47
    # 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.
Lee Starnes's avatar
Lee Starnes committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

    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

Haelwenn's avatar
Haelwenn committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  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

  def update(%Pleroma.Filter{} = filter) do
    destination = Map.from_struct(filter)

    Pleroma.Filter.get(filter.filter_id, %{id: filter.user_id})
    |> cast(destination, [:phrase, :context, :hide, :expires_at, :whole_word])
    |> Repo.update()
  end
end