ChatController: Handle pagination.
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4 defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
5 use Pleroma.Web.ConnCase, async: true
6
7 alias Pleroma.Chat
8 alias Pleroma.Web.CommonAPI
9
10 import Pleroma.Factory
11
12 describe "POST /api/v1/pleroma/chats/:id/messages" do
13 test "it posts a message to the chat", %{conn: conn} do
14 user = insert(:user)
15 other_user = insert(:user)
16
17 {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
18
19 result =
20 conn
21 |> assign(:user, user)
22 |> post("/api/v1/pleroma/chats/#{chat.id}/messages", %{"content" => "Hallo!!"})
23 |> json_response(200)
24
25 assert result["content"] == "Hallo!!"
26 assert result["chat_id"] == chat.id |> to_string()
27 end
28 end
29
30 describe "GET /api/v1/pleroma/chats/:id/messages" do
31 test "it paginates", %{conn: conn} do
32 user = insert(:user)
33 recipient = insert(:user)
34
35 Enum.each(1..30, fn _ ->
36 {:ok, _} = CommonAPI.post_chat_message(user, recipient, "hey")
37 end)
38
39 chat = Chat.get(user.id, recipient.ap_id)
40
41 result =
42 conn
43 |> assign(:user, user)
44 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
45 |> json_response(200)
46
47 assert length(result) == 20
48
49 result =
50 conn
51 |> assign(:user, user)
52 |> get("/api/v1/pleroma/chats/#{chat.id}/messages", %{"max_id" => List.last(result)["id"]})
53 |> json_response(200)
54
55 assert length(result) == 10
56 end
57
58 # TODO
59 # - Test the case where it's not the user's chat
60 test "it returns the messages for a given chat", %{conn: conn} do
61 user = insert(:user)
62 other_user = insert(:user)
63 third_user = insert(:user)
64
65 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
66 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
67 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
68 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
69
70 chat = Chat.get(user.id, other_user.ap_id)
71
72 result =
73 conn
74 |> assign(:user, user)
75 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
76 |> json_response(200)
77
78 result
79 |> Enum.each(fn message ->
80 assert message["chat_id"] == chat.id |> to_string()
81 end)
82
83 assert length(result) == 3
84 end
85 end
86
87 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
88 test "it creates or returns a chat", %{conn: conn} do
89 user = insert(:user)
90 other_user = insert(:user)
91
92 result =
93 conn
94 |> assign(:user, user)
95 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
96 |> json_response(200)
97
98 assert result["id"]
99 end
100 end
101
102 describe "GET /api/v1/pleroma/chats" do
103 test "it paginates", %{conn: conn} do
104 user = insert(:user)
105
106 Enum.each(1..30, fn _ ->
107 recipient = insert(:user)
108 {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
109 end)
110
111 result =
112 conn
113 |> assign(:user, user)
114 |> get("/api/v1/pleroma/chats")
115 |> json_response(200)
116
117 assert length(result) == 20
118
119 result =
120 conn
121 |> assign(:user, user)
122 |> get("/api/v1/pleroma/chats", %{max_id: List.last(result)["id"]})
123 |> json_response(200)
124
125 assert length(result) == 10
126 end
127
128 test "it return a list of chats the current user is participating in, in descending order of updates",
129 %{conn: conn} do
130 user = insert(:user)
131 har = insert(:user)
132 jafnhar = insert(:user)
133 tridi = insert(:user)
134
135 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
136 :timer.sleep(1000)
137 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
138 :timer.sleep(1000)
139 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
140 :timer.sleep(1000)
141
142 # bump the second one
143 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
144
145 result =
146 conn
147 |> assign(:user, user)
148 |> get("/api/v1/pleroma/chats")
149 |> json_response(200)
150
151 ids = Enum.map(result, & &1["id"])
152
153 assert ids == [
154 chat_2.id |> to_string(),
155 chat_3.id |> to_string(),
156 chat_1.id |> to_string()
157 ]
158 end
159 end
160 end