2019-04-10 07:34:53 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-04-10 07:34:53 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Conversation.Participation do
|
|
|
|
use Ecto.Schema
|
|
|
|
alias Pleroma.Conversation
|
2019-08-02 09:55:41 +00:00
|
|
|
alias Pleroma.Conversation.Participation.RecipientShip
|
2019-04-10 07:34:53 +00:00
|
|
|
alias Pleroma.Repo
|
2019-04-10 16:17:22 +00:00
|
|
|
alias Pleroma.User
|
2019-04-10 15:28:02 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2019-04-10 07:34:53 +00:00
|
|
|
import Ecto.Changeset
|
2019-04-10 15:05:33 +00:00
|
|
|
import Ecto.Query
|
2019-04-10 07:34:53 +00:00
|
|
|
|
|
|
|
schema "conversation_participations" do
|
2019-09-18 14:54:31 +00:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
2019-04-10 07:34:53 +00:00
|
|
|
belongs_to(:conversation, Conversation)
|
|
|
|
field(:read, :boolean, default: false)
|
2019-09-18 14:54:31 +00:00
|
|
|
field(:last_activity_id, FlakeId.Ecto.CompatType, virtual: true)
|
2019-04-10 07:34:53 +00:00
|
|
|
|
2019-08-02 09:55:41 +00:00
|
|
|
has_many(:recipient_ships, RecipientShip)
|
|
|
|
has_many(:recipients, through: [:recipient_ships, :user])
|
|
|
|
|
2019-04-10 07:34:53 +00:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def creation_cng(struct, params) do
|
|
|
|
struct
|
2019-05-09 14:39:28 +00:00
|
|
|
|> cast(params, [:user_id, :conversation_id, :read])
|
2019-04-10 07:34:53 +00:00
|
|
|
|> validate_required([:user_id, :conversation_id])
|
|
|
|
end
|
|
|
|
|
2019-05-09 14:39:28 +00:00
|
|
|
def create_for_user_and_conversation(user, conversation, opts \\ []) do
|
|
|
|
read = !!opts[:read]
|
2019-10-25 18:29:23 +00:00
|
|
|
invisible_conversation = !!opts[:invisible_conversation]
|
|
|
|
|
|
|
|
update_on_conflict =
|
|
|
|
if(invisible_conversation, do: [], else: [read: read])
|
|
|
|
|> Keyword.put(:updated_at, NaiveDateTime.utc_now())
|
2019-05-09 14:39:28 +00:00
|
|
|
|
2019-04-10 07:34:53 +00:00
|
|
|
%__MODULE__{}
|
2019-10-25 18:29:23 +00:00
|
|
|
|> creation_cng(%{
|
|
|
|
user_id: user.id,
|
|
|
|
conversation_id: conversation.id,
|
|
|
|
read: invisible_conversation || read
|
|
|
|
})
|
2019-04-10 14:33:45 +00:00
|
|
|
|> Repo.insert(
|
2019-10-25 18:29:23 +00:00
|
|
|
on_conflict: [set: update_on_conflict],
|
2019-04-10 14:33:45 +00:00
|
|
|
returning: true,
|
|
|
|
conflict_target: [:user_id, :conversation_id]
|
|
|
|
)
|
2019-04-10 07:34:53 +00:00
|
|
|
end
|
2019-04-10 13:12:01 +00:00
|
|
|
|
|
|
|
def read_cng(struct, params) do
|
|
|
|
struct
|
|
|
|
|> cast(params, [:read])
|
|
|
|
|> validate_required([:read])
|
|
|
|
end
|
|
|
|
|
2019-10-17 12:25:15 +00:00
|
|
|
def mark_as_read(%User{} = user, %Conversation{} = conversation) do
|
|
|
|
with %__MODULE__{} = participation <- for_user_and_conversation(user, conversation) do
|
|
|
|
mark_as_read(participation)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
def mark_as_read(%__MODULE__{} = participation) do
|
|
|
|
participation
|
|
|
|
|> change(read: true)
|
|
|
|
|> Repo.update()
|
2019-04-10 13:12:01 +00:00
|
|
|
end
|
|
|
|
|
2019-10-25 18:29:23 +00:00
|
|
|
def mark_all_as_read(%User{local: true} = user, %User{} = target_user) do
|
|
|
|
target_conversation_ids =
|
|
|
|
__MODULE__
|
|
|
|
|> where([p], p.user_id == ^target_user.id)
|
|
|
|
|> select([p], p.conversation_id)
|
|
|
|
|> Repo.all()
|
|
|
|
|
|
|
|
__MODULE__
|
|
|
|
|> where([p], p.user_id == ^user.id)
|
|
|
|
|> where([p], p.conversation_id in ^target_conversation_ids)
|
|
|
|
|> update([p], set: [read: true])
|
|
|
|
|> Repo.update_all([])
|
|
|
|
|
|
|
|
{:ok, user, []}
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_all_as_read(%User{} = user, %User{}), do: {:ok, user, []}
|
|
|
|
|
|
|
|
def mark_all_as_read(%User{} = user) do
|
2019-10-11 03:40:58 +00:00
|
|
|
{_, participations} =
|
|
|
|
__MODULE__
|
|
|
|
|> where([p], p.user_id == ^user.id)
|
|
|
|
|> where([p], not p.read)
|
|
|
|
|> update([p], set: [read: true])
|
|
|
|
|> select([p], p)
|
|
|
|
|> Repo.update_all([])
|
|
|
|
|
2019-10-25 18:29:23 +00:00
|
|
|
{:ok, user, participations}
|
2019-10-11 03:40:58 +00:00
|
|
|
end
|
|
|
|
|
2019-04-10 13:12:01 +00:00
|
|
|
def mark_as_unread(participation) do
|
|
|
|
participation
|
|
|
|
|> read_cng(%{read: false})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-04-10 15:05:33 +00:00
|
|
|
|
|
|
|
def for_user(user, params \\ %{}) do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
where: p.user_id == ^user.id,
|
2019-06-07 15:51:47 +00:00
|
|
|
order_by: [desc: p.updated_at],
|
|
|
|
preload: [conversation: [:users]]
|
2019-04-10 15:05:33 +00:00
|
|
|
)
|
2019-11-07 00:00:21 +00:00
|
|
|
|> restrict_recipients(user, params)
|
2019-04-10 15:05:33 +00:00
|
|
|
|> Pleroma.Pagination.fetch_paginated(params)
|
|
|
|
end
|
2019-04-10 15:28:02 +00:00
|
|
|
|
2020-05-05 13:44:46 +00:00
|
|
|
def restrict_recipients(query, user, %{recipients: user_ids}) do
|
2020-03-25 14:01:45 +00:00
|
|
|
user_binary_ids =
|
2019-11-07 00:00:21 +00:00
|
|
|
[user.id | user_ids]
|
|
|
|
|> Enum.uniq()
|
2020-03-25 14:01:45 +00:00
|
|
|
|> User.binary_id()
|
2019-11-07 00:00:21 +00:00
|
|
|
|
|
|
|
conversation_subquery =
|
|
|
|
__MODULE__
|
|
|
|
|> group_by([p], p.conversation_id)
|
|
|
|
|> having(
|
|
|
|
[p],
|
2020-03-25 14:01:45 +00:00
|
|
|
count(p.user_id) == ^length(user_binary_ids) and
|
|
|
|
fragment("array_agg(?) @> ?", p.user_id, ^user_binary_ids)
|
2019-11-07 00:00:21 +00:00
|
|
|
)
|
|
|
|
|> 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
|
|
|
|
|
2019-07-31 13:12:29 +00:00
|
|
|
def for_user_and_conversation(user, conversation) do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
where: p.user_id == ^user.id,
|
|
|
|
where: p.conversation_id == ^conversation.id
|
|
|
|
)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2019-04-10 15:28:02 +00:00
|
|
|
def for_user_with_last_activity_id(user, params \\ %{}) do
|
|
|
|
for_user(user, params)
|
|
|
|
|> Enum.map(fn participation ->
|
2019-04-20 17:40:41 +00:00
|
|
|
activity_id =
|
2020-06-15 10:27:13 +00:00
|
|
|
ActivityPub.fetch_latest_direct_activity_id_for_context(
|
|
|
|
participation.conversation.ap_id,
|
|
|
|
%{
|
|
|
|
user: user,
|
|
|
|
blocking_user: user
|
|
|
|
}
|
|
|
|
)
|
2019-04-10 15:28:02 +00:00
|
|
|
|
|
|
|
%{
|
|
|
|
participation
|
|
|
|
| last_activity_id: activity_id
|
|
|
|
}
|
|
|
|
end)
|
2020-05-05 13:44:46 +00:00
|
|
|
|> Enum.reject(&is_nil(&1.last_activity_id))
|
2019-04-10 15:28:02 +00:00
|
|
|
end
|
2019-08-02 13:05:27 +00:00
|
|
|
|
2019-08-14 15:01:11 +00:00
|
|
|
def get(_, _ \\ [])
|
|
|
|
def get(nil, _), do: nil
|
|
|
|
|
|
|
|
def get(id, params) do
|
|
|
|
query =
|
|
|
|
if preload = params[:preload] do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
preload: ^preload
|
|
|
|
)
|
|
|
|
else
|
|
|
|
__MODULE__
|
|
|
|
end
|
2019-08-02 13:05:27 +00:00
|
|
|
|
2019-08-14 15:01:11 +00:00
|
|
|
Repo.get(query, id)
|
2019-08-02 13:05:27 +00:00
|
|
|
end
|
2019-08-05 13:09:19 +00:00
|
|
|
|
|
|
|
def set_recipients(participation, user_ids) do
|
2019-08-06 12:51:17 +00:00
|
|
|
user_ids =
|
|
|
|
[participation.user_id | user_ids]
|
|
|
|
|> Enum.uniq()
|
|
|
|
|
2019-08-05 13:09:19 +00:00
|
|
|
Repo.transaction(fn ->
|
|
|
|
query =
|
|
|
|
from(r in RecipientShip,
|
|
|
|
where: r.participation_id == ^participation.id
|
|
|
|
)
|
|
|
|
|
|
|
|
Repo.delete_all(query)
|
|
|
|
|
|
|
|
users =
|
|
|
|
from(u in User,
|
|
|
|
where: u.id in ^user_ids
|
|
|
|
)
|
|
|
|
|> Repo.all()
|
|
|
|
|
|
|
|
RecipientShip.create(users, participation)
|
|
|
|
:ok
|
|
|
|
end)
|
2019-08-06 12:51:17 +00:00
|
|
|
|
|
|
|
{:ok, Repo.preload(participation, :recipients, force: true)}
|
2019-08-05 13:09:19 +00:00
|
|
|
end
|
2019-10-01 21:37:08 +00:00
|
|
|
|
2020-09-01 06:37:08 +00:00
|
|
|
@spec unread_count(User.t()) :: integer()
|
|
|
|
def unread_count(%User{id: user_id}) do
|
|
|
|
from(q in __MODULE__, where: q.user_id == ^user_id and q.read == false)
|
|
|
|
|> Repo.aggregate(:count, :id)
|
|
|
|
end
|
|
|
|
|
2019-10-01 21:37:08 +00:00
|
|
|
def unread_conversation_count_for_user(user) do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
where: p.user_id == ^user.id,
|
|
|
|
where: not p.read,
|
|
|
|
select: %{count: count(p.id)}
|
|
|
|
)
|
|
|
|
end
|
2021-02-15 17:48:13 +00:00
|
|
|
|
|
|
|
def delete(%__MODULE__{} = participation) do
|
|
|
|
Repo.delete(participation)
|
|
|
|
end
|
2019-04-10 07:34:53 +00:00
|
|
|
end
|