added paginate links to headers for /chats/:id/messages
[akkoma] / lib / pleroma / chat.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Chat do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10
11 alias Pleroma.Repo
12 alias Pleroma.User
13
14 @moduledoc """
15 Chat keeps a reference to ChatMessage conversations between a user and an recipient. The recipient can be a user (for now) or a group (not implemented yet).
16
17 It is a helper only, to make it easy to display a list of chats with other people, ordered by last bump. The actual messages are retrieved by querying the recipients of the ChatMessages.
18 """
19
20 @type t :: %__MODULE__{}
21 @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
22
23 schema "chats" do
24 belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
25 field(:recipient, :string)
26
27 timestamps()
28 end
29
30 def changeset(struct, params) do
31 struct
32 |> cast(params, [:user_id, :recipient])
33 |> validate_change(:recipient, fn
34 :recipient, recipient ->
35 case User.get_cached_by_ap_id(recipient) do
36 nil -> [recipient: "must be an existing user"]
37 _ -> []
38 end
39 end)
40 |> validate_required([:user_id, :recipient])
41 |> unique_constraint(:user_id, name: :chats_user_id_recipient_index)
42 end
43
44 @spec get_by_user_and_id(User.t(), FlakeId.Ecto.CompatType.t()) ::
45 {:ok, t()} | {:error, :not_found}
46 def get_by_user_and_id(%User{id: user_id}, id) do
47 from(c in __MODULE__,
48 where: c.id == ^id,
49 where: c.user_id == ^user_id
50 )
51 |> Repo.find_resource()
52 end
53
54 @spec get_by_id(FlakeId.Ecto.CompatType.t()) :: t() | nil
55 def get_by_id(id) do
56 Repo.get(__MODULE__, id)
57 end
58
59 @spec get(FlakeId.Ecto.CompatType.t(), String.t()) :: t() | nil
60 def get(user_id, recipient) do
61 Repo.get_by(__MODULE__, user_id: user_id, recipient: recipient)
62 end
63
64 @spec get_or_create(FlakeId.Ecto.CompatType.t(), String.t()) ::
65 {:ok, t()} | {:error, Ecto.Changeset.t()}
66 def get_or_create(user_id, recipient) do
67 %__MODULE__{}
68 |> changeset(%{user_id: user_id, recipient: recipient})
69 |> Repo.insert(
70 # Need to set something, otherwise we get nothing back at all
71 on_conflict: [set: [recipient: recipient]],
72 returning: true,
73 conflict_target: [:user_id, :recipient]
74 )
75 end
76
77 @spec bump_or_create(FlakeId.Ecto.CompatType.t(), String.t()) ::
78 {:ok, t()} | {:error, Ecto.Changeset.t()}
79 def bump_or_create(user_id, recipient) do
80 %__MODULE__{}
81 |> changeset(%{user_id: user_id, recipient: recipient})
82 |> Repo.insert(
83 on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
84 returning: true,
85 conflict_target: [:user_id, :recipient]
86 )
87 end
88 end