2020-04-15 18:23:16 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-06 11:51:10 +02:00
|
|
|
defmodule Pleroma.Web.PleromaAPI.Chat.MessageReferenceView do
|
2020-04-15 18:23:16 +02:00
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
2020-10-31 03:50:48 +01:00
|
|
|
alias Pleroma.Maps
|
2020-04-27 17:55:29 +02:00
|
|
|
alias Pleroma.User
|
2020-04-20 14:38:53 +02:00
|
|
|
alias Pleroma.Web.CommonAPI.Utils
|
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
2020-04-15 18:23:16 +02:00
|
|
|
|
2020-12-18 17:44:46 +01:00
|
|
|
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
|
|
|
|
|
2020-04-15 18:23:16 +02:00
|
|
|
def render(
|
|
|
|
"show.json",
|
|
|
|
%{
|
2020-06-03 12:30:12 +02:00
|
|
|
chat_message_reference: %{
|
|
|
|
id: id,
|
2020-07-30 19:57:26 +02:00
|
|
|
object: %{data: chat_message} = object,
|
2020-06-03 12:30:12 +02:00
|
|
|
chat_id: chat_id,
|
2020-06-04 17:14:42 +02:00
|
|
|
unread: unread
|
2020-06-03 12:30:12 +02:00
|
|
|
}
|
2020-04-15 18:23:16 +02:00
|
|
|
}
|
|
|
|
) do
|
|
|
|
%{
|
2020-04-16 18:43:31 +02:00
|
|
|
id: id |> to_string(),
|
2020-04-15 18:23:16 +02:00
|
|
|
content: chat_message["content"],
|
2020-04-16 18:43:31 +02:00
|
|
|
chat_id: chat_id |> to_string(),
|
2020-04-27 17:48:34 +02:00
|
|
|
account_id: User.get_cached_by_ap_id(chat_message["actor"]).id,
|
2020-04-20 14:38:53 +02:00
|
|
|
created_at: Utils.to_masto_date(chat_message["published"]),
|
2020-05-06 16:12:36 +02:00
|
|
|
emojis: StatusView.build_emojis(chat_message["emoji"]),
|
|
|
|
attachment:
|
|
|
|
chat_message["attachment"] &&
|
2020-06-03 12:30:12 +02:00
|
|
|
StatusView.render("attachment.json", attachment: chat_message["attachment"]),
|
2020-07-30 19:57:26 +02:00
|
|
|
unread: unread,
|
|
|
|
card:
|
|
|
|
StatusView.render(
|
|
|
|
"card.json",
|
|
|
|
Pleroma.Web.RichMedia.Helpers.fetch_data_for_object(object)
|
|
|
|
)
|
2020-04-15 18:23:16 +02:00
|
|
|
}
|
2020-10-31 03:50:48 +01:00
|
|
|
|> put_idempotency_key()
|
2020-04-15 18:23:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def render("index.json", opts) do
|
2020-06-03 12:30:12 +02:00
|
|
|
render_many(
|
|
|
|
opts[:chat_message_references],
|
|
|
|
__MODULE__,
|
|
|
|
"show.json",
|
|
|
|
Map.put(opts, :as, :chat_message_reference)
|
|
|
|
)
|
2020-04-15 18:23:16 +02:00
|
|
|
end
|
2020-10-31 03:50:48 +01:00
|
|
|
|
|
|
|
defp put_idempotency_key(data) do
|
2020-12-18 17:44:46 +01:00
|
|
|
with {:ok, idempotency_key} <- @cachex.get(:chat_message_id_idempotency_key_cache, data.id) do
|
2020-10-31 03:50:48 +01:00
|
|
|
data
|
|
|
|
|> Maps.put_if_present(:idempotency_key, idempotency_key)
|
|
|
|
else
|
|
|
|
_ -> data
|
|
|
|
end
|
|
|
|
end
|
2020-04-15 18:23:16 +02:00
|
|
|
end
|