ChatMessageReferences: Change seen -> unread

This commit is contained in:
lain 2020-06-04 17:14:42 +02:00
parent 6e103a18af
commit 00748e9650
6 changed files with 50 additions and 20 deletions

View File

@ -23,15 +23,15 @@ defmodule Pleroma.ChatMessageReference do
belongs_to(:object, Object)
belongs_to(:chat, Chat)
field(:seen, :boolean, default: false)
field(:unread, :boolean, default: true)
timestamps()
end
def changeset(struct, params) do
struct
|> cast(params, [:object_id, :chat_id, :seen])
|> validate_required([:object_id, :chat_id, :seen])
|> cast(params, [:object_id, :chat_id, :unread])
|> validate_required([:object_id, :chat_id, :unread])
end
def get_by_id(id) do
@ -73,11 +73,11 @@ defmodule Pleroma.ChatMessageReference do
|> Repo.one()
end
def create(chat, object, seen) do
def create(chat, object, unread) do
params = %{
chat_id: chat.id,
object_id: object.id,
seen: seen
unread: unread
}
%__MODULE__{}
@ -88,13 +88,13 @@ defmodule Pleroma.ChatMessageReference do
def unread_count_for_chat(chat) do
chat
|> for_chat_query()
|> where([cmr], cmr.seen == false)
|> where([cmr], cmr.unread == true)
|> Repo.aggregate(:count)
end
def mark_as_read(cm_ref) do
cm_ref
|> changeset(%{seen: true})
|> changeset(%{unread: false})
|> Repo.update()
end
@ -103,7 +103,7 @@ defmodule Pleroma.ChatMessageReference do
|> for_chat_query()
|> exclude(:order_by)
|> exclude(:preload)
|> where([cmr], cmr.seen == false)
|> Repo.update_all(set: [seen: true])
|> where([cmr], cmr.unread == true)
|> Repo.update_all(set: [unread: false])
end
end

View File

@ -140,7 +140,7 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
|> Enum.each(fn [user, other_user] ->
if user.local do
{:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
{:ok, cm_ref} = ChatMessageReference.create(chat, object, user.ap_id == actor.ap_id)
{:ok, cm_ref} = ChatMessageReference.create(chat, object, user.ap_id != actor.ap_id)
Streamer.stream(
["user", "user:pleroma_chat"],

View File

@ -16,7 +16,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceView do
id: id,
object: %{data: chat_message},
chat_id: chat_id,
seen: seen
unread: unread
}
}
) do
@ -30,7 +30,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceView do
attachment:
chat_message["attachment"] &&
StatusView.render("attachment.json", attachment: chat_message["attachment"]),
unread: !seen
unread: unread
}
end

View File

@ -0,0 +1,30 @@
defmodule Pleroma.Repo.Migrations.MigrateSeenToUnreadInChatMessageReferences do
use Ecto.Migration
def change do
drop(
index(:chat_message_references, [:chat_id],
where: "seen = false",
name: "unseen_messages_count_index"
)
)
alter table(:chat_message_references) do
add(:unread, :boolean, default: true)
end
execute("update chat_message_references set unread = not seen")
alter table(:chat_message_references) do
modify(:unread, :boolean, default: true, null: false)
remove(:seen, :boolean, default: false, null: false)
end
create(
index(:chat_message_references, [:chat_id],
where: "unread = true",
name: "unread_messages_count_index"
)
)
end
end

View File

@ -325,10 +325,10 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
stream: fn _, payload ->
case payload do
{^author, cm_ref} ->
assert cm_ref.seen == true
assert cm_ref.unread == false
{^recipient, cm_ref} ->
assert cm_ref.seen == false
assert cm_ref.unread == true
view =
Pleroma.Web.PleromaAPI.ChatView.render("show.json",
@ -369,14 +369,14 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
[cm_ref] = ChatMessageReference.for_chat_query(chat) |> Repo.all()
assert cm_ref.object.data["content"] == "hey"
assert cm_ref.seen == true
assert cm_ref.unread == false
chat = Chat.get(recipient.id, author.ap_id)
[cm_ref] = ChatMessageReference.for_chat_query(chat) |> Repo.all()
assert cm_ref.object.data["content"] == "hey"
assert cm_ref.seen == false
assert cm_ref.unread == true
end
test "it creates a Chat for the local users and bumps the unread count" do

View File

@ -25,7 +25,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
object = Object.normalize(create, false)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == false
assert cm_ref.unread == true
result =
conn
@ -36,7 +36,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == true
assert cm_ref.unread == false
end
end
@ -52,7 +52,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
object = Object.normalize(create, false)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == false
assert cm_ref.unread == true
result =
conn
@ -63,7 +63,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == true
assert cm_ref.unread == false
end
end