Add status expiration
This commit is contained in:
parent
57c12e4fad
commit
16079b4db5
11 changed files with 120 additions and 1 deletions
|
@ -19,6 +19,7 @@ module AccountAssociations
|
|||
has_many :notifications, inverse_of: :account, dependent: :destroy
|
||||
has_many :conversations, class_name: 'AccountConversation', dependent: :destroy, inverse_of: :account
|
||||
has_many :scheduled_statuses, inverse_of: :account, dependent: :destroy
|
||||
has_many :scheduled_expiration_statuses, inverse_of: :account, dependent: :destroy
|
||||
|
||||
# Pinned statuses
|
||||
has_many :status_pins, inverse_of: :account, dependent: :destroy
|
||||
|
|
36
app/models/scheduled_expiration_status.rb
Normal file
36
app/models/scheduled_expiration_status.rb
Normal file
|
@ -0,0 +1,36 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: scheduled_expiration_statuses
|
||||
#
|
||||
# id :bigint(8) not null, primary key
|
||||
# account_id :bigint(8)
|
||||
# status_id :bigint(8) not null
|
||||
# scheduled_at :datetime
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
|
||||
class ScheduledExpirationStatus < ApplicationRecord
|
||||
include Paginable
|
||||
|
||||
TOTAL_LIMIT = 300
|
||||
DAILY_LIMIT = 25
|
||||
|
||||
belongs_to :account, inverse_of: :scheduled_expiration_statuses
|
||||
belongs_to :status, inverse_of: :scheduled_expiration_status
|
||||
|
||||
validate :validate_total_limit
|
||||
validate :validate_daily_limit
|
||||
|
||||
private
|
||||
|
||||
def validate_total_limit
|
||||
errors.add(:base, I18n.t('scheduled_expiration_statuses.over_total_limit', limit: TOTAL_LIMIT)) if account.scheduled_expiration_statuses.count >= TOTAL_LIMIT
|
||||
end
|
||||
|
||||
def validate_daily_limit
|
||||
errors.add(:base, I18n.t('scheduled_expiration_statuses.over_daily_limit', limit: DAILY_LIMIT)) if account.scheduled_expiration_statuses.where('scheduled_at::date = ?::date', scheduled_at).count >= DAILY_LIMIT
|
||||
end
|
||||
end
|
|
@ -81,6 +81,7 @@ class Status < ApplicationRecord
|
|||
has_one :status_stat, inverse_of: :status
|
||||
has_one :poll, inverse_of: :status, dependent: :destroy
|
||||
has_one :trend, class_name: 'StatusTrend', inverse_of: :status
|
||||
has_one :scheduled_expiration_status, inverse_of: :status, dependent: :destroy
|
||||
|
||||
validates :uri, uniqueness: true, presence: true, unless: :local?
|
||||
validates :text, presence: true, unless: -> { with_media? || reblog? }
|
||||
|
|
|
@ -26,6 +26,7 @@ class DeleteAccountService < BaseService
|
|||
passive_relationships
|
||||
report_notes
|
||||
scheduled_statuses
|
||||
scheduled_expiration_statuses
|
||||
status_pins
|
||||
).freeze
|
||||
|
||||
|
@ -51,6 +52,7 @@ class DeleteAccountService < BaseService
|
|||
notifications
|
||||
owned_lists
|
||||
scheduled_statuses
|
||||
scheduled_expiration_statuses
|
||||
status_pins
|
||||
)
|
||||
|
||||
|
|
|
@ -116,6 +116,7 @@ class PostStatusService < BaseService
|
|||
end
|
||||
|
||||
def postprocess_status!
|
||||
UpdateStatusExpirationService.new.call(@status)
|
||||
process_hashtags_service.call(@status)
|
||||
Trends.tags.register(@status)
|
||||
LinkCrawlWorker.perform_async(@status.id)
|
||||
|
|
19
app/services/update_status_expiration_service.rb
Normal file
19
app/services/update_status_expiration_service.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class UpdateStatusExpirationService < BaseService
|
||||
SCAN_EXPIRATION_RE = /#exp((\d.\d|\d)+)([dms]+)/
|
||||
|
||||
def call(status)
|
||||
existing_expiration = ScheduledExpirationStatus.find_by(status: status)
|
||||
existing_expiration.destroy! if existing_expiration
|
||||
|
||||
expiration = status.text.scan(SCAN_EXPIRATION_RE).first
|
||||
return if !expiration
|
||||
|
||||
expiration_num = expiration[0].to_f
|
||||
expiration_option = expiration[1]
|
||||
|
||||
expired_at = Time.now.utc + (expiration_option == 'd' ? expiration_num.days : expiration_option == 's' ? expiration_num.seconds : expiration_num.minutes)
|
||||
ScheduledExpirationStatus.create!(account: status.account, status: status, scheduled_at: expired_at)
|
||||
end
|
||||
end
|
|
@ -30,6 +30,7 @@ class UpdateStatusService < BaseService
|
|||
update_media_attachments! if @options.key?(:media_ids)
|
||||
update_poll! if @options.key?(:poll)
|
||||
update_immediate_attributes!
|
||||
update_expiration!
|
||||
create_edit! unless @options[:no_history]
|
||||
end
|
||||
|
||||
|
@ -122,6 +123,10 @@ class UpdateStatusService < BaseService
|
|||
@status.save!
|
||||
end
|
||||
|
||||
def update_expiration!
|
||||
UpdateStatusExpirationService.new.call(@status)
|
||||
end
|
||||
|
||||
def reset_preview_card!
|
||||
return unless @status.text_previously_changed?
|
||||
|
||||
|
|
16
app/workers/remove_expired_status_worker.rb
Normal file
16
app/workers/remove_expired_status_worker.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RemoveExpiredStatusWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options lock: :until_executed
|
||||
|
||||
def perform(scheduled_expiration_status_id)
|
||||
scheduled_expiration_status = ScheduledExpirationStatus.find(scheduled_expiration_status_id)
|
||||
scheduled_expiration_status.destroy!
|
||||
|
||||
RemoveStatusService.new.call(scheduled_expiration_status.status)
|
||||
rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
|
||||
true
|
||||
end
|
||||
end
|
|
@ -7,6 +7,7 @@ class Scheduler::ScheduledStatusesScheduler
|
|||
|
||||
def perform
|
||||
publish_scheduled_statuses!
|
||||
unpublish_expired_statuses!
|
||||
publish_scheduled_announcements!
|
||||
unpublish_expired_announcements!
|
||||
end
|
||||
|
@ -19,10 +20,20 @@ class Scheduler::ScheduledStatusesScheduler
|
|||
end
|
||||
end
|
||||
|
||||
def unpublish_expired_statuses!
|
||||
expired_statuses.find_each do |expired_status|
|
||||
RemoveExpiredStatusWorker.perform_at(expired_status.scheduled_at, expired_status.id)
|
||||
end
|
||||
end
|
||||
|
||||
def due_statuses
|
||||
ScheduledStatus.where('scheduled_at <= ?', Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET)
|
||||
end
|
||||
|
||||
def expired_statuses
|
||||
ScheduledExpirationStatus.where('scheduled_at <= ?', Time.now.utc + PostStatusService::MIN_SCHEDULE_OFFSET)
|
||||
end
|
||||
|
||||
def publish_scheduled_announcements!
|
||||
due_announcements.find_each do |announcement|
|
||||
PublishScheduledAnnouncementWorker.perform_at(announcement.scheduled_at, announcement.id)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue