Conversations: Create or bump on inserting a dm.
authorlain <lain@soykaf.club>
Wed, 10 Apr 2019 14:33:45 +0000 (16:33 +0200)
committerlain <lain@soykaf.club>
Wed, 10 Apr 2019 14:33:45 +0000 (16:33 +0200)
lib/conversation.ex
lib/conversation/participation.ex
lib/pleroma/web/activity_pub/activity_pub.ex
priv/repo/migrations/20190408123347_create_conversations.exs
test/conversation/participation_test.exs
test/conversation_test.exs

index cfb78d9253149ef2ab657923b9853e3df2d28830..3d53e91b7b9f97417d03a4357a3c6855f79180da 100644 (file)
@@ -5,10 +5,12 @@
 defmodule Pleroma.Conversation do
   alias Pleroma.Repo
   alias Pleroma.Conversation.Participation
+  alias Pleroma.User
   use Ecto.Schema
   import Ecto.Changeset
 
   schema "conversations" do
+    # This is the context ap id.
     field(:ap_id, :string)
     has_many(:participations, Participation)
 
@@ -25,6 +27,44 @@ defmodule Pleroma.Conversation do
   def create_for_ap_id(ap_id) do
     %__MODULE__{}
     |> creation_cng(%{ap_id: ap_id})
-    |> Repo.insert()
+    |> Repo.insert(
+      on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
+      returning: true,
+      conflict_target: :ap_id
+    )
+  end
+
+  def get_for_ap_id(ap_id) do
+    Repo.get_by(__MODULE__, ap_id: ap_id)
+  end
+
+  @doc """
+  This will
+  1. Create a conversation if there isn't one already
+  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
+    with true <- Pleroma.Web.ActivityPub.Visibility.is_direct?(activity),
+         "Create" <- activity.data["type"],
+         "Note" <- activity.data["object"]["type"],
+         ap_id when is_binary(ap_id) <- activity.data["object"]["context"] do
+      {:ok, conversation} = create_for_ap_id(ap_id)
+
+      local_users = User.get_users_from_set(activity.recipients, true)
+
+      participations =
+        Enum.map(local_users, fn user ->
+          {:ok, participation} =
+            Participation.create_for_user_and_conversation(user, conversation)
+
+          participation
+        end)
+
+      %{
+        conversation
+        | participations: participations
+      }
+    end
   end
 end
index ab59a529e61f2b6824247553acbd21d790fb8dbc..a58d0ca0dea91b21fc4e4ed196ef3c799899332a 100644 (file)
@@ -26,7 +26,11 @@ defmodule Pleroma.Conversation.Participation do
   def create_for_user_and_conversation(user, conversation) do
     %__MODULE__{}
     |> creation_cng(%{user_id: user.id, conversation_id: conversation.id})
-    |> Repo.insert()
+    |> Repo.insert(
+      on_conflict: [set: [read: false, updated_at: NaiveDateTime.utc_now()]],
+      returning: true,
+      conflict_target: [:user_id, :conversation_id]
+    )
   end
 
   def read_cng(struct, params) do
index f217e7bac35c9271f633c3765ddb9477769d1e31..880d19a5ee56eea698e937c4f027442acb209852 100644 (file)
@@ -4,6 +4,7 @@
 
 defmodule Pleroma.Web.ActivityPub.ActivityPub do
   alias Pleroma.Activity
+  alias Pleroma.Conversation
   alias Pleroma.Instances
   alias Pleroma.Notification
   alias Pleroma.Object
@@ -143,6 +144,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       end)
 
       Notification.create_notifications(activity)
+      Conversation.create_or_bump_for(activity)
       stream_out(activity)
       {:ok, activity}
     else
index 68bf766bcebc1a049662c812be07f1daa5703fb6..0e0af30ae80bc1ae30e6557e17b0d6748ad538c5 100644 (file)
@@ -19,8 +19,8 @@ defmodule Pleroma.Repo.Migrations.CreateConversations do
       timestamps()
     end
 
-    create index(:conversation_participations, [:user_id])
     create index(:conversation_participations, [:conversation_id])
+    create unique_index(:conversation_participations, [:user_id, :conversation_id])
     create unique_index(:conversations, [:ap_id])
   end
 end
index eae1873cacc2bc457b65aa3e257ec77ffb385ad0..4e7d9dc92494db7db380d45086047726a73fdc2f 100644 (file)
@@ -4,9 +4,7 @@
 
 defmodule Pleroma.Conversation.ParticipationTest do
   use Pleroma.DataCase
-
   import Pleroma.Factory
-
   alias Pleroma.Conversation.Participation
 
   test "it creates a participation for a conversation and a user" do
@@ -18,6 +16,26 @@ defmodule Pleroma.Conversation.ParticipationTest do
 
     assert participation.user_id == user.id
     assert participation.conversation_id == conversation.id
+
+    :timer.sleep(1000)
+    # Creating again returns the same participation
+    {:ok, %Participation{} = participation_two} =
+      Participation.create_for_user_and_conversation(user, conversation)
+
+    assert participation.id == participation_two.id
+    refute participation.updated_at == participation_two.updated_at
+  end
+
+  test "recreating an existing participations sets it to unread" do
+    participation = insert(:participation, %{read: true})
+
+    {:ok, participation} =
+      Participation.create_for_user_and_conversation(
+        participation.user,
+        participation.conversation
+      )
+
+    refute participation.read
   end
 
   test "it marks a participation as read" do
index 8fb55d51c368c97d150d1a0b321940acf1c2a060..1c9d485ff5c063e302266ce57d5dae05fe4ad41b 100644 (file)
@@ -5,8 +5,90 @@
 defmodule Pleroma.ConversationTest do
   use Pleroma.DataCase
   alias Pleroma.Conversation
+  alias Pleroma.Web.CommonAPI
+
+  import Pleroma.Factory
 
   test "it creates a conversation for given ap_id" do
-    assert {:ok, %Conversation{}} = Conversation.create_for_ap_id("https://some_ap_id")
+    assert {:ok, %Conversation{} = conversation} =
+             Conversation.create_for_ap_id("https://some_ap_id")
+
+    # Inserting again returns the same
+    assert {:ok, conversation_two} = Conversation.create_for_ap_id("https://some_ap_id")
+    assert conversation_two.id == conversation.id
+  end
+
+  test "public posts don't create conversations" do
+    user = insert(:user)
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "Hey"})
+
+    context = activity.data["object"]["context"]
+
+    conversation = Conversation.get_for_ap_id(context)
+
+    refute conversation
+  end
+
+  test "it creates or updates a conversation and participations for a given DM" do
+    har = insert(:user)
+    jafnhar = insert(:user)
+    tridi = insert(:user)
+
+    {:ok, activity} =
+      CommonAPI.post(har, %{"status" => "Hey @#{jafnhar.nickname}", "visibility" => "direct"})
+
+    context = activity.data["object"]["context"]
+
+    conversation =
+      Conversation.get_for_ap_id(context)
+      |> Repo.preload(:participations)
+
+    assert conversation
+    [har_participation, jafnhar_participation] = conversation.participations
+
+    assert har_participation.user_id == har.id
+    assert jafnhar_participation.user_id == jafnhar.id
+
+    {:ok, activity} =
+      CommonAPI.post(jafnhar, %{
+        "status" => "Hey @#{har.nickname}",
+        "visibility" => "direct",
+        "in_reply_to_status_id" => activity.id
+      })
+
+    context = activity.data["object"]["context"]
+
+    conversation_two =
+      Conversation.get_for_ap_id(context)
+      |> Repo.preload(:participations)
+
+    assert conversation_two.id == conversation.id
+
+    [har_participation_two, jafnhar_participation_two] = conversation_two.participations
+
+    assert har_participation_two.user_id == har.id
+    assert jafnhar_participation_two.user_id == jafnhar.id
+
+    {:ok, activity} =
+      CommonAPI.post(tridi, %{
+        "status" => "Hey @#{har.nickname}",
+        "visibility" => "direct",
+        "in_reply_to_status_id" => activity.id
+      })
+
+    context = activity.data["object"]["context"]
+
+    conversation_three =
+      Conversation.get_for_ap_id(context)
+      |> Repo.preload(:participations)
+
+    assert conversation_three.id == conversation.id
+
+    [har_participation_three, jafnhar_participation_three, tridi_participation] =
+      conversation_three.participations
+
+    assert har_participation_three.user_id == har.id
+    assert jafnhar_participation_three.user_id == jafnhar.id
+    assert tridi_participation.user_id == tridi.id
   end
 end