Add compatibility with bookwyrm's weird entities
[akkoma] / lib / pleroma / report_note.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.ReportNote do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10
11 alias Pleroma.Activity
12 alias Pleroma.Repo
13 alias Pleroma.ReportNote
14 alias Pleroma.User
15
16 @type t :: %__MODULE__{}
17
18 schema "report_notes" do
19 field(:content, :string)
20 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
21 belongs_to(:activity, Activity, type: FlakeId.Ecto.CompatType)
22
23 timestamps()
24 end
25
26 @spec create(FlakeId.Ecto.CompatType.t(), FlakeId.Ecto.CompatType.t(), String.t()) ::
27 {:ok, ReportNote.t()} | {:error, Changeset.t()}
28 def create(user_id, activity_id, content) do
29 attrs = %{
30 user_id: user_id,
31 activity_id: activity_id,
32 content: content
33 }
34
35 %ReportNote{}
36 |> cast(attrs, [:user_id, :activity_id, :content])
37 |> validate_required([:user_id, :activity_id, :content])
38 |> Repo.insert()
39 end
40
41 @spec destroy(FlakeId.Ecto.CompatType.t()) ::
42 {:ok, ReportNote.t()} | {:error, Changeset.t()}
43 def destroy(id) do
44 from(r in ReportNote, where: r.id == ^id)
45 |> Repo.one()
46 |> Repo.delete()
47 end
48 end