Chat: Add views, don't return them in timeline queries.
[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
27 end
28 end
29
30 describe "GET /api/v1/pleroma/chats/:id/messages" do
31 # TODO
32 # - Test the case where it's not the user's chat
33 test "it returns the messages for a given chat", %{conn: conn} do
34 user = insert(:user)
35 other_user = insert(:user)
36 third_user = insert(:user)
37
38 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "hey")
39 {:ok, _} = CommonAPI.post_chat_message(user, third_user, "hey")
40 {:ok, _} = CommonAPI.post_chat_message(user, other_user, "how are you?")
41 {:ok, _} = CommonAPI.post_chat_message(other_user, user, "fine, how about you?")
42
43 chat = Chat.get(user.id, other_user.ap_id)
44
45 result =
46 conn
47 |> assign(:user, user)
48 |> get("/api/v1/pleroma/chats/#{chat.id}/messages")
49 |> json_response(200)
50
51 result
52 |> Enum.each(fn message ->
53 assert message["chat_id"] == chat.id
54 end)
55
56 assert length(result) == 3
57 end
58 end
59
60 describe "POST /api/v1/pleroma/chats/by-ap-id/:id" do
61 test "it creates or returns a chat", %{conn: conn} do
62 user = insert(:user)
63 other_user = insert(:user)
64
65 result =
66 conn
67 |> assign(:user, user)
68 |> post("/api/v1/pleroma/chats/by-ap-id/#{URI.encode_www_form(other_user.ap_id)}")
69 |> json_response(200)
70
71 assert result["id"]
72 end
73 end
74
75 describe "GET /api/v1/pleroma/chats" do
76 test "it return a list of chats the current user is participating in, in descending order of updates",
77 %{conn: conn} do
78 user = insert(:user)
79 har = insert(:user)
80 jafnhar = insert(:user)
81 tridi = insert(:user)
82
83 {:ok, chat_1} = Chat.get_or_create(user.id, har.ap_id)
84 :timer.sleep(1000)
85 {:ok, _chat_2} = Chat.get_or_create(user.id, jafnhar.ap_id)
86 :timer.sleep(1000)
87 {:ok, chat_3} = Chat.get_or_create(user.id, tridi.ap_id)
88 :timer.sleep(1000)
89
90 # bump the second one
91 {:ok, chat_2} = Chat.bump_or_create(user.id, jafnhar.ap_id)
92
93 result =
94 conn
95 |> assign(:user, user)
96 |> get("/api/v1/pleroma/chats")
97 |> json_response(200)
98
99 ids = Enum.map(result, & &1["id"])
100
101 assert ids == [chat_2.id, chat_3.id, chat_1.id]
102 end
103 end
104 end