Repository navigation
Make the event txnId mapping rely on the device_id instead of the access_token_id.
#13083
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Make the event `txnId` mapping rely on the `device_id` instead of the `access_token_id`. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -199,7 +199,8 @@ def __init__(self, internal_metadata_dict: JsonDict): | |
| proactively_send: DictProperty[bool] = DictProperty("proactively_send") | ||
| redacted: DictProperty[bool] = DictProperty("redacted") | ||
| txn_id: DictProperty[str] = DictProperty("txn_id") | ||
| token_id: DictProperty[int] = DictProperty("token_id") | ||
| device_id: DictProperty[str] = DictProperty("device_id") | ||
| token_id: DictProperty[Optional[int]] = DefaultDictProperty("token_id", None) | ||
|
Comment on lines
+202
to
+203
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we use a |
||
| historical: DictProperty[bool] = DictProperty("historical") | ||
|
|
||
| # XXX: These are set by StreamWorkerStore._set_before_and_after. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -317,8 +317,8 @@ class SerializeEventConfig: | |
| as_client_event: bool = True | ||
| # Function to convert from federation format to client format | ||
| event_format: Callable[[JsonDict], JsonDict] = format_event_for_client_v1 | ||
| # ID of the user's auth token - used for namespacing of transaction IDs | ||
| token_id: Optional[int] = None | ||
| # The user's device ID which did that request - used for namespacing of transaction IDs | ||
| device_id: Optional[str] = None | ||
| # List of event fields to include. If empty, all fields will be returned. | ||
| only_event_fields: Optional[List[str]] = None | ||
| # Some events can have stripped room state stored in the `unsigned` field. | ||
|
|
@@ -368,11 +368,12 @@ def serialize_event( | |
| e.unsigned["redacted_because"], time_now_ms, config=config | ||
| ) | ||
|
|
||
| if config.token_id is not None: | ||
| if config.token_id == getattr(e.internal_metadata, "token_id", None): | ||
| txn_id = getattr(e.internal_metadata, "txn_id", None) | ||
| if txn_id is not None: | ||
| d["unsigned"]["transaction_id"] = txn_id | ||
| if config.device_id is not None and config.device_id == getattr( | ||
| e.internal_metadata, "device_id", None | ||
| ): | ||
|
Comment on lines
+371
to
+373
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comments here would be helpful. Why do we need to (just because the code was under-commented before doesn't mean you need to maintain that! What is this block actually doing?) |
||
| txn_id = getattr(e.internal_metadata, "txn_id", None) | ||
| if txn_id is not None: | ||
| d["unsigned"]["transaction_id"] = txn_id | ||
|
|
||
| # invite_room_state and knock_room_state are a list of stripped room state events | ||
| # that are meant to provide metadata about a room to an invitee/knocker. They are | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||
| /* Copyright 2022 The Matrix.org Foundation C.I.C | ||||
| * | ||||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| * you may not use this file except in compliance with the License. | ||||
| * You may obtain a copy of the License at | ||||
| * | ||||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||||
| * | ||||
| * Unless required by applicable law or agreed to in writing, software | ||||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| * See the License for the specific language governing permissions and | ||||
| * limitations under the License. | ||||
| */ | ||||
|
|
||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more comments please. What is this delta doing? why? (see
-- comments rather than a /* one.
|
||||
| ALTER TABLE event_txn_id | ||||
| ADD COLUMN device_id TEXT; | ||||
|
|
||||
| UPDATE event_txn_id | ||||
| SET device_id = access_tokens.device_id | ||||
| FROM access_tokens | ||||
| WHERE event_txn_id.token_id = access_tokens.id; | ||||
|
|
||||
| DELETE FROM event_txn_id | ||||
| WHERE device_id IS NULL; | ||||
|
|
||||
| DROP INDEX event_txn_id_txn_id; | ||||
| CREATE UNIQUE INDEX event_txn_id_txn_id ON event_txn_id (room_id, user_id, device_id, txn_id); | ||||
| -- Keep this (non-unique) index in case we're rolling back | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, what does "in case we're rolling back" mean? |
||||
| CREATE INDEX event_txn_id_txn_id_token_id ON event_txn_id (room_id, user_id, token_id, txn_id); | ||||
|
|
||||
| -- Make the device_id NOT NULL and remove the NOT NULL constraint from the token_id | ||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we have a foreign key constraint on |
||||
| ALTER TABLE event_txn_id | ||||
| ALTER COLUMN device_id SET NOT NULL, | ||||
| ALTER COLUMN token_id DROP NOT NULL; | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* Copyright 2022 The Matrix.org Foundation C.I.C | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| CREATE TABLE event_txn_id2 ( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we seem to be missing the foreign key constraints here? |
||
| event_id TEXT NOT NULL REFERENCES events (event_id) ON DELETE CASCADE, | ||
| room_id TEXT NOT NULL, | ||
| user_id TEXT NOT NULL, | ||
| device_id TEXT NOT NULL, | ||
| token_id TEXT, -- Keep this field in case we're rolling back | ||
| txn_id TEXT NOT NULL, | ||
| inserted_ts BIGINT NOT NULL | ||
| ); | ||
|
|
||
| DROP INDEX event_txn_id_event_id; | ||
| DROP INDEX event_txn_id_txn_id; | ||
| DROP INDEX event_txn_id_ts; | ||
|
Comment on lines
+26
to
+28
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this will happen automatically when you drop the table? |
||
|
|
||
| CREATE UNIQUE INDEX event_txn_id_event_id ON event_txn_id2(event_id); | ||
| CREATE UNIQUE INDEX event_txn_id_txn_id ON event_txn_id2(room_id, user_id, device_id, txn_id); | ||
| -- Keep this index in case we're rolling back | ||
| CREATE INDEX event_txn_id_txn_id_token_id ON event_txn_id2(room_id, user_id, token_id, txn_id); | ||
|
Comment on lines
+30
to
+33
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's normally quicker to create the index once all the inserts are done. |
||
|
|
||
| INSERT INTO event_txn_id2 | ||
| SELECT | ||
| event_txn_id.event_id, | ||
| event_txn_id.room_id, | ||
| event_txn_id.user_id, | ||
| event_txn_id.token_id, | ||
| access_tokens.device_id, | ||
| event_txn_id.txn_id, | ||
| event_txn_id.inserted_ts | ||
| FROM event_txn_id | ||
| INNER JOIN access_tokens | ||
| ON access_tokens.id = event_txn_id.token_id | ||
| WHERE device_id IS NOT NULL; | ||
|
|
||
| CREATE INDEX event_txn_id_ts ON event_txn_id2(inserted_ts); | ||
|
|
||
| DROP TABLE event_txn_id; | ||
|
|
||
| ALTER TABLE event_txn_id2 RENAME TO event_txn_id; | ||
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.
comments please. What do these fields mean? What does it mean for them to be absent, or None?