Mastodon API: Mark the conversation as read for the author when they send a new direc...
[akkoma] / test / web / mastodon_api / controllers / conversation_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.User
9 alias Pleroma.Web.CommonAPI
10
11 import Pleroma.Factory
12
13 test "returns a list of conversations", %{conn: conn} do
14 user_one = insert(:user)
15 user_two = insert(:user)
16 user_three = insert(:user)
17
18 {:ok, user_two} = User.follow(user_two, user_one)
19
20 assert User.get_cached_by_id(user_two.id).info.unread_conversation_count == 0
21
22 {:ok, direct} =
23 CommonAPI.post(user_one, %{
24 "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
25 "visibility" => "direct"
26 })
27
28 assert User.get_cached_by_id(user_two.id).info.unread_conversation_count == 1
29
30 {:ok, _follower_only} =
31 CommonAPI.post(user_one, %{
32 "status" => "Hi @#{user_two.nickname}!",
33 "visibility" => "private"
34 })
35
36 res_conn =
37 conn
38 |> assign(:user, user_one)
39 |> get("/api/v1/conversations")
40
41 assert response = json_response(res_conn, 200)
42
43 assert [
44 %{
45 "id" => res_id,
46 "accounts" => res_accounts,
47 "last_status" => res_last_status,
48 "unread" => unread
49 }
50 ] = response
51
52 account_ids = Enum.map(res_accounts, & &1["id"])
53 assert length(res_accounts) == 2
54 assert user_two.id in account_ids
55 assert user_three.id in account_ids
56 assert is_binary(res_id)
57 assert unread == false
58 assert res_last_status["id"] == direct.id
59 assert User.get_cached_by_id(user_one.id).info.unread_conversation_count == 0
60 end
61
62 test "updates the last_status on reply", %{conn: conn} do
63 user_one = insert(:user)
64 user_two = insert(:user)
65
66 {:ok, direct} =
67 CommonAPI.post(user_one, %{
68 "status" => "Hi @#{user_two.nickname}",
69 "visibility" => "direct"
70 })
71
72 {:ok, direct_reply} =
73 CommonAPI.post(user_two, %{
74 "status" => "reply",
75 "visibility" => "direct",
76 "in_reply_to_status_id" => direct.id
77 })
78
79 [%{"last_status" => res_last_status}] =
80 conn
81 |> assign(:user, user_one)
82 |> get("/api/v1/conversations")
83 |> json_response(200)
84
85 assert res_last_status["id"] == direct_reply.id
86 end
87
88 test "the user marks a conversation as read", %{conn: conn} do
89 user_one = insert(:user)
90 user_two = insert(:user)
91
92 {:ok, direct} =
93 CommonAPI.post(user_one, %{
94 "status" => "Hi @#{user_two.nickname}",
95 "visibility" => "direct"
96 })
97
98 assert User.get_cached_by_id(user_one.id).info.unread_conversation_count == 0
99 assert User.get_cached_by_id(user_two.id).info.unread_conversation_count == 1
100
101 [%{"id" => direct_conversation_id, "unread" => true}] =
102 conn
103 |> assign(:user, user_two)
104 |> get("/api/v1/conversations")
105 |> json_response(200)
106
107 %{"unread" => false} =
108 conn
109 |> assign(:user, user_two)
110 |> post("/api/v1/conversations/#{direct_conversation_id}/read")
111 |> json_response(200)
112
113 assert User.get_cached_by_id(user_one.id).info.unread_conversation_count == 0
114 assert User.get_cached_by_id(user_two.id).info.unread_conversation_count == 0
115
116 # The conversation is marked as unread on reply
117 {:ok, _} =
118 CommonAPI.post(user_two, %{
119 "status" => "reply",
120 "visibility" => "direct",
121 "in_reply_to_status_id" => direct.id
122 })
123
124 [%{"unread" => true}] =
125 conn
126 |> assign(:user, user_one)
127 |> get("/api/v1/conversations")
128 |> json_response(200)
129
130 assert User.get_cached_by_id(user_one.id).info.unread_conversation_count == 1
131 assert User.get_cached_by_id(user_two.id).info.unread_conversation_count == 0
132
133 # A reply doesn't increment the user's unread_conversation_count if the conversation is unread
134 {:ok, _} =
135 CommonAPI.post(user_two, %{
136 "status" => "reply",
137 "visibility" => "direct",
138 "in_reply_to_status_id" => direct.id
139 })
140
141 assert User.get_cached_by_id(user_one.id).info.unread_conversation_count == 1
142 assert User.get_cached_by_id(user_two.id).info.unread_conversation_count == 0
143 end
144
145 test "(vanilla) Mastodon frontend behaviour", %{conn: conn} do
146 user_one = insert(:user)
147 user_two = insert(:user)
148
149 {:ok, direct} =
150 CommonAPI.post(user_one, %{
151 "status" => "Hi @#{user_two.nickname}!",
152 "visibility" => "direct"
153 })
154
155 res_conn =
156 conn
157 |> assign(:user, user_one)
158 |> get("/api/v1/statuses/#{direct.id}/context")
159
160 assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)
161 end
162 end