total rebase
[anni] / test / pleroma / web / pleroma_api / views / chat_view_test.exs
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
5 defmodule Pleroma.Web.PleromaAPI.ChatViewTest do
6   use Pleroma.DataCase, async: true
7
8   alias Pleroma.Chat
9   alias Pleroma.Chat.MessageReference
10   alias Pleroma.Object
11   alias Pleroma.Web.CommonAPI
12   alias Pleroma.Web.CommonAPI.Utils
13   alias Pleroma.Web.MastodonAPI.AccountView
14   alias Pleroma.Web.PleromaAPI.Chat.MessageReferenceView
15   alias Pleroma.Web.PleromaAPI.ChatView
16
17   import Pleroma.Factory
18
19   test "it represents a chat" do
20     user = insert(:user)
21     recipient = insert(:user)
22
23     {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
24
25     represented_chat = ChatView.render("show.json", chat: chat)
26
27     assert represented_chat == %{
28              id: "#{chat.id}",
29              account:
30                AccountView.render("show.json", user: recipient, skip_visibility_check: true),
31              unread: 0,
32              last_message: nil,
33              updated_at: Utils.to_masto_date(chat.updated_at)
34            }
35
36     {:ok, chat_message_creation} = CommonAPI.post_chat_message(user, recipient, "hello")
37
38     chat_message = Object.normalize(chat_message_creation, fetch: false)
39
40     {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
41
42     represented_chat = ChatView.render("show.json", chat: chat)
43
44     cm_ref = MessageReference.for_chat_and_object(chat, chat_message)
45
46     assert represented_chat[:last_message] ==
47              MessageReferenceView.render("show.json", chat_message_reference: cm_ref)
48   end
49 end