Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
Implement Read Marker API #2120
Conversation
lukebarnard1
added some commits
Apr 11, 2017
lukebarnard1
assigned
erikjohnston
Apr 11, 2017
| @@ -0,0 +1,91 @@ | ||
| +# -*- coding: utf-8 -*- | ||
| +# Copyright 2015, 2016 OpenMarket Ltd |
| + """ | ||
| + | ||
| + # Get ordering for existing read marker | ||
| + with (yield self.read_marker_linearizer.queue(room_id + "_" + user_id)): |
| + retcols=["topological_ordering", "stream_ordering"], | ||
| + keyvalues={"event_id": event_id}, | ||
| + allow_none=True | ||
| + ) |
erikjohnston
Apr 11, 2017
Owner
This should be a function inside the storage function. Conventionally functions that start with _ are private.
This also should have a desc="..." field so that that metrics work correctly. Usually its just the name of the calling function
| + if existing_to > new_to: | ||
| + should_update = False | ||
| + elif existing_to == new_to and existing_so >= new_so: | ||
| + should_update = False |
erikjohnston
Apr 11, 2017
Owner
I'd move this entire block of code into the storage function and have a dedicated is_event_after function or some such
erikjohnston
changed the title from
Luke/read markers
to
Implement Read Marker API
Apr 11, 2017
erikjohnston
assigned
lukebarnard1
and unassigned
erikjohnston
Apr 11, 2017
|
Also, you upset pep8:
|
lukebarnard1
added some commits
Apr 11, 2017
|
Actually, it turns out that there's a bug if you haven't set a RM for a room previously. In the handler on line 46 it assumes that there is a |
|
Fixed! |
| + the read marker has changed. | ||
| + """ | ||
| + | ||
| + # Get ordering for existing read marker |
| + | ||
| + # Get ordering for existing read marker | ||
| + with (yield self.read_marker_linearizer.queue((room_id, user_id))): | ||
| + account_data = yield self.store.get_account_data_for_room(user_id, room_id) |
erikjohnston
Apr 12, 2017
Owner
We probably should be using a storage function that pulls out based on type too. I think there already is one?
| + | ||
| + existing_read_marker = None | ||
| + if "m.read_marker" in account_data: | ||
| + existing_read_marker = account_data["m.read_marker"] |
erikjohnston
Apr 12, 2017
Owner
This could also be written as:
existing_read_marker = account_data.get("m.read_marker", None)| + user_id, room_id, "m.read_marker", content | ||
| + ) | ||
| + self.notifier.on_new_event( | ||
| + "account_data_key", max_id, users=[user_id], rooms=[room_id] |
erikjohnston
Apr 12, 2017
Owner
We don't need to notify the entire room about this change, just the user.
| + raise SynapseError( | ||
| + 405, | ||
| + "Cannot set m.read_marker through this API. " | ||
| + "Use /rooms/!roomId:server.name/read_marker" |
erikjohnston
Apr 12, 2017
Owner
For future reference we tend to put spaces at the start:
"Cannot set m.read_marker through this API."
" Use /rooms/!roomId:server.name/read_marker"This makes it more obvious that there is at least one space, which is quite important for e.g. SQL
| + | ||
| + def __init__(self, hs): | ||
| + super(ReadMarkerRestServlet, self).__init__() | ||
| + self.hs = hs |
erikjohnston
Apr 12, 2017
Owner
Try to avoid taking a reference to hs if possible, and instead just pull out everything you need. This is for cleanliness and makes it clearer what the dependencies of this class is.
| + body = parse_json_object_from_request(request) | ||
| + | ||
| + if "m.read" in body: | ||
| + read_event_id = body["m.read"] |
erikjohnston
Apr 12, 2017
Owner
I usually do:
read_event_id = body.get("m.read", None)
if read_event_id:
...but its a matter of taste
| + if to_1 > to_2: | ||
| + is_after = False | ||
| + elif to_1 == to_2 and so_1 >= so_2: | ||
| + is_after = False |
erikjohnston
Apr 12, 2017
Owner
I think you can actually write this as:
is_after = (to_1, so_1) > (to_2, so_2)
lukebarnard1
Apr 12, 2017
Contributor
Sadly,
>>> ( 1, 2 ) < ( 1, 2 )
False
>>> ( 1, 2 ) < ( 1, 3 )
TrueSo we'll have to go for the form with two comparisons. is_event_after is now also an accurate name with this change...
|
Just a few nits, nothing particularly important. Feel free to ignore the various style comments depending on your taste |
lukebarnard1
added some commits
Apr 12, 2017
lukebarnard1
assigned
erikjohnston
and unassigned
lukebarnard1
Apr 12, 2017
lukebarnard1
added a commit
to matrix-org/matrix-js-sdk
that referenced
this pull request
Apr 12, 2017
lukebarnard1
referenced this pull request
in matrix-org/matrix-js-sdk
Apr 12, 2017
Merged
Implement API for setting RM #419
| + """ | ||
| + to_1, so_1 = yield self._get_event_ordering(event_id1) | ||
| + to_2, so_2 = yield self._get_event_ordering(event_id2) | ||
| + defer.returnValue(to_1 > to_2 and so_1 > so_2) |
lukebarnard1
Apr 13, 2017
Contributor
in IRL we disproved my lovely boolean algebra; it had an error in it. sads.
|
lgtm |
lukebarnard1 commentedApr 11, 2017
•
edited
This implements the design at https://docs.google.com/document/d/1UWqdS-e1sdwkLDUY0wA4gZyIkRp-ekjsLZ8k6g_Zvso/edit#heading=h.8jqdhkapw60, summarised below.
Update: see #2128 for further alterations. The summary below has been updated.
m.read_marker event
{ "content": { "event_id”: “$123456789098765hhjGslf:matrix.org" }, "room_id": "!someroomid:matrix.org", "type": "m.read_marker" }To update it, the client sends a PUT.
RESTful HTTP endpoint
The REST API hits:
m.read_markerevent.