message_reference_view.ex 1.68 KB
Newer Older
1
2
3
4
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

5
defmodule Pleroma.Web.PleromaAPI.Chat.MessageReferenceView do
6
7
  use Pleroma.Web, :view

8
  alias Pleroma.Maps
lain's avatar
lain committed
9
  alias Pleroma.User
lain's avatar
lain committed
10
11
  alias Pleroma.Web.CommonAPI.Utils
  alias Pleroma.Web.MastodonAPI.StatusView
12
13
14
15

  def render(
        "show.json",
        %{
16
17
          chat_message_reference: %{
            id: id,
18
            object: %{data: chat_message} = object,
19
            chat_id: chat_id,
20
            unread: unread
21
          }
22
23
24
        }
      ) do
    %{
lain's avatar
lain committed
25
      id: id |> to_string(),
26
      content: chat_message["content"],
lain's avatar
lain committed
27
      chat_id: chat_id |> to_string(),
28
      account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
lain's avatar
lain committed
29
      created_at: Utils.to_masto_date(chat_message["published"]),
lain's avatar
lain committed
30
31
32
      emojis: StatusView.build_emojis(chat_message["emoji"]),
      attachment:
        chat_message["attachment"] &&
33
          StatusView.render("attachment.json", attachment: chat_message["attachment"]),
34
35
36
37
38
39
      unread: unread,
      card:
        StatusView.render(
          "card.json",
          Pleroma.Web.RichMedia.Helpers.fetch_data_for_object(object)
        )
40
    }
41
    |> put_idempotency_key()
42
43
44
  end

  def render("index.json", opts) do
45
46
47
48
49
50
    render_many(
      opts[:chat_message_references],
      __MODULE__,
      "show.json",
      Map.put(opts, :as, :chat_message_reference)
    )
51
  end
52
53
54
55
56
57
58
59
60

  defp put_idempotency_key(data) do
    with {:ok, idempotency_key} <- Cachex.get(:chat_message_id_idempotency_key_cache, data.id) do
      data
      |> Maps.put_if_present(:idempotency_key, idempotency_key)
    else
      _ -> data
    end
  end
61
end