2017-03-27 09:58:26 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2017 Vector Creations Ltd
|
2019-03-27 05:58:42 -04:00
|
|
|
# Copyright 2019 New Vector Ltd
|
2017-03-27 09:58:26 -04:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
|
|
|
import logging
|
2018-07-09 02:09:20 -04:00
|
|
|
from collections import namedtuple
|
2020-04-17 09:49:55 -04:00
|
|
|
from typing import Any, Awaitable, Callable, Iterable, List, Optional, Tuple
|
2020-01-22 05:37:00 -05:00
|
|
|
|
|
|
|
import attr
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
from synapse.replication.http.streams import ReplicationGetStreamUpdates
|
2020-03-20 10:40:47 -04:00
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-04-23 10:45:12 -04:00
|
|
|
# the number of rows to request from an update_function.
|
|
|
|
_STREAM_UPDATE_TARGET_ROW_COUNT = 100
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
# Some type aliases to make things a bit easier.
|
|
|
|
|
|
|
|
# A stream position token
|
|
|
|
Token = int
|
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
# The type of a stream update row, after JSON deserialisation, but before
|
|
|
|
# parsing with Stream.parse_row (which turns it into a `ROW_TYPE`). Normally it's
|
|
|
|
# just a row from a database query, though this is dependent on the stream in question.
|
|
|
|
#
|
|
|
|
StreamRow = Tuple
|
|
|
|
|
|
|
|
# The type returned by the update_function of a stream, as well as get_updates(),
|
|
|
|
# get_updates_since, etc.
|
|
|
|
#
|
|
|
|
# It consists of a triplet `(updates, new_last_token, limited)`, where:
|
|
|
|
# * `updates` is a list of `(token, row)` entries.
|
|
|
|
# * `new_last_token` is the new position in stream.
|
|
|
|
# * `limited` is whether there are more updates to fetch.
|
|
|
|
#
|
|
|
|
StreamUpdateResult = Tuple[List[Tuple[Token, StreamRow]], Token, bool]
|
|
|
|
|
|
|
|
# The type of an update_function for a stream
|
|
|
|
#
|
|
|
|
# The arguments are:
|
|
|
|
#
|
|
|
|
# * from_token: the previous stream token: the starting point for fetching the
|
|
|
|
# updates
|
|
|
|
# * to_token: the new stream token: the point to get updates up to
|
2020-04-23 10:45:12 -04:00
|
|
|
# * target_row_count: a target for the number of rows to be returned.
|
|
|
|
#
|
|
|
|
# The update_function is expected to return up to _approximately_ target_row_count rows.
|
|
|
|
# If there are more updates available, it should set `limited` in the result, and
|
|
|
|
# it will be called again to get the next batch.
|
2020-04-17 09:49:55 -04:00
|
|
|
#
|
|
|
|
UpdateFunction = Callable[[Token, Token, int], Awaitable[StreamUpdateResult]]
|
2020-03-25 10:54:01 -04:00
|
|
|
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
class Stream(object):
|
|
|
|
"""Base class for the streams.
|
|
|
|
|
|
|
|
Provides a `get_updates()` function that returns new updates since the last
|
2020-03-20 10:40:47 -04:00
|
|
|
time it was called.
|
2017-03-27 09:58:26 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-01-14 09:08:06 -05:00
|
|
|
NAME = None # type: str # The name of the stream
|
|
|
|
# The type of the row. Used by the default impl of parse_row.
|
|
|
|
ROW_TYPE = None # type: Any
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2019-03-27 03:40:32 -04:00
|
|
|
@classmethod
|
2020-04-17 09:49:55 -04:00
|
|
|
def parse_row(cls, row: StreamRow):
|
2019-03-27 03:40:32 -04:00
|
|
|
"""Parse a row received over replication
|
|
|
|
|
|
|
|
By default, assumes that the row data is an array object and passes its contents
|
|
|
|
to the constructor of the ROW_TYPE for this stream.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
row: row data from the incoming RDATA command, after json decoding
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
ROW_TYPE object for this stream
|
|
|
|
"""
|
|
|
|
return cls.ROW_TYPE(*row)
|
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
current_token_function: Callable[[], Token],
|
|
|
|
update_function: UpdateFunction,
|
|
|
|
):
|
|
|
|
"""Instantiate a Stream
|
|
|
|
|
|
|
|
current_token_function and update_function are callbacks which should be
|
|
|
|
implemented by subclasses.
|
|
|
|
|
|
|
|
current_token_function is called to get the current token of the underlying
|
|
|
|
stream.
|
|
|
|
|
|
|
|
update_function is called to get updates for this stream between a pair of
|
|
|
|
stream tokens. See the UpdateFunction type definition for more info.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
current_token_function: callback to get the current token, as above
|
|
|
|
update_function: callback go get stream updates, as above
|
|
|
|
"""
|
|
|
|
self.current_token = current_token_function
|
|
|
|
self.update_function = update_function
|
2020-03-25 10:54:01 -04:00
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
# The token from which we last asked for updates
|
|
|
|
self.last_token = self.current_token()
|
|
|
|
|
2017-04-04 08:19:26 -04:00
|
|
|
def discard_updates_and_advance(self):
|
|
|
|
"""Called when the stream should advance but the updates would be discarded,
|
|
|
|
e.g. when there are no currently connected workers.
|
|
|
|
"""
|
2020-03-20 10:40:47 -04:00
|
|
|
self.last_token = self.current_token()
|
2017-04-04 08:19:26 -04:00
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
async def get_updates(self) -> StreamUpdateResult:
|
2017-03-27 09:58:26 -04:00
|
|
|
"""Gets all updates since the last time this function was called (or
|
2020-03-20 10:40:47 -04:00
|
|
|
since the stream was constructed if it hadn't been called before).
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
Returns:
|
2020-03-25 10:54:01 -04:00
|
|
|
A triplet `(updates, new_last_token, limited)`, where `updates` is
|
|
|
|
a list of `(token, row)` entries, `new_last_token` is the new
|
|
|
|
position in stream, and `limited` is whether there are more updates
|
|
|
|
to fetch.
|
2017-03-27 09:58:26 -04:00
|
|
|
"""
|
2020-03-25 10:54:01 -04:00
|
|
|
current_token = self.current_token()
|
|
|
|
updates, current_token, limited = await self.get_updates_since(
|
|
|
|
self.last_token, current_token
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
self.last_token = current_token
|
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
return updates, current_token, limited
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2020-03-20 10:40:47 -04:00
|
|
|
async def get_updates_since(
|
2020-04-23 10:45:12 -04:00
|
|
|
self, from_token: Token, upto_token: Token
|
2020-04-17 09:49:55 -04:00
|
|
|
) -> StreamUpdateResult:
|
2017-03-27 09:58:26 -04:00
|
|
|
"""Like get_updates except allows specifying from when we should
|
|
|
|
stream updates
|
|
|
|
|
|
|
|
Returns:
|
2020-03-25 10:54:01 -04:00
|
|
|
A triplet `(updates, new_last_token, limited)`, where `updates` is
|
|
|
|
a list of `(token, row)` entries, `new_last_token` is the new
|
|
|
|
position in stream, and `limited` is whether there are more updates
|
|
|
|
to fetch.
|
2017-03-27 09:58:26 -04:00
|
|
|
"""
|
2020-03-20 10:40:47 -04:00
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
from_token = int(from_token)
|
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
if from_token == upto_token:
|
|
|
|
return [], upto_token, False
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
updates, upto_token, limited = await self.update_function(
|
2020-04-23 10:45:12 -04:00
|
|
|
from_token, upto_token, _STREAM_UPDATE_TARGET_ROW_COUNT,
|
2020-03-20 10:40:47 -04:00
|
|
|
)
|
2020-03-25 10:54:01 -04:00
|
|
|
return updates, upto_token, limited
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
def db_query_to_update_function(
|
2020-04-17 09:49:55 -04:00
|
|
|
query_function: Callable[[Token, Token, int], Awaitable[Iterable[tuple]]]
|
|
|
|
) -> UpdateFunction:
|
2020-03-25 10:54:01 -04:00
|
|
|
"""Wraps a db query function which returns a list of rows to make it
|
|
|
|
suitable for use as an `update_function` for the Stream class
|
|
|
|
"""
|
|
|
|
|
|
|
|
async def update_function(from_token, upto_token, limit):
|
|
|
|
rows = await query_function(from_token, upto_token, limit)
|
|
|
|
updates = [(row[0], row[1:]) for row in rows]
|
|
|
|
limited = False
|
|
|
|
if len(updates) == limit:
|
2020-04-16 09:37:06 -04:00
|
|
|
upto_token = updates[-1][0]
|
2020-03-25 10:54:01 -04:00
|
|
|
limited = True
|
2020-04-16 09:37:06 -04:00
|
|
|
assert len(updates) <= limit
|
2020-03-25 10:54:01 -04:00
|
|
|
|
|
|
|
return updates, upto_token, limited
|
|
|
|
|
|
|
|
return update_function
|
|
|
|
|
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
def make_http_update_function(hs, stream_name: str) -> UpdateFunction:
|
2020-03-25 10:54:01 -04:00
|
|
|
"""Makes a suitable function for use as an `update_function` that queries
|
|
|
|
the master process for updates.
|
|
|
|
"""
|
|
|
|
|
|
|
|
client = ReplicationGetStreamUpdates.make_client(hs)
|
|
|
|
|
|
|
|
async def update_function(
|
|
|
|
from_token: int, upto_token: int, limit: int
|
2020-04-17 09:49:55 -04:00
|
|
|
) -> StreamUpdateResult:
|
2020-04-07 06:01:04 -04:00
|
|
|
result = await client(
|
2020-04-23 10:31:25 -04:00
|
|
|
stream_name=stream_name, from_token=from_token, upto_token=upto_token,
|
2020-03-25 10:54:01 -04:00
|
|
|
)
|
2020-04-07 06:01:04 -04:00
|
|
|
return result["updates"], result["upto_token"], result["limited"]
|
2020-03-25 10:54:01 -04:00
|
|
|
|
|
|
|
return update_function
|
|
|
|
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
class BackfillStream(Stream):
|
|
|
|
"""We fetched some old events and either we had never seen that event before
|
|
|
|
or it went from being an outlier to not.
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
BackfillStreamRow = namedtuple(
|
|
|
|
"BackfillStreamRow",
|
|
|
|
(
|
|
|
|
"event_id", # str
|
|
|
|
"room_id", # str
|
|
|
|
"type", # str
|
|
|
|
"state_key", # str, optional
|
|
|
|
"redacts", # str, optional
|
|
|
|
"relates_to", # str, optional
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "backfill"
|
|
|
|
ROW_TYPE = BackfillStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_current_backfill_token,
|
|
|
|
db_query_to_update_function(store.get_all_new_backfill_event_rows),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PresenceStream(Stream):
|
2020-03-23 09:59:11 -04:00
|
|
|
PresenceStreamRow = namedtuple(
|
|
|
|
"PresenceStreamRow",
|
|
|
|
(
|
|
|
|
"user_id", # str
|
|
|
|
"state", # str
|
|
|
|
"last_active_ts", # int
|
|
|
|
"last_federation_update_ts", # int
|
|
|
|
"last_user_sync_ts", # int
|
|
|
|
"status_msg", # str
|
|
|
|
"currently_active", # bool
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "presence"
|
|
|
|
ROW_TYPE = PresenceStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-03-25 10:54:01 -04:00
|
|
|
|
|
|
|
if hs.config.worker_app is None:
|
2020-04-17 09:49:55 -04:00
|
|
|
# on the master, query the presence handler
|
|
|
|
presence_handler = hs.get_presence_handler()
|
|
|
|
update_function = db_query_to_update_function(
|
|
|
|
presence_handler.get_all_presence_updates
|
|
|
|
)
|
2020-03-25 10:54:01 -04:00
|
|
|
else:
|
|
|
|
# Query master process
|
2020-04-17 09:49:55 -04:00
|
|
|
update_function = make_http_update_function(hs, self.NAME)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(store.get_current_presence_token, update_function)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TypingStream(Stream):
|
2020-03-23 09:59:11 -04:00
|
|
|
TypingStreamRow = namedtuple(
|
|
|
|
"TypingStreamRow", ("room_id", "user_ids") # str # list(str)
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "typing"
|
|
|
|
ROW_TYPE = TypingStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
typing_handler = hs.get_typing_handler()
|
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
if hs.config.worker_app is None:
|
2020-04-17 09:49:55 -04:00
|
|
|
# on the master, query the typing handler
|
|
|
|
update_function = db_query_to_update_function(
|
|
|
|
typing_handler.get_all_typing_updates
|
|
|
|
)
|
2020-03-25 10:54:01 -04:00
|
|
|
else:
|
|
|
|
# Query master process
|
2020-04-17 09:49:55 -04:00
|
|
|
update_function = make_http_update_function(hs, self.NAME)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(typing_handler.get_current_token, update_function)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ReceiptsStream(Stream):
|
2020-03-23 09:59:11 -04:00
|
|
|
ReceiptsStreamRow = namedtuple(
|
|
|
|
"ReceiptsStreamRow",
|
|
|
|
(
|
|
|
|
"room_id", # str
|
|
|
|
"receipt_type", # str
|
|
|
|
"user_id", # str
|
|
|
|
"event_id", # str
|
|
|
|
"data", # dict
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "receipts"
|
|
|
|
ROW_TYPE = ReceiptsStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_max_receipt_stream_id,
|
|
|
|
db_query_to_update_function(store.get_all_updated_receipts),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PushRulesStream(Stream):
|
|
|
|
"""A user has changed their push rules
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
PushRulesStreamRow = namedtuple("PushRulesStreamRow", ("user_id",)) # str
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "push_rules"
|
|
|
|
ROW_TYPE = PushRulesStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super(PushRulesStream, self).__init__(
|
|
|
|
self._current_token, self._update_function
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
def _current_token(self) -> int:
|
2017-03-27 09:58:26 -04:00
|
|
|
push_rules_token, _ = self.store.get_push_rules_stream_token()
|
|
|
|
return push_rules_token
|
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
async def _update_function(self, from_token: Token, to_token: Token, limit: int):
|
2020-01-16 04:16:12 -05:00
|
|
|
rows = await self.store.get_all_push_rule_updates(from_token, to_token, limit)
|
2020-03-25 10:54:01 -04:00
|
|
|
|
|
|
|
limited = False
|
|
|
|
if len(rows) == limit:
|
|
|
|
to_token = rows[-1][0]
|
|
|
|
limited = True
|
|
|
|
|
|
|
|
return [(row[0], (row[2],)) for row in rows], to_token, limited
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PushersStream(Stream):
|
|
|
|
"""A user has added/changed/removed a pusher
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
PushersStreamRow = namedtuple(
|
|
|
|
"PushersStreamRow",
|
|
|
|
("user_id", "app_id", "pushkey", "deleted"), # str # str # str # bool
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "pushers"
|
|
|
|
ROW_TYPE = PushersStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
|
|
|
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_pushers_stream_token,
|
|
|
|
db_query_to_update_function(store.get_all_updated_pushers_rows),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class CachesStream(Stream):
|
|
|
|
"""A cache was invalidated on the master and no other stream would invalidate
|
|
|
|
the cache on the workers
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
@attr.s
|
|
|
|
class CachesStreamRow:
|
|
|
|
"""Stream to inform workers they should invalidate their cache.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
cache_func: Name of the cached function.
|
|
|
|
keys: The entry in the cache to invalidate. If None then will
|
|
|
|
invalidate all.
|
|
|
|
invalidation_ts: Timestamp of when the invalidation took place.
|
|
|
|
"""
|
|
|
|
|
|
|
|
cache_func = attr.ib(type=str)
|
|
|
|
keys = attr.ib(type=Optional[List[Any]])
|
|
|
|
invalidation_ts = attr.ib(type=int)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "caches"
|
|
|
|
ROW_TYPE = CachesStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_cache_stream_token,
|
|
|
|
db_query_to_update_function(store.get_all_updated_caches),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PublicRoomsStream(Stream):
|
|
|
|
"""The public rooms list changed
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
PublicRoomsStreamRow = namedtuple(
|
|
|
|
"PublicRoomsStreamRow",
|
|
|
|
(
|
|
|
|
"room_id", # str
|
|
|
|
"visibility", # str
|
|
|
|
"appservice_id", # str, optional
|
|
|
|
"network_id", # str, optional
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "public_rooms"
|
|
|
|
ROW_TYPE = PublicRoomsStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_current_public_room_stream_id,
|
|
|
|
db_query_to_update_function(store.get_all_new_public_rooms),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceListsStream(Stream):
|
2020-02-28 06:24:05 -05:00
|
|
|
"""Either a user has updated their devices or a remote server needs to be
|
|
|
|
told about a device update.
|
2017-03-27 09:58:26 -04:00
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
@attr.s
|
|
|
|
class DeviceListsStreamRow:
|
|
|
|
entity = attr.ib(type=str)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "device_lists"
|
|
|
|
ROW_TYPE = DeviceListsStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_device_stream_token,
|
|
|
|
db_query_to_update_function(store.get_all_device_list_changes_for_remotes),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ToDeviceStream(Stream):
|
|
|
|
"""New to_device messages for a client
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
ToDeviceStreamRow = namedtuple("ToDeviceStreamRow", ("entity",)) # str
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "to_device"
|
|
|
|
ROW_TYPE = ToDeviceStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_to_device_stream_token,
|
|
|
|
db_query_to_update_function(store.get_all_new_device_messages),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TagAccountDataStream(Stream):
|
|
|
|
"""Someone added/removed a tag for a room
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
TagAccountDataStreamRow = namedtuple(
|
|
|
|
"TagAccountDataStreamRow", ("user_id", "room_id", "data") # str # str # dict
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "tag_account_data"
|
|
|
|
ROW_TYPE = TagAccountDataStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_max_account_data_stream_id,
|
|
|
|
db_query_to_update_function(store.get_all_updated_tags),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
|
|
|
class AccountDataStream(Stream):
|
|
|
|
"""Global or per room account data was changed
|
|
|
|
"""
|
2019-06-20 05:32:02 -04:00
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
AccountDataStreamRow = namedtuple(
|
|
|
|
"AccountDataStream", ("user_id", "room_id", "data_type") # str # str # str
|
|
|
|
)
|
|
|
|
|
2017-03-27 09:58:26 -04:00
|
|
|
NAME = "account_data"
|
|
|
|
ROW_TYPE = AccountDataStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
self.store.get_max_account_data_stream_id,
|
|
|
|
db_query_to_update_function(self._update_function),
|
|
|
|
)
|
2017-03-27 09:58:26 -04:00
|
|
|
|
2020-03-25 10:54:01 -04:00
|
|
|
async def _update_function(self, from_token, to_token, limit):
|
2020-01-16 04:16:12 -05:00
|
|
|
global_results, room_results = await self.store.get_all_updated_account_data(
|
2017-03-27 09:58:26 -04:00
|
|
|
from_token, from_token, to_token, limit
|
|
|
|
)
|
|
|
|
|
|
|
|
results = list(room_results)
|
|
|
|
results.extend(
|
2019-11-08 06:42:55 -05:00
|
|
|
(stream_id, user_id, None, account_data_type)
|
|
|
|
for stream_id, user_id, account_data_type in global_results
|
2017-03-27 09:58:26 -04:00
|
|
|
)
|
|
|
|
|
2019-07-23 09:00:55 -04:00
|
|
|
return results
|
2017-03-27 09:58:26 -04:00
|
|
|
|
|
|
|
|
2017-07-20 12:13:18 -04:00
|
|
|
class GroupServerStream(Stream):
|
2020-03-23 09:59:11 -04:00
|
|
|
GroupsStreamRow = namedtuple(
|
|
|
|
"GroupsStreamRow",
|
|
|
|
("group_id", "user_id", "type", "content"), # str # str # str # dict
|
|
|
|
)
|
|
|
|
|
2017-07-20 12:13:18 -04:00
|
|
|
NAME = "groups"
|
|
|
|
ROW_TYPE = GroupsStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_group_stream_token,
|
|
|
|
db_query_to_update_function(store.get_all_groups_changes),
|
|
|
|
)
|
2019-10-30 17:22:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
class UserSignatureStream(Stream):
|
|
|
|
"""A user has signed their own device with their user-signing key
|
|
|
|
"""
|
|
|
|
|
2020-03-23 09:59:11 -04:00
|
|
|
UserSignatureStreamRow = namedtuple("UserSignatureStreamRow", ("user_id")) # str
|
|
|
|
|
2019-10-30 17:22:52 -04:00
|
|
|
NAME = "user_signature"
|
|
|
|
ROW_TYPE = UserSignatureStreamRow
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
store = hs.get_datastore()
|
2020-04-17 09:49:55 -04:00
|
|
|
super().__init__(
|
|
|
|
store.get_device_stream_token,
|
|
|
|
db_query_to_update_function(
|
|
|
|
store.get_all_user_signature_changes_for_remotes
|
|
|
|
),
|
|
|
|
)
|