2019-04-10 09:34:53 +02: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 11:55:41 +02:00
|
|
|
alias Pleroma.Conversation.Participation.RecipientShip
|
2019-04-10 09:34:53 +02:00
|
|
|
alias Pleroma.Repo
|
2019-04-10 18:17:22 +02:00
|
|
|
alias Pleroma.User
|
2019-04-10 17:28:02 +02:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
2019-04-10 09:34:53 +02:00
|
|
|
import Ecto.Changeset
|
2019-04-10 17:05:33 +02:00
|
|
|
import Ecto.Query
|
2019-04-10 09:34:53 +02:00
|
|
|
|
|
|
|
schema "conversation_participations" do
|
2019-09-18 16:54:31 +02:00
|
|
|
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
2019-04-10 09:34:53 +02:00
|
|
|
belongs_to(:conversation, Conversation)
|
|
|
|
field(:read, :boolean, default: false)
|
2019-09-18 16:54:31 +02:00
|
|
|
field(:last_activity_id, FlakeId.Ecto.CompatType, virtual: true)
|
2019-04-10 09:34:53 +02:00
|
|
|
|
2019-08-02 11:55:41 +02:00
|
|
|
has_many(:recipient_ships, RecipientShip)
|
|
|
|
has_many(:recipients, through: [:recipient_ships, :user])
|
|
|
|
|
2019-04-10 09:34:53 +02:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def creation_cng(struct, params) do
|
|
|
|
struct
|
2019-05-09 16:39:28 +02:00
|
|
|
|> cast(params, [:user_id, :conversation_id, :read])
|
2019-04-10 09:34:53 +02:00
|
|
|
|> validate_required([:user_id, :conversation_id])
|
|
|
|
end
|
|
|
|
|
2019-05-09 16:39:28 +02:00
|
|
|
def create_for_user_and_conversation(user, conversation, opts \\ []) do
|
|
|
|
read = !!opts[:read]
|
|
|
|
|
2019-04-10 09:34:53 +02:00
|
|
|
%__MODULE__{}
|
2019-05-09 16:39:28 +02:00
|
|
|
|> creation_cng(%{user_id: user.id, conversation_id: conversation.id, read: read})
|
2019-04-10 16:33:45 +02:00
|
|
|
|> Repo.insert(
|
2019-05-09 16:39:28 +02:00
|
|
|
on_conflict: [set: [read: read, updated_at: NaiveDateTime.utc_now()]],
|
2019-04-10 16:33:45 +02:00
|
|
|
returning: true,
|
|
|
|
conflict_target: [:user_id, :conversation_id]
|
|
|
|
)
|
2019-04-10 09:34:53 +02:00
|
|
|
end
|
2019-04-10 15:12:01 +02:00
|
|
|
|
|
|
|
def read_cng(struct, params) do
|
|
|
|
struct
|
|
|
|
|> cast(params, [:read])
|
|
|
|
|> validate_required([:read])
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_read(participation) do
|
|
|
|
participation
|
|
|
|
|> read_cng(%{read: true})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
def mark_as_unread(participation) do
|
|
|
|
participation
|
|
|
|
|> read_cng(%{read: false})
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
2019-04-10 17:05:33 +02:00
|
|
|
|
|
|
|
def for_user(user, params \\ %{}) do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
where: p.user_id == ^user.id,
|
2019-06-07 17:51:47 +02:00
|
|
|
order_by: [desc: p.updated_at],
|
|
|
|
preload: [conversation: [:users]]
|
2019-04-10 17:05:33 +02:00
|
|
|
)
|
|
|
|
|> Pleroma.Pagination.fetch_paginated(params)
|
|
|
|
end
|
2019-04-10 17:28:02 +02:00
|
|
|
|
2019-07-31 15:12:29 +02: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 17:28:02 +02:00
|
|
|
def for_user_with_last_activity_id(user, params \\ %{}) do
|
|
|
|
for_user(user, params)
|
|
|
|
|> Enum.map(fn participation ->
|
2019-04-20 19:40:41 +02:00
|
|
|
activity_id =
|
|
|
|
ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
|
2019-04-10 17:28:02 +02:00
|
|
|
"user" => user,
|
|
|
|
"blocking_user" => user
|
|
|
|
})
|
|
|
|
|
|
|
|
%{
|
|
|
|
participation
|
|
|
|
| last_activity_id: activity_id
|
|
|
|
}
|
|
|
|
end)
|
2019-06-04 13:58:36 +02:00
|
|
|
|> Enum.filter(& &1.last_activity_id)
|
2019-04-10 17:28:02 +02:00
|
|
|
end
|
2019-08-02 15:05:27 +02:00
|
|
|
|
2019-08-14 17:01:11 +02: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 15:05:27 +02:00
|
|
|
|
2019-08-14 17:01:11 +02:00
|
|
|
Repo.get(query, id)
|
2019-08-02 15:05:27 +02:00
|
|
|
end
|
2019-08-05 15:09:19 +02:00
|
|
|
|
|
|
|
def set_recipients(participation, user_ids) do
|
2019-08-06 14:51:17 +02:00
|
|
|
user_ids =
|
|
|
|
[participation.user_id | user_ids]
|
|
|
|
|> Enum.uniq()
|
|
|
|
|
2019-08-05 15:09:19 +02: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 14:51:17 +02:00
|
|
|
|
|
|
|
{:ok, Repo.preload(participation, :recipients, force: true)}
|
2019-08-05 15:09:19 +02:00
|
|
|
end
|
2019-04-10 09:34:53 +02:00
|
|
|
end
|