ChatMessageReferences: Change seen -> unread
authorlain <lain@soykaf.club>
Thu, 4 Jun 2020 15:14:42 +0000 (17:14 +0200)
committerlain <lain@soykaf.club>
Thu, 4 Jun 2020 15:14:42 +0000 (17:14 +0200)
lib/pleroma/chat_message_reference.ex
lib/pleroma/web/activity_pub/side_effects.ex
lib/pleroma/web/pleroma_api/views/chat_message_reference_view.ex
priv/repo/migrations/20200604150318_migrate_seen_to_unread_in_chat_message_references.exs [new file with mode: 0644]
test/web/activity_pub/side_effects_test.exs
test/web/pleroma_api/controllers/chat_controller_test.exs

index 9b00443f51540c8dc20d07ca8df84889b86662a9..fc2aaae7a3897d0ea15a727ca5834b106f019c3c 100644 (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
index 0c5709356d595e3ef33744de86127f6b07ad418c..e9f109d8012004fe39eb40d9ab1b41fac2b77129 100644 (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"],
index f9405aec5ff9508e119eac22ad667b499aa01106..592bb17f0517ee1ab4413d029761084bc1926d60 100644 (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
 
diff --git a/priv/repo/migrations/20200604150318_migrate_seen_to_unread_in_chat_message_references.exs b/priv/repo/migrations/20200604150318_migrate_seen_to_unread_in_chat_message_references.exs
new file mode 100644 (file)
index 0000000..fd6bc7b
--- /dev/null
@@ -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
index 82d72119e8c37da02883c9a17ff6d92719b39292..40df664eb5f0c85424652a65ffb478aaaf55879f 100644 (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
index e7892142a3cfce7dc05c9c8eca163122c928c5b5..7af6dec1c26576c0c50975d14bb9f3b5a3739b05 100644 (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