Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
mastodon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Pierre Boudes
mastodon
Commits
0b249ebd
Unverified
Commit
0b249ebd
authored
1 year ago
by
Matt Jankowski
Committed by
GitHub
1 year ago
Browse files
Options
Downloads
Patches
Plain Diff
Extract StatusSafeReblogInsert concern from Status (#24821)
parent
830e6cef
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.rubocop_todo.yml
+6
-0
6 additions, 0 deletions
.rubocop_todo.yml
app/models/concerns/status_safe_reblog_insert.rb
+72
-0
72 additions, 0 deletions
app/models/concerns/status_safe_reblog_insert.rb
app/models/status.rb
+1
-65
1 addition, 65 deletions
app/models/status.rb
with
79 additions
and
65 deletions
.rubocop_todo.yml
+
6
−
0
View file @
0b249ebd
...
...
@@ -192,6 +192,12 @@ Lint/Void:
Metrics/AbcSize
:
Max
:
150
# Configuration parameters: CountComments, Max, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
# AllowedMethods: refine
Metrics/BlockLength
:
Exclude
:
-
'
app/models/concerns/status_safe_reblog_insert.rb'
# Configuration parameters: CountBlocks, Max.
Metrics/BlockNesting
:
Exclude
:
...
...
This diff is collapsed.
Click to expand it.
app/models/concerns/status_safe_reblog_insert.rb
0 → 100644
+
72
−
0
View file @
0b249ebd
# frozen_string_literal: true
module
StatusSafeReblogInsert
extend
ActiveSupport
::
Concern
class_methods
do
# This is a hack to ensure that no reblogs of discarded statuses are created,
# as this cannot be enforced through database constraints the same way we do
# for reblogs of deleted statuses.
#
# To achieve this, we redefine the internal method responsible for issuing
# the "INSERT" statement and replace the "INSERT INTO ... VALUES ..." query
# with an "INSERT INTO ... SELECT ..." query with a "WHERE deleted_at IS NULL"
# clause on the reblogged status to ensure consistency at the database level.
#
# Otherwise, the code is kept as close as possible to ActiveRecord::Persistence
# code, and actually calls it if we are not handling a reblog.
def
_insert_record
(
values
)
return
super
unless
values
.
is_a?
(
Hash
)
&&
values
[
'reblog_of_id'
].
present?
primary_key
=
self
.
primary_key
primary_key_value
=
nil
if
primary_key
primary_key_value
=
values
[
primary_key
]
if
!
primary_key_value
&&
prefetch_primary_key?
primary_key_value
=
next_sequence_value
values
[
primary_key
]
=
primary_key_value
end
end
# The following line is where we differ from stock ActiveRecord implementation
im
=
_compile_reblog_insert
(
values
)
# Since we are using SELECT instead of VALUES, a non-error `nil` return is possible.
# For our purposes, it's equivalent to a foreign key constraint violation
result
=
connection
.
insert
(
im
,
"
#{
self
}
Create"
,
primary_key
||
false
,
primary_key_value
)
raise
ActiveRecord
::
InvalidForeignKey
,
"(reblog_of_id)=(
#{
values
[
'reblog_of_id'
]
}
) is not present in table
\"
statuses
\"
"
if
result
.
nil?
result
end
def
_compile_reblog_insert
(
values
)
# This is somewhat equivalent to the following code of ActiveRecord::Persistence:
# `arel_table.compile_insert(_substitute_values(values))`
# The main difference is that we use a `SELECT` instead of a `VALUES` clause,
# which means we have to build the `SELECT` clause ourselves and do a bit more
# manual work.
# Instead of using Arel::InsertManager#values, we are going to use Arel::InsertManager#select
im
=
Arel
::
InsertManager
.
new
im
.
into
(
arel_table
)
binds
=
[]
reblog_bind
=
nil
values
.
each
do
|
name
,
value
|
attr
=
arel_table
[
name
]
bind
=
predicate_builder
.
build_bind_attribute
(
attr
.
name
,
value
)
im
.
columns
<<
attr
binds
<<
bind
reblog_bind
=
bind
if
name
==
'reblog_of_id'
end
im
.
select
(
arel_table
.
where
(
arel_table
[
:id
].
eq
(
reblog_bind
)).
where
(
arel_table
[
:deleted_at
].
eq
(
nil
)).
project
(
*
binds
))
im
end
end
end
This diff is collapsed.
Click to expand it.
app/models/status.rb
+
1
−
65
View file @
0b249ebd
...
...
@@ -36,6 +36,7 @@ class Status < ApplicationRecord
include
StatusThreadingConcern
include
StatusSnapshotConcern
include
RateLimitable
include
StatusSafeReblogInsert
rate_limit
by: :account
,
family: :statuses
...
...
@@ -393,71 +394,6 @@ class Status < ApplicationRecord
super
||
build_status_stat
end
# This is a hack to ensure that no reblogs of discarded statuses are created,
# as this cannot be enforced through database constraints the same way we do
# for reblogs of deleted statuses.
#
# To achieve this, we redefine the internal method responsible for issuing
# the "INSERT" statement and replace the "INSERT INTO ... VALUES ..." query
# with an "INSERT INTO ... SELECT ..." query with a "WHERE deleted_at IS NULL"
# clause on the reblogged status to ensure consistency at the database level.
#
# Otherwise, the code is kept as close as possible to ActiveRecord::Persistence
# code, and actually calls it if we are not handling a reblog.
def
self
.
_insert_record
(
values
)
return
super
unless
values
.
is_a?
(
Hash
)
&&
values
[
'reblog_of_id'
].
present?
primary_key
=
self
.
primary_key
primary_key_value
=
nil
if
primary_key
primary_key_value
=
values
[
primary_key
]
if
!
primary_key_value
&&
prefetch_primary_key?
primary_key_value
=
next_sequence_value
values
[
primary_key
]
=
primary_key_value
end
end
# The following line is where we differ from stock ActiveRecord implementation
im
=
_compile_reblog_insert
(
values
)
# Since we are using SELECT instead of VALUES, a non-error `nil` return is possible.
# For our purposes, it's equivalent to a foreign key constraint violation
result
=
connection
.
insert
(
im
,
"
#{
self
}
Create"
,
primary_key
||
false
,
primary_key_value
)
raise
ActiveRecord
::
InvalidForeignKey
,
"(reblog_of_id)=(
#{
values
[
'reblog_of_id'
]
}
) is not present in table
\"
statuses
\"
"
if
result
.
nil?
result
end
def
self
.
_compile_reblog_insert
(
values
)
# This is somewhat equivalent to the following code of ActiveRecord::Persistence:
# `arel_table.compile_insert(_substitute_values(values))`
# The main difference is that we use a `SELECT` instead of a `VALUES` clause,
# which means we have to build the `SELECT` clause ourselves and do a bit more
# manual work.
# Instead of using Arel::InsertManager#values, we are going to use Arel::InsertManager#select
im
=
Arel
::
InsertManager
.
new
im
.
into
(
arel_table
)
binds
=
[]
reblog_bind
=
nil
values
.
each
do
|
name
,
value
|
attr
=
arel_table
[
name
]
bind
=
predicate_builder
.
build_bind_attribute
(
attr
.
name
,
value
)
im
.
columns
<<
attr
binds
<<
bind
reblog_bind
=
bind
if
name
==
'reblog_of_id'
end
im
.
select
(
arel_table
.
where
(
arel_table
[
:id
].
eq
(
reblog_bind
)).
where
(
arel_table
[
:deleted_at
].
eq
(
nil
)).
project
(
*
binds
))
im
end
def
discard_with_reblogs
discard_time
=
Time
.
current
Status
.
unscoped
.
where
(
reblog_of_id:
id
,
deleted_at:
[
nil
,
deleted_at
]).
in_batches
.
update_all
(
deleted_at:
discard_time
)
unless
reblog?
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment