Merge branch 'chore/missing-link' into 'develop'
[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.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 @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 def get_by_id(id) do
45 __MODULE__
46 |> Repo.get(id)
47 end
48
49 def get(user_id, recipient) do
50 __MODULE__
51 |> Repo.get_by(user_id: user_id, recipient: recipient)
52 end
53
54 def get_or_create(user_id, recipient) do
55 %__MODULE__{}
56 |> changeset(%{user_id: user_id, recipient: recipient})
57 |> Repo.insert(
58 # Need to set something, otherwise we get nothing back at all
59 on_conflict: [set: [recipient: recipient]],
60 returning: true,
61 conflict_target: [:user_id, :recipient]
62 )
63 end
64
65 def bump_or_create(user_id, recipient) do
66 %__MODULE__{}
67 |> changeset(%{user_id: user_id, recipient: recipient})
68 |> Repo.insert(
69 on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
70 returning: true,
71 conflict_target: [:user_id, :recipient]
72 )
73 end
74
75 @spec for_user_query(FlakeId.Ecto.CompatType.t()) :: Ecto.Query.t()
76 def for_user_query(user_id) do
77 from(c in Chat,
78 where: c.user_id == ^user_id,
79 order_by: [desc: c.updated_at]
80 )
81 end
82 end