Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

matrix-org / synapse / 4532

23 Sep 2019 - 19:39 coverage decreased (-49.7%) to 17.596%
4532

Pull #6079

buildkite

Richard van der Hoff
update changelog
Pull Request #6079: Add submit_url response parameter to msisdn /requestToken

359 of 12986 branches covered (2.76%)

Branch coverage included in aggregate %.

0 of 7 new or added lines in 1 file covered. (0.0%)

18869 existing lines in 281 files now uncovered.

8809 of 39116 relevant lines covered (22.52%)

0.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

20.22
/synapse/handlers/events.py
1
# -*- coding: utf-8 -*-
2
# Copyright 2014-2016 OpenMarket Ltd
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15

16
import logging
1×
17
import random
1×
18

19
from twisted.internet import defer
1×
20

21
from synapse.api.constants import EventTypes, Membership
1×
22
from synapse.api.errors import AuthError, SynapseError
1×
23
from synapse.events import EventBase
1×
24
from synapse.logging.utils import log_function
1×
25
from synapse.types import UserID
1×
26
from synapse.visibility import filter_events_for_client
1×
27

28
from ._base import BaseHandler
1×
29

30
logger = logging.getLogger(__name__)
1×
31

32

33
class EventStreamHandler(BaseHandler):
1×
34
    def __init__(self, hs):
1×
UNCOV
35
        super(EventStreamHandler, self).__init__(hs)
!
36

37
        # Count of active streams per user
UNCOV
38
        self._streams_per_user = {}
!
39
        # Grace timers per user to delay the "stopped" signal
UNCOV
40
        self._stop_timer_per_user = {}
!
41

UNCOV
42
        self.distributor = hs.get_distributor()
!
UNCOV
43
        self.distributor.declare("started_user_eventstream")
!
UNCOV
44
        self.distributor.declare("stopped_user_eventstream")
!
45

UNCOV
46
        self.clock = hs.get_clock()
!
47

UNCOV
48
        self.notifier = hs.get_notifier()
!
UNCOV
49
        self.state = hs.get_state_handler()
!
UNCOV
50
        self._server_notices_sender = hs.get_server_notices_sender()
!
UNCOV
51
        self._event_serializer = hs.get_event_client_serializer()
!
52

53
    @defer.inlineCallbacks
1×
54
    @log_function
1×
55
    def get_stream(
1×
56
        self,
57
        auth_user_id,
58
        pagin_config,
59
        timeout=0,
60
        as_client_event=True,
61
        affect_presence=True,
62
        only_keys=None,
63
        room_id=None,
64
        is_guest=False,
65
    ):
66
        """Fetches the events stream for a given user.
67

68
        If `only_keys` is not None, events from keys will be sent down.
69
        """
70

UNCOV
71
        if room_id:
Branches [[0, 72], [0, 77]] missed. !
UNCOV
72
            blocked = yield self.store.is_room_blocked(room_id)
!
UNCOV
73
            if blocked:
Branches [[0, 74], [0, 77]] missed. !
74
                raise SynapseError(403, "This room has been blocked on this server")
!
75

76
        # send any outstanding server notices to the user.
UNCOV
77
        yield self._server_notices_sender.on_user_syncing(auth_user_id)
!
78

UNCOV
79
        auth_user = UserID.from_string(auth_user_id)
!
UNCOV
80
        presence_handler = self.hs.get_presence_handler()
!
81

UNCOV
82
        context = yield presence_handler.user_syncing(
!
83
            auth_user_id, affect_presence=affect_presence
84
        )
UNCOV
85
        with context:
!
UNCOV
86
            if timeout:
Branches [[0, 88], [0, 94]] missed. !
87
                # If they've set a timeout set a minimum limit.
UNCOV
88
                timeout = max(timeout, 500)
!
89

90
                # Add some randomness to this value to try and mitigate against
91
                # thundering herds on restart.
UNCOV
92
                timeout = random.randint(int(timeout * 0.9), int(timeout * 1.1))
!
93

UNCOV
94
            events, tokens = yield self.notifier.get_events_for(
!
95
                auth_user,
96
                pagin_config,
97
                timeout,
98
                only_keys=only_keys,
99
                is_guest=is_guest,
100
                explicit_room_id=room_id,
101
            )
102

103
            # When the user joins a new room, or another user joins a currently
104
            # joined room, we need to send down presence for those users.
UNCOV
105
            to_add = []
!
UNCOV
106
            for event in events:
Branches [[0, 107], [0, 127]] missed. !
UNCOV
107
                if not isinstance(event, EventBase):
Branches [[0, 108], [0, 109]] missed. !
UNCOV
108
                    continue
!
UNCOV
109
                if event.type == EventTypes.Member:
Branches [[0, 106], [0, 110]] missed. !
UNCOV
110
                    if event.membership != Membership.JOIN:
Branches [[0, 111], [0, 113]] missed. !
UNCOV
111
                        continue
!
112
                    # Send down presence.
UNCOV
113
                    if event.state_key == auth_user_id:
Branches [[0, 115], [0, 122]] missed. !
114
                        # Send down presence for everyone in the room.
UNCOV
115
                        users = yield self.state.get_current_users_in_room(
!
116
                            event.room_id
117
                        )
UNCOV
118
                        states = yield presence_handler.get_states(users, as_event=True)
!
UNCOV
119
                        to_add.extend(states)
!
120
                    else:
121

UNCOV
122
                        ev = yield presence_handler.get_state(
!
123
                            UserID.from_string(event.state_key), as_event=True
124
                        )
UNCOV
125
                        to_add.append(ev)
!
126

UNCOV
127
            events.extend(to_add)
!
128

UNCOV
129
            time_now = self.clock.time_msec()
!
130

UNCOV
131
            chunks = yield self._event_serializer.serialize_events(
!
132
                events,
133
                time_now,
134
                as_client_event=as_client_event,
135
                # We don't bundle "live" events, as otherwise clients
136
                # will end up double counting annotations.
137
                bundle_aggregations=False,
138
            )
139

UNCOV
140
            chunk = {
!
141
                "chunk": chunks,
142
                "start": tokens[0].to_string(),
143
                "end": tokens[1].to_string(),
144
            }
145

UNCOV
146
            return chunk
!
147

148

149
class EventHandler(BaseHandler):
1×
150
    @defer.inlineCallbacks
1×
151
    def get_event(self, user, room_id, event_id):
152
        """Retrieve a single specified event.
153

154
        Args:
155
            user (synapse.types.UserID): The user requesting the event
156
            room_id (str|None): The expected room id. We'll return None if the
157
                event's room does not match.
158
            event_id (str): The event ID to obtain.
159
        Returns:
160
            dict: An event, or None if there is no event matching this ID.
161
        Raises:
162
            SynapseError if there was a problem retrieving this event, or
163
            AuthError if the user does not have the rights to inspect this
164
            event.
165
        """
UNCOV
166
        event = yield self.store.get_event(event_id, check_room_id=room_id)
!
167

UNCOV
168
        if not event:
Branches [[0, 169], [0, 171]] missed. !
169
            return None
!
170

UNCOV
171
        users = yield self.store.get_users_in_room(event.room_id)
!
UNCOV
172
        is_peeking = user.to_string() not in users
!
173

UNCOV
174
        filtered = yield filter_events_for_client(
!
175
            self.store, user.to_string(), [event], is_peeking=is_peeking
176
        )
177

UNCOV
178
        if not filtered:
Branches [[0, 179], [0, 181]] missed. !
UNCOV
179
            raise AuthError(403, "You don't have permission to access that event.")
!
180

UNCOV
181
        return event
!
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC