-
Couldn't load subscription status.
- Fork 405
Add support for MSC4293 - Redact on Kick/Ban #18540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0766119
add support for MSC4293
H-Shay ce555e7
tests
H-Shay 8c606cd
fix msc number typo
H-Shay e8d328f
add indexes in background
H-Shay b6aa6c0
newsfragment + run indexes in background
H-Shay e4a1014
lint
H-Shay 5ad9e66
ensure redact flag is respected when using /kick and /ban
H-Shay 656b648
remove debugging artifact
H-Shay 5a53b93
requested changes
H-Shay 80e0979
Merge branch 'develop' into shay/redact_on_ban
anoadragon453 4323879
requested changes
H-Shay 65d7b41
change table architecture
H-Shay 8d7eddd
update code to reflect new table architecture
H-Shay 17ce194
fix column type
H-Shay af6ca26
fix other column type
H-Shay bf8d9c2
invalidate cache by room id
H-Shay c06ab2e
requested changes
H-Shay 7c1cac6
Merge branch 'develop' into shay/redact_on_ban
H-Shay c43a5a3
requested changes
H-Shay 47de415
requested changes
H-Shay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Add support for [MSC4293](https://github.com/matrix-org/matrix-spec-proposals/pull/4293) - Redact on Kick/Ban. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
synapse/storage/schema/main/delta/92/06_redactions_multitarget.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from synapse.storage.database import LoggingTransaction | ||
| from synapse.storage.engines import BaseDatabaseEngine, PostgresEngine | ||
|
|
||
|
|
||
| def run_create( | ||
| cur: LoggingTransaction, | ||
| database_engine: BaseDatabaseEngine, | ||
| ) -> None: | ||
| """ | ||
| Drop unique constraint event_id and add unique constraint (event_id, redacts) | ||
H-Shay marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| if isinstance(database_engine, PostgresEngine): | ||
| add_constraint_sql = """ | ||
| ALTER TABLE ONLY redactions ADD CONSTRAINT redactions_event_id_redacts_key UNIQUE (event_id, redacts); | ||
| """ | ||
| cur.execute(add_constraint_sql) | ||
|
|
||
| drop_constraint_sql = """ | ||
| ALTER TABLE ONLY redactions DROP CONSTRAINT redactions_event_id_key; | ||
| """ | ||
| cur.execute(drop_constraint_sql) | ||
|
|
||
| else: | ||
| # in SQLite we need to rewrite the table to change the constraint. | ||
| # First drop any temporary table that might be here from a previous failed migration. | ||
| cur.execute("DROP TABLE IF EXISTS temp_redactions") | ||
|
|
||
| create_sql = """ | ||
| CREATE TABLE temp_redactions ( | ||
| event_id TEXT NOT NULL, | ||
| redacts TEXT NOT NULL, | ||
| have_censored BOOL NOT NULL DEFAULT false, | ||
| received_ts BIGINT, | ||
| UNIQUE(event_id, redacts) | ||
| ); | ||
| """ | ||
| cur.execute(create_sql) | ||
|
|
||
| copy_sql = """ | ||
| INSERT INTO temp_redactions ( | ||
| event_id, | ||
| redacts, | ||
| have_censored, | ||
| received_ts | ||
| ) SELECT r.event_id, r.redacts, r.have_censored, r.received_ts FROM redactions AS r; | ||
| """ | ||
| cur.execute(copy_sql) | ||
|
|
||
| drop_sql = """ | ||
| DROP TABLE redactions | ||
| """ | ||
| cur.execute(drop_sql) | ||
|
|
||
| rename_sql = """ | ||
| ALTER TABLE temp_redactions RENAME to redactions | ||
| """ | ||
| cur.execute(rename_sql) | ||
|
|
||
| sqlite3_idx_update_sql = """ | ||
| INSERT INTO background_updates (ordering, update_name, progress_json) \ | ||
| VALUES (?, ?, ?); | ||
| """ | ||
| cur.execute(sqlite3_idx_update_sql, (9206, "redactions_add_redacts_idx", "{}")) | ||
| cur.execute( | ||
| sqlite3_idx_update_sql, (9206, "redactions_add_have_censored_ts", "{}") | ||
| ) | ||
|
|
||
| # in either case the event_id index needs to be re-created | ||
| idx_sql = """ | ||
| INSERT INTO background_updates (ordering, update_name, progress_json) VALUES (?, ?, ?); | ||
| """ | ||
| cur.execute(idx_sql, (9206, "redactions_add_event_id_idx", "{}")) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if this is the right architecture for dealing with such redactions. Pulling out all events sent by the user has a few potential issues: a) if there are a lot of events it might take a lot of time, and b) won't redact events that we haven't backfilled over federation yet. The MSC also seems to suggest that events should get unredacted if the ban is replaced with another state event without a redactions?
An alternate method may be to have a separate table that is
room_redactions(room_id, sender), which we then check (as well asredactionstable) when fetching the event? The newroom_redactionswould be updated when we add the ban/kick to thecurrent_state_eventstable, and removed if it gets replaced?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They shouldn't get unredacted, but the auto-redaction should cease for newly received events. I'll try to clarify this in the MSC itself.
An example series of events:
redact_events: trueredact_events: falseThe messages in steps 1 and 4 remain redacted even after step 5.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@erikjohnston Looking at it I think a
room_redactionstable might work (with an addedend_tscolumn or something to indicate the end of a redaction application in the case that the ban is rescinded, etc), but would still need to pull all of the user's event ids (but not full events) out of the db because they are needed to invalidate the cache.I don't have a sense of the time difference to pull event ids out of the database vs pulling the full events so it is unclear to me whether having to still pull the ids will negate the benefit of the new table - right now the PR does both so it is an obvious improvement but is it enough or does another approach need to be found?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't removing the row from the table be enough to stop redacting new events?
Pulling out only event IDs will certainly be much less data than full events, which should contribute directly to time taken executing the query. By how much depends on how large a user's events typically are. But you're right in that we'll still need to do the hard work of querying for every event a user has sent in a room.
The overview of this PR is:
The
redactionstable is updated to change the unique constraint fromevent_idto(event_id, redacts). This is so one event (a kick/ban membership) can redact multiple other events.When a new event comes in over federation, add
redacted_becauseto itsunsignedand add a redaction to the local DB, then invalidate the event cache for that event./banand/kickare updated with aorg.matrix.msc4293.redact_eventsJSON body parameter. If provided, that field is added to thecontentof the ban/kick membership event.When any event is persisted (local or over federation), all event IDs that a user has sent in a room are pulled out and entries in
redactionsare created for them. Each event hasredacted_becauseadded to it. Theget_eventcache is invalidated for each of these events.What's more concerning to me is that the query to lookup all events a user has sent in a room happens is blocking during processing of a membership event. Perhaps that should be moved to a background task?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can invalidate those caches by room as well as by event ID?
We would probably want to do this by some sort of stream ordering or whatever. We might do this as a two step:
or something