@ -15,8 +15,6 @@
# visibility :integer default("public"), not null
# spoiler_text :text default(""), not null
# reply :boolean default(FALSE), not null
# favourites_count :integer default(0), not null
# reblogs_count :integer default(0), not null
# language :string
# conversation_id :bigint(8)
# local :boolean
@ -28,6 +26,8 @@
#
class Status < ApplicationRecord
self . cache_versioning = false
include Paginable
include Streamable
include Cacheable
@ -62,6 +62,7 @@ class Status < ApplicationRecord
has_one :notification , as : :activity , dependent : :destroy
has_one :stream_entry , as : :activity , inverse_of : :status
has_one :status_stat , inverse_of : :status
validates :uri , uniqueness : true , presence : true , unless : :local?
validates :text , presence : true , unless : - > { with_media? || reblog? }
@ -86,7 +87,25 @@ class Status < ApplicationRecord
scope :not_local_only , - > { where ( local_only : [ false , nil ] ) }
cache_associated :account , :application , :media_attachments , :conversation , :tags , :stream_entry , mentions : :account , reblog : [ :account , :application , :stream_entry , :tags , :media_attachments , :conversation , mentions : :account ] , thread : :account
cache_associated :account ,
:application ,
:media_attachments ,
:conversation ,
:status_stat ,
:tags ,
:stream_entry ,
mentions : :account ,
reblog : [
:account ,
:application ,
:stream_entry ,
:tags ,
:media_attachments ,
:conversation ,
:status_stat ,
mentions : :account ,
] ,
thread : :account
delegate :domain , to : :account , prefix : true
@ -180,6 +199,26 @@ class Status < ApplicationRecord
@marked_for_mass_destruction
end
def replies_count
status_stat & . replies_count || 0
end
def reblogs_count
status_stat & . reblogs_count || 0
end
def favourites_count
status_stat & . favourites_count || 0
end
def increment_count! ( key )
update_status_stat! ( key = > public_send ( key ) + 1 )
end
def decrement_count! ( key )
update_status_stat! ( key = > [ public_send ( key ) - 1 , 0 ] . max )
end
after_create :increment_counter_caches
after_destroy :decrement_counter_caches
@ -197,6 +236,10 @@ class Status < ApplicationRecord
before_validation :set_local
class << self
def cache_ids
left_outer_joins ( :status_stat ) . select ( 'statuses.id, greatest(statuses.updated_at, status_stats.updated_at) AS updated_at' )
end
def in_chosen_languages ( account )
where ( language : nil ) . or where ( language : account . chosen_languages )
end
@ -372,6 +415,11 @@ class Status < ApplicationRecord
private
def update_status_stat! ( attrs )
record = status_stat || build_status_stat
record . update ( attrs )
end
def store_uri
update_attribute ( :uri , ActivityPub :: TagManager . instance . uri_for ( self ) ) if uri . nil?
end
@ -434,13 +482,8 @@ class Status < ApplicationRecord
Account . where ( id : account_id ) . update_all ( 'statuses_count = COALESCE(statuses_count, 0) + 1' )
end
return unless reblog?
if association ( :reblog ) . loaded?
reblog . update_attribute ( :reblogs_count , reblog . reblogs_count + 1 )
else
Status . where ( id : reblog_of_id ) . update_all ( 'reblogs_count = COALESCE(reblogs_count, 0) + 1' )
end
reblog . increment_count! ( :reblogs_count ) if reblog?
thread . increment_count! ( :replies_count ) if in_reply_to_id . present? && ( public_visibility? || unlisted_visibility? )
end
def decrement_counter_caches
@ -452,12 +495,7 @@ class Status < ApplicationRecord
Account . where ( id : account_id ) . update_all ( 'statuses_count = GREATEST(COALESCE(statuses_count, 0) - 1, 0)' )
end
return unless reblog?
if association ( :reblog ) . loaded?
reblog . update_attribute ( :reblogs_count , [ reblog . reblogs_count - 1 , 0 ] . max )
else
Status . where ( id : reblog_of_id ) . update_all ( 'reblogs_count = GREATEST(COALESCE(reblogs_count, 0) - 1, 0)' )
end
reblog . decrement_count! ( :reblogs_count ) if reblog?
thread . decrement_count! ( :replies_count ) if in_reply_to_id . present? && ( public_visibility? || unlisted_visibility? )
end
end