def presence_listener( self, event: Dict[str, Any], presence_update_id: int # pylint: disable=unused-argument ) -> None: address = address_from_userid(event["sender"]) if address: presence = UserPresence(event["content"]["presence"]) self.address_presence[address] = presence
def _presence_listener(self, event: Dict[str, Any], presence_update_id: int) -> None: """ Update cached user presence state from Matrix presence events. Due to the possibility of nodes using accounts on multiple homeservers a composite address state is synthesised from the cached individual user presence states. """ if self._stop_event.ready(): return user_id = event["sender"] if event["type"] != "m.presence" or user_id == self._user_id: return address = address_from_userid(user_id) # Not a user we've whitelisted, skip. This needs to be on the top of # the function so that we don't request they displayname of users that # are not important for the node. The presence is updated for every # user on the first sync, since every Raiden node is a member of a # broadcast room. This can result in thousands requests to the Matrix # server in the first sync which will lead to slow startup times and # presence problems. if address is None or not self.is_address_known(address): return user = self._user_from_id(user_id, event["content"].get("displayname")) if not user: return self._displayname_cache.warm_users([user]) # If for any reason we cannot resolve the displayname, then there was a server error. # Any properly logged in user that joined a room, will have a displayname. # A reason for not resolving it could be rate limiting by the other server. if user.displayname is None: new_state = UserPresence.SERVER_ERROR self._set_user_presence(user_id, new_state, presence_update_id) return address = self._validate_userid_signature(user) if not address: return self.add_userid_for_address(address, user_id) new_state = UserPresence(event["content"]["presence"]) self._set_user_presence(user_id, new_state, presence_update_id) self._maybe_address_reachability_changed(address)
def _presence_listener(self, event: Dict[str, Any], presence_update_id: int) -> None: """ Update cached user presence state from Matrix presence events. Due to the possibility of nodes using accounts on multiple homeservers a composite address state is synthesised from the cached individual user presence states. This is a copy from the super class with a slight change. Any user will automatically be added to the whitelist whenever the PFS receives a presence update. We do this since we can assume that the PFS needs to know about all raiden users. """ if self._stop_event.ready(): return user_id = event["sender"] if event["type"] != "m.presence" or user_id == self._user_id: return address = address_from_userid(user_id) # not a user authenticated by EthAuthProvider if address is None: return user = self._user_from_id(user_id, event["content"].get("displayname")) if not user: return self._displayname_cache.warm_users([user]) # If for any reason we cannot resolve the displayname, then there was a server error. # Any properly logged in user that joined a room, will have a displayname. # A reason for not resolving it could be rate limiting by the other server. if user.displayname is None: new_state = UserPresence.SERVER_ERROR self._set_user_presence(user_id, new_state, presence_update_id) return address = self._validate_userid_signature(user) if not address: return self.add_userid_for_address(address, user_id) new_state = UserPresence(event["content"]["presence"]) self._set_user_presence(user_id, new_state, presence_update_id) self._maybe_address_reachability_changed(address)
def get_userid_presence(self, user_id: str) -> UserPresence: """Return the current presence state of ``user_id``.""" address = address_from_userid(user_id) return (UserPresence.ONLINE if address is not None and self.get_address_reachability(address) == AddressReachability.REACHABLE else UserPresence.UNKNOWN)