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 "updates the last_status on reply", %{conn: conn} do
63 user_one = insert(:user)
64 user_two = insert(:user)
67 CommonAPI.post(user_one, %{
68 "status" => "Hi @#{user_two.nickname}",
69 "visibility" => "direct"
73 CommonAPI.post(user_two, %{
75 "visibility" => "direct",
76 "in_reply_to_status_id" => direct.id
79 [%{"last_status" => res_last_status}] =
81 |> assign(:user, user_one)
82 |> get("/api/v1/conversations")
85 assert res_last_status["id"] == direct_reply.id
88 test "the user marks a conversation as read", %{conn: conn} do
89 user_one = insert(:user)
90 user_two = insert(:user)
93 CommonAPI.post(user_one, %{
94 "status" => "Hi @#{user_two.nickname}",
95 "visibility" => "direct"
98 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
99 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 1
101 [%{"id" => direct_conversation_id, "unread" => true}] =
103 |> assign(:user, user_two)
104 |> get("/api/v1/conversations")
105 |> json_response(200)
107 %{"unread" => false} =
109 |> assign(:user, user_two)
110 |> post("/api/v1/conversations/#{direct_conversation_id}/read")
111 |> json_response(200)
113 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
114 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
116 # The conversation is marked as unread on reply
118 CommonAPI.post(user_two, %{
120 "visibility" => "direct",
121 "in_reply_to_status_id" => direct.id
124 [%{"unread" => true}] =
126 |> assign(:user, user_one)
127 |> get("/api/v1/conversations")
128 |> json_response(200)
130 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
131 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
133 # A reply doesn't increment the user's unread_conversation_count if the conversation is unread
135 CommonAPI.post(user_two, %{
137 "visibility" => "direct",
138 "in_reply_to_status_id" => direct.id
141 assert User.get_cached_by_id(user_one.id).unread_conversation_count == 1
142 assert User.get_cached_by_id(user_two.id).unread_conversation_count == 0
145 test "(vanilla) Mastodon frontend behaviour", %{conn: conn} do
146 user_one = insert(:user)
147 user_two = insert(:user)
150 CommonAPI.post(user_one, %{
151 "status" => "Hi @#{user_two.nickname}!",
152 "visibility" => "direct"
157 |> assign(:user, user_one)
158 |> get("/api/v1/statuses/#{direct.id}/context")
160 assert %{"ancestors" => [], "descendants" => []} == json_response(res_conn, 200)