about summary refs log tree commit diff
path: root/db/migrate/20180707193142_migrate_filters.rb
diff options
context:
space:
mode:
authorThibaut Girka <thib@sitedethib.com>2018-07-07 21:40:49 +0200
committerThibaut Girka <thib@sitedethib.com>2018-07-09 19:19:23 +0200
commit402da8065c2b378cca6361f2c7252bd766f25dde (patch)
treeeda07c40c934ab9a55d7847afe4af3e54bce86b8 /db/migrate/20180707193142_migrate_filters.rb
parentee41947e9defce24ecd9f46329bb953d46b335bd (diff)
Migrate glitch-soc keyword mutes to Mastodon's
Completely remove glitch-soc's Keyword Mutes, migrate
existing database records to CustomFilters.

Handling of client-side filters is still not implemented
in the glitch-soc front-end.
Diffstat (limited to 'db/migrate/20180707193142_migrate_filters.rb')
-rw-r--r--db/migrate/20180707193142_migrate_filters.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/db/migrate/20180707193142_migrate_filters.rb b/db/migrate/20180707193142_migrate_filters.rb
new file mode 100644
index 000000000..455ce71ed
--- /dev/null
+++ b/db/migrate/20180707193142_migrate_filters.rb
@@ -0,0 +1,54 @@
+class MigrateFilters < ActiveRecord::Migration[5.2]
+  class GlitchKeywordMute < ApplicationRecord
+    # Dummy class, as we removed Glitch::KeywordMute
+    belongs_to :account, required: true
+    validates_presence_of :keyword
+  end
+
+  class CustomFilter < ApplicationRecord
+    # Dummy class, in case CustomFilter gets altered in the future
+    belongs_to :account
+    validates :phrase, :context, presence: true
+
+    before_validation :clean_up_contexts
+
+    private
+
+    def clean_up_contexts
+      self.context = Array(context).map(&:strip).map(&:presence).compact
+    end
+  end
+
+  disable_ddl_transaction!
+
+  def up
+    GlitchKeywordMute.find_each do |filter|
+      filter.account.custom_filters.create!(
+        phrase: filter.keyword,
+        context: filter.apply_to_mentions ? %w(home public notifications) : %w(home public),
+        whole_word: filter.whole_word,
+        irreversible: true)
+    end
+
+    drop_table :glitch_keyword_mutes
+  end
+
+  def down
+    create_table "glitch_keyword_mutes" do |t|
+      t.references :account, null: false
+      t.string :keyword, null: false
+      t.boolean :whole_word, default: true, null: false
+      t.boolean :apply_to_mentions, default: true, null: false
+      t.timestamps
+    end
+
+    add_foreign_key :glitch_keyword_mutes, :accounts, on_delete: :cascade
+
+    CustomFilter.where(irreversible: true).find_each do |filter|
+      GlitchKeywordMute.where(account: filter.account).create!(
+        keyword: filter.phrase,
+        whole_word: filter.whole_word,
+        apply_to_mentions: filter.context.include?('notifications'))
+    end
+  end
+end