Commit 20d9b907 authored by lain's avatar lain

Participation: Get for a user.

parent 280172f6
......@@ -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)
......
......@@ -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
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment