update mastofe paths (#95)
[akkoma] / lib / pleroma / web / pleroma_api / views / chat / message_reference_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.Chat.MessageReferenceView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.Maps
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.StatusView
12
13 @cachex Pleroma.Config.get([:cachex, :provider], Cachex)
14
15 def render(
16 "show.json",
17 %{
18 chat_message_reference: %{
19 id: id,
20 object: %{data: chat_message} = object,
21 chat_id: chat_id,
22 unread: unread
23 }
24 }
25 ) do
26 %{
27 id: id |> to_string(),
28 content: chat_message["content"],
29 chat_id: chat_id |> to_string(),
30 account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
31 created_at: Utils.to_masto_date(chat_message["published"]),
32 emojis: StatusView.build_emojis(chat_message["emoji"]),
33 attachment:
34 chat_message["attachment"] &&
35 StatusView.render("attachment.json", attachment: chat_message["attachment"]),
36 unread: unread,
37 card:
38 StatusView.render(
39 "card.json",
40 Pleroma.Web.RichMedia.Helpers.fetch_data_for_object(object)
41 )
42 }
43 |> put_idempotency_key()
44 end
45
46 def render("index.json", opts) do
47 render_many(
48 opts[:chat_message_references],
49 __MODULE__,
50 "show.json",
51 Map.put(opts, :as, :chat_message_reference)
52 )
53 end
54
55 defp put_idempotency_key(data) do
56 with {:ok, idempotency_key} <- @cachex.get(:chat_message_id_idempotency_key_cache, data.id) do
57 data
58 |> Maps.put_if_present(:idempotency_key, idempotency_key)
59 else
60 _ -> data
61 end
62 end
63 end