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

View file

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

View file

@ -16,7 +16,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceView do
id: id, id: id,
object: %{data: chat_message}, object: %{data: chat_message},
chat_id: chat_id, chat_id: chat_id,
seen: seen unread: unread
} }
} }
) do ) do
@ -30,7 +30,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatMessageReferenceView do
attachment: attachment:
chat_message["attachment"] && chat_message["attachment"] &&
StatusView.render("attachment.json", attachment: chat_message["attachment"]), StatusView.render("attachment.json", attachment: chat_message["attachment"]),
unread: !seen unread: unread
} }
end 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 -> stream: fn _, payload ->
case payload do case payload do
{^author, cm_ref} -> {^author, cm_ref} ->
assert cm_ref.seen == true assert cm_ref.unread == false
{^recipient, cm_ref} -> {^recipient, cm_ref} ->
assert cm_ref.seen == false assert cm_ref.unread == true
view = view =
Pleroma.Web.PleromaAPI.ChatView.render("show.json", 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() [cm_ref] = ChatMessageReference.for_chat_query(chat) |> Repo.all()
assert cm_ref.object.data["content"] == "hey" 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) chat = Chat.get(recipient.id, author.ap_id)
[cm_ref] = ChatMessageReference.for_chat_query(chat) |> Repo.all() [cm_ref] = ChatMessageReference.for_chat_query(chat) |> Repo.all()
assert cm_ref.object.data["content"] == "hey" assert cm_ref.object.data["content"] == "hey"
assert cm_ref.seen == false assert cm_ref.unread == true
end end
test "it creates a Chat for the local users and bumps the unread count" do 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) object = Object.normalize(create, false)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object) cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == false assert cm_ref.unread == true
result = result =
conn conn
@ -36,7 +36,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
cm_ref = ChatMessageReference.for_chat_and_object(chat, object) cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == true assert cm_ref.unread == false
end end
end end
@ -52,7 +52,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
object = Object.normalize(create, false) object = Object.normalize(create, false)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object) cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == false assert cm_ref.unread == true
result = result =
conn conn
@ -63,7 +63,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
cm_ref = ChatMessageReference.for_chat_and_object(chat, object) cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
assert cm_ref.seen == true assert cm_ref.unread == false
end end
end end