StatusView: Return direct conversation id.
authorlain <lain@soykaf.club>
Wed, 31 Jul 2019 13:12:29 +0000 (15:12 +0200)
committerlain <lain@soykaf.club>
Wed, 31 Jul 2019 13:12:29 +0000 (15:12 +0200)
lib/pleroma/conversation/participation.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
test/web/mastodon_api/status_view_test.exs

index 5883e4183780ef3e1f32b4d44cb4da60ed0ca4f8..77b3f61e9af95c4b30adfa5822b40683c1282fca 100644 (file)
@@ -65,6 +65,14 @@ defmodule Pleroma.Conversation.Participation do
     |> Pleroma.Pagination.fetch_paginated(params)
   end
 
+  def for_user_and_conversation(user, conversation) do
+    from(p in __MODULE__,
+      where: p.user_id == ^user.id,
+      where: p.conversation_id == ^conversation.id
+    )
+    |> Repo.one()
+  end
+
   def for_user_with_last_activity_id(user, params \\ %{}) do
     for_user(user, params)
     |> Enum.map(fn participation ->
index 80df9b2ace5ea6b8690454c9aebfc7ca3d4349e5..a862554b1d4b1f85b1ec4eff76231bfae0b5caa0 100644 (file)
@@ -6,6 +6,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
   use Pleroma.Web, :view
 
   alias Pleroma.Activity
+  alias Pleroma.Conversation
+  alias Pleroma.Conversation.Participation
   alias Pleroma.HTML
   alias Pleroma.Object
   alias Pleroma.Repo
@@ -225,6 +227,19 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         object.data["url"] || object.data["external_url"] || object.data["id"]
       end
 
+    direct_conversation_id =
+      with {_, 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),
+           %Participation{id: participation_id} <-
+             Participation.for_user_and_conversation(for_user, conversation) do
+        participation_id
+      else
+        _e ->
+          nil
+      end
+
     %{
       id: to_string(activity.id),
       uri: object.data["id"],
@@ -262,7 +277,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
         conversation_id: get_context_id(activity),
         in_reply_to_account_acct: reply_to_user && reply_to_user.nickname,
         content: %{"text/plain" => content_plaintext},
-        spoiler_text: %{"text/plain" => summary_plaintext}
+        spoiler_text: %{"text/plain" => summary_plaintext},
+        direct_conversation_id: direct_conversation_id
       }
     }
   end
index 0b167f839bcde6b12fa85067493ba7050c592be4..c983b494ffe42f6c73d252cfca4218c8645f8945 100644 (file)
@@ -23,6 +23,21 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
     :ok
   end
 
+  test "returns the direct conversation id when given the `with_conversation_id` option" do
+    user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey @shp!", "visibility" => "direct"})
+
+    status =
+      StatusView.render("status.json",
+        activity: activity,
+        with_direct_conversation_id: true,
+        for: user
+      )
+
+    assert status[:pleroma][:direct_conversation_id]
+  end
+
   test "returns a temporary ap_id based user for activities missing db users" do
     user = insert(:user)
 
@@ -133,7 +148,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
         conversation_id: convo_id,
         in_reply_to_account_acct: nil,
         content: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["content"])},
-        spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["summary"])}
+        spoiler_text: %{"text/plain" => HtmlSanitizeEx.strip_tags(object_data["summary"])},
+        direct_conversation_id: nil
       }
     }