X-Git-Url: https://git.squeep.com/?a=blobdiff_plain;ds=inline;f=lib%2Fpleroma%2Fconversation%2Fparticipation.ex;h=693825cf5fbef554bffda87bc7bb20757afb7e12;hb=7605fe5065b62dcb8aaf7db07e5d168138b80d3e;hp=176b82a2092e46cd48745c719a5f4c6fd4dd206d;hpb=3ac6160299d2bdee487a008b37cdcfa831eac668;p=akkoma
diff --git a/lib/pleroma/conversation/participation.ex b/lib/pleroma/conversation/participation.ex
index 176b82a20..693825cf5 100644
--- a/lib/pleroma/conversation/participation.ex
+++ b/lib/pleroma/conversation/participation.ex
@@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
-# Copyright © 2017-2019 Pleroma Authors
+# Copyright © 2017-2020 Pleroma Authors
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Conversation.Participation do
@@ -64,11 +64,13 @@ defmodule Pleroma.Conversation.Participation do
end
def mark_as_read(participation) do
- participation
- |> read_cng(%{read: true})
- |> Repo.update()
+ __MODULE__
+ |> where(id: ^participation.id)
+ |> update(set: [read: true])
+ |> select([p], p)
+ |> Repo.update_all([])
|> case do
- {:ok, participation} ->
+ {1, [participation]} ->
participation = Repo.preload(participation, :user)
User.set_unread_conversation_count(participation.user)
{:ok, participation}
@@ -122,9 +124,35 @@ defmodule Pleroma.Conversation.Participation do
order_by: [desc: p.updated_at],
preload: [conversation: [:users]]
)
+ |> restrict_recipients(user, params)
|> Pleroma.Pagination.fetch_paginated(params)
end
+ def restrict_recipients(query, user, %{"recipients" => user_ids}) do
+ user_ids =
+ [user.id | user_ids]
+ |> Enum.uniq()
+ |> Enum.reduce([], fn user_id, acc ->
+ {:ok, user_id} = FlakeId.Ecto.CompatType.dump(user_id)
+ [user_id | acc]
+ end)
+
+ conversation_subquery =
+ __MODULE__
+ |> group_by([p], p.conversation_id)
+ |> having(
+ [p],
+ count(p.user_id) == ^length(user_ids) and
+ fragment("array_agg(?) @> ?", p.user_id, ^user_ids)
+ )
+ |> select([p], %{id: p.conversation_id})
+
+ query
+ |> join(:inner, [p], c in subquery(conversation_subquery), on: p.conversation_id == c.id)
+ end
+
+ def restrict_recipients(query, _, _), do: query
+
def for_user_and_conversation(user, conversation) do
from(p in __MODULE__,
where: p.user_id == ^user.id,