total rebase
[anni] / test / pleroma / web / pleroma_api / controllers / conversation_controller_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.ConversationControllerTest do
6   use Pleroma.Web.ConnCase, async: true
7
8   alias Pleroma.Conversation.Participation
9   alias Pleroma.Repo
10   alias Pleroma.User
11   alias Pleroma.Web.CommonAPI
12
13   import Pleroma.Factory
14
15   test "/api/v1/pleroma/conversations/:id" do
16     user = insert(:user)
17     %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
18
19     {:ok, _activity} =
20       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
21
22     [participation] = Participation.for_user(other_user)
23
24     result =
25       conn
26       |> get("/api/v1/pleroma/conversations/#{participation.id}")
27       |> json_response_and_validate_schema(200)
28
29     assert result["id"] == participation.id |> to_string()
30   end
31
32   test "/api/v1/pleroma/conversations/:id/statuses" do
33     user = insert(:user)
34     %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
35     third_user = insert(:user)
36
37     {:ok, _activity} =
38       CommonAPI.post(user, %{status: "Hi @#{third_user.nickname}!", visibility: "direct"})
39
40     {:ok, activity} =
41       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
42
43     [participation] = Participation.for_user(other_user)
44
45     {:ok, activity_two} =
46       CommonAPI.post(other_user, %{
47         status: "Hi!",
48         in_reply_to_status_id: activity.id,
49         in_reply_to_conversation_id: participation.id
50       })
51
52     result =
53       conn
54       |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
55       |> json_response_and_validate_schema(200)
56
57     assert length(result) == 2
58
59     id_one = activity.id
60     id_two = activity_two.id
61     assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
62
63     {:ok, %{id: id_three}} =
64       CommonAPI.post(other_user, %{
65         status: "Bye!",
66         in_reply_to_status_id: activity.id,
67         in_reply_to_conversation_id: participation.id
68       })
69
70     assert [%{"id" => ^id_two}, %{"id" => ^id_three}] =
71              conn
72              |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?limit=2")
73              |> json_response_and_validate_schema(:ok)
74
75     assert [%{"id" => ^id_three}] =
76              conn
77              |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?min_id=#{id_two}")
78              |> json_response_and_validate_schema(:ok)
79   end
80
81   test "PATCH /api/v1/pleroma/conversations/:id" do
82     %{user: user, conn: conn} = oauth_access(["write:conversations"])
83     other_user = insert(:user)
84
85     {:ok, _activity} = CommonAPI.post(user, %{status: "Hi", visibility: "direct"})
86
87     [participation] = Participation.for_user(user)
88
89     participation = Repo.preload(participation, :recipients)
90
91     user = User.get_cached_by_id(user.id)
92     assert [user] == participation.recipients
93     assert other_user not in participation.recipients
94
95     query = "recipients[]=#{user.id}&recipients[]=#{other_user.id}"
96
97     result =
98       conn
99       |> patch("/api/v1/pleroma/conversations/#{participation.id}?#{query}")
100       |> json_response_and_validate_schema(200)
101
102     assert result["id"] == participation.id |> to_string
103
104     [participation] = Participation.for_user(user)
105     participation = Repo.preload(participation, :recipients)
106
107     assert refresh_record(user) in participation.recipients
108     assert other_user in participation.recipients
109   end
110
111   test "POST /api/v1/pleroma/conversations/read" do
112     user = insert(:user)
113     %{user: other_user, conn: conn} = oauth_access(["write:conversations"])
114
115     {:ok, _activity} =
116       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
117
118     {:ok, _activity} =
119       CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
120
121     [participation2, participation1] = Participation.for_user(other_user)
122     assert Participation.get(participation2.id).read == false
123     assert Participation.get(participation1.id).read == false
124     assert Participation.unread_count(other_user) == 2
125
126     [%{"unread" => false}, %{"unread" => false}] =
127       conn
128       |> post("/api/v1/pleroma/conversations/read", %{})
129       |> json_response_and_validate_schema(200)
130
131     [participation2, participation1] = Participation.for_user(other_user)
132     assert Participation.get(participation2.id).read == true
133     assert Participation.get(participation1.id).read == true
134     assert Participation.unread_count(other_user) == 0
135   end
136 end