Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Add rate limit for editing (mastodon#17728)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored Mar 9, 2022
1 parent 803f536 commit b2cd344
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/v1/statuses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Api::V1::StatusesController < Api::BaseController
before_action :set_thread, only: [:create]

override_rate_limit_headers :create, family: :statuses
override_rate_limit_headers :update, family: :statuses

# This API was originally unlimited, pagination cannot be introduced without
# breaking backwards-compatibility. Arbitrarily high number to cover most
Expand Down
5 changes: 3 additions & 2 deletions app/models/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def distributable?
public_visibility? || unlisted_visibility?
end

def snapshot!(account_id: nil, at_time: nil)
def snapshot!(account_id: nil, at_time: nil, rate_limit: true)
edits.create!(
text: text,
spoiler_text: spoiler_text,
Expand All @@ -221,7 +221,8 @@ def snapshot!(account_id: nil, at_time: nil)
media_descriptions: ordered_media_attachments.map(&:description),
poll_options: preloadable_poll&.options,
account_id: account_id || self.account_id,
created_at: at_time || edited_at
created_at: at_time || edited_at,
rate_limit: rate_limit
)
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/status_edit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#

class StatusEdit < ApplicationRecord
include RateLimitable

self.ignored_columns = %w(
media_attachments_changed
)
Expand All @@ -26,6 +28,8 @@ class PreservedMediaAttachment < ActiveModelSerializers::Model
delegate :id, :type, :url, :preview_url, :remote_url, :preview_remote_url, :text_url, :meta, :blurhash, to: :media_attachment
end

rate_limit by: :account, family: :statuses

belongs_to :status
belongs_to :account, optional: true

Expand Down
4 changes: 2 additions & 2 deletions app/services/activitypub/process_status_update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ def create_previous_edit!

return if @status.edits.any?

@status.snapshot!(at_time: @status.created_at)
@status.snapshot!(at_time: @status.created_at, rate_limit: false)
end

def create_edit!
return unless significant_changes?

@status.snapshot!(account_id: @account.id)
@status.snapshot!(account_id: @account.id, rate_limit: false)
end

def skip_download?
Expand Down
2 changes: 1 addition & 1 deletion app/services/update_status_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def create_previous_edit!

return if @status.edits.any?

@status.snapshot!(at_time: @status.created_at)
@status.snapshot!(at_time: @status.created_at, rate_limit: false)
end

def create_edit!
Expand Down

0 comments on commit b2cd344

Please sign in to comment.