logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

chat.ex (2932B)


  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. defmodule Pleroma.Chat do
  5. use Ecto.Schema
  6. import Ecto.Changeset
  7. import Ecto.Query
  8. alias Pleroma.Chat
  9. alias Pleroma.Repo
  10. alias Pleroma.User
  11. @moduledoc """
  12. 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).
  13. 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.
  14. """
  15. @type t :: %__MODULE__{}
  16. @primary_key {:id, FlakeId.Ecto.CompatType, autogenerate: true}
  17. schema "chats" do
  18. belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
  19. field(:recipient, :string)
  20. timestamps()
  21. end
  22. def changeset(struct, params) do
  23. struct
  24. |> cast(params, [:user_id, :recipient])
  25. |> validate_change(:recipient, fn
  26. :recipient, recipient ->
  27. case User.get_cached_by_ap_id(recipient) do
  28. nil -> [recipient: "must be an existing user"]
  29. _ -> []
  30. end
  31. end)
  32. |> validate_required([:user_id, :recipient])
  33. |> unique_constraint(:user_id, name: :chats_user_id_recipient_index)
  34. end
  35. @spec get_by_user_and_id(User.t(), Ecto.UUID.t()) ::
  36. {:ok, t()} | {:error, :not_found}
  37. def get_by_user_and_id(%User{id: user_id}, id) do
  38. from(c in __MODULE__,
  39. where: c.id == ^id,
  40. where: c.user_id == ^user_id
  41. )
  42. |> Repo.find_resource()
  43. end
  44. @spec get_by_id(Ecto.UUID.t()) :: t() | nil
  45. def get_by_id(id) do
  46. Repo.get(__MODULE__, id)
  47. end
  48. @spec get(Ecto.UUID.t(), String.t()) :: t() | nil
  49. def get(user_id, recipient) do
  50. Repo.get_by(__MODULE__, user_id: user_id, recipient: recipient)
  51. end
  52. @spec get_or_create(Ecto.UUID.t(), String.t()) ::
  53. {:ok, t()} | {:error, Ecto.Changeset.t()}
  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. @spec bump_or_create(Ecto.UUID.t(), String.t()) ::
  65. {:ok, t()} | {:error, Ecto.Changeset.t()}
  66. def bump_or_create(user_id, recipient) do
  67. %__MODULE__{}
  68. |> changeset(%{user_id: user_id, recipient: recipient})
  69. |> Repo.insert(
  70. on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
  71. returning: true,
  72. conflict_target: [:user_id, :recipient]
  73. )
  74. end
  75. @spec for_user_query(Ecto.UUID.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