This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add the Notifications API #1028
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a24bc5b
Add GET /notifications API
dbkr b791a53
Actually make the 'read' flag correct
dbkr 37b7e84
Include the ts the notif was received at
dbkr b4ecf0b
Merge remote-tracking branch 'origin/develop' into dbkr/notifications…
dbkr 602c84c
Merge remote-tracking branch 'origin/develop' into dbkr/notifications…
dbkr 0acdd0f
Use tuple comparison
dbkr 1e4217c
Explicit join
dbkr 6e80c03
Merge branch 'develop' into dbkr/notifications_api
ara4n 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 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 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,100 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2016 OpenMarket Ltd | ||
# | ||
# 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. | ||
|
||
from twisted.internet import defer | ||
|
||
from synapse.http.servlet import ( | ||
RestServlet, parse_string, parse_integer | ||
) | ||
from synapse.events.utils import ( | ||
serialize_event, format_event_for_client_v2_without_room_id, | ||
) | ||
|
||
from ._base import client_v2_patterns | ||
|
||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class NotificationsServlet(RestServlet): | ||
PATTERNS = client_v2_patterns("/notifications$", releases=()) | ||
|
||
def __init__(self, hs): | ||
super(NotificationsServlet, self).__init__() | ||
self.store = hs.get_datastore() | ||
self.auth = hs.get_auth() | ||
self.clock = hs.get_clock() | ||
|
||
@defer.inlineCallbacks | ||
def on_GET(self, request): | ||
requester = yield self.auth.get_user_by_req(request) | ||
user_id = requester.user.to_string() | ||
|
||
from_token = parse_string(request, "from", required=False) | ||
limit = parse_integer(request, "limit", default=50) | ||
|
||
limit = min(limit, 500) | ||
|
||
push_actions = yield self.store.get_push_actions_for_user( | ||
user_id, from_token, limit | ||
) | ||
|
||
receipts_by_room = yield self.store.get_receipts_for_user_with_orderings( | ||
user_id, 'm.read' | ||
) | ||
|
||
notif_event_ids = [pa["event_id"] for pa in push_actions] | ||
notif_events = yield self.store.get_events(notif_event_ids) | ||
|
||
returned_push_actions = [] | ||
|
||
next_token = None | ||
|
||
for pa in push_actions: | ||
returned_pa = { | ||
"room_id": pa["room_id"], | ||
"profile_tag": pa["profile_tag"], | ||
"actions": pa["actions"], | ||
"ts": pa["received_ts"], | ||
"event": serialize_event( | ||
notif_events[pa["event_id"]], | ||
self.clock.time_msec(), | ||
event_format=format_event_for_client_v2_without_room_id, | ||
), | ||
} | ||
|
||
if pa["room_id"] not in receipts_by_room: | ||
returned_pa["read"] = False | ||
else: | ||
receipt = receipts_by_room[pa["room_id"]] | ||
|
||
returned_pa["read"] = ( | ||
receipt["topological_ordering"] >= pa["topological_ordering"] or ( | ||
receipt["topological_ordering"] == pa["topological_ordering"] and | ||
receipt["stream_ordering"] >= pa["stream_ordering"] | ||
) | ||
) | ||
returned_push_actions.append(returned_pa) | ||
next_token = pa["stream_ordering"] | ||
|
||
defer.returnValue((200, { | ||
"notifications": returned_push_actions, | ||
"next_token": next_token, | ||
})) | ||
|
||
|
||
def register_servlets(hs, http_server): | ||
NotificationsServlet(hs).register(http_server) |
This file contains 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 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 |
---|---|---|
|
@@ -94,6 +94,31 @@ def get_receipts_for_user(self, user_id, receipt_type): | |
|
||
defer.returnValue({row["room_id"]: row["event_id"] for row in rows}) | ||
|
||
@defer.inlineCallbacks | ||
def get_receipts_for_user_with_orderings(self, user_id, receipt_type): | ||
def f(txn): | ||
sql = ( | ||
"SELECT rl.room_id, rl.event_id," | ||
" e.topological_ordering, e.stream_ordering" | ||
" FROM receipts_linearized rl," | ||
" events e" | ||
" WHERE rl.room_id = e.room_id" | ||
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. Generally I prefer to use explicit joins, as it makes it easier to see what the actual conditions are. i.e.: SELECT rl.room_id, rl.event_id, e.topological_ordering, e.stream_ordering FROM receipts_linearized AS rl
INNER JOIN events AS e USING (room_id, event_id)
WHERE user_id = ? |
||
" AND rl.event_id = e.event_id" | ||
" AND user_id = ?" | ||
) | ||
txn.execute(sql, (user_id,)) | ||
return txn.fetchall() | ||
rows = yield self.runInteraction( | ||
"get_receipts_for_user_with_orderings", f | ||
) | ||
defer.returnValue({ | ||
row[0]: { | ||
"event_id": row[1], | ||
"topological_ordering": row[2], | ||
"stream_ordering": row[3], | ||
} for row in rows | ||
}) | ||
|
||
@defer.inlineCallbacks | ||
def get_linearized_receipts_for_rooms(self, room_ids, to_key, from_key=None): | ||
"""Get receipts for multiple rooms for sending to clients. | ||
|
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.
(5, 3) > (5,2)
works as expected