2014-08-12 10:10:52 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-01-06 08:21:39 -05:00
|
|
|
# Copyright 2014, 2015 OpenMarket Ltd
|
2014-08-12 10:10:52 -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.
|
2014-08-12 22:14:34 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
from twisted.internet import defer
|
|
|
|
|
|
|
|
from synapse.api.errors import SynapseError, AuthError
|
|
|
|
from synapse.api.constants import PresenceState
|
|
|
|
|
2015-05-08 14:53:34 -04:00
|
|
|
from synapse.util.logcontext import PreserveLoggingContext
|
2014-08-28 13:45:00 -04:00
|
|
|
from synapse.util.logutils import log_function
|
2015-01-23 06:47:15 -05:00
|
|
|
from synapse.types import UserID
|
2015-03-04 11:50:23 -05:00
|
|
|
import synapse.metrics
|
2014-08-28 09:58:51 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
from ._base import BaseHandler
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-03-04 11:50:23 -05:00
|
|
|
metrics = synapse.metrics.get_metrics_for(__name__)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-03-23 13:25:44 -04:00
|
|
|
# Don't bother bumping "last active" time if it differs by less than 60 seconds
|
|
|
|
LAST_ACTIVE_GRANULARITY = 60*1000
|
|
|
|
|
2015-04-23 13:40:47 -04:00
|
|
|
# Keep no more than this number of offline serial revisions
|
|
|
|
MAX_OFFLINE_SERIALS = 1000
|
|
|
|
|
2015-03-23 13:25:44 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
# TODO(paul): Maybe there's one of these I can steal from somewhere
|
|
|
|
def partition(l, func):
|
|
|
|
"""Partition the list by the result of func applied to each element."""
|
|
|
|
ret = {}
|
|
|
|
|
|
|
|
for x in l:
|
|
|
|
key = func(x)
|
|
|
|
if key not in ret:
|
|
|
|
ret[key] = []
|
|
|
|
ret[key].append(x)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def partitionbool(l, func):
|
|
|
|
def boolfunc(x):
|
|
|
|
return bool(func(x))
|
|
|
|
|
|
|
|
ret = partition(l, boolfunc)
|
|
|
|
return ret.get(True, []), ret.get(False, [])
|
|
|
|
|
|
|
|
|
|
|
|
class PresenceHandler(BaseHandler):
|
|
|
|
|
2014-09-01 10:38:37 -04:00
|
|
|
STATE_LEVELS = {
|
|
|
|
PresenceState.OFFLINE: 0,
|
|
|
|
PresenceState.UNAVAILABLE: 1,
|
|
|
|
PresenceState.ONLINE: 2,
|
|
|
|
PresenceState.FREE_FOR_CHAT: 3,
|
|
|
|
}
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
def __init__(self, hs):
|
|
|
|
super(PresenceHandler, self).__init__(hs)
|
|
|
|
|
|
|
|
self.homeserver = hs
|
|
|
|
|
2014-08-13 14:18:55 -04:00
|
|
|
self.clock = hs.get_clock()
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
distributor = hs.get_distributor()
|
|
|
|
distributor.observe("registered_user", self.registered_user)
|
|
|
|
|
|
|
|
distributor.observe(
|
|
|
|
"started_user_eventstream", self.started_user_eventstream
|
|
|
|
)
|
|
|
|
distributor.observe(
|
|
|
|
"stopped_user_eventstream", self.stopped_user_eventstream
|
|
|
|
)
|
|
|
|
|
2014-10-30 07:10:17 -04:00
|
|
|
distributor.observe("user_joined_room", self.user_joined_room)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
distributor.declare("collect_presencelike_data")
|
|
|
|
|
|
|
|
distributor.declare("changed_presencelike_data")
|
|
|
|
distributor.observe(
|
|
|
|
"changed_presencelike_data", self.changed_presencelike_data
|
|
|
|
)
|
|
|
|
|
2015-01-28 06:55:49 -05:00
|
|
|
# outbound signal from the presence module to advertise when a user's
|
|
|
|
# presence has changed
|
|
|
|
distributor.declare("user_presence_changed")
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
self.distributor = distributor
|
|
|
|
|
|
|
|
self.federation = hs.get_replication_layer()
|
|
|
|
|
|
|
|
self.federation.register_edu_handler(
|
|
|
|
"m.presence", self.incoming_presence
|
|
|
|
)
|
|
|
|
self.federation.register_edu_handler(
|
|
|
|
"m.presence_invite",
|
|
|
|
lambda origin, content: self.invite_presence(
|
2015-01-23 06:47:15 -05:00
|
|
|
observed_user=UserID.from_string(content["observed_user"]),
|
|
|
|
observer_user=UserID.from_string(content["observer_user"]),
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.federation.register_edu_handler(
|
|
|
|
"m.presence_accept",
|
|
|
|
lambda origin, content: self.accept_presence(
|
2015-01-23 06:47:15 -05:00
|
|
|
observed_user=UserID.from_string(content["observed_user"]),
|
|
|
|
observer_user=UserID.from_string(content["observer_user"]),
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.federation.register_edu_handler(
|
|
|
|
"m.presence_deny",
|
|
|
|
lambda origin, content: self.deny_presence(
|
2015-01-23 06:47:15 -05:00
|
|
|
observed_user=UserID.from_string(content["observed_user"]),
|
|
|
|
observer_user=UserID.from_string(content["observer_user"]),
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
# IN-MEMORY store, mapping local userparts to sets of local users to
|
|
|
|
# be informed of state changes.
|
|
|
|
self._local_pushmap = {}
|
|
|
|
# map local users to sets of remote /domain names/ who are interested
|
|
|
|
# in them
|
|
|
|
self._remote_sendmap = {}
|
|
|
|
# map remote users to sets of local users who're interested in them
|
|
|
|
self._remote_recvmap = {}
|
2015-04-23 13:40:19 -04:00
|
|
|
# list of (serial, set of(userids)) tuples, ordered by serial, latest
|
|
|
|
# first
|
|
|
|
self._remote_offline_serials = []
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
# map any user to a UserPresenceCache
|
2015-05-12 10:44:21 -04:00
|
|
|
self._user_cachemap = {}
|
2014-08-12 10:10:52 -04:00
|
|
|
self._user_cachemap_latest_serial = 0
|
|
|
|
|
2015-05-18 11:21:51 -04:00
|
|
|
# map room_ids to the latest presence serial for a member of that
|
|
|
|
# room
|
|
|
|
self._room_serials = {}
|
|
|
|
|
2015-03-12 12:24:38 -04:00
|
|
|
metrics.register_callback(
|
|
|
|
"userCachemap:size",
|
|
|
|
lambda: len(self._user_cachemap),
|
2015-03-04 11:50:23 -05:00
|
|
|
)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
def _get_or_make_usercache(self, user):
|
|
|
|
"""If the cache entry doesn't exist, initialise a new one."""
|
|
|
|
if user not in self._user_cachemap:
|
|
|
|
self._user_cachemap[user] = UserPresenceCache()
|
|
|
|
return self._user_cachemap[user]
|
|
|
|
|
|
|
|
def _get_or_offline_usercache(self, user):
|
|
|
|
"""If the cache entry doesn't exist, return an OFFLINE one but do not
|
|
|
|
store it into the cache."""
|
|
|
|
if user in self._user_cachemap:
|
|
|
|
return self._user_cachemap[user]
|
|
|
|
else:
|
2014-11-19 12:30:35 -05:00
|
|
|
return UserPresenceCache()
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
def registered_user(self, user):
|
2014-11-20 11:24:00 -05:00
|
|
|
return self.store.create_presence(user.localpart)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def is_presence_visible(self, observer_user, observed_user):
|
2014-12-02 05:42:28 -05:00
|
|
|
assert(self.hs.is_mine(observed_user))
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
if observer_user == observed_user:
|
|
|
|
defer.returnValue(True)
|
|
|
|
|
2014-09-02 11:16:51 -04:00
|
|
|
if (yield self.store.user_rooms_intersect(
|
2014-10-30 07:10:17 -04:00
|
|
|
[u.to_string() for u in observer_user, observed_user])):
|
2014-09-02 08:30:36 -04:00
|
|
|
defer.returnValue(True)
|
|
|
|
|
|
|
|
if (yield self.store.is_presence_visible(
|
2014-10-30 07:10:17 -04:00
|
|
|
observed_localpart=observed_user.localpart,
|
|
|
|
observer_userid=observer_user.to_string())):
|
2014-08-12 10:10:52 -04:00
|
|
|
defer.returnValue(True)
|
|
|
|
|
2014-09-02 08:30:36 -04:00
|
|
|
defer.returnValue(False)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-11-18 10:25:55 -05:00
|
|
|
def get_state(self, target_user, auth_user, as_event=False):
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(target_user):
|
2014-10-30 07:10:17 -04:00
|
|
|
visible = yield self.is_presence_visible(
|
|
|
|
observer_user=auth_user,
|
2014-08-12 10:10:52 -04:00
|
|
|
observed_user=target_user
|
|
|
|
)
|
|
|
|
|
2014-09-01 10:38:37 -04:00
|
|
|
if not visible:
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(404, "Presence information not visible")
|
2014-09-01 10:38:37 -04:00
|
|
|
state = yield self.store.get_presence_state(target_user.localpart)
|
|
|
|
if "mtime" in state:
|
|
|
|
del state["mtime"]
|
2014-09-03 10:37:10 -04:00
|
|
|
state["presence"] = state.pop("state")
|
2014-09-01 10:38:37 -04:00
|
|
|
|
|
|
|
if target_user in self._user_cachemap:
|
2014-11-18 10:10:11 -05:00
|
|
|
cached_state = self._user_cachemap[target_user].get_state()
|
|
|
|
if "last_active" in cached_state:
|
|
|
|
state["last_active"] = cached_state["last_active"]
|
2014-08-12 10:10:52 -04:00
|
|
|
else:
|
|
|
|
# TODO(paul): Have remote server send us permissions set
|
2014-08-13 14:18:55 -04:00
|
|
|
state = self._get_or_offline_usercache(target_user).get_state()
|
|
|
|
|
2014-09-01 10:38:37 -04:00
|
|
|
if "last_active" in state:
|
|
|
|
state["last_active_ago"] = int(
|
|
|
|
self.clock.time_msec() - state.pop("last_active")
|
2014-08-14 09:15:54 -04:00
|
|
|
)
|
2014-11-18 10:25:55 -05:00
|
|
|
|
|
|
|
if as_event:
|
|
|
|
content = state
|
|
|
|
|
|
|
|
content["user_id"] = target_user.to_string()
|
|
|
|
|
|
|
|
if "last_active" in content:
|
|
|
|
content["last_active_ago"] = int(
|
|
|
|
self._clock.time_msec() - content.pop("last_active")
|
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue({"type": "m.presence", "content": content})
|
|
|
|
else:
|
|
|
|
defer.returnValue(state)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-08-28 13:45:00 -04:00
|
|
|
@log_function
|
2014-08-12 10:10:52 -04:00
|
|
|
def set_state(self, target_user, auth_user, state):
|
2014-08-28 09:58:51 -04:00
|
|
|
# return
|
2014-08-22 13:02:45 -04:00
|
|
|
# TODO (erikj): Turn this back on. Why did we end up sending EDUs
|
|
|
|
# everywhere?
|
|
|
|
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(target_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
|
|
|
if target_user != auth_user:
|
2014-09-03 06:45:30 -04:00
|
|
|
raise AuthError(400, "Cannot set another user's presence")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
if "status_msg" not in state:
|
|
|
|
state["status_msg"] = None
|
|
|
|
|
|
|
|
for k in state.keys():
|
2014-09-03 10:37:10 -04:00
|
|
|
if k not in ("presence", "status_msg"):
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(
|
|
|
|
400, "Unexpected presence state key '%s'" % (k,)
|
|
|
|
)
|
|
|
|
|
2014-09-01 10:38:37 -04:00
|
|
|
if state["presence"] not in self.STATE_LEVELS:
|
2014-10-30 07:10:17 -04:00
|
|
|
raise SynapseError(400, "'%s' is not a valid presence state" % (
|
|
|
|
state["presence"],
|
|
|
|
))
|
2014-09-01 10:38:37 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
logger.debug("Updating presence state of %s to %s",
|
2014-09-01 09:45:35 -04:00
|
|
|
target_user.localpart, state["presence"])
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
state_to_store = dict(state)
|
2014-09-01 09:45:35 -04:00
|
|
|
state_to_store["state"] = state_to_store.pop("presence")
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-10-30 07:10:17 -04:00
|
|
|
statuscache = self._get_or_offline_usercache(target_user)
|
2014-09-01 10:38:37 -04:00
|
|
|
was_level = self.STATE_LEVELS[statuscache.get_state()["presence"]]
|
|
|
|
now_level = self.STATE_LEVELS[state["presence"]]
|
|
|
|
|
2014-11-20 11:24:00 -05:00
|
|
|
yield self.store.set_presence_state(
|
|
|
|
target_user.localpart, state_to_store
|
|
|
|
)
|
|
|
|
yield self.distributor.fire(
|
|
|
|
"collect_presencelike_data", target_user, state
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-01 10:38:37 -04:00
|
|
|
if now_level > was_level:
|
|
|
|
state["last_active"] = self.clock.time_msec()
|
2014-08-13 14:18:55 -04:00
|
|
|
|
2014-09-01 09:45:35 -04:00
|
|
|
now_online = state["presence"] != PresenceState.OFFLINE
|
2014-08-12 10:10:52 -04:00
|
|
|
was_polling = target_user in self._user_cachemap
|
|
|
|
|
2015-05-08 11:32:18 -04:00
|
|
|
if now_online and not was_polling:
|
|
|
|
self.start_polling_presence(target_user, state=state)
|
|
|
|
elif not now_online and was_polling:
|
|
|
|
self.stop_polling_presence(target_user)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-05-08 11:32:18 -04:00
|
|
|
# TODO(paul): perform a presence push as part of start/stop poll so
|
|
|
|
# we don't have to do this all the time
|
|
|
|
self.changed_presencelike_data(target_user, state)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-09-01 11:16:35 -04:00
|
|
|
def bump_presence_active_time(self, user, now=None):
|
|
|
|
if now is None:
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
|
2015-03-23 13:25:44 -04:00
|
|
|
prev_state = self._get_or_make_usercache(user)
|
|
|
|
if now - prev_state.state.get("last_active", 0) < LAST_ACTIVE_GRANULARITY:
|
|
|
|
return
|
|
|
|
|
2014-09-01 11:16:35 -04:00
|
|
|
self.changed_presencelike_data(user, {"last_active": now})
|
|
|
|
|
2015-05-18 08:46:47 -04:00
|
|
|
def get_joined_rooms_for_user(self, user):
|
|
|
|
"""Get the list of rooms a user is joined to.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user(UserID): The user.
|
|
|
|
Returns:
|
|
|
|
A Deferred of a list of room id strings.
|
|
|
|
"""
|
|
|
|
rm_handler = self.homeserver.get_handlers().room_member_handler
|
|
|
|
return rm_handler.get_joined_rooms_for_user(user)
|
|
|
|
|
2015-05-18 10:46:37 -04:00
|
|
|
def get_joined_users_for_room_id(self, room_id):
|
|
|
|
rm_handler = self.homeserver.get_handlers().room_member_handler
|
|
|
|
return rm_handler.get_room_members(room_id)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-08-12 10:10:52 -04:00
|
|
|
def changed_presencelike_data(self, user, state):
|
2015-05-18 08:46:47 -04:00
|
|
|
"""Updates the presence state of a local user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user(UserID): The user being updated.
|
|
|
|
state(dict): The new presence state for the user.
|
|
|
|
Returns:
|
|
|
|
A Deferred
|
|
|
|
"""
|
2015-05-12 10:44:21 -04:00
|
|
|
self._user_cachemap_latest_serial += 1
|
2015-05-18 10:46:37 -04:00
|
|
|
statuscache = yield self.update_presence_cache(user, state)
|
|
|
|
yield self.push_presence(user, statuscache=statuscache)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-28 13:45:00 -04:00
|
|
|
@log_function
|
2014-08-12 10:10:52 -04:00
|
|
|
def started_user_eventstream(self, user):
|
|
|
|
# TODO(paul): Use "last online" state
|
2014-09-01 09:45:35 -04:00
|
|
|
self.set_state(user, user, {"presence": PresenceState.ONLINE})
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-28 13:45:00 -04:00
|
|
|
@log_function
|
2014-08-12 10:10:52 -04:00
|
|
|
def stopped_user_eventstream(self, user):
|
|
|
|
# TODO(paul): Save current state as "last online" state
|
2014-09-01 09:45:35 -04:00
|
|
|
self.set_state(user, user, {"presence": PresenceState.OFFLINE})
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def user_joined_room(self, user, room_id):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Called via the distributor whenever a user joins a room.
|
|
|
|
Notifies the new member of the presence of the current members.
|
|
|
|
Notifies the current members of the room of the new member's presence.
|
2015-05-15 06:09:47 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserID): The user who joined the room.
|
|
|
|
room_id(str): The room id the user joined.
|
|
|
|
"""
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(user):
|
2014-08-29 14:53:33 -04:00
|
|
|
# No actual update but we need to bump the serial anyway for the
|
|
|
|
# event source
|
2015-05-12 10:44:21 -04:00
|
|
|
self._user_cachemap_latest_serial += 1
|
2015-05-18 10:46:37 -04:00
|
|
|
statuscache = yield self.update_presence_cache(
|
|
|
|
user, room_ids=[room_id]
|
|
|
|
)
|
2014-08-29 14:53:33 -04:00
|
|
|
self.push_update_to_local_and_remote(
|
2014-08-28 13:43:03 -04:00
|
|
|
observed_user=user,
|
|
|
|
room_ids=[room_id],
|
2014-08-29 14:53:33 -04:00
|
|
|
statuscache=statuscache,
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
|
2014-08-29 07:08:33 -04:00
|
|
|
# We also want to tell them about current presence of people.
|
2015-05-18 10:46:37 -04:00
|
|
|
curr_users = yield self.get_joined_users_for_room_id(room_id)
|
2014-08-29 07:08:33 -04:00
|
|
|
|
2014-12-02 05:42:28 -05:00
|
|
|
for local_user in [c for c in curr_users if self.hs.is_mine(c)]:
|
2015-05-18 10:46:37 -04:00
|
|
|
statuscache = yield self.update_presence_cache(
|
|
|
|
local_user, room_ids=[room_id], add_to_cache=False
|
|
|
|
)
|
|
|
|
|
2014-08-29 07:08:33 -04:00
|
|
|
self.push_update_to_local_and_remote(
|
|
|
|
observed_user=local_user,
|
|
|
|
users_to_push=[user],
|
2015-05-18 10:46:37 -04:00
|
|
|
statuscache=statuscache,
|
2014-08-29 07:08:33 -04:00
|
|
|
)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def send_invite(self, observer_user, observed_user):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Request the presence of a local or remote user for a local user"""
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(observer_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
|
|
|
yield self.store.add_presence_list_pending(
|
|
|
|
observer_user.localpart, observed_user.to_string()
|
|
|
|
)
|
|
|
|
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(observed_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
yield self.invite_presence(observed_user, observer_user)
|
|
|
|
else:
|
|
|
|
yield self.federation.send_edu(
|
|
|
|
destination=observed_user.domain,
|
|
|
|
edu_type="m.presence_invite",
|
|
|
|
content={
|
|
|
|
"observed_user": observed_user.to_string(),
|
|
|
|
"observer_user": observer_user.to_string(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _should_accept_invite(self, observed_user, observer_user):
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(observed_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
defer.returnValue(False)
|
|
|
|
|
|
|
|
row = yield self.store.has_presence_state(observed_user.localpart)
|
|
|
|
if not row:
|
|
|
|
defer.returnValue(False)
|
|
|
|
|
|
|
|
# TODO(paul): Eventually we'll ask the user's permission for this
|
|
|
|
# before accepting. For now just accept any invite request
|
|
|
|
defer.returnValue(True)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def invite_presence(self, observed_user, observer_user):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Handles a m.presence_invite EDU. A remote or local user has
|
|
|
|
requested presence updates for a local user. If the invite is accepted
|
|
|
|
then allow the local or remote user to see the presence of the local
|
|
|
|
user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
observed_user(UserID): The local user whose presence is requested.
|
|
|
|
observer_user(UserID): The remote or local user requesting presence.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
accept = yield self._should_accept_invite(observed_user, observer_user)
|
|
|
|
|
|
|
|
if accept:
|
|
|
|
yield self.store.allow_presence_visible(
|
|
|
|
observed_user.localpart, observer_user.to_string()
|
|
|
|
)
|
|
|
|
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(observer_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
if accept:
|
|
|
|
yield self.accept_presence(observed_user, observer_user)
|
|
|
|
else:
|
|
|
|
yield self.deny_presence(observed_user, observer_user)
|
|
|
|
else:
|
|
|
|
edu_type = "m.presence_accept" if accept else "m.presence_deny"
|
|
|
|
|
|
|
|
yield self.federation.send_edu(
|
|
|
|
destination=observer_user.domain,
|
|
|
|
edu_type=edu_type,
|
|
|
|
content={
|
|
|
|
"observed_user": observed_user.to_string(),
|
|
|
|
"observer_user": observer_user.to_string(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def accept_presence(self, observed_user, observer_user):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Handles a m.presence_accept EDU. Mark a presence invite from a
|
|
|
|
local or remote user as accepted in a local user's presence list.
|
|
|
|
Starts polling for presence updates from the local or remote user.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
observed_user(UserID): The user to update in the presence list.
|
|
|
|
observer_user(UserID): The owner of the presence list to update.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
yield self.store.set_presence_list_accepted(
|
|
|
|
observer_user.localpart, observed_user.to_string()
|
|
|
|
)
|
2015-05-08 11:32:18 -04:00
|
|
|
|
|
|
|
self.start_polling_presence(
|
|
|
|
observer_user, target_user=observed_user
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def deny_presence(self, observed_user, observer_user):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Handle a m.presence_deny EDU. Removes a local or remote user from a
|
|
|
|
local user's presence list.
|
|
|
|
|
|
|
|
Args:
|
2015-05-14 12:48:12 -04:00
|
|
|
observed_user(UserID): The local or remote user to remove from the
|
|
|
|
list.
|
|
|
|
observer_user(UserID): The local owner of the presence list.
|
2015-05-14 10:29:58 -04:00
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
yield self.store.del_presence_list(
|
|
|
|
observer_user.localpart, observed_user.to_string()
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO(paul): Inform the user somehow?
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def drop(self, observed_user, observer_user):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Remove a local or remote user from a local user's presence list and
|
|
|
|
unsubscribe the local user from updates that user.
|
|
|
|
|
|
|
|
Args:
|
2015-05-14 12:48:12 -04:00
|
|
|
observed_user(UserId): The local or remote user to remove from the
|
|
|
|
list.
|
|
|
|
observer_user(UserId): The local owner of the presence list.
|
2015-05-14 10:29:58 -04:00
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(observer_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
|
|
|
yield self.store.del_presence_list(
|
|
|
|
observer_user.localpart, observed_user.to_string()
|
|
|
|
)
|
|
|
|
|
2015-05-08 11:32:18 -04:00
|
|
|
self.stop_polling_presence(
|
|
|
|
observer_user, target_user=observed_user
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_presence_list(self, observer_user, accepted=None):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Get the presence list for a local user. The retured list includes
|
|
|
|
the current presence state for each user listed.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
observer_user(UserID): The local user whose presence list to fetch.
|
|
|
|
accepted(bool or None): If not none then only include users who
|
|
|
|
have or have not accepted the presence invite request.
|
|
|
|
Returns:
|
|
|
|
A Deferred list of presence state events.
|
|
|
|
"""
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(observer_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
|
|
|
presence = yield self.store.get_presence_list(
|
|
|
|
observer_user.localpart, accepted=accepted
|
|
|
|
)
|
|
|
|
|
|
|
|
for p in presence:
|
2015-01-23 06:47:15 -05:00
|
|
|
observed_user = UserID.from_string(p.pop("observed_user_id"))
|
2014-08-12 10:10:52 -04:00
|
|
|
p["observed_user"] = observed_user
|
|
|
|
p.update(self._get_or_offline_usercache(observed_user).get_state())
|
2014-09-01 10:38:37 -04:00
|
|
|
if "last_active" in p:
|
|
|
|
p["last_active_ago"] = int(
|
|
|
|
self.clock.time_msec() - p.pop("last_active")
|
2014-08-14 09:15:54 -04:00
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
defer.returnValue(presence)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-08-28 13:45:00 -04:00
|
|
|
@log_function
|
2014-08-12 10:10:52 -04:00
|
|
|
def start_polling_presence(self, user, target_user=None, state=None):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Subscribe a local user to presence updates from a local or remote
|
|
|
|
user. If no target_user is supplied then subscribe to all users stored
|
|
|
|
in the presence list for the local user.
|
|
|
|
|
|
|
|
Additonally this pushes the current presence state of this user to all
|
|
|
|
target_users. That state can be provided directly or will be read from
|
|
|
|
the stored state for the local user.
|
|
|
|
|
|
|
|
Also this attempts to notify the local user of the current state of
|
|
|
|
any local target users.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user(UserID): The local user that whishes for presence updates.
|
|
|
|
target_user(UserID): The local or remote user whose updates are
|
|
|
|
wanted.
|
|
|
|
state(dict): Optional presence state for the local user.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
logger.debug("Start polling for presence from %s", user)
|
|
|
|
|
|
|
|
if target_user:
|
2014-08-18 11:52:29 -04:00
|
|
|
target_users = set([target_user])
|
2014-08-28 13:43:03 -04:00
|
|
|
room_ids = []
|
2014-08-12 10:10:52 -04:00
|
|
|
else:
|
|
|
|
presence = yield self.store.get_presence_list(
|
|
|
|
user.localpart, accepted=True
|
|
|
|
)
|
2014-08-15 12:12:07 -04:00
|
|
|
target_users = set([
|
2015-01-23 06:47:15 -05:00
|
|
|
UserID.from_string(x["observed_user_id"]) for x in presence
|
2014-08-15 12:12:07 -04:00
|
|
|
])
|
|
|
|
|
|
|
|
# Also include people in all my rooms
|
|
|
|
|
2015-05-18 08:46:47 -04:00
|
|
|
room_ids = yield self.get_joined_rooms_for_user(user)
|
2014-08-15 12:12:07 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
if state is None:
|
|
|
|
state = yield self.store.get_presence_state(user.localpart)
|
2014-08-29 07:08:33 -04:00
|
|
|
else:
|
2015-02-10 12:58:36 -05:00
|
|
|
# statuscache = self._get_or_make_usercache(user)
|
|
|
|
# self._user_cachemap_latest_serial += 1
|
|
|
|
# statuscache.update(state, self._user_cachemap_latest_serial)
|
2014-08-29 07:08:33 -04:00
|
|
|
pass
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-29 07:08:33 -04:00
|
|
|
yield self.push_update_to_local_and_remote(
|
2014-08-28 13:43:03 -04:00
|
|
|
observed_user=user,
|
|
|
|
users_to_push=target_users,
|
|
|
|
room_ids=room_ids,
|
|
|
|
statuscache=self._get_or_make_usercache(user),
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
|
2014-08-28 13:43:03 -04:00
|
|
|
for target_user in target_users:
|
2014-12-02 05:42:28 -05:00
|
|
|
if self.hs.is_mine(target_user):
|
2014-08-28 13:43:03 -04:00
|
|
|
self._start_polling_local(user, target_user)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-29 07:08:33 -04:00
|
|
|
# We want to tell the person that just came online
|
|
|
|
# presence state of people they are interested in?
|
|
|
|
self.push_update_to_clients(
|
|
|
|
users_to_push=[user],
|
|
|
|
)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
deferreds = []
|
2014-12-02 05:42:28 -05:00
|
|
|
remote_users = [u for u in target_users if not self.hs.is_mine(u)]
|
2014-08-28 13:43:03 -04:00
|
|
|
remoteusers_by_domain = partition(remote_users, lambda u: u.domain)
|
|
|
|
# Only poll for people in our get_presence_list
|
2014-08-12 10:10:52 -04:00
|
|
|
for domain in remoteusers_by_domain:
|
|
|
|
remoteusers = remoteusers_by_domain[domain]
|
|
|
|
|
|
|
|
deferreds.append(self._start_polling_remote(
|
|
|
|
user, domain, remoteusers
|
|
|
|
))
|
|
|
|
|
2015-02-17 05:03:23 -05:00
|
|
|
yield defer.DeferredList(deferreds, consumeErrors=True)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
def _start_polling_local(self, user, target_user):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Subscribe a local user to presence updates for a local user
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserId): The local user that wishes for updates.
|
|
|
|
target_user(UserId): The local users whose updates are wanted.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
target_localpart = target_user.localpart
|
|
|
|
|
|
|
|
if target_localpart not in self._local_pushmap:
|
|
|
|
self._local_pushmap[target_localpart] = set()
|
|
|
|
|
|
|
|
self._local_pushmap[target_localpart].add(user)
|
|
|
|
|
|
|
|
def _start_polling_remote(self, user, domain, remoteusers):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Subscribe a local user to presence updates for remote users on a
|
2015-05-14 12:48:12 -04:00
|
|
|
given remote domain.
|
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserID): The local user that wishes for updates.
|
|
|
|
domain(str): The remote server the local user wants updates from.
|
|
|
|
remoteusers(UserID): The remote users that local user wants to be
|
|
|
|
told about.
|
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-08-28 11:43:55 -04:00
|
|
|
to_poll = set()
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
for u in remoteusers:
|
|
|
|
if u not in self._remote_recvmap:
|
|
|
|
self._remote_recvmap[u] = set()
|
2014-08-28 11:43:55 -04:00
|
|
|
to_poll.add(u)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
self._remote_recvmap[u].add(user)
|
|
|
|
|
2014-08-28 11:43:55 -04:00
|
|
|
if not to_poll:
|
|
|
|
return defer.succeed(None)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
return self.federation.send_edu(
|
|
|
|
destination=domain,
|
|
|
|
edu_type="m.presence",
|
2014-08-28 11:43:55 -04:00
|
|
|
content={"poll": [u.to_string() for u in to_poll]}
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
|
2014-08-28 13:45:00 -04:00
|
|
|
@log_function
|
2014-08-12 10:10:52 -04:00
|
|
|
def stop_polling_presence(self, user, target_user=None):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Unsubscribe a local user from presence updates from a local or
|
|
|
|
remote user. If no target user is supplied then unsubscribe the user
|
|
|
|
from all presence updates that the user had subscribed to.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserID): The local user that no longer wishes for updates.
|
|
|
|
target_user(UserID or None): The user whose updates are no longer
|
|
|
|
wanted.
|
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
logger.debug("Stop polling for presence from %s", user)
|
|
|
|
|
2014-12-02 05:42:28 -05:00
|
|
|
if not target_user or self.hs.is_mine(target_user):
|
2014-08-12 10:10:52 -04:00
|
|
|
self._stop_polling_local(user, target_user=target_user)
|
|
|
|
|
|
|
|
deferreds = []
|
|
|
|
|
|
|
|
if target_user:
|
2014-08-18 11:43:18 -04:00
|
|
|
if target_user not in self._remote_recvmap:
|
|
|
|
return
|
|
|
|
target_users = set([target_user])
|
|
|
|
else:
|
|
|
|
target_users = self._remote_recvmap.keys()
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-18 11:43:18 -04:00
|
|
|
remoteusers = [u for u in target_users
|
2014-08-12 10:10:52 -04:00
|
|
|
if user in self._remote_recvmap[u]]
|
|
|
|
remoteusers_by_domain = partition(remoteusers, lambda u: u.domain)
|
|
|
|
|
|
|
|
for domain in remoteusers_by_domain:
|
|
|
|
remoteusers = remoteusers_by_domain[domain]
|
|
|
|
|
|
|
|
deferreds.append(
|
|
|
|
self._stop_polling_remote(user, domain, remoteusers)
|
|
|
|
)
|
|
|
|
|
2015-02-17 05:03:23 -05:00
|
|
|
return defer.DeferredList(deferreds, consumeErrors=True)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
def _stop_polling_local(self, user, target_user):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Unsubscribe a local user from presence updates from a local user on
|
|
|
|
this server.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserID): The local user that no longer wishes for updates.
|
|
|
|
target_user(UserID): The user whose updates are no longer wanted.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
for localpart in self._local_pushmap.keys():
|
|
|
|
if target_user and localpart != target_user.localpart:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if user in self._local_pushmap[localpart]:
|
|
|
|
self._local_pushmap[localpart].remove(user)
|
|
|
|
|
|
|
|
if not self._local_pushmap[localpart]:
|
|
|
|
del self._local_pushmap[localpart]
|
|
|
|
|
2014-08-28 13:45:00 -04:00
|
|
|
@log_function
|
2014-08-12 10:10:52 -04:00
|
|
|
def _stop_polling_remote(self, user, domain, remoteusers):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Unsubscribe a local user from presence updates from remote users on
|
|
|
|
a given domain.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserID): The local user that no longer wishes for updates.
|
|
|
|
domain(str): The remote server to unsubscribe from.
|
|
|
|
remoteusers([UserID]): The users on that remote server that the
|
|
|
|
local user no longer wishes to be updated about.
|
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-08-28 11:43:55 -04:00
|
|
|
to_unpoll = set()
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
for u in remoteusers:
|
|
|
|
self._remote_recvmap[u].remove(user)
|
|
|
|
|
|
|
|
if not self._remote_recvmap[u]:
|
|
|
|
del self._remote_recvmap[u]
|
2014-08-28 11:43:55 -04:00
|
|
|
to_unpoll.add(u)
|
|
|
|
|
|
|
|
if not to_unpoll:
|
|
|
|
return defer.succeed(None)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
return self.federation.send_edu(
|
|
|
|
destination=domain,
|
|
|
|
edu_type="m.presence",
|
2014-08-28 11:43:55 -04:00
|
|
|
content={"unpoll": [u.to_string() for u in to_unpoll]}
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2014-08-28 13:45:00 -04:00
|
|
|
@log_function
|
2014-08-12 10:10:52 -04:00
|
|
|
def push_presence(self, user, statuscache):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""
|
|
|
|
Notify local and remote users of a change in presence of a local user.
|
|
|
|
Pushes the update to local clients and remote domains that are directly
|
|
|
|
subscribed to the presence of the local user.
|
|
|
|
Also pushes that update to any local user or remote domain that shares
|
|
|
|
a room with the local user.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserID): The local user whose presence was updated.
|
|
|
|
statuscache(UserPresenceCache): Cache of the user's presence state
|
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-12-02 05:42:28 -05:00
|
|
|
assert(self.hs.is_mine(user))
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
logger.debug("Pushing presence update from %s", user)
|
|
|
|
|
|
|
|
localusers = set(self._local_pushmap.get(user.localpart, set()))
|
|
|
|
remotedomains = set(self._remote_sendmap.get(user.localpart, set()))
|
|
|
|
|
|
|
|
# Reflect users' status changes back to themselves, so UIs look nice
|
|
|
|
# and also user is informed of server-forced pushes
|
|
|
|
localusers.add(user)
|
|
|
|
|
2015-05-18 08:46:47 -04:00
|
|
|
room_ids = yield self.get_joined_rooms_for_user(user)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-28 13:43:03 -04:00
|
|
|
if not localusers and not room_ids:
|
2014-08-12 10:10:52 -04:00
|
|
|
defer.returnValue(None)
|
|
|
|
|
2014-08-28 13:43:03 -04:00
|
|
|
yield self.push_update_to_local_and_remote(
|
|
|
|
observed_user=user,
|
|
|
|
users_to_push=localusers,
|
2014-08-29 07:08:33 -04:00
|
|
|
remote_domains=remotedomains,
|
2014-08-28 12:43:15 -04:00
|
|
|
room_ids=room_ids,
|
|
|
|
statuscache=statuscache,
|
|
|
|
)
|
2015-01-28 06:55:49 -05:00
|
|
|
yield self.distributor.fire("user_presence_changed", user, statuscache)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def incoming_presence(self, origin, content):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Handle an incoming m.presence EDU.
|
|
|
|
For each presence update in the "push" list update our local cache and
|
|
|
|
notify the appropriate local clients. Only clients that share a room
|
|
|
|
or are directly subscribed to the presence for a user should be
|
|
|
|
notified of the update.
|
|
|
|
For each subscription request in the "poll" list start pushing presence
|
|
|
|
updates to the remote server.
|
|
|
|
For unsubscribe request in the "unpoll" list stop pushing presence
|
|
|
|
updates to the remote server.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
orgin(str): The source of this m.presence EDU.
|
|
|
|
content(dict): The content of this m.presence EDU.
|
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-08-12 10:10:52 -04:00
|
|
|
deferreds = []
|
|
|
|
|
|
|
|
for push in content.get("push", []):
|
2015-01-23 06:47:15 -05:00
|
|
|
user = UserID.from_string(push["user_id"])
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
logger.debug("Incoming presence update from %s", user)
|
|
|
|
|
|
|
|
observers = set(self._remote_recvmap.get(user, set()))
|
2014-12-02 15:51:28 -05:00
|
|
|
if observers:
|
2015-02-10 11:30:48 -05:00
|
|
|
logger.debug(
|
|
|
|
" | %d interested local observers %r", len(observers), observers
|
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-05-18 08:46:47 -04:00
|
|
|
room_ids = yield self.get_joined_rooms_for_user(user)
|
2014-12-02 15:51:28 -05:00
|
|
|
if room_ids:
|
|
|
|
logger.debug(" | %d interested room IDs %r", len(room_ids), room_ids)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
state = dict(push)
|
|
|
|
del state["user_id"]
|
|
|
|
|
2014-09-03 10:37:10 -04:00
|
|
|
if "presence" not in state:
|
2014-10-30 07:10:17 -04:00
|
|
|
logger.warning(
|
|
|
|
"Received a presence 'push' EDU from %s without a"
|
|
|
|
" 'presence' key", origin
|
2014-09-02 06:17:43 -04:00
|
|
|
)
|
|
|
|
continue
|
|
|
|
|
2014-09-01 10:38:37 -04:00
|
|
|
if "last_active_ago" in state:
|
|
|
|
state["last_active"] = int(
|
|
|
|
self.clock.time_msec() - state.pop("last_active_ago")
|
2014-08-14 09:15:54 -04:00
|
|
|
)
|
2014-08-13 14:18:55 -04:00
|
|
|
|
2015-05-12 10:44:21 -04:00
|
|
|
self._user_cachemap_latest_serial += 1
|
2015-05-18 10:46:37 -04:00
|
|
|
yield self.update_presence_cache(user, state, room_ids=room_ids)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-12-03 14:06:24 -05:00
|
|
|
if not observers and not room_ids:
|
|
|
|
logger.debug(" | no interested observers or room IDs")
|
|
|
|
continue
|
|
|
|
|
2014-08-29 07:08:33 -04:00
|
|
|
self.push_update_to_clients(
|
2015-05-14 09:59:31 -04:00
|
|
|
users_to_push=observers, room_ids=room_ids
|
2014-08-28 12:43:15 -04:00
|
|
|
)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-04-23 14:01:37 -04:00
|
|
|
user_id = user.to_string()
|
|
|
|
|
2014-09-01 09:45:35 -04:00
|
|
|
if state["presence"] == PresenceState.OFFLINE:
|
2015-04-23 13:40:19 -04:00
|
|
|
self._remote_offline_serials.insert(
|
|
|
|
0,
|
2015-04-23 14:01:37 -04:00
|
|
|
(self._user_cachemap_latest_serial, set([user_id]))
|
2015-04-23 13:40:19 -04:00
|
|
|
)
|
2015-04-23 13:40:47 -04:00
|
|
|
while len(self._remote_offline_serials) > MAX_OFFLINE_SERIALS:
|
|
|
|
self._remote_offline_serials.pop() # remove the oldest
|
2014-08-12 10:10:52 -04:00
|
|
|
del self._user_cachemap[user]
|
2015-04-23 14:01:37 -04:00
|
|
|
else:
|
|
|
|
# Remove the user from remote_offline_serials now that they're
|
|
|
|
# no longer offline
|
|
|
|
for idx, elem in enumerate(self._remote_offline_serials):
|
|
|
|
(_, user_ids) = elem
|
|
|
|
user_ids.discard(user_id)
|
|
|
|
if not user_ids:
|
|
|
|
self._remote_offline_serials.pop(idx)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
|
|
|
for poll in content.get("poll", []):
|
2015-01-23 06:47:15 -05:00
|
|
|
user = UserID.from_string(poll)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(user):
|
2014-08-12 10:10:52 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
# TODO(paul) permissions checks
|
|
|
|
|
2015-02-10 12:58:36 -05:00
|
|
|
if user not in self._remote_sendmap:
|
2014-08-12 10:10:52 -04:00
|
|
|
self._remote_sendmap[user] = set()
|
|
|
|
|
|
|
|
self._remote_sendmap[user].add(origin)
|
|
|
|
|
|
|
|
deferreds.append(self._push_presence_remote(user, origin))
|
|
|
|
|
|
|
|
for unpoll in content.get("unpoll", []):
|
2015-01-23 06:47:15 -05:00
|
|
|
user = UserID.from_string(unpoll)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-12-02 05:42:28 -05:00
|
|
|
if not self.hs.is_mine(user):
|
2014-08-12 10:10:52 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
if user in self._remote_sendmap:
|
|
|
|
self._remote_sendmap[user].remove(origin)
|
|
|
|
|
|
|
|
if not self._remote_sendmap[user]:
|
|
|
|
del self._remote_sendmap[user]
|
|
|
|
|
2015-05-08 11:32:18 -04:00
|
|
|
yield defer.DeferredList(deferreds, consumeErrors=True)
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2015-05-18 10:46:37 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def update_presence_cache(self, user, state={}, room_ids=None,
|
|
|
|
add_to_cache=True):
|
|
|
|
"""Update the presence cache for a user with a new state and bump the
|
|
|
|
serial to the latest value.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user(UserID): The user being updated
|
|
|
|
state(dict): The presence state being updated
|
|
|
|
room_ids(None or list of str): A list of room_ids to update. If
|
|
|
|
room_ids is None then fetch the list of room_ids the user is
|
|
|
|
joined to.
|
|
|
|
add_to_cache: Whether to add an entry to the presence cache if the
|
|
|
|
user isn't already in the cache.
|
|
|
|
Returns:
|
|
|
|
A Deferred UserPresenceCache for the user being updated.
|
|
|
|
"""
|
|
|
|
if room_ids is None:
|
|
|
|
room_ids = yield self.get_joined_rooms_for_user(user)
|
2015-05-18 11:21:51 -04:00
|
|
|
|
|
|
|
for room_id in room_ids:
|
|
|
|
self._room_serials[room_id] = self._user_cachemap_latest_serial
|
2015-05-18 10:46:37 -04:00
|
|
|
if add_to_cache:
|
|
|
|
statuscache = self._get_or_make_usercache(user)
|
|
|
|
else:
|
|
|
|
statuscache = self._get_or_offline_usercache(user)
|
|
|
|
statuscache.update(state, serial=self._user_cachemap_latest_serial)
|
|
|
|
defer.returnValue(statuscache)
|
|
|
|
|
2014-08-28 13:43:03 -04:00
|
|
|
@defer.inlineCallbacks
|
2014-09-01 09:45:35 -04:00
|
|
|
def push_update_to_local_and_remote(self, observed_user, statuscache,
|
2014-08-28 13:43:03 -04:00
|
|
|
users_to_push=[], room_ids=[],
|
2014-09-01 09:45:35 -04:00
|
|
|
remote_domains=[]):
|
2015-05-14 10:29:58 -04:00
|
|
|
"""Notify local clients and remote servers of a change in the presence
|
|
|
|
of a user.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
observed_user(UserID): The user to push the presence state for.
|
|
|
|
statuscache(UserPresenceCache): The cache for the presence state to
|
|
|
|
push.
|
|
|
|
users_to_push([UserID]): A list of local and remote users to
|
|
|
|
notify.
|
|
|
|
room_ids([str]): Notify the local and remote occupants of these
|
|
|
|
rooms.
|
|
|
|
remote_domains([str]): A list of remote servers to notify in
|
|
|
|
addition to those implied by the users_to_push and the
|
|
|
|
room_ids.
|
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-08-13 14:18:55 -04:00
|
|
|
|
2014-08-28 13:43:03 -04:00
|
|
|
localusers, remoteusers = partitionbool(
|
|
|
|
users_to_push,
|
2014-12-02 05:42:28 -05:00
|
|
|
lambda u: self.hs.is_mine(u)
|
2014-08-28 13:43:03 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
localusers = set(localusers)
|
|
|
|
|
2014-08-29 07:08:33 -04:00
|
|
|
self.push_update_to_clients(
|
2015-05-14 09:59:31 -04:00
|
|
|
users_to_push=localusers, room_ids=room_ids
|
2014-08-12 10:10:52 -04:00
|
|
|
)
|
|
|
|
|
2014-08-29 07:08:33 -04:00
|
|
|
remote_domains = set(remote_domains)
|
|
|
|
remote_domains |= set([r.domain for r in remoteusers])
|
2014-08-28 13:43:03 -04:00
|
|
|
for room_id in room_ids:
|
|
|
|
remote_domains.update(
|
|
|
|
(yield self.store.get_joined_hosts_for_room(room_id))
|
|
|
|
)
|
|
|
|
|
|
|
|
remote_domains.discard(self.hs.hostname)
|
|
|
|
|
|
|
|
deferreds = []
|
|
|
|
for domain in remote_domains:
|
|
|
|
logger.debug(" | push to remote domain %s", domain)
|
|
|
|
deferreds.append(
|
|
|
|
self._push_presence_remote(
|
|
|
|
observed_user, domain, state=statuscache.get_state()
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2015-02-17 05:03:23 -05:00
|
|
|
yield defer.DeferredList(deferreds, consumeErrors=True)
|
2014-08-28 13:43:03 -04:00
|
|
|
|
|
|
|
defer.returnValue((localusers, remote_domains))
|
|
|
|
|
2015-05-14 09:59:31 -04:00
|
|
|
def push_update_to_clients(self, users_to_push=[], room_ids=[]):
|
|
|
|
"""Notify clients of a new presence event.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 09:59:31 -04:00
|
|
|
Args:
|
2015-05-14 10:29:58 -04:00
|
|
|
users_to_push([UserID]): List of users to notify.
|
|
|
|
room_ids([str]): List of room_ids to notify.
|
2015-05-14 09:59:31 -04:00
|
|
|
"""
|
2015-05-08 14:53:34 -04:00
|
|
|
with PreserveLoggingContext():
|
|
|
|
self.notifier.on_new_user_event(
|
2015-05-13 08:42:21 -04:00
|
|
|
"presence_key",
|
|
|
|
self._user_cachemap_latest_serial,
|
2015-05-08 14:53:34 -04:00
|
|
|
users_to_push,
|
|
|
|
room_ids,
|
|
|
|
)
|
2014-08-28 12:43:15 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _push_presence_remote(self, user, destination, state=None):
|
|
|
|
"""Push a user's presence to a remote server. If a presence state event
|
|
|
|
that event is sent. Otherwise a new state event is constructed from the
|
|
|
|
stored presence state.
|
|
|
|
The last_active is replaced with last_active_ago in case the wallclock
|
|
|
|
time on the remote server is different to the time on this server.
|
|
|
|
Sends an EDU to the remote server with the current presence state.
|
2015-05-14 12:48:12 -04:00
|
|
|
|
2015-05-14 10:29:58 -04:00
|
|
|
Args:
|
|
|
|
user(UserID): The user to push the presence state for.
|
|
|
|
destination(str): The remote server to send state to.
|
|
|
|
state(dict): The state to push, or None to use the current stored
|
|
|
|
state.
|
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
|
|
|
if state is None:
|
|
|
|
state = yield self.store.get_presence_state(user.localpart)
|
|
|
|
del state["mtime"]
|
|
|
|
state["presence"] = state.pop("state")
|
|
|
|
|
|
|
|
if user in self._user_cachemap:
|
|
|
|
state["last_active"] = (
|
|
|
|
self._user_cachemap[user].get_state()["last_active"]
|
|
|
|
)
|
|
|
|
|
|
|
|
yield self.distributor.fire(
|
|
|
|
"collect_presencelike_data", user, state
|
|
|
|
)
|
|
|
|
|
|
|
|
if "last_active" in state:
|
|
|
|
state = dict(state)
|
|
|
|
state["last_active_ago"] = int(
|
|
|
|
self.clock.time_msec() - state.pop("last_active")
|
|
|
|
)
|
|
|
|
|
|
|
|
user_state = {"user_id": user.to_string(), }
|
|
|
|
user_state.update(state)
|
|
|
|
|
|
|
|
yield self.federation.send_edu(
|
|
|
|
destination=destination,
|
|
|
|
edu_type="m.presence",
|
|
|
|
content={"push": [user_state, ], }
|
|
|
|
)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-29 12:09:15 -04:00
|
|
|
class PresenceEventSource(object):
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.hs = hs
|
|
|
|
self.clock = hs.get_clock()
|
|
|
|
|
2014-09-02 11:29:04 -04:00
|
|
|
@defer.inlineCallbacks
|
2014-12-03 14:48:14 -05:00
|
|
|
@log_function
|
2014-08-29 14:13:55 -04:00
|
|
|
def get_new_events_for_user(self, user, from_key, limit):
|
|
|
|
from_key = int(from_key)
|
2014-08-29 12:09:15 -04:00
|
|
|
|
|
|
|
presence = self.hs.get_handlers().presence_handler
|
|
|
|
cachemap = presence._user_cachemap
|
2015-04-28 12:24:24 -04:00
|
|
|
|
|
|
|
max_serial = presence._user_cachemap_latest_serial
|
|
|
|
|
2015-04-23 13:27:25 -04:00
|
|
|
clock = self.clock
|
2015-04-28 12:20:32 -04:00
|
|
|
latest_serial = 0
|
2014-08-29 12:09:15 -04:00
|
|
|
|
2015-05-20 08:21:59 -04:00
|
|
|
user_ids_to_check = {user}
|
2015-05-18 11:21:51 -04:00
|
|
|
presence_list = yield presence.store.get_presence_list(
|
|
|
|
user.localpart, accepted=True
|
|
|
|
)
|
2015-05-20 08:21:59 -04:00
|
|
|
if presence_list is not None:
|
|
|
|
user_ids_to_check |= set(
|
|
|
|
UserID.from_string(p["observed_user_id"]) for p in presence_list
|
|
|
|
)
|
2015-05-18 11:21:51 -04:00
|
|
|
room_ids = yield presence.get_joined_rooms_for_user(user)
|
|
|
|
for room_id in set(room_ids) & set(presence._room_serials):
|
|
|
|
if presence._room_serials[room_id] > from_key:
|
|
|
|
joined = yield presence.get_joined_users_for_room_id(room_id)
|
|
|
|
user_ids_to_check |= set(joined)
|
|
|
|
|
2014-09-02 11:29:04 -04:00
|
|
|
updates = []
|
2015-05-18 11:21:51 -04:00
|
|
|
for observed_user in user_ids_to_check & set(cachemap):
|
2014-09-06 12:38:11 -04:00
|
|
|
cached = cachemap[observed_user]
|
2014-12-03 14:48:14 -05:00
|
|
|
|
2015-05-12 10:44:21 -04:00
|
|
|
if cached.serial <= from_key or cached.serial > max_serial:
|
2014-09-02 11:29:04 -04:00
|
|
|
continue
|
|
|
|
|
2015-04-28 12:20:32 -04:00
|
|
|
latest_serial = max(cached.serial, latest_serial)
|
2015-04-23 13:27:25 -04:00
|
|
|
updates.append(cached.make_event(user=observed_user, clock=clock))
|
2014-09-02 11:29:04 -04:00
|
|
|
|
|
|
|
# TODO(paul): limit
|
2014-08-29 12:09:15 -04:00
|
|
|
|
2015-04-23 13:40:19 -04:00
|
|
|
for serial, user_ids in presence._remote_offline_serials:
|
2015-04-29 12:37:11 -04:00
|
|
|
if serial <= from_key:
|
2015-04-23 13:40:19 -04:00
|
|
|
break
|
|
|
|
|
2015-04-28 12:24:24 -04:00
|
|
|
if serial > max_serial:
|
|
|
|
continue
|
|
|
|
|
2015-04-28 12:23:53 -04:00
|
|
|
latest_serial = max(latest_serial, serial)
|
2015-04-23 13:40:19 -04:00
|
|
|
for u in user_ids:
|
|
|
|
updates.append({
|
|
|
|
"type": "m.presence",
|
|
|
|
"content": {"user_id": u, "presence": PresenceState.OFFLINE},
|
|
|
|
})
|
|
|
|
# TODO(paul): For the v2 API we want to tell the client their from_key
|
|
|
|
# is too old if we fell off the end of the _remote_offline_serials
|
|
|
|
# list, and get them to invalidate+resync. In v1 we have no such
|
|
|
|
# concept so this is a best-effort result.
|
|
|
|
|
2014-08-29 12:09:15 -04:00
|
|
|
if updates:
|
2015-04-23 13:27:25 -04:00
|
|
|
defer.returnValue((updates, latest_serial))
|
2014-08-29 12:09:15 -04:00
|
|
|
else:
|
2014-09-02 11:29:04 -04:00
|
|
|
defer.returnValue(([], presence._user_cachemap_latest_serial))
|
2014-08-29 12:09:15 -04:00
|
|
|
|
2014-08-29 14:15:23 -04:00
|
|
|
def get_current_key(self):
|
2014-08-29 12:09:15 -04:00
|
|
|
presence = self.hs.get_handlers().presence_handler
|
|
|
|
return presence._user_cachemap_latest_serial
|
|
|
|
|
2014-09-02 11:29:04 -04:00
|
|
|
@defer.inlineCallbacks
|
2014-08-29 12:09:15 -04:00
|
|
|
def get_pagination_rows(self, user, pagination_config, key):
|
|
|
|
# TODO (erikj): Does this make sense? Ordering?
|
|
|
|
|
2014-10-29 11:57:23 -04:00
|
|
|
from_key = int(pagination_config.from_key)
|
2014-08-29 12:09:15 -04:00
|
|
|
|
2014-10-29 11:57:23 -04:00
|
|
|
if pagination_config.to_key:
|
|
|
|
to_key = int(pagination_config.to_key)
|
2014-08-29 12:09:15 -04:00
|
|
|
else:
|
|
|
|
to_key = -1
|
|
|
|
|
|
|
|
presence = self.hs.get_handlers().presence_handler
|
|
|
|
cachemap = presence._user_cachemap
|
|
|
|
|
2015-05-18 13:21:06 -04:00
|
|
|
user_ids_to_check = {user}
|
|
|
|
presence_list = yield presence.store.get_presence_list(
|
|
|
|
user.localpart, accepted=True
|
|
|
|
)
|
2015-05-20 08:21:59 -04:00
|
|
|
if presence_list is not None:
|
|
|
|
user_ids_to_check |= set(
|
|
|
|
UserID.from_string(p["observed_user_id"]) for p in presence_list
|
|
|
|
)
|
2015-05-18 13:21:06 -04:00
|
|
|
room_ids = yield presence.get_joined_rooms_for_user(user)
|
|
|
|
for room_id in set(room_ids) & set(presence._room_serials):
|
|
|
|
if presence._room_serials[room_id] >= from_key:
|
|
|
|
joined = yield presence.get_joined_users_for_room_id(room_id)
|
|
|
|
user_ids_to_check |= set(joined)
|
|
|
|
|
2014-09-02 11:29:04 -04:00
|
|
|
updates = []
|
2015-05-18 13:21:06 -04:00
|
|
|
for observed_user in user_ids_to_check & set(cachemap):
|
2014-10-29 12:15:48 -04:00
|
|
|
if not (to_key < cachemap[observed_user].serial <= from_key):
|
2014-09-02 11:29:04 -04:00
|
|
|
continue
|
|
|
|
|
2015-05-18 13:21:06 -04:00
|
|
|
updates.append((observed_user, cachemap[observed_user]))
|
2014-09-02 11:29:04 -04:00
|
|
|
|
|
|
|
# TODO(paul): limit
|
|
|
|
|
2014-08-29 12:09:15 -04:00
|
|
|
if updates:
|
|
|
|
clock = self.clock
|
|
|
|
|
|
|
|
earliest_serial = max([x[1].serial for x in updates])
|
|
|
|
data = [x[1].make_event(user=x[0], clock=clock) for x in updates]
|
|
|
|
|
2014-10-29 11:57:23 -04:00
|
|
|
defer.returnValue((data, earliest_serial))
|
2014-08-29 12:09:15 -04:00
|
|
|
else:
|
2014-10-29 11:57:23 -04:00
|
|
|
defer.returnValue(([], 0))
|
2014-08-29 12:09:15 -04:00
|
|
|
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
class UserPresenceCache(object):
|
|
|
|
"""Store an observed user's state and status message.
|
|
|
|
|
|
|
|
Includes the update timestamp.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
2014-11-19 12:30:35 -05:00
|
|
|
self.state = {"presence": PresenceState.OFFLINE}
|
2014-08-12 10:10:52 -04:00
|
|
|
self.serial = None
|
|
|
|
|
|
|
|
def update(self, state, serial):
|
2014-08-13 14:18:55 -04:00
|
|
|
assert("mtime_age" not in state)
|
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
self.state.update(state)
|
|
|
|
# Delete keys that are now 'None'
|
|
|
|
for k in self.state.keys():
|
|
|
|
if self.state[k] is None:
|
|
|
|
del self.state[k]
|
|
|
|
|
|
|
|
self.serial = serial
|
|
|
|
|
|
|
|
if "status_msg" in state:
|
|
|
|
self.status_msg = state["status_msg"]
|
|
|
|
else:
|
|
|
|
self.status_msg = None
|
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
# clone it so caller can't break our cache
|
2014-09-01 09:45:35 -04:00
|
|
|
state = dict(self.state)
|
|
|
|
return state
|
2014-08-12 10:10:52 -04:00
|
|
|
|
2014-08-13 14:18:55 -04:00
|
|
|
def make_event(self, user, clock):
|
2014-08-12 10:10:52 -04:00
|
|
|
content = self.get_state()
|
|
|
|
content["user_id"] = user.to_string()
|
|
|
|
|
2014-09-01 10:38:37 -04:00
|
|
|
if "last_active" in content:
|
|
|
|
content["last_active_ago"] = int(
|
|
|
|
clock.time_msec() - content.pop("last_active")
|
2014-08-14 09:15:54 -04:00
|
|
|
)
|
2014-08-13 14:18:55 -04:00
|
|
|
|
2014-08-12 10:10:52 -04:00
|
|
|
return {"type": "m.presence", "content": content}
|