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