Conversations: Import order, import as read.
authorlain <lain@soykaf.club>
Thu, 9 May 2019 14:39:28 +0000 (16:39 +0200)
committerlain <lain@soykaf.club>
Thu, 9 May 2019 14:39:28 +0000 (16:39 +0200)
lib/pleroma/conversation.ex
lib/pleroma/conversation/participation.ex
lib/pleroma/web/activity_pub/activity_pub.ex
test/conversation_test.exs

index 0c6ca9f72e0f6f69ff3aa74ad56b486fb6eff830..5f6ab902c1ebb1bf4d2ba6abe0dc9f45b82ff632 100644 (file)
@@ -45,7 +45,7 @@ defmodule Pleroma.Conversation do
   2. Create a participation for all the people involved who don't have one already
   3. Bump all relevant participations to 'unread'
   """
-  def create_or_bump_for(activity) do
+  def create_or_bump_for(activity, opts \\ []) do
     with true <- Pleroma.Web.ActivityPub.Visibility.is_direct?(activity),
          object <- Pleroma.Object.normalize(activity),
          "Create" <- activity.data["type"],
@@ -58,7 +58,7 @@ defmodule Pleroma.Conversation do
       participations =
         Enum.map(users, fn user ->
           {:ok, participation} =
-            Participation.create_for_user_and_conversation(user, conversation)
+            Participation.create_for_user_and_conversation(user, conversation, opts)
 
           participation
         end)
@@ -84,7 +84,7 @@ defmodule Pleroma.Conversation do
     Repo.transaction(
       fn ->
         stream
-        |> Enum.each(&create_or_bump_for/1)
+        |> Enum.each(fn a -> create_or_bump_for(a, read: true) end)
       end,
       timeout: :infinity
     )
index 61021fb18104bfc07e0c706f52859602d6a82811..2a11f9069940d221aec41bb39d6f758fa4dd71aa 100644 (file)
@@ -22,15 +22,17 @@ defmodule Pleroma.Conversation.Participation do
 
   def creation_cng(struct, params) do
     struct
-    |> cast(params, [:user_id, :conversation_id])
+    |> cast(params, [:user_id, :conversation_id, :read])
     |> validate_required([:user_id, :conversation_id])
   end
 
-  def create_for_user_and_conversation(user, conversation) do
+  def create_for_user_and_conversation(user, conversation, opts \\ []) do
+    read = !!opts[:read]
+
     %__MODULE__{}
-    |> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
+    |> creation_cng(%{user_id: user.id, conversation_id: conversation.id, read: read})
     |> Repo.insert(
-      on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
+      on_conflict: [set: [read: read, updated_at: NaiveDateTime.utc_now()]],
       returning: true,
       conflict_target: [:user_id, :conversation_id]
     )
index 8137ac83b74ab204a01d8ad47e042fb44280c8b7..728761ebd5490a75a016c138115c3a307687e33c 100644 (file)
@@ -1056,5 +1056,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     Activity
     |> restrict_type(%{"type" => "Create"})
     |> restrict_visibility(%{visibility: "direct"})
+    |> order_by([activity], asc: activity.id)
   end
 end
index 59368b0e7dba3f86ef4dbcc01e373235c670baa7..cdec18f0f2f68dbe3d1cc3df0d024438f73e6fe7 100644 (file)
@@ -24,7 +24,9 @@ defmodule Pleroma.ConversationTest do
     Conversation.bump_for_all_activities()
 
     assert Repo.one(Conversation)
-    assert length(Repo.all(Conversation.Participation)) == 2
+    [participation, _p2] = Repo.all(Conversation.Participation)
+
+    assert participation.read
   end
 
   test "it creates a conversation for given ap_id" do