Conversations: Add a function to 'import' old DMs.
[akkoma] / lib / pleroma / conversation.ex
index d9c84cb1b62d4375770b9f45726457ce29f59f02..aa73edd75574876190b0a183bc0cbc5ed48b5f1b 100644 (file)
@@ -47,9 +47,10 @@ defmodule Pleroma.Conversation do
   """
   def create_or_bump_for(activity) do
     with true <- Pleroma.Web.ActivityPub.Visibility.is_direct?(activity),
+         object <- Pleroma.Object.normalize(activity),
          "Create" <- activity.data["type"],
-         "Note" <- activity.data["object"]["type"],
-         ap_id when is_binary(ap_id) <- activity.data["object"]["context"] do
+         "Note" <- object.data["type"],
+         ap_id when is_binary(ap_id) and byte_size(ap_id) > 0 <- object.data["context"] do
       {:ok, conversation} = create_for_ap_id(ap_id)
 
       users = User.get_users_from_set(activity.recipients, false)
@@ -62,10 +63,27 @@ defmodule Pleroma.Conversation do
           participation
         end)
 
-      %{
-        conversation
-        | participations: participations
-      }
+      {:ok,
+       %{
+         conversation
+         | participations: participations
+       }}
+    else
+      e -> {:error, e}
     end
   end
+
+  @doc """
+  This is only meant to be run by a mix task. It creates conversations/participations for all direct messages in the database.
+  """
+  def bump_for_all_activities() do
+    stream =
+      Pleroma.Web.ActivityPub.ActivityPub.fetch_direct_messages_query()
+      |> Repo.stream()
+
+    Repo.transaction(fn ->
+      stream
+      |> Enum.each(&create_or_bump_for/1)
+    end)
+  end
 end