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