Participations: Add last activity.
authorlain <lain@soykaf.club>
Wed, 10 Apr 2019 15:28:02 +0000 (17:28 +0200)
committerlain <lain@soykaf.club>
Wed, 10 Apr 2019 15:28:02 +0000 (17:28 +0200)
lib/conversation/participation.ex
test/conversation/participation_test.exs

index 23e6409f13af62bd2a46a79f84fcc5793853e1af..4183af8a7dad6b7c9c2c965eca20e117b4b2a6a0 100644 (file)
@@ -7,6 +7,7 @@ defmodule Pleroma.Conversation.Participation do
   alias Pleroma.User
   alias Pleroma.Conversation
   alias Pleroma.Repo
+  alias Pleroma.Web.ActivityPub.ActivityPub
   import Ecto.Changeset
   import Ecto.Query
 
@@ -14,6 +15,7 @@ defmodule Pleroma.Conversation.Participation do
     belongs_to(:user, User, type: Pleroma.FlakeId)
     belongs_to(:conversation, Conversation)
     field(:read, :boolean, default: false)
+    field(:last_activity_id, Pleroma.FlakeId, virtual: true)
 
     timestamps()
   end
@@ -59,4 +61,29 @@ defmodule Pleroma.Conversation.Participation do
     )
     |> Pleroma.Pagination.fetch_paginated(params)
   end
+
+  def for_user_with_last_activity_id(user, params \\ %{}) do
+    for_user(user, params)
+    |> Repo.preload(:conversation)
+    |> Enum.map(fn participation ->
+      # TODO: Don't load all those activities, just get the most recent
+      # Involves splitting up the query.
+      activities =
+        ActivityPub.fetch_activities_for_context(participation.conversation.ap_id, %{
+          "user" => user,
+          "blocking_user" => user
+        })
+
+      activity_id =
+        case activities do
+          [activity | _] -> activity.id
+          _ -> nil
+        end
+
+      %{
+        participation
+        | last_activity_id: activity_id
+      }
+    end)
+  end
 end
index c52b4ed88b5d82465196dcf28bac5d95d4f1f016..5791fa0dbec86c3060ce4b8642c540b2c9821a05 100644 (file)
@@ -80,5 +80,12 @@ defmodule Pleroma.Conversation.ParticipationTest do
              |> Repo.preload(:conversation)
 
     assert participation_one.conversation.ap_id == activity_three.data["object"]["context"]
+
+    # With last_activity_id
+    assert [participation_one] =
+             Participation.for_user_with_last_activity_id(user, %{limit: 1})
+             |> Repo.preload(:conversation)
+
+    assert participation_one.last_activity_id == activity_three.id
   end
 end