logo

pleroma

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

chat_view.ex (1482B)


  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.Web.PleromaAPI.ChatView do
  5. use Pleroma.Web, :view
  6. alias Pleroma.Chat
  7. alias Pleroma.Chat.MessageReference
  8. alias Pleroma.User
  9. alias Pleroma.Web.CommonAPI.Utils
  10. alias Pleroma.Web.MastodonAPI.AccountView
  11. alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
  12. def render("show.json", %{chat: %Chat{} = chat} = opts) do
  13. recipient = User.get_cached_by_ap_id(chat.recipient)
  14. last_message = opts[:last_message] || MessageReference.last_message_for_chat(chat)
  15. account_view_opts = account_view_opts(opts, recipient)
  16. %{
  17. id: chat.id |> to_string(),
  18. account: AccountView.render("show.json", account_view_opts),
  19. unread: MessageReference.unread_count_for_chat(chat),
  20. last_message:
  21. last_message &&
  22. MessageReferenceView.render("show.json", chat_message_reference: last_message),
  23. updated_at: Utils.to_masto_date(chat.updated_at)
  24. }
  25. end
  26. def render("index.json", %{chats: chats} = opts) do
  27. render_many(chats, __MODULE__, "show.json", Map.delete(opts, :chats))
  28. end
  29. defp account_view_opts(opts, recipient) do
  30. account_view_opts = Map.put(opts, :user, recipient)
  31. if Map.has_key?(account_view_opts, :for) do
  32. account_view_opts
  33. else
  34. Map.put(account_view_opts, :skip_visibility_check, true)
  35. end
  36. end
  37. end