2019-04-10 07:34:53 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# 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-04-10 07:34:53 +00:00
|
|
|
%__MODULE__{}
|
2019-05-09 14:39:28 +00:00
|
|
|
|> creation_cng(%{user_id: user.id, conversation_id: conversation.id, read: read})
|
2019-04-10 14:33:45 +00:00
|
|
|
|> Repo.insert(
|
2019-05-09 14:39:28 +00:00
|
|
|
on_conflict: [set: [read: read, updated_at: NaiveDateTime.utc_now()]],
|
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
|
|
|
|
|
2019-04-10 13:12:01 +00:00
|
|
|
def mark_as_read(participation) do
|
|
|
|
participation
|
|
|
|
|> read_cng(%{read: true})
|
|
|
|
|> Repo.update()
|
2019-10-01 21:37:08 +00:00
|
|
|
|> case do
|
|
|
|
{:ok, participation} ->
|
|
|
|
participation = Repo.preload(participation, :user)
|
|
|
|
User.set_unread_conversation_count(participation.user)
|
|
|
|
{:ok, participation}
|
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
end
|
2019-04-10 13:12:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|> Pleroma.Pagination.fetch_paginated(params)
|
|
|
|
end
|
2019-04-10 15:28:02 +00:00
|
|
|
|
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 =
|
|
|
|
ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
|
2019-04-10 15:28:02 +00:00
|
|
|
"user" => user,
|
|
|
|
"blocking_user" => user
|
|
|
|
})
|
|
|
|
|
|
|
|
%{
|
|
|
|
participation
|
|
|
|
| last_activity_id: activity_id
|
|
|
|
}
|
|
|
|
end)
|
2019-06-04 11:58:36 +00:00
|
|
|
|> Enum.filter(& &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
|
|
|
|
|
|
|
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 07:34:53 +00:00
|
|
|
end
|