1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.ConversationControllerTest do
6 use Pleroma.Web.ConnCase, async: true
8 alias Pleroma.Conversation.Participation
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 test "/api/v1/pleroma/conversations/:id" do
17 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
20 CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
22 [participation] = Participation.for_user(other_user)
26 |> get("/api/v1/pleroma/conversations/#{participation.id}")
27 |> json_response_and_validate_schema(200)
29 assert result["id"] == participation.id |> to_string()
32 test "/api/v1/pleroma/conversations/:id/statuses" do
34 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
35 third_user = insert(:user)
38 CommonAPI.post(user, %{status: "Hi @#{third_user.nickname}!", visibility: "direct"})
41 CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}!", visibility: "direct"})
43 [participation] = Participation.for_user(other_user)
46 CommonAPI.post(other_user, %{
48 in_reply_to_status_id: activity.id,
49 in_reply_to_conversation_id: participation.id
54 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
55 |> json_response_and_validate_schema(200)
57 assert length(result) == 2
60 id_two = activity_two.id
61 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
63 {:ok, %{id: id_three}} =
64 CommonAPI.post(other_user, %{
66 in_reply_to_status_id: activity.id,
67 in_reply_to_conversation_id: participation.id
70 assert [%{"id" => ^id_two}, %{"id" => ^id_three}] =
72 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?limit=2")
73 |> json_response_and_validate_schema(:ok)
75 assert [%{"id" => ^id_three}] =
77 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?min_id=#{id_two}")
78 |> json_response_and_validate_schema(:ok)
81 test "PATCH /api/v1/pleroma/conversations/:id" do
82 %{user: user, conn: conn} = oauth_access(["write:conversations"])
83 other_user = insert(:user)
85 {:ok, _activity} = CommonAPI.post(user, %{status: "Hi", visibility: "direct"})
87 [participation] = Participation.for_user(user)
89 participation = Repo.preload(participation, :recipients)
91 user = User.get_cached_by_id(user.id)
92 assert [user] == participation.recipients
93 assert other_user not in participation.recipients
95 query = "recipients[]=#{user.id}&recipients[]=#{other_user.id}"
99 |> patch("/api/v1/pleroma/conversations/#{participation.id}?#{query}")
100 |> json_response_and_validate_schema(200)
102 assert result["id"] == participation.id |> to_string
104 [participation] = Participation.for_user(user)
105 participation = Repo.preload(participation, :recipients)
107 assert refresh_record(user) in participation.recipients
108 assert other_user in participation.recipients
111 test "POST /api/v1/pleroma/conversations/read" do
113 %{user: other_user, conn: conn} = oauth_access(["write:conversations"])
116 CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
119 CommonAPI.post(user, %{status: "Hi @#{other_user.nickname}", visibility: "direct"})
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
126 [%{"unread" => false}, %{"unread" => false}] =
128 |> post("/api/v1/pleroma/conversations/read", %{})
129 |> json_response_and_validate_schema(200)
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