conversation_controller_test.exs 6.98 KB
Newer Older
1
# Pleroma: A lightweight social networking server
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3
4
5
6
7
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
  use Pleroma.Web.ConnCase

8
  alias Pleroma.Conversation.Participation
9
10
11
12
13
  alias Pleroma.User
  alias Pleroma.Web.CommonAPI

  import Pleroma.Factory

14
15
  setup do: oauth_access(["read:statuses"])

Steven Fuchs's avatar
Steven Fuchs committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  describe "returns a list of conversations" do
    setup(%{user: user_one, conn: conn}) do
      user_two = insert(:user)
      user_three = insert(:user)

      {:ok, user_two} = User.follow(user_two, user_one)

      {:ok, %{user: user_one, user_two: user_two, user_three: user_three, conn: conn}}
    end

    test "returns correct conversations", %{
      user: user_one,
      user_two: user_two,
      user_three: user_three,
      conn: conn
    } do
32
      assert Participation.unread_count(user_two) == 0
Steven Fuchs's avatar
Steven Fuchs committed
33
34
      {:ok, direct} = create_direct_message(user_one, [user_two, user_three])

35
      assert Participation.unread_count(user_two) == 1
Steven Fuchs's avatar
Steven Fuchs committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

      {:ok, _follower_only} =
        CommonAPI.post(user_one, %{
          status: "Hi @#{user_two.nickname}!",
          visibility: "private"
        })

      res_conn = get(conn, "/api/v1/conversations")

      assert response = json_response_and_validate_schema(res_conn, 200)

      assert [
               %{
                 "id" => res_id,
                 "accounts" => res_accounts,
                 "last_status" => res_last_status,
                 "unread" => unread
               }
             ] = response

      account_ids = Enum.map(res_accounts, & &1["id"])
57
58
      assert length(res_accounts) == 2
      assert user_one.id not in account_ids
Steven Fuchs's avatar
Steven Fuchs committed
59
60
61
62
63
      assert user_two.id in account_ids
      assert user_three.id in account_ids
      assert is_binary(res_id)
      assert unread == false
      assert res_last_status["id"] == direct.id
64
      assert res_last_status["account"]["id"] == user_one.id
65
      assert Participation.unread_count(user_one) == 0
Steven Fuchs's avatar
Steven Fuchs committed
66
67
    end

lain's avatar
lain committed
68
    test "includes the user if the user is the only participant", %{
69
70
71
      user: user_one,
      conn: conn
    } do
lain's avatar
lain committed
72
      {:ok, _direct} = create_direct_message(user_one, [])
73
74
75
76

      res_conn = get(conn, "/api/v1/conversations")

      assert response = json_response_and_validate_schema(res_conn, 200)
77

78
79
      assert [
               %{
lain's avatar
lain committed
80
                 "accounts" => [account]
81
82
               }
             ] = response
83

lain's avatar
lain committed
84
      assert user_one.id == account["id"]
85
86
    end

Steven Fuchs's avatar
Steven Fuchs committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    test "observes limit params", %{
      user: user_one,
      user_two: user_two,
      user_three: user_three,
      conn: conn
    } do
      {:ok, _} = create_direct_message(user_one, [user_two, user_three])
      {:ok, _} = create_direct_message(user_two, [user_one, user_three])
      {:ok, _} = create_direct_message(user_three, [user_two, user_one])

      res_conn = get(conn, "/api/v1/conversations?limit=1")

      assert response = json_response_and_validate_schema(res_conn, 200)

      assert Enum.count(response) == 1

      res_conn = get(conn, "/api/v1/conversations?limit=2")

      assert response = json_response_and_validate_schema(res_conn, 200)

      assert Enum.count(response) == 2
    end
109
  end
110

111
  test "filters conversations by recipients", %{user: user_one, conn: conn} do
112
113
    user_two = insert(:user)
    user_three = insert(:user)
Steven Fuchs's avatar
Steven Fuchs committed
114
115
116
117
118
    {:ok, direct1} = create_direct_message(user_one, [user_two])
    {:ok, _direct2} = create_direct_message(user_one, [user_three])
    {:ok, direct3} = create_direct_message(user_one, [user_two, user_three])
    {:ok, _direct4} = create_direct_message(user_two, [user_three])
    {:ok, direct5} = create_direct_message(user_two, [user_one])
119

120
121
122
123
    assert [conversation1, conversation2] =
             conn
             |> get("/api/v1/conversations?recipients[]=#{user_two.id}")
             |> json_response_and_validate_schema(200)
124
125
126
127
128
129

    assert conversation1["last_status"]["id"] == direct5.id
    assert conversation2["last_status"]["id"] == direct1.id

    [conversation1] =
      conn
130
131
      |> get("/api/v1/conversations?recipients[]=#{user_two.id}&recipients[]=#{user_three.id}")
      |> json_response_and_validate_schema(200)
132
133
134
135

    assert conversation1["last_status"]["id"] == direct3.id
  end

136
  test "updates the last_status on reply", %{user: user_one, conn: conn} do
137
    user_two = insert(:user)
Steven Fuchs's avatar
Steven Fuchs committed
138
    {:ok, direct} = create_direct_message(user_one, [user_two])
139
140
141

    {:ok, direct_reply} =
      CommonAPI.post(user_two, %{
142
143
144
        status: "reply",
        visibility: "direct",
        in_reply_to_status_id: direct.id
145
146
147
      })

    [%{"last_status" => res_last_status}] =
148
      conn
149
      |> get("/api/v1/conversations")
150
      |> json_response_and_validate_schema(200)
151

152
153
154
    assert res_last_status["id"] == direct_reply.id
  end

155
  test "the user marks a conversation as read", %{user: user_one, conn: conn} do
156
    user_two = insert(:user)
Steven Fuchs's avatar
Steven Fuchs committed
157
    {:ok, direct} = create_direct_message(user_one, [user_two])
158

159
160
    assert Participation.unread_count(user_one) == 0
    assert Participation.unread_count(user_two) == 1
161

162
163
    user_two_conn =
      build_conn()
164
      |> assign(:user, user_two)
165
166
167
168
169
170
171
      |> assign(
        :token,
        insert(:oauth_token, user: user_two, scopes: ["read:statuses", "write:conversations"])
      )

    [%{"id" => direct_conversation_id, "unread" => true}] =
      user_two_conn
172
      |> get("/api/v1/conversations")
173
      |> json_response_and_validate_schema(200)
174
175

    %{"unread" => false} =
176
      user_two_conn
177
      |> post("/api/v1/conversations/#{direct_conversation_id}/read")
178
      |> json_response_and_validate_schema(200)
179

180
181
    assert Participation.unread_count(user_one) == 0
    assert Participation.unread_count(user_two) == 0
182
183
184
185

    # The conversation is marked as unread on reply
    {:ok, _} =
      CommonAPI.post(user_two, %{
186
187
188
        status: "reply",
        visibility: "direct",
        in_reply_to_status_id: direct.id
189
190
191
192
193
      })

    [%{"unread" => true}] =
      conn
      |> get("/api/v1/conversations")
194
      |> json_response_and_validate_schema(200)
195

196
197
    assert Participation.unread_count(user_one) == 1
    assert Participation.unread_count(user_two) == 0
198
199
200
201

    # A reply doesn't increment the user's unread_conversation_count if the conversation is unread
    {:ok, _} =
      CommonAPI.post(user_two, %{
202
203
204
        status: "reply",
        visibility: "direct",
        in_reply_to_status_id: direct.id
205
206
      })

207
208
    assert Participation.unread_count(user_one) == 1
    assert Participation.unread_count(user_two) == 0
209
210
  end

211
  test "(vanilla) Mastodon frontend behaviour", %{user: user_one, conn: conn} do
212
    user_two = insert(:user)
Steven Fuchs's avatar
Steven Fuchs committed
213
    {:ok, direct} = create_direct_message(user_one, [user_two])
214

215
    res_conn = get(conn, "/api/v1/statuses/#{direct.id}/context")
216
217
218

    assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
  end
Steven Fuchs's avatar
Steven Fuchs committed
219
220
221
222
223
224
225
226
227
228
229
230

  defp create_direct_message(sender, recips) do
    hellos =
      recips
      |> Enum.map(fn s -> "@#{s.nickname}" end)
      |> Enum.join(", ")

    CommonAPI.post(sender, %{
      status: "Hi #{hellos}!",
      visibility: "direct"
    })
  end
231
end