1# Copyright 2014-2016 OpenMarket Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15from typing import TYPE_CHECKING, Dict, List, Tuple
16
17from synapse.http.server import HttpServer
18from synapse.http.servlet import RestServlet, parse_boolean
19from synapse.http.site import SynapseRequest
20from synapse.rest.client._base import client_patterns
21from synapse.streams.config import PaginationConfig
22from synapse.types import JsonDict
23
24if TYPE_CHECKING:
25    from synapse.server import HomeServer
26
27
28# TODO: Needs unit testing
29class InitialSyncRestServlet(RestServlet):
30    PATTERNS = client_patterns("/initialSync$", v1=True)
31
32    def __init__(self, hs: "HomeServer"):
33        super().__init__()
34        self.initial_sync_handler = hs.get_initial_sync_handler()
35        self.auth = hs.get_auth()
36        self.store = hs.get_datastore()
37
38    async def on_GET(self, request: SynapseRequest) -> Tuple[int, JsonDict]:
39        requester = await self.auth.get_user_by_req(request)
40        args: Dict[bytes, List[bytes]] = request.args  # type: ignore
41        as_client_event = b"raw" not in args
42        pagination_config = await PaginationConfig.from_request(self.store, request)
43        include_archived = parse_boolean(request, "archived", default=False)
44        content = await self.initial_sync_handler.snapshot_all_rooms(
45            user_id=requester.user.to_string(),
46            pagin_config=pagination_config,
47            as_client_event=as_client_event,
48            include_archived=include_archived,
49        )
50
51        return 200, content
52
53
54def register_servlets(hs: "HomeServer", http_server: HttpServer) -> None:
55    InitialSyncRestServlet(hs).register(http_server)
56