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)
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class CreateScheduledExpirationStatuses < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
create_table :scheduled_expiration_statuses do |t|
|
||||
t.belongs_to :account, foreign_key: { on_delete: :cascade }
|
||||
t.belongs_to :status, null: false, foreign_key: { on_delete: :cascade }
|
||||
t.datetime :scheduled_at, index: true
|
||||
|
||||
t.datetime :created_at, null: false
|
||||
t.datetime :updated_at, null: false
|
||||
end
|
||||
end
|
||||
end
|
15
db/schema.rb
15
db/schema.rb
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2023_03_14_121142) do
|
||||
ActiveRecord::Schema.define(version: 2023_03_20_234918) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
@ -844,6 +844,17 @@ ActiveRecord::Schema.define(version: 2023_03_14_121142) do
|
|||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
create_table "scheduled_expiration_statuses", force: :cascade do |t|
|
||||
t.bigint "account_id"
|
||||
t.bigint "status_id", null: false
|
||||
t.datetime "scheduled_at"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.index ["account_id"], name: "index_scheduled_expiration_statuses_on_account_id"
|
||||
t.index ["scheduled_at"], name: "index_scheduled_expiration_statuses_on_scheduled_at"
|
||||
t.index ["status_id"], name: "index_scheduled_expiration_statuses_on_status_id"
|
||||
end
|
||||
|
||||
create_table "scheduled_statuses", force: :cascade do |t|
|
||||
t.bigint "account_id"
|
||||
t.datetime "scheduled_at"
|
||||
|
@ -1222,6 +1233,8 @@ ActiveRecord::Schema.define(version: 2023_03_14_121142) do
|
|||
add_foreign_key "reports", "accounts", column: "assigned_account_id", on_delete: :nullify
|
||||
add_foreign_key "reports", "accounts", column: "target_account_id", name: "fk_eb37af34f0", on_delete: :cascade
|
||||
add_foreign_key "reports", "accounts", name: "fk_4b81f7522c", on_delete: :cascade
|
||||
add_foreign_key "scheduled_expiration_statuses", "accounts", on_delete: :cascade
|
||||
add_foreign_key "scheduled_expiration_statuses", "statuses", on_delete: :cascade
|
||||
add_foreign_key "scheduled_statuses", "accounts", on_delete: :cascade
|
||||
add_foreign_key "session_activations", "oauth_access_tokens", column: "access_token_id", name: "fk_957e5bda89", on_delete: :cascade
|
||||
add_foreign_key "session_activations", "users", name: "fk_e5fda67334", on_delete: :cascade
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue