Participation: Get for a user.

This commit is contained in:
lain 2019-04-10 17:05:33 +02:00
parent 280172f6f6
commit 20d9b90760
3 changed files with 41 additions and 2 deletions

View file

@ -51,10 +51,10 @@ def create_or_bump_for(activity) do
ap_id when is_binary(ap_id) <- activity.data["object"]["context"] do ap_id when is_binary(ap_id) <- activity.data["object"]["context"] do
{:ok, conversation} = create_for_ap_id(ap_id) {: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 = participations =
Enum.map(local_users, fn user -> Enum.map(users, fn user ->
{:ok, participation} = {:ok, participation} =
Participation.create_for_user_and_conversation(user, conversation) Participation.create_for_user_and_conversation(user, conversation)

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Conversation.Participation do
alias Pleroma.Conversation alias Pleroma.Conversation
alias Pleroma.Repo alias Pleroma.Repo
import Ecto.Changeset import Ecto.Changeset
import Ecto.Query
schema "conversation_participations" do schema "conversation_participations" do
belongs_to(:user, User, type: Pleroma.FlakeId) belongs_to(:user, User, type: Pleroma.FlakeId)
@ -50,4 +51,12 @@ def mark_as_unread(participation) do
|> read_cng(%{read: false}) |> read_cng(%{read: false})
|> Repo.update() |> Repo.update()
end 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 end

View file

@ -6,6 +6,7 @@ defmodule Pleroma.Conversation.ParticipationTest do
use Pleroma.DataCase use Pleroma.DataCase
import Pleroma.Factory import Pleroma.Factory
alias Pleroma.Conversation.Participation alias Pleroma.Conversation.Participation
alias Pleroma.Web.CommonAPI
test "it creates a participation for a conversation and a user" do test "it creates a participation for a conversation and a user" do
user = insert(:user) user = insert(:user)
@ -51,4 +52,33 @@ test "it marks a participation as unread" do
refute participation.read refute participation.read
end 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 end