moderation log: fix improperly migrated data
authorrinpatch <rinpatch@sdf.org>
Sat, 7 Mar 2020 10:51:28 +0000 (13:51 +0300)
committerrinpatch <rinpatch@sdf.org>
Sat, 7 Mar 2020 14:00:58 +0000 (17:00 +0300)
Some of the actions used to have a user map as a subject, which was then
changed to an array of user maps. However instead of migrating old data
there was just a hack to transform it every time, moreover this hack
didn't include all possible actions, which resulted in crashes. This
commit fixes the crashes by introducing a proper database migration for old data.

Closes #1606

lib/pleroma/moderation_log.ex
priv/repo/migrations/20200307103755_fix_moderation_log_subjects.exs [new file with mode: 0644]

index c81477f481e1737b260f04435f5415599ae0695b..e32895f70d5d269bcae8f0ed73b8b650fa358651 100644 (file)
@@ -387,24 +387,6 @@ defmodule Pleroma.ModerationLog do
     "@#{actor_nickname} created users: #{users_to_nicknames_string(subjects)}"
   end
 
-  @spec get_log_entry_message(ModerationLog) :: String.t()
-  def get_log_entry_message(%ModerationLog{
-        data: %{
-          "actor" => %{"nickname" => actor_nickname},
-          "action" => "activate",
-          "subject" => user
-        }
-      })
-      when is_map(user) do
-    get_log_entry_message(%ModerationLog{
-      data: %{
-        "actor" => %{"nickname" => actor_nickname},
-        "action" => "activate",
-        "subject" => [user]
-      }
-    })
-  end
-
   @spec get_log_entry_message(ModerationLog) :: String.t()
   def get_log_entry_message(%ModerationLog{
         data: %{
@@ -416,24 +398,6 @@ defmodule Pleroma.ModerationLog do
     "@#{actor_nickname} activated users: #{users_to_nicknames_string(users)}"
   end
 
-  @spec get_log_entry_message(ModerationLog) :: String.t()
-  def get_log_entry_message(%ModerationLog{
-        data: %{
-          "actor" => %{"nickname" => actor_nickname},
-          "action" => "deactivate",
-          "subject" => user
-        }
-      })
-      when is_map(user) do
-    get_log_entry_message(%ModerationLog{
-      data: %{
-        "actor" => %{"nickname" => actor_nickname},
-        "action" => "deactivate",
-        "subject" => [user]
-      }
-    })
-  end
-
   @spec get_log_entry_message(ModerationLog) :: String.t()
   def get_log_entry_message(%ModerationLog{
         data: %{
@@ -473,26 +437,6 @@ defmodule Pleroma.ModerationLog do
     "@#{actor_nickname} removed tags: #{tags_string} from users: #{nicknames_to_string(nicknames)}"
   end
 
-  @spec get_log_entry_message(ModerationLog) :: String.t()
-  def get_log_entry_message(%ModerationLog{
-        data: %{
-          "actor" => %{"nickname" => actor_nickname},
-          "action" => "grant",
-          "subject" => user,
-          "permission" => permission
-        }
-      })
-      when is_map(user) do
-    get_log_entry_message(%ModerationLog{
-      data: %{
-        "actor" => %{"nickname" => actor_nickname},
-        "action" => "grant",
-        "subject" => [user],
-        "permission" => permission
-      }
-    })
-  end
-
   @spec get_log_entry_message(ModerationLog) :: String.t()
   def get_log_entry_message(%ModerationLog{
         data: %{
@@ -505,26 +449,6 @@ defmodule Pleroma.ModerationLog do
     "@#{actor_nickname} made #{users_to_nicknames_string(users)} #{permission}"
   end
 
-  @spec get_log_entry_message(ModerationLog) :: String.t()
-  def get_log_entry_message(%ModerationLog{
-        data: %{
-          "actor" => %{"nickname" => actor_nickname},
-          "action" => "revoke",
-          "subject" => user,
-          "permission" => permission
-        }
-      })
-      when is_map(user) do
-    get_log_entry_message(%ModerationLog{
-      data: %{
-        "actor" => %{"nickname" => actor_nickname},
-        "action" => "revoke",
-        "subject" => [user],
-        "permission" => permission
-      }
-    })
-  end
-
   @spec get_log_entry_message(ModerationLog) :: String.t()
   def get_log_entry_message(%ModerationLog{
         data: %{
diff --git a/priv/repo/migrations/20200307103755_fix_moderation_log_subjects.exs b/priv/repo/migrations/20200307103755_fix_moderation_log_subjects.exs
new file mode 100644 (file)
index 0000000..d1c8539
--- /dev/null
@@ -0,0 +1,9 @@
+defmodule Pleroma.Repo.Migrations.FixModerationLogSubjects do
+  use Ecto.Migration
+
+  def change do
+    execute(
+      "update moderation_log set data = safe_jsonb_set(data, '{subject}', safe_jsonb_set('[]'::jsonb, '{0}', data->'subject')) where jsonb_typeof(data->'subject') != 'array' and data->>'action' = ANY('{revoke,grant,activate,deactivate,delete}');"
+    )
+  end
+end