move to 2.5.5
[anni] / lib / pleroma / chat.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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.Chat
12   alias Pleroma.Repo
13   alias Pleroma.User
14
15   @moduledoc """
16   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).
17
18   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.
19   """
20
21   @type t :: %__MODULE__{}
22   @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
23
24   schema "chats" do
25     belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
26     field(:recipient, :string)
27
28     timestamps()
29   end
30
31   def changeset(struct, params) do
32     struct
33     |> cast(params, [:user_id, :recipient])
34     |> validate_change(:recipient, fn
35       :recipient, recipient ->
36         case User.get_cached_by_ap_id(recipient) do
37           nil -> [recipient: "must be an existing user"]
38           _ -> []
39         end
40     end)
41     |> validate_required([:user_id, :recipient])
42     |> unique_constraint(:user_id, name: :chats_user_id_recipient_index)
43   end
44
45   @spec get_by_user_and_id(User.t(), FlakeId.Ecto.CompatType.t()) ::
46           {:ok, t()} | {:error, :not_found}
47   def get_by_user_and_id(%User{id: user_id}, id) do
48     from(c in __MODULE__,
49       where: c.id == ^id,
50       where: c.user_id == ^user_id
51     )
52     |> Repo.find_resource()
53   end
54
55   @spec get_by_id(FlakeId.Ecto.CompatType.t()) :: t() | nil
56   def get_by_id(id) do
57     Repo.get(__MODULE__, id)
58   end
59
60   @spec get(FlakeId.Ecto.CompatType.t(), String.t()) :: t() | nil
61   def get(user_id, recipient) do
62     Repo.get_by(__MODULE__, user_id: user_id, recipient: recipient)
63   end
64
65   @spec get_or_create(FlakeId.Ecto.CompatType.t(), String.t()) ::
66           {:ok, t()} | {:error, Ecto.Changeset.t()}
67   def get_or_create(user_id, recipient) do
68     %__MODULE__{}
69     |> changeset(%{user_id: user_id, recipient: recipient})
70     |> Repo.insert(
71       # Need to set something, otherwise we get nothing back at all
72       on_conflict: [set: [recipient: recipient]],
73       returning: true,
74       conflict_target: [:user_id, :recipient]
75     )
76   end
77
78   @spec bump_or_create(FlakeId.Ecto.CompatType.t(), String.t()) ::
79           {:ok, t()} | {:error, Ecto.Changeset.t()}
80   def bump_or_create(user_id, recipient) do
81     %__MODULE__{}
82     |> changeset(%{user_id: user_id, recipient: recipient})
83     |> Repo.insert(
84       on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
85       returning: true,
86       conflict_target: [:user_id, :recipient]
87     )
88   end
89
90   @spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t()
91   def for_user_query(user_id) do
92     from(c in Chat,
93       where: c.user_id == ^user_id,
94       order_by: [desc: c.updated_at]
95     )
96   end
97 end