ConversationView: fix last_status.account being empty, fix current user being include...
authorAlibek Omarov <a1ba.omarov@gmail.com>
Fri, 30 Oct 2020 12:01:58 +0000 (13:01 +0100)
committerGitea <gitea@fake.local>
Fri, 30 Oct 2020 12:59:53 +0000 (13:59 +0100)
lib/pleroma/web/mastodon_api/views/conversation_view.ex
test/pleroma/web/mastodon_api/controllers/conversation_controller_test.exs

index cf34933ab52e0a1c9a3483923addc0dcfe191531..4636c00e339c4440fb9edce8a0647e5f490b62b4 100644 (file)
@@ -34,14 +34,22 @@ defmodule Pleroma.Web.MastodonAPI.ConversationView do
 
     activity = Activity.get_by_id_with_object(last_activity_id)
 
+    # Conversations return all users except current user when current user is not only participant
+    users = if length(participation.recipients) > 1 do
+      Enum.reject(participation.recipients, &(&1.id == user.id))
+    else
+      participation.recipients
+    end
+
     %{
       id: participation.id |> to_string(),
-      accounts: render(AccountView, "index.json", users: participation.recipients, for: user),
+      accounts: render(AccountView, "index.json", users: users, for: user),
       unread: !participation.read,
       last_status:
         render(StatusView, "show.json",
           activity: activity,
-          direct_conversation_id: participation.id
+          direct_conversation_id: participation.id,
+          for: user
         )
     }
   end
index afc24027b670eae6ec1f97bced195d4b2d1785c9..8d07cff3f50914cfa96e993ef3ff3154e00693e7 100644 (file)
@@ -54,16 +54,37 @@ defmodule Pleroma.Web.MastodonAPI.ConversationControllerTest do
              ] = response
 
       account_ids = Enum.map(res_accounts, & &1["id"])
-      assert length(res_accounts) == 3
-      assert user_one.id in account_ids
+      assert length(res_accounts) == 2
+      assert user_one.id not in account_ids
       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
+      assert res_last_status["account"]["id"] == user_one.id
       assert Participation.unread_count(user_one) == 0
     end
 
+    test "special behaviour when conversation have only one user", %{
+      user: user_one,
+      user_two: user_two,
+      conn: conn
+    } do
+      {:ok, direct} = create_direct_message(user_one, [])
+
+      res_conn = get(conn, "/api/v1/conversations")
+
+      assert response = json_response_and_validate_schema(res_conn, 200)
+      assert [
+               %{
+                 "accounts" => res_accounts,
+                 "last_status" => res_last_status
+               }
+             ] = response
+      assert length(res_accounts) == 1
+      assert res_accounts[0]["id"] == user_one.id
+    end
+
     test "observes limit params", %{
       user: user_one,
       user_two: user_two,