logo

pleroma

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

message_reference_view.ex (1781B)


  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.Chat.MessageReferenceView do
  5. use Pleroma.Web, :view
  6. alias Pleroma.Maps
  7. alias Pleroma.User
  8. alias Pleroma.Web.CommonAPI.Utils
  9. alias Pleroma.Web.MastodonAPI.StatusView
  10. @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
  11. def render(
  12. "show.json",
  13. %{
  14. chat_message_reference: %{
  15. id: id,
  16. object: %{data: chat_message} = object,
  17. chat_id: chat_id,
  18. unread: unread
  19. }
  20. }
  21. ) do
  22. %{
  23. id: id |> to_string(),
  24. content: chat_message["content"],
  25. chat_id: chat_id |> to_string(),
  26. account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
  27. created_at: Utils.to_masto_date(chat_message["published"]),
  28. emojis: StatusView.build_emojis(chat_message["emoji"]),
  29. attachment:
  30. chat_message["attachment"] &&
  31. StatusView.render("attachment.json", attachment: chat_message["attachment"]),
  32. unread: unread,
  33. card:
  34. StatusView.render(
  35. "card.json",
  36. Pleroma.Web.RichMedia.Helpers.fetch_data_for_object(object)
  37. )
  38. }
  39. |> put_idempotency_key()
  40. end
  41. def render("index.json", opts) do
  42. render_many(
  43. opts[:chat_message_references],
  44. __MODULE__,
  45. "show.json",
  46. Map.put(opts, :as, :chat_message_reference)
  47. )
  48. end
  49. defp put_idempotency_key(data) do
  50. with {:ok, idempotency_key} <- @cachex.get(:chat_message_id_idempotency_key_cache, data.id) do
  51. data
  52. |> Maps.put_if_present(:idempotency_key, idempotency_key)
  53. else
  54. _ -> data
  55. end
  56. end
  57. end