about summary refs log tree commit diff
path: root/app/models/status.rb
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 /app/models/status.rb
parent120683f52266900d0fede7bd67c43133bbc26bea (diff)
[Feature] Introduce nest_level attribute to posts and API to help apps visualize nesting in threads
Diffstat (limited to 'app/models/status.rb')
-rw-r--r--app/models/status.rb12
1 files changed, 12 insertions, 0 deletions
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?