1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Web.CommonAPI
11 import Pleroma.Factory
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)
18 {:ok, user_two} = User.follow(user_two, user_one)
20 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
23 CommonAPI.post(user_one, %{
24 "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
25 "visibility" => "direct"
28 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
30 {:ok, _follower_only} =
31 CommonAPI.post(user_one, %{
32 "status" => "Hi @#{user_two.nickname}!",
33 "visibility" => "private"
38 |> assign(:user, user_one)
39 |> get("/api/v1/conversations")
41 assert response = json_response(res_conn, 200)
46 "accounts" => res_accounts,
47 "last_status" => res_last_status,
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).unread_conversation_count == 0
62 test "filters conversations by recipients", %{conn: conn} do
63 user_one = insert(:user)
64 user_two = insert(:user)
65 user_three = insert(:user)
68 CommonAPI.post(user_one, %{
69 "status" => "Hi @#{user_two.nickname}!",
70 "visibility" => "direct"
74 CommonAPI.post(user_one, %{
75 "status" => "Hi @#{user_three.nickname}!",
76 "visibility" => "direct"
80 CommonAPI.post(user_one, %{
81 "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
82 "visibility" => "direct"
86 CommonAPI.post(user_two, %{
87 "status" => "Hi @#{user_three.nickname}!",
88 "visibility" => "direct"
92 CommonAPI.post(user_two, %{
93 "status" => "Hi @#{user_one.nickname}!",
94 "visibility" => "direct"
97 [conversation1, conversation2] =
99 |> assign(:user, user_one)
100 |> get("/api/v1/conversations", %{"recipients" => [user_two.id]})
101 |> json_response(200)
103 assert conversation1["last_status"]["id"] == direct5.id
104 assert conversation2["last_status"]["id"] == direct1.id
108 |> assign(:user, user_one)
109 |> get("/api/v1/conversations", %{"recipients" => [user_two.id, user_three.id]})
110 |> json_response(200)
112 assert conversation1["last_status"]["id"] == direct3.id
115 test "updates the last_status on reply", %{conn: conn} do
116 user_one = insert(:user)
117 user_two = insert(:user)
120 CommonAPI.post(user_one, %{
121 "status" => "Hi @#{user_two.nickname}",
122 "visibility" => "direct"
125 {:ok, direct_reply} =
126 CommonAPI.post(user_two, %{
128 "visibility" => "direct",
129 "in_reply_to_status_id" => direct.id
132 [%{"last_status" => res_last_status}] =
134 |> assign(:user, user_one)
135 |> get("/api/v1/conversations")
136 |> json_response(200)
138 assert res_last_status["id"] == direct_reply.id
141 test "the user marks a conversation as read", %{conn: conn} do
142 user_one = insert(:user)
143 user_two = insert(:user)
146 CommonAPI.post(user_one, %{
147 "status" => "Hi @#{user_two.nickname}",
148 "visibility" => "direct"
151 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
152 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
154 [%{"id" => direct_conversation_id, "unread" => true}] =
156 |> assign(:user, user_two)
157 |> get("/api/v1/conversations")
158 |> json_response(200)
160 %{"unread" => false} =
162 |> assign(:user, user_two)
163 |> post("/api/v1/conversations/#{direct_conversation_id}/read")
164 |> json_response(200)
166 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
167 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
169 # The conversation is marked as unread on reply
171 CommonAPI.post(user_two, %{
173 "visibility" => "direct",
174 "in_reply_to_status_id" => direct.id
177 [%{"unread" => true}] =
179 |> assign(:user, user_one)
180 |> get("/api/v1/conversations")
181 |> json_response(200)
183 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
184 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
186 # A reply doesn't increment the user's unread_conversation_count if the conversation is unread
188 CommonAPI.post(user_two, %{
190 "visibility" => "direct",
191 "in_reply_to_status_id" => direct.id
194 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
195 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
198 test "(vanilla) Mastodon frontend behaviour", %{conn: conn} do
199 user_one = insert(:user)
200 user_two = insert(:user)
203 CommonAPI.post(user_one, %{
204 "status" => "Hi @#{user_two.nickname}!",
205 "visibility" => "direct"
210 |> assign(:user, user_one)
211 |> get("/api/v1/statuses/#{direct.id}/context")
213 assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)