Mastodon API, streaming: Add `pleroma.direct_conversation_id` to the `conversation...
authoreugenijm <eugenijm@protonmail.com>
Thu, 31 Oct 2019 00:44:27 +0000 (03:44 +0300)
committereugenijm <eugenijm@protonmail.com>
Mon, 4 Nov 2019 15:36:16 +0000 (18:36 +0300)
CHANGELOG.md
lib/pleroma/web/mastodon_api/views/conversation_view.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/views/conversation_view_test.exs
test/web/mastodon_api/views/status_view_test.exs
test/web/streamer/streamer_test.exs

index 00dd62e24039b1df8ce7903c293fa093f07558f8..77edf7bf0d2897e780013109b3e7e061e853131b 100644 (file)
@@ -27,6 +27,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Mastodon API: Add `pleroma.direct_conversation_id` to the status endpoint (`GET /api/v1/statuses/:id`)
 - Mastodon API: `pleroma.thread_muted` to the Status entity
 - Mastodon API: Mark the direct conversation as read for the author when they send a new direct message
+- Mastodon API, streaming: Add `pleroma.direct_conversation_id` to the `conversation` stream event payload.
 </details>
 
 ### Added
index e9d2735b39660af8738a85c60f6f6c06572e0a29..c5998e6611d2c0917016353549479e002a0b1108 100644 (file)
@@ -34,7 +34,11 @@ defmodule Pleroma.Web.MastodonAPI.ConversationView do
       id: participation.id |> to_string(),
       accounts: render(AccountView, "index.json", users: users, as: :user),
       unread: !participation.read,
-      last_status: render(StatusView, "show.json", activity: activity, for: user)
+      last_status:
+        render(StatusView, "show.json",
+          activity: activity,
+          direct_conversation_id: participation.id
+        )
     }
   end
 end
index b785ca9d47d60854ba51863b11a87428dec1740c..baff5415113a7d8230dce1d40d23a5dc16511b4c 100644 (file)
@@ -243,7 +243,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
       end
 
     direct_conversation_id =
-      with {_, true} <- {:include_id, opts[:with_direct_conversation_id]},
+      with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]},
+           {_, true} <- {:include_id, opts[:with_direct_conversation_id]},
            {_, %User{} = for_user} <- {:for_user, opts[:for]},
            %{data: %{"context" => context}} when is_binary(context) <- activity,
            %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
@@ -251,6 +252,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
              Participation.for_user_and_conversation(for_user, conversation) do
         participation_id
       else
+        {:direct_conversation_id, participation_id} when is_integer(participation_id) ->
+          participation_id
+
         _e ->
           nil
       end
index a2a880705b644edde3f0d4a78dff8ac9217ab2ef..6ed22597d0cc7ef4395fdf8e37adbdffe8700904 100644 (file)
@@ -30,5 +30,6 @@ defmodule Pleroma.Web.MastodonAPI.ConversationViewTest do
 
     assert [account] = conversation.accounts
     assert account.id == other_user.id
+    assert conversation.last_status.pleroma.direct_conversation_id == participation.id
   end
 end
index 7526f2f27e76bf9240d15131be13f243e81de239..d46ecc646b624a8297cec9903317f29ade3d74c8 100644 (file)
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
 
   alias Pleroma.Activity
   alias Pleroma.Bookmark
+  alias Pleroma.Conversation.Participation
   alias Pleroma.HTML
   alias Pleroma.Object
   alias Pleroma.Repo
@@ -23,10 +24,11 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
     :ok
   end
 
-  test "returns the direct conversation id when given the `with_conversation_id` option" do
+  test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do
     user = insert(:user)
 
     {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
+    [participation] = Participation.for_user(user)
 
     status =
       StatusView.render("show.json",
@@ -35,7 +37,26 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
         for: user
       )
 
-    assert status[:pleroma][:direct_conversation_id]
+    assert status[:pleroma][:direct_conversation_id] == participation.id
+
+    status = StatusView.render("show.json", activity: activity, for: user)
+    assert status[:pleroma][:direct_conversation_id] == nil
+  end
+
+  test "returns the direct conversation id when given the `direct_conversation_id` option" do
+    user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
+    [participation] = Participation.for_user(user)
+
+    status =
+      StatusView.render("show.json",
+        activity: activity,
+        direct_conversation_id: participation.id,
+        for: user
+      )
+
+    assert status[:pleroma][:direct_conversation_id] == participation.id
   end
 
   test "returns a temporary ap_id based user for activities missing db users" do
index d6968f4d049f975d5cc9ff195b46026aa43641d1..80a7541b22f3a84bd731737f54ee0d7589e1e914 100644 (file)
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.StreamerTest do
 
   import Pleroma.Factory
 
+  alias Pleroma.Conversation.Participation
   alias Pleroma.List
   alias Pleroma.User
   alias Pleroma.Web.CommonAPI
@@ -481,7 +482,14 @@ defmodule Pleroma.Web.StreamerTest do
 
       task =
         Task.async(fn ->
-          assert_receive {:text, _received_event}, 4_000
+          assert_receive {:text, received_event}, 4_000
+
+          assert %{"event" => "conversation", "payload" => received_payload} =
+                   Jason.decode!(received_event)
+
+          assert %{"last_status" => last_status} = Jason.decode!(received_payload)
+          [participation] = Participation.for_user(user)
+          assert last_status["pleroma"]["direct_conversation_id"] == participation.id
         end)
 
       Streamer.add_socket(
@@ -498,7 +506,7 @@ defmodule Pleroma.Web.StreamerTest do
       Task.await(task)
     end
 
-    test "it doesn't send conversation update to the 'direct' streamj when the last message in the conversation is deleted" do
+    test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
       user = insert(:user)
       another_user = insert(:user)