From af8a1309bde2a85bd94d06f3f152ae6355677095 Mon Sep 17 00:00:00 2001 From: Fire Demon Date: Fri, 17 Jul 2020 19:56:35 -0500 Subject: [Feature] Introduce nest_level attribute to posts and API to help apps visualize nesting in threads --- app/models/status.rb | 12 +++++++++++ app/serializers/rest/status_serializer.rb | 2 +- .../20200717014609_add_nest_level_to_statuses.rb | 7 +++++++ .../20200717015413_backfill_status_nest_level.rb | 24 ++++++++++++++++++++++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20200717014609_add_nest_level_to_statuses.rb create mode 100644 db/migrate/20200717015413_backfill_status_nest_level.rb diff --git a/app/models/status.rb b/app/models/status.rb index 731bb6eee..bee7d1e67 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -26,6 +26,7 @@ # content_type :string # deleted_at :datetime # edited :integer default(0), not null +# nest_level :integer default(0), not null # class Status < ApplicationRecord @@ -272,6 +273,7 @@ class Status < ApplicationRecord around_create Mastodon::Snowflake::Callbacks before_create :set_locality + before_create :set_nest_level before_validation :prepare_contents, if: :local? before_validation :set_reblog @@ -546,6 +548,16 @@ class Status < ApplicationRecord self.local = account.local? end + def set_nest_level + return if attribute_changed?(:nest_level) + + if reply? + self.nest_level = [thread&.account_id == account_id ? thread&.nest_level.to_i : thread&.nest_level.to_i + 1, 127].min + else + self.nest_level = 0 + end + end + def update_statistics return unless distributable? diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index 26748f683..6baa20ca9 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -4,7 +4,7 @@ class REST::StatusSerializer < ActiveModel::Serializer attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id, :sensitive, :spoiler_text, :visibility, :language, :uri, :url, :replies_count, :reblogs_count, - :favourites_count + :favourites_count, :nest_level # Monsterfork additions attributes :updated_at, :edited 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 -- cgit