2020-06-03 04:30:12 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-06-03 04:30:12 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-06 03:51:10 -06:00
|
|
|
defmodule Pleroma.Chat.MessageReference do
|
2020-06-03 04:30:12 -06:00
|
|
|
@moduledoc """
|
|
|
|
A reference that builds a relation between an AP chat message that a user can see and whether it has been seen
|
|
|
|
by them, or should be displayed to them. Used to build the chat view that is presented to the user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
alias Pleroma.Chat
|
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Ecto.Query
|
|
|
|
|
2020-06-06 02:42:24 -06:00
|
|
|
@primary_key {:id, FlakeId.Ecto.Type, autogenerate: true}
|
2020-06-03 04:30:12 -06:00
|
|
|
|
|
|
|
schema "chat_message_references" do
|
|
|
|
belongs_to(:object, Object)
|
2020-06-07 06:25:30 -06:00
|
|
|
belongs_to(:chat, Chat, type: FlakeId.Ecto.CompatType)
|
2020-06-03 04:30:12 -06:00
|
|
|
|
2020-06-04 09:14:42 -06:00
|
|
|
field(:unread, :boolean, default: true)
|
2020-06-03 04:30:12 -06:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
def changeset(struct, params) do
|
|
|
|
struct
|
2020-06-04 09:14:42 -06:00
|
|
|
|> cast(params, [:object_id, :chat_id, :unread])
|
|
|
|
|> validate_required([:object_id, :chat_id, :unread])
|
2020-06-03 04:30:12 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_by_id(id) do
|
|
|
|
__MODULE__
|
|
|
|
|> Repo.get(id)
|
|
|
|
|> Repo.preload(:object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(cm_ref) do
|
|
|
|
cm_ref
|
|
|
|
|> Repo.delete()
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_for_object(%{id: object_id}) do
|
|
|
|
from(cr in __MODULE__,
|
|
|
|
where: cr.object_id == ^object_id
|
|
|
|
)
|
|
|
|
|> Repo.delete_all()
|
|
|
|
end
|
|
|
|
|
|
|
|
def for_chat_and_object(%{id: chat_id}, %{id: object_id}) do
|
|
|
|
__MODULE__
|
|
|
|
|> Repo.get_by(chat_id: chat_id, object_id: object_id)
|
|
|
|
|> Repo.preload(:object)
|
|
|
|
end
|
|
|
|
|
|
|
|
def for_chat_query(chat) do
|
|
|
|
from(cr in __MODULE__,
|
|
|
|
where: cr.chat_id == ^chat.id,
|
|
|
|
order_by: [desc: :id],
|
|
|
|
preload: [:object]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-06-03 04:49:53 -06:00
|
|
|
def last_message_for_chat(chat) do
|
|
|
|
chat
|
|
|
|
|> for_chat_query()
|
|
|
|
|> limit(1)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
2020-06-04 09:14:42 -06:00
|
|
|
def create(chat, object, unread) do
|
2020-06-03 04:30:12 -06:00
|
|
|
params = %{
|
|
|
|
chat_id: chat.id,
|
|
|
|
object_id: object.id,
|
2020-06-04 09:14:42 -06:00
|
|
|
unread: unread
|
2020-06-03 04:30:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
%__MODULE__{}
|
|
|
|
|> changeset(params)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
2020-06-03 06:26:50 -06:00
|
|
|
|
|
|
|
def unread_count_for_chat(chat) do
|
|
|
|
chat
|
|
|
|
|> for_chat_query()
|
2020-06-04 09:14:42 -06:00
|
|
|
|> where([cmr], cmr.unread == true)
|
2020-06-03 06:26:50 -06:00
|
|
|
|> Repo.aggregate(:count)
|
|
|
|
end
|
|
|
|
|
2020-06-03 11:21:23 -06:00
|
|
|
def mark_as_read(cm_ref) do
|
|
|
|
cm_ref
|
2020-06-04 09:14:42 -06:00
|
|
|
|> changeset(%{unread: false})
|
2020-06-03 11:21:23 -06:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
2020-06-07 07:38:33 -06:00
|
|
|
def set_all_seen_for_chat(chat, last_read_id \\ nil) do
|
|
|
|
query =
|
|
|
|
chat
|
|
|
|
|> for_chat_query()
|
|
|
|
|> exclude(:order_by)
|
|
|
|
|> exclude(:preload)
|
|
|
|
|> where([cmr], cmr.unread == true)
|
|
|
|
|
|
|
|
if last_read_id do
|
|
|
|
query
|
|
|
|
|> where([cmr], cmr.id <= ^last_read_id)
|
|
|
|
else
|
|
|
|
query
|
|
|
|
end
|
2020-06-04 09:14:42 -06:00
|
|
|
|> Repo.update_all(set: [unread: false])
|
2020-06-03 06:26:50 -06:00
|
|
|
end
|
2020-06-03 04:30:12 -06:00
|
|
|
end
|