Merge pull request 'Manually define PATH for Arch Linux users in systemd unit' (...
[akkoma] / lib / pleroma / user_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.UserNote do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.UserNote
14
15 schema "user_notes" do
16 belongs_to(:source, User, type: FlakeId.Ecto.CompatType)
17 belongs_to(:target, User, type: FlakeId.Ecto.CompatType)
18 field(:comment, :string)
19
20 timestamps()
21 end
22
23 def changeset(%UserNote{} = user_note, params \\ %{}) do
24 user_note
25 |> cast(params, [:source_id, :target_id, :comment])
26 |> validate_required([:source_id, :target_id])
27 end
28
29 def show(%User{} = source, %User{} = target) do
30 with %UserNote{} = note <-
31 UserNote
32 |> where(source_id: ^source.id, target_id: ^target.id)
33 |> Repo.one() do
34 note.comment
35 else
36 _ -> ""
37 end
38 end
39
40 def create(%User{} = source, %User{} = target, comment) do
41 %UserNote{}
42 |> changeset(%{
43 source_id: source.id,
44 target_id: target.id,
45 comment: comment
46 })
47 |> Repo.insert(
48 on_conflict: {:replace, [:comment]},
49 conflict_target: [:source_id, :target_id]
50 )
51 end
52 end