2022-02-05 17:03:53 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Rule do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.Rule
|
|
|
|
|
|
|
|
schema "rules" do
|
|
|
|
field(:priority, :integer, default: 0)
|
|
|
|
field(:text, :string)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def changeset(%Rule{} = rule, params \\ %{}) do
|
|
|
|
rule
|
|
|
|
|> cast(params, [:priority, :text])
|
|
|
|
|> validate_required([:text])
|
|
|
|
end
|
|
|
|
|
|
|
|
def query do
|
|
|
|
Rule
|
|
|
|
|> order_by(asc: :priority)
|
2022-03-05 20:45:34 +00:00
|
|
|
|> order_by(asc: :id)
|
2022-02-05 17:03:53 +00:00
|
|
|
end
|
|
|
|
|
2022-02-20 20:44:42 +00:00
|
|
|
def get(ids) when is_list(ids) do
|
|
|
|
from(r in __MODULE__, where: r.id in ^ids)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2022-02-05 17:03:53 +00:00
|
|
|
def get(id), do: Repo.get(__MODULE__, id)
|
|
|
|
|
2022-06-01 14:03:22 +00:00
|
|
|
def exists?(id) do
|
|
|
|
from(r in __MODULE__, where: r.id == ^id)
|
|
|
|
|> Repo.exists?()
|
|
|
|
end
|
2022-05-30 10:30:03 +00:00
|
|
|
|
2022-02-05 17:03:53 +00:00
|
|
|
def create(params) do
|
|
|
|
{:ok, rule} =
|
|
|
|
%Rule{}
|
|
|
|
|> changeset(params)
|
|
|
|
|> Repo.insert()
|
|
|
|
|
|
|
|
rule
|
|
|
|
end
|
|
|
|
|
|
|
|
def update(params, id) do
|
|
|
|
{:ok, rule} =
|
|
|
|
get(id)
|
|
|
|
|> changeset(params)
|
|
|
|
|> Repo.update()
|
|
|
|
|
|
|
|
rule
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(id) do
|
|
|
|
get(id)
|
|
|
|
|> Repo.delete()
|
|
|
|
end
|
|
|
|
end
|