about summary refs log tree commit diff
path: root/db
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-07-17 19:56:35 -0500
committerFire Demon <firedemon@creature.cafe>2020-08-30 05:41:03 -0500
commitaf8a1309bde2a85bd94d06f3f152ae6355677095 (patch)
tree158298011141f9c586c75da60c0298fcf3764067 /db
parent120683f52266900d0fede7bd67c43133bbc26bea (diff)
[Feature] Introduce nest_level attribute to posts and API to help apps visualize nesting in threads
Diffstat (limited to 'db')
-rw-r--r--db/migrate/20200717014609_add_nest_level_to_statuses.rb7
-rw-r--r--db/migrate/20200717015413_backfill_status_nest_level.rb24
2 files changed, 31 insertions, 0 deletions
diff --git a/db/migrate/20200717014609_add_nest_level_to_statuses.rb b/db/migrate/20200717014609_add_nest_level_to_statuses.rb
new file mode 100644
index 000000000..0b2196ad6
--- /dev/null
+++ b/db/migrate/20200717014609_add_nest_level_to_statuses.rb
@@ -0,0 +1,7 @@
+class AddNestLevelToStatuses < ActiveRecord::Migration[5.2]
+  def change
+    safety_assured do
+      add_column :statuses, :nest_level, :integer, limit: 1, null: false, default: 0
+    end
+  end
+end
diff --git a/db/migrate/20200717015413_backfill_status_nest_level.rb b/db/migrate/20200717015413_backfill_status_nest_level.rb
new file mode 100644
index 000000000..1f37ef847
--- /dev/null
+++ b/db/migrate/20200717015413_backfill_status_nest_level.rb
@@ -0,0 +1,24 @@
+class BackfillStatusNestLevel < ActiveRecord::Migration[5.2]
+  disable_ddl_transaction!
+
+  def up
+    Rails.logger.info("Populating nest levels for orphaned replies...")
+    Status.select(:id, :account_id).where(reply: true, in_reply_to_id: nil).reorder(nil).in_batches.update_all(nest_level: 1)
+
+    count = 1.0
+    total = Conversation.count
+
+    Conversation.reorder('conversations.id DESC').find_each do |conversation|
+      Rails.logger.info("(#{(count/total*100).to_i}%) Populating nest levels for threads...")
+      conversation.statuses.where(reply: true).reorder('statuses.id ASC').find_each do |status|
+        level = [status.thread&.account_id == status.account_id ? status.thread&.nest_level.to_i : status.thread&.nest_level.to_i + 1, 127].min
+        status.update(nest_level: level) if level != status.nest_level
+      end
+      count += 1
+    end
+  end
+
+  def down
+    true
+  end
+end