Chats: Remove unread from the db, calculate from unseen messages.
This commit is contained in:
parent
8edead7c1d
commit
7f5c5b11a5
9 changed files with 40 additions and 30 deletions
|
|
@ -19,14 +19,13 @@ defmodule Pleroma.Chat do
|
|||
schema "chats" do
|
||||
belongs_to(:user, User, type: FlakeId.Ecto.CompatType)
|
||||
field(:recipient, :string)
|
||||
field(:unread, :integer, default: 0, read_after_writes: true)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
def creation_cng(struct, params) do
|
||||
struct
|
||||
|> cast(params, [:user_id, :recipient, :unread])
|
||||
|> cast(params, [:user_id, :recipient])
|
||||
|> validate_change(:recipient, fn
|
||||
:recipient, recipient ->
|
||||
case User.get_cached_by_ap_id(recipient) do
|
||||
|
|
@ -61,16 +60,10 @@ defmodule Pleroma.Chat do
|
|||
|
||||
def bump_or_create(user_id, recipient) do
|
||||
%__MODULE__{}
|
||||
|> creation_cng(%{user_id: user_id, recipient: recipient, unread: 1})
|
||||
|> creation_cng(%{user_id: user_id, recipient: recipient})
|
||||
|> Repo.insert(
|
||||
on_conflict: [set: [updated_at: NaiveDateTime.utc_now()], inc: [unread: 1]],
|
||||
on_conflict: [set: [updated_at: NaiveDateTime.utc_now()]],
|
||||
conflict_target: [:user_id, :recipient]
|
||||
)
|
||||
end
|
||||
|
||||
def mark_as_read(chat) do
|
||||
chat
|
||||
|> change(%{unread: 0})
|
||||
|> Repo.update()
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -84,4 +84,20 @@ defmodule Pleroma.ChatMessageReference do
|
|||
|> changeset(params)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def unread_count_for_chat(chat) do
|
||||
chat
|
||||
|> for_chat_query()
|
||||
|> where([cmr], cmr.seen == false)
|
||||
|> Repo.aggregate(:count)
|
||||
end
|
||||
|
||||
def set_all_seen_for_chat(chat) do
|
||||
chat
|
||||
|> for_chat_query()
|
||||
|> exclude(:order_by)
|
||||
|> exclude(:preload)
|
||||
|> where([cmr], cmr.seen == false)
|
||||
|> Repo.update_all(set: [seen: true])
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -139,13 +139,8 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
|
|||
[[actor, recipient], [recipient, actor]]
|
||||
|> Enum.each(fn [user, other_user] ->
|
||||
if user.local do
|
||||
if user.ap_id == actor.ap_id do
|
||||
{:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
|
||||
ChatMessageReference.create(chat, object, true)
|
||||
else
|
||||
{:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
|
||||
ChatMessageReference.create(chat, object, false)
|
||||
end
|
||||
{:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
|
||||
ChatMessageReference.create(chat, object, user.ap_id == actor.ap_id)
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
|
|||
|
|
@ -90,7 +90,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
|
|||
|
||||
def mark_as_read(%{assigns: %{user: %{id: user_id}}} = conn, %{id: id}) do
|
||||
with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id),
|
||||
{:ok, chat} <- Chat.mark_as_read(chat) do
|
||||
{_n, _} <- ChatMessageReference.set_all_seen_for_chat(chat) do
|
||||
conn
|
||||
|> put_view(ChatView)
|
||||
|> render("show.json", chat: chat)
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatView do
|
|||
%{
|
||||
id: chat.id |> to_string(),
|
||||
account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
|
||||
unread: chat.unread,
|
||||
unread: ChatMessageReference.unread_count_for_chat(chat),
|
||||
last_message:
|
||||
last_message &&
|
||||
ChatMessageReferenceView.render("show.json", chat_message_reference: last_message),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue