Participation: Get for a user.
authorlain <lain@soykaf.club>
Wed, 10 Apr 2019 15:05:33 +0000 (17:05 +0200)
committerlain <lain@soykaf.club>
Wed, 10 Apr 2019 15:05:33 +0000 (17:05 +0200)
lib/conversation.ex
lib/conversation/participation.ex
test/conversation/participation_test.exs

index 3d53e91b7b9f97417d03a4357a3c6855f79180da..6eedc72b6d462055057e854a66dd8a314aa99404 100644 (file)
@@ -51,10 +51,10 @@ defmodule Pleroma.Conversation do
          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)
+      users = User.get_users_from_set(activity.recipients)
 
       participations =
-        Enum.map(local_users, fn user ->
+        Enum.map(users, fn user ->
           {:ok, participation} =
             Participation.create_for_user_and_conversation(user, conversation)
 
index a58d0ca0dea91b21fc4e4ed196ef3c799899332a..23e6409f13af62bd2a46a79f84fcc5793853e1af 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.Conversation.Participation do
   alias Pleroma.Conversation
   alias Pleroma.Repo
   import Ecto.Changeset
+  import Ecto.Query
 
   schema "conversation_participations" do
     belongs_to(:user, User, type: Pleroma.FlakeId)
@@ -50,4 +51,12 @@ defmodule Pleroma.Conversation.Participation do
     |> read_cng(%{read: false})
     |> Repo.update()
   end
+
+  def for_user(user, params \\ %{}) do
+    from(p in __MODULE__,
+      where: p.user_id == ^user.id,
+      order_by: [desc: p.updated_at]
+    )
+    |> Pleroma.Pagination.fetch_paginated(params)
+  end
 end
index 4e7d9dc92494db7db380d45086047726a73fdc2f..c52b4ed88b5d82465196dcf28bac5d95d4f1f016 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Conversation.ParticipationTest do
   use Pleroma.DataCase
   import Pleroma.Factory
   alias Pleroma.Conversation.Participation
+  alias Pleroma.Web.CommonAPI
 
   test "it creates a participation for a conversation and a user" do
     user = insert(:user)
@@ -51,4 +52,33 @@ defmodule Pleroma.Conversation.ParticipationTest do
 
     refute participation.read
   end
+
+  test "gets all the participations for a user, ordered by updated at descending" do
+    user = insert(:user)
+    {:ok, activity_one} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
+    :timer.sleep(1000)
+    {:ok, activity_two} = CommonAPI.post(user, %{"status" => "x", "visibility" => "direct"})
+    :timer.sleep(1000)
+
+    {:ok, activity_three} =
+      CommonAPI.post(user, %{
+        "status" => "x",
+        "visibility" => "direct",
+        "in_reply_to_status_id" => activity_one.id
+      })
+
+    assert [participation_one, participation_two] =
+             Participation.for_user(user)
+             |> Repo.preload(:conversation)
+
+    assert participation_one.conversation.ap_id == activity_three.data["object"]["context"]
+    assert participation_two.conversation.ap_id == activity_two.data["object"]["context"]
+
+    # Pagination
+    assert [participation_one] =
+             Participation.for_user(user, %{limit: 1})
+             |> Repo.preload(:conversation)
+
+    assert participation_one.conversation.ap_id == activity_three.data["object"]["context"]
+  end
 end