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