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
|> 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__{}
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
|> 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
|> 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"],
id: id,
object: %{data: chat_message},
chat_id: chat_id,
- seen: seen
+ unread: unread
}
}
) do
attachment:
chat_message["attachment"] &&
StatusView.render("attachment.json", attachment: chat_message["attachment"]),
- unread: !seen
+ unread: unread
}
end
--- /dev/null
+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
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",
[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
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
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
- assert cm_ref.seen == true
+ assert cm_ref.unread == false
end
end
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
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
- assert cm_ref.seen == true
+ assert cm_ref.unread == false
end
end