2014-08-12 08:10:52 -06:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-01-06 21:26:29 -07:00
|
|
|
# Copyright 2014-2016 OpenMarket Ltd
|
2014-08-12 08:10:52 -06: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 20:14:34 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
"""This module is responsible for keeping track of presence status of local
|
|
|
|
and remote users.
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
The methods that define policy are:
|
|
|
|
- PresenceHandler._update_states
|
|
|
|
- PresenceHandler._handle_timeouts
|
|
|
|
- should_notify
|
|
|
|
"""
|
|
|
|
|
|
|
|
from twisted.internet import defer, reactor
|
|
|
|
from contextlib import contextmanager
|
|
|
|
|
|
|
|
from synapse.api.errors import SynapseError
|
2014-08-12 08:10:52 -06:00
|
|
|
from synapse.api.constants import PresenceState
|
2016-02-15 10:10:40 -07:00
|
|
|
from synapse.storage.presence import UserPresenceState
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
from synapse.util.logcontext import preserve_fn
|
2014-08-28 11:45:00 -06:00
|
|
|
from synapse.util.logutils import log_function
|
2016-02-19 04:50:48 -07:00
|
|
|
from synapse.util.metrics import Measure
|
2016-02-15 10:10:40 -07:00
|
|
|
from synapse.util.wheel_timer import WheelTimer
|
2015-01-23 04:47:15 -07:00
|
|
|
from synapse.types import UserID
|
2015-03-04 09:50:23 -07:00
|
|
|
import synapse.metrics
|
2014-08-28 07:58:51 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
from ._base import BaseHandler
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-03-04 09:50:23 -07:00
|
|
|
metrics = synapse.metrics.get_metrics_for(__name__)
|
|
|
|
|
2016-02-19 02:50:54 -07:00
|
|
|
notified_presence_counter = metrics.register_counter("notified_presence")
|
2016-02-23 07:03:46 -07:00
|
|
|
federation_presence_out_counter = metrics.register_counter("federation_presence_out")
|
2016-02-19 02:50:54 -07:00
|
|
|
presence_updates_counter = metrics.register_counter("presence_updates")
|
2016-02-19 03:21:41 -07:00
|
|
|
timers_fired_counter = metrics.register_counter("timers_fired")
|
|
|
|
federation_presence_counter = metrics.register_counter("federation_presence")
|
2016-02-19 04:32:04 -07:00
|
|
|
bump_active_time_counter = metrics.register_counter("bump_active_time")
|
2016-02-19 02:50:54 -07:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# If a user was last active in the last LAST_ACTIVE_GRANULARITY, consider them
|
|
|
|
# "currently_active"
|
2016-02-02 10:18:50 -07:00
|
|
|
LAST_ACTIVE_GRANULARITY = 60 * 1000
|
2015-03-23 11:25:44 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# How long to wait until a new /events or /sync request before assuming
|
|
|
|
# the client has gone.
|
|
|
|
SYNC_ONLINE_TIMEOUT = 30 * 1000
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# How long to wait before marking the user as idle. Compared against last active
|
|
|
|
IDLE_TIMER = 5 * 60 * 1000
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# How often we expect remote servers to resend us presence.
|
|
|
|
FEDERATION_TIMEOUT = 30 * 60 * 1000
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# How often to resend presence to remote servers
|
|
|
|
FEDERATION_PING_INTERVAL = 25 * 60 * 1000
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
assert LAST_ACTIVE_GRANULARITY < IDLE_TIMER
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
|
2015-12-01 13:53:04 -07:00
|
|
|
def user_presence_changed(distributor, user, statuscache):
|
|
|
|
return distributor.fire("user_presence_changed", user, statuscache)
|
|
|
|
|
|
|
|
|
|
|
|
def collect_presencelike_data(distributor, user, content):
|
|
|
|
return distributor.fire("collect_presencelike_data", user, content)
|
|
|
|
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
class PresenceHandler(BaseHandler):
|
|
|
|
|
|
|
|
def __init__(self, hs):
|
|
|
|
super(PresenceHandler, self).__init__(hs)
|
2016-02-15 10:10:40 -07:00
|
|
|
self.hs = hs
|
2014-08-13 12:18:55 -06:00
|
|
|
self.clock = hs.get_clock()
|
2016-02-15 10:10:40 -07:00
|
|
|
self.store = hs.get_datastore()
|
|
|
|
self.wheel_timer = WheelTimer()
|
|
|
|
self.notifier = hs.get_notifier()
|
2014-08-12 08:10:52 -06:00
|
|
|
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 04:47:15 -07:00
|
|
|
observed_user=UserID.from_string(content["observed_user"]),
|
|
|
|
observer_user=UserID.from_string(content["observer_user"]),
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.federation.register_edu_handler(
|
|
|
|
"m.presence_accept",
|
|
|
|
lambda origin, content: self.accept_presence(
|
2015-01-23 04:47:15 -07:00
|
|
|
observed_user=UserID.from_string(content["observed_user"]),
|
|
|
|
observer_user=UserID.from_string(content["observer_user"]),
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
)
|
|
|
|
self.federation.register_edu_handler(
|
|
|
|
"m.presence_deny",
|
|
|
|
lambda origin, content: self.deny_presence(
|
2015-01-23 04:47:15 -07:00
|
|
|
observed_user=UserID.from_string(content["observed_user"]),
|
|
|
|
observer_user=UserID.from_string(content["observer_user"]),
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
distributor = hs.get_distributor()
|
|
|
|
distributor.observe("user_joined_room", self.user_joined_room)
|
|
|
|
|
|
|
|
active_presence = self.store.take_presence_startup_info()
|
|
|
|
|
|
|
|
# A dictionary of the current state of users. This is prefilled with
|
|
|
|
# non-offline presence from the DB. We should fetch from the DB if
|
|
|
|
# we can't find a users presence in here.
|
|
|
|
self.user_to_current_state = {
|
|
|
|
state.user_id: state
|
|
|
|
for state in active_presence
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:56:39 -07:00
|
|
|
metrics.register_callback(
|
|
|
|
"user_to_current_state_size", lambda: len(self.user_to_current_state)
|
|
|
|
)
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
now = self.clock.time_msec()
|
|
|
|
for state in active_presence:
|
|
|
|
self.wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=state.user_id,
|
2016-02-18 03:11:43 -07:00
|
|
|
then=state.last_active_ts + IDLE_TIMER,
|
2016-02-15 10:10:40 -07:00
|
|
|
)
|
|
|
|
self.wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=state.user_id,
|
2016-02-18 03:11:43 -07:00
|
|
|
then=state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT,
|
2016-02-15 10:10:40 -07:00
|
|
|
)
|
|
|
|
if self.hs.is_mine_id(state.user_id):
|
|
|
|
self.wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=state.user_id,
|
2016-02-18 03:11:43 -07:00
|
|
|
then=state.last_federation_update_ts + FEDERATION_PING_INTERVAL,
|
2016-02-15 10:10:40 -07:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=state.user_id,
|
2016-02-18 03:11:43 -07:00
|
|
|
then=state.last_federation_update_ts + FEDERATION_TIMEOUT,
|
2016-02-15 10:10:40 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
# Set of users who have presence in the `user_to_current_state` that
|
|
|
|
# have not yet been persisted
|
|
|
|
self.unpersisted_users_changes = set()
|
|
|
|
|
|
|
|
reactor.addSystemEventTrigger("before", "shutdown", self._on_shutdown)
|
|
|
|
|
|
|
|
self.serial_to_user = {}
|
|
|
|
self._next_serial = 1
|
|
|
|
|
|
|
|
# Keeps track of the number of *ongoing* syncs. While this is non zero
|
|
|
|
# a user will never go offline.
|
|
|
|
self.user_to_num_current_syncs = {}
|
|
|
|
|
|
|
|
# Start a LoopingCall in 30s that fires every 5s.
|
|
|
|
# The initial delay is to allow disconnected clients a chance to
|
|
|
|
# reconnect before we treat them as offline.
|
|
|
|
self.clock.call_later(
|
|
|
|
0 * 1000,
|
|
|
|
self.clock.looping_call,
|
|
|
|
self._handle_timeouts,
|
|
|
|
5000,
|
2015-03-04 09:50:23 -07:00
|
|
|
)
|
|
|
|
|
2016-02-19 02:50:54 -07:00
|
|
|
metrics.register_callback("wheel_timer_size", lambda: len(self.wheel_timer))
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _on_shutdown(self):
|
|
|
|
"""Gets called when shutting down. This lets us persist any updates that
|
|
|
|
we haven't yet persisted, e.g. updates that only changes some internal
|
|
|
|
timers. This allows changes to persist across startup without having to
|
|
|
|
persist every single change.
|
|
|
|
|
|
|
|
If this does not run it simply means that some of the timers will fire
|
|
|
|
earlier than they should when synapse is restarted. This affect of this
|
|
|
|
is some spurious presence changes that will self-correct.
|
|
|
|
"""
|
|
|
|
logger.info(
|
|
|
|
"Performing _on_shutdown. Persiting %d unpersisted changes",
|
|
|
|
len(self.user_to_current_state)
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
if self.unpersisted_users_changes:
|
|
|
|
yield self.store.update_presence([
|
|
|
|
self.user_to_current_state[user_id]
|
|
|
|
for user_id in self.unpersisted_users_changes
|
|
|
|
])
|
|
|
|
logger.info("Finished _on_shutdown")
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
2016-02-15 10:10:40 -07:00
|
|
|
def _update_states(self, new_states):
|
|
|
|
"""Updates presence of users. Sets the appropriate timeouts. Pokes
|
|
|
|
the notifier and federation if and only if the changed presence state
|
|
|
|
should be sent to clients/servers.
|
|
|
|
"""
|
|
|
|
now = self.clock.time_msec()
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
with Measure(self.clock, "presence_update_states"):
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
# NOTE: We purposefully don't yield between now and when we've
|
|
|
|
# calculated what we want to do with the new states, to avoid races.
|
2016-02-18 02:54:08 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
to_notify = {} # Changes we want to notify everyone about
|
|
|
|
to_federation_ping = {} # These need sending keep-alives
|
2014-09-02 06:30:36 -06:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
for new_state in new_states:
|
|
|
|
user_id = new_state.user_id
|
2015-08-18 03:30:07 -06:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
# Its fine to not hit the database here, as the only thing not in
|
|
|
|
# the current state cache are OFFLINE states, where the only field
|
|
|
|
# of interest is last_active which is safe enough to assume is 0
|
|
|
|
# here.
|
|
|
|
prev_state = self.user_to_current_state.get(
|
|
|
|
user_id, UserPresenceState.default(user_id)
|
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
new_state, should_notify, should_ping = handle_update(
|
|
|
|
prev_state, new_state,
|
|
|
|
is_mine=self.hs.is_mine_id(user_id),
|
|
|
|
wheel_timer=self.wheel_timer,
|
|
|
|
now=now
|
|
|
|
)
|
2016-02-15 10:10:40 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
self.user_to_current_state[user_id] = new_state
|
2014-08-13 12:18:55 -06:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
if should_notify:
|
|
|
|
to_notify[user_id] = new_state
|
|
|
|
elif should_ping:
|
|
|
|
to_federation_ping[user_id] = new_state
|
2016-02-19 02:50:54 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
# TODO: We should probably ensure there are no races hereafter
|
2016-02-15 10:10:40 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
presence_updates_counter.inc_by(len(new_states))
|
2016-02-15 10:10:40 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
if to_notify:
|
|
|
|
notified_presence_counter.inc_by(len(to_notify))
|
|
|
|
yield self._persist_and_notify(to_notify.values())
|
|
|
|
|
|
|
|
self.unpersisted_users_changes |= set(s.user_id for s in new_states)
|
|
|
|
self.unpersisted_users_changes -= set(to_notify.keys())
|
2014-11-18 08:25:55 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
to_federation_ping = {
|
|
|
|
user_id: state for user_id, state in to_federation_ping.items()
|
|
|
|
if user_id not in to_notify
|
|
|
|
}
|
|
|
|
if to_federation_ping:
|
2016-02-23 07:03:46 -07:00
|
|
|
federation_presence_out_counter.inc_by(len(to_federation_ping))
|
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
_, _, hosts_to_states = yield self._get_interested_parties(
|
|
|
|
to_federation_ping.values()
|
|
|
|
)
|
|
|
|
|
|
|
|
self._push_to_remotes(hosts_to_states)
|
2014-11-18 08:25:55 -07:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
def _handle_timeouts(self):
|
|
|
|
"""Checks the presence of users that have timed out and updates as
|
|
|
|
appropriate.
|
|
|
|
"""
|
|
|
|
now = self.clock.time_msec()
|
2014-11-18 08:25:55 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
with Measure(self.clock, "presence_handle_timeouts"):
|
|
|
|
# Fetch the list of users that *may* have timed out. Things may have
|
|
|
|
# changed since the timeout was set, so we won't necessarily have to
|
|
|
|
# take any action.
|
|
|
|
users_to_check = self.wheel_timer.fetch(now)
|
2014-11-18 08:25:55 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
states = [
|
|
|
|
self.user_to_current_state.get(
|
|
|
|
user_id, UserPresenceState.default(user_id)
|
|
|
|
)
|
|
|
|
for user_id in set(users_to_check)
|
|
|
|
]
|
2016-02-18 04:52:33 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
timers_fired_counter.inc_by(len(states))
|
2016-02-19 03:21:41 -07:00
|
|
|
|
2016-02-19 04:50:48 -07:00
|
|
|
changes = handle_timeouts(
|
|
|
|
states,
|
|
|
|
is_mine_fn=self.hs.is_mine_id,
|
|
|
|
user_to_num_current_syncs=self.user_to_num_current_syncs,
|
|
|
|
now=now,
|
|
|
|
)
|
2015-08-18 03:30:07 -06:00
|
|
|
|
2016-02-18 04:52:33 -07:00
|
|
|
preserve_fn(self._update_states)(changes)
|
2016-02-15 10:10:40 -07:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def bump_presence_active_time(self, user):
|
|
|
|
"""We've seen the user do something that indicates they're interacting
|
|
|
|
with the app.
|
2015-08-18 03:30:07 -06:00
|
|
|
"""
|
2016-02-15 10:10:40 -07:00
|
|
|
user_id = user.to_string()
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-19 04:32:04 -07:00
|
|
|
bump_active_time_counter.inc()
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
prev_state = yield self.current_state_for_user(user_id)
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-18 02:54:08 -07:00
|
|
|
new_fields = {
|
2016-02-18 03:11:43 -07:00
|
|
|
"last_active_ts": self.clock.time_msec(),
|
2016-02-18 02:54:08 -07:00
|
|
|
}
|
|
|
|
if prev_state.state == PresenceState.UNAVAILABLE:
|
|
|
|
new_fields["state"] = PresenceState.ONLINE
|
|
|
|
|
|
|
|
yield self._update_states([prev_state.copy_and_replace(**new_fields)])
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def user_syncing(self, user_id, affect_presence=True):
|
|
|
|
"""Returns a context manager that should surround any stream requests
|
|
|
|
from the user.
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
This allows us to keep track of who is currently streaming and who isn't
|
|
|
|
without having to have timers outside of this module to avoid flickering
|
|
|
|
when users disconnect/reconnect.
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
Args:
|
|
|
|
user_id (str)
|
|
|
|
affect_presence (bool): If false this function will be a no-op.
|
|
|
|
Useful for streams that are not associated with an actual
|
|
|
|
client that is being used by a user.
|
|
|
|
"""
|
|
|
|
if affect_presence:
|
|
|
|
curr_sync = self.user_to_num_current_syncs.get(user_id, 0)
|
|
|
|
self.user_to_num_current_syncs[user_id] = curr_sync + 1
|
|
|
|
|
|
|
|
prev_state = yield self.current_state_for_user(user_id)
|
|
|
|
if prev_state.state == PresenceState.OFFLINE:
|
|
|
|
# If they're currently offline then bring them online, otherwise
|
|
|
|
# just update the last sync times.
|
|
|
|
yield self._update_states([prev_state.copy_and_replace(
|
|
|
|
state=PresenceState.ONLINE,
|
2016-02-18 03:11:43 -07:00
|
|
|
last_active_ts=self.clock.time_msec(),
|
|
|
|
last_user_sync_ts=self.clock.time_msec(),
|
2016-02-15 10:10:40 -07:00
|
|
|
)])
|
|
|
|
else:
|
|
|
|
yield self._update_states([prev_state.copy_and_replace(
|
2016-02-18 03:11:43 -07:00
|
|
|
last_user_sync_ts=self.clock.time_msec(),
|
2016-02-15 10:10:40 -07:00
|
|
|
)])
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _end():
|
|
|
|
if affect_presence:
|
|
|
|
self.user_to_num_current_syncs[user_id] -= 1
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
prev_state = yield self.current_state_for_user(user_id)
|
|
|
|
yield self._update_states([prev_state.copy_and_replace(
|
2016-02-18 03:11:43 -07:00
|
|
|
last_user_sync_ts=self.clock.time_msec(),
|
2016-02-15 10:10:40 -07:00
|
|
|
)])
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
@contextmanager
|
|
|
|
def _user_syncing():
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
preserve_fn(_end)()
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
defer.returnValue(_user_syncing())
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def current_state_for_user(self, user_id):
|
|
|
|
"""Get the current presence state for a user.
|
|
|
|
"""
|
|
|
|
res = yield self.current_state_for_users([user_id])
|
|
|
|
defer.returnValue(res[user_id])
|
2015-08-17 03:40:23 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
@defer.inlineCallbacks
|
2016-02-15 10:10:40 -07:00
|
|
|
def current_state_for_users(self, user_ids):
|
|
|
|
"""Get the current presence state for multiple users.
|
2014-08-22 11:02:45 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
Returns:
|
|
|
|
dict: `user_id` -> `UserPresenceState`
|
|
|
|
"""
|
|
|
|
states = {
|
|
|
|
user_id: self.user_to_current_state.get(user_id, None)
|
|
|
|
for user_id in user_ids
|
|
|
|
}
|
|
|
|
|
|
|
|
missing = [user_id for user_id, state in states.items() if not state]
|
|
|
|
if missing:
|
|
|
|
# There are things not in our in memory cache. Lets pull them out of
|
|
|
|
# the database.
|
|
|
|
res = yield self.store.get_presence_for_users(missing)
|
|
|
|
states.update({state.user_id: state for state in res})
|
|
|
|
|
|
|
|
missing = [user_id for user_id, state in states.items() if not state]
|
|
|
|
if missing:
|
2016-02-18 03:26:24 -07:00
|
|
|
new = {
|
2016-02-15 10:10:40 -07:00
|
|
|
user_id: UserPresenceState.default(user_id)
|
|
|
|
for user_id in missing
|
2016-02-18 03:26:24 -07:00
|
|
|
}
|
|
|
|
states.update(new)
|
|
|
|
self.user_to_current_state.update(new)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
defer.returnValue(states)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _get_interested_parties(self, states):
|
|
|
|
"""Given a list of states return which entities (rooms, users, servers)
|
|
|
|
are interested in the given states.
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
Returns:
|
|
|
|
3-tuple: `(room_ids_to_states, users_to_states, hosts_to_states)`,
|
|
|
|
with each item being a dict of `entity_name` -> `[UserPresenceState]`
|
|
|
|
"""
|
|
|
|
room_ids_to_states = {}
|
|
|
|
users_to_states = {}
|
|
|
|
for state in states:
|
|
|
|
events = yield self.store.get_rooms_for_user(state.user_id)
|
|
|
|
for e in events:
|
|
|
|
room_ids_to_states.setdefault(e.room_id, []).append(state)
|
|
|
|
|
|
|
|
plist = yield self.store.get_presence_list_observers_accepted(state.user_id)
|
|
|
|
for u in plist:
|
|
|
|
users_to_states.setdefault(u, []).append(state)
|
|
|
|
|
|
|
|
# Always notify self
|
|
|
|
users_to_states.setdefault(state.user_id, []).append(state)
|
|
|
|
|
|
|
|
hosts_to_states = {}
|
|
|
|
for room_id, states in room_ids_to_states.items():
|
2016-02-19 05:27:35 -07:00
|
|
|
local_states = filter(lambda s: self.hs.is_mine_id(s.user_id), states)
|
2016-02-19 05:07:49 -07:00
|
|
|
if not local_states:
|
|
|
|
continue
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
hosts = yield self.store.get_joined_hosts_for_room(room_id)
|
|
|
|
for host in hosts:
|
2016-02-19 05:07:49 -07:00
|
|
|
hosts_to_states.setdefault(host, []).extend(local_states)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
for user_id, states in users_to_states.items():
|
2016-02-19 05:27:35 -07:00
|
|
|
local_states = filter(lambda s: self.hs.is_mine_id(s.user_id), states)
|
2016-02-19 05:07:49 -07:00
|
|
|
if not local_states:
|
|
|
|
continue
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
host = UserID.from_string(user_id).domain
|
2016-02-19 05:07:49 -07:00
|
|
|
hosts_to_states.setdefault(host, []).extend(local_states)
|
2014-09-01 08:38:37 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# TODO: de-dup hosts_to_states, as a single host might have multiple
|
|
|
|
# of same presence
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
defer.returnValue((room_ids_to_states, users_to_states, hosts_to_states))
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def _persist_and_notify(self, states):
|
|
|
|
"""Persist states in the database, poke the notifier and send to
|
|
|
|
interested remote servers
|
|
|
|
"""
|
|
|
|
stream_id, max_token = yield self.store.update_presence(states)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
parties = yield self._get_interested_parties(states)
|
|
|
|
room_ids_to_states, users_to_states, hosts_to_states = parties
|
2014-09-01 08:38:37 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
self.notifier.on_new_event(
|
|
|
|
"presence_key", stream_id, rooms=room_ids_to_states.keys(),
|
|
|
|
users=[UserID.from_string(u) for u in users_to_states.keys()]
|
2014-11-20 09:24:00 -07:00
|
|
|
)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
self._push_to_remotes(hosts_to_states)
|
|
|
|
|
|
|
|
def _push_to_remotes(self, hosts_to_states):
|
|
|
|
"""Sends state updates to remote servers.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
hosts_to_states (dict): Mapping `server_name` -> `[UserPresenceState]`
|
|
|
|
"""
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
for host, states in hosts_to_states.items():
|
|
|
|
self.federation.send_edu(
|
|
|
|
destination=host,
|
|
|
|
edu_type="m.presence",
|
|
|
|
content={
|
|
|
|
"push": [
|
|
|
|
_format_user_presence_state(state, now)
|
|
|
|
for state in states
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def incoming_presence(self, origin, content):
|
|
|
|
"""Called when we receive a `m.presence` EDU from a remote server.
|
|
|
|
"""
|
|
|
|
now = self.clock.time_msec()
|
|
|
|
updates = []
|
|
|
|
for push in content.get("push", []):
|
|
|
|
# A "push" contains a list of presence that we are probably interested
|
|
|
|
# in.
|
|
|
|
# TODO: Actually check if we're interested, rather than blindly
|
|
|
|
# accepting presence updates.
|
|
|
|
user_id = push.get("user_id", None)
|
|
|
|
if not user_id:
|
|
|
|
logger.info(
|
|
|
|
"Got presence update from %r with no 'user_id': %r",
|
|
|
|
origin, push,
|
|
|
|
)
|
|
|
|
continue
|
2014-08-13 12:18:55 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
presence_state = push.get("presence", None)
|
|
|
|
if not presence_state:
|
|
|
|
logger.info(
|
|
|
|
"Got presence update from %r with no 'presence_state': %r",
|
|
|
|
origin, push,
|
|
|
|
)
|
|
|
|
continue
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
new_fields = {
|
|
|
|
"state": presence_state,
|
2016-02-18 03:11:43 -07:00
|
|
|
"last_federation_update_ts": now,
|
2016-02-15 10:10:40 -07:00
|
|
|
}
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
last_active_ago = push.get("last_active_ago", None)
|
|
|
|
if last_active_ago is not None:
|
2016-02-18 03:11:43 -07:00
|
|
|
new_fields["last_active_ts"] = now - last_active_ago
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
new_fields["status_msg"] = push.get("status_msg", None)
|
2016-02-23 03:40:11 -07:00
|
|
|
new_fields["currently_active"] = push.get("currently_active", False)
|
2014-09-01 09:16:35 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
prev_state = yield self.current_state_for_user(user_id)
|
|
|
|
updates.append(prev_state.copy_and_replace(**new_fields))
|
2015-03-23 11:25:44 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
if updates:
|
2016-02-19 03:21:41 -07:00
|
|
|
federation_presence_counter.inc_by(len(updates))
|
2016-02-15 10:10:40 -07:00
|
|
|
yield self._update_states(updates)
|
2014-09-01 09:16:35 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_state(self, target_user, as_event=False):
|
|
|
|
results = yield self.get_states(
|
|
|
|
[target_user.to_string()],
|
|
|
|
as_event=as_event,
|
|
|
|
)
|
|
|
|
|
|
|
|
defer.returnValue(results[0])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_states(self, target_user_ids, as_event=False):
|
|
|
|
"""Get the presence state for users.
|
2015-05-18 06:46:47 -06:00
|
|
|
|
|
|
|
Args:
|
2016-02-15 10:10:40 -07:00
|
|
|
target_user_ids (list)
|
|
|
|
as_event (bool): Whether to format it as a client event or not.
|
|
|
|
|
2015-05-18 06:46:47 -06:00
|
|
|
Returns:
|
2016-02-15 10:10:40 -07:00
|
|
|
list
|
2015-05-18 06:46:47 -06:00
|
|
|
"""
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
updates = yield self.current_state_for_users(target_user_ids)
|
|
|
|
updates = updates.values()
|
2015-05-18 08:46:37 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
for user_id in set(target_user_ids) - set(u.user_id for u in updates):
|
|
|
|
updates.append(UserPresenceState.default(user_id))
|
2015-05-18 06:46:47 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
now = self.clock.time_msec()
|
|
|
|
if as_event:
|
|
|
|
defer.returnValue([
|
|
|
|
{
|
|
|
|
"type": "m.presence",
|
|
|
|
"content": _format_user_presence_state(state, now),
|
|
|
|
}
|
|
|
|
for state in updates
|
|
|
|
])
|
|
|
|
else:
|
|
|
|
defer.returnValue([
|
|
|
|
_format_user_presence_state(state, now) for state in updates
|
|
|
|
])
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def set_state(self, target_user, state):
|
|
|
|
"""Set the presence state of the user.
|
2015-05-18 06:46:47 -06:00
|
|
|
"""
|
2016-02-15 10:10:40 -07:00
|
|
|
status_msg = state.get("status_msg", None)
|
|
|
|
presence = state["presence"]
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-18 02:16:32 -07:00
|
|
|
valid_presence = (
|
|
|
|
PresenceState.ONLINE, PresenceState.UNAVAILABLE, PresenceState.OFFLINE
|
|
|
|
)
|
|
|
|
if presence not in valid_presence:
|
|
|
|
raise SynapseError(400, "Invalid presence state")
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
user_id = target_user.to_string()
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
prev_state = yield self.current_state_for_user(user_id)
|
|
|
|
|
|
|
|
new_fields = {
|
|
|
|
"state": presence,
|
2016-02-18 02:54:08 -07:00
|
|
|
"status_msg": status_msg if presence != PresenceState.OFFLINE else None
|
2016-02-15 10:10:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if presence == PresenceState.ONLINE:
|
2016-02-18 03:11:43 -07:00
|
|
|
new_fields["last_active_ts"] = self.clock.time_msec()
|
2016-02-15 10:10:40 -07:00
|
|
|
|
|
|
|
yield self._update_states([prev_state.copy_and_replace(**new_fields)])
|
2014-08-12 08:10:52 -06:00
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def user_joined_room(self, user, room_id):
|
2016-02-15 10:10:40 -07:00
|
|
|
"""Called (via the distributor) when a user joins a room. This funciton
|
|
|
|
sends presence updates to servers, either:
|
|
|
|
1. the joining user is a local user and we send their presence to
|
|
|
|
all servers in the room.
|
|
|
|
2. the joining user is a remote user and so we send presence for all
|
|
|
|
local users in the room.
|
2015-05-14 08:29:58 -06:00
|
|
|
"""
|
2016-02-15 10:10:40 -07:00
|
|
|
# We only need to send presence to servers that don't have it yet. We
|
|
|
|
# don't need to send to local clients here, as that is done as part
|
|
|
|
# of the event stream/sync.
|
|
|
|
# TODO: Only send to servers not already in the room.
|
2014-12-02 03:42:28 -07:00
|
|
|
if self.hs.is_mine(user):
|
2016-02-15 10:10:40 -07:00
|
|
|
state = yield self.current_state_for_user(user.to_string())
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
hosts = yield self.store.get_joined_hosts_for_room(room_id)
|
|
|
|
self._push_to_remotes({host: (state,) for host in hosts})
|
|
|
|
else:
|
|
|
|
user_ids = yield self.store.get_users_in_room(room_id)
|
|
|
|
user_ids = filter(self.hs.is_mine_id, user_ids)
|
2014-08-29 05:08:33 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
states = yield self.current_state_for_users(user_ids)
|
2015-05-18 08:46:37 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
self._push_to_remotes({user.domain: states.values()})
|
2014-08-29 05:08:33 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
@defer.inlineCallbacks
|
2016-02-15 10:10:40 -07:00
|
|
|
def get_presence_list(self, observer_user, accepted=None):
|
|
|
|
"""Returns the presence for all users in their presence list.
|
|
|
|
"""
|
2014-12-02 03:42:28 -07:00
|
|
|
if not self.hs.is_mine(observer_user):
|
2014-08-12 08:10:52 -06:00
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
presence_list = yield self.store.get_presence_list(
|
|
|
|
observer_user.localpart, accepted=accepted
|
|
|
|
)
|
|
|
|
|
|
|
|
results = yield self.get_states(
|
|
|
|
target_user_ids=[row["observed_user_id"] for row in presence_list],
|
|
|
|
as_event=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
is_accepted = {
|
|
|
|
row["observed_user_id"]: row["accepted"] for row in presence_list
|
|
|
|
}
|
|
|
|
|
|
|
|
for result in results:
|
|
|
|
result.update({
|
|
|
|
"accepted": is_accepted,
|
|
|
|
})
|
|
|
|
|
|
|
|
defer.returnValue(results)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def send_presence_invite(self, observer_user, observed_user):
|
|
|
|
"""Sends a presence invite.
|
|
|
|
"""
|
2014-08-12 08:10:52 -06:00
|
|
|
yield self.store.add_presence_list_pending(
|
|
|
|
observer_user.localpart, observed_user.to_string()
|
|
|
|
)
|
|
|
|
|
2014-12-02 03:42:28 -07:00
|
|
|
if self.hs.is_mine(observed_user):
|
2014-08-12 08:10:52 -06: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 invite_presence(self, observed_user, observer_user):
|
2016-02-15 10:10:40 -07:00
|
|
|
"""Handles new presence invites.
|
2015-05-14 08:29:58 -06:00
|
|
|
"""
|
2016-02-15 10:10:40 -07:00
|
|
|
if not self.hs.is_mine(observed_user):
|
|
|
|
raise SynapseError(400, "User is not hosted on this Home Server")
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# TODO: Don't auto accept
|
2014-12-02 03:42:28 -07:00
|
|
|
if self.hs.is_mine(observer_user):
|
2016-02-15 10:10:40 -07:00
|
|
|
yield self.accept_presence(observed_user, observer_user)
|
2014-08-12 08:10:52 -06:00
|
|
|
else:
|
2016-02-15 10:10:40 -07:00
|
|
|
self.federation.send_edu(
|
2014-08-12 08:10:52 -06:00
|
|
|
destination=observer_user.domain,
|
2016-02-15 10:10:40 -07:00
|
|
|
edu_type="m.presence_accept",
|
2014-08-12 08:10:52 -06:00
|
|
|
content={
|
|
|
|
"observed_user": observed_user.to_string(),
|
|
|
|
"observer_user": observer_user.to_string(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
state_dict = yield self.get_state(observed_user, as_event=False)
|
|
|
|
|
|
|
|
self.federation.send_edu(
|
|
|
|
destination=observer_user.domain,
|
|
|
|
edu_type="m.presence",
|
|
|
|
content={
|
|
|
|
"push": [state_dict]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def accept_presence(self, observed_user, observer_user):
|
2015-05-14 08:29:58 -06: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 08:10:52 -06:00
|
|
|
yield self.store.set_presence_list_accepted(
|
|
|
|
observer_user.localpart, observed_user.to_string()
|
|
|
|
)
|
2015-05-08 09:32:18 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def deny_presence(self, observed_user, observer_user):
|
2015-05-14 08:29:58 -06:00
|
|
|
"""Handle a m.presence_deny EDU. Removes a local or remote user from a
|
|
|
|
local user's presence list.
|
|
|
|
Args:
|
2015-05-14 10:48:12 -06: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 08:29:58 -06:00
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-08-12 08:10:52 -06: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 08:29:58 -06: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 10:48:12 -06: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 08:29:58 -06:00
|
|
|
Returns:
|
|
|
|
A Deferred.
|
|
|
|
"""
|
2014-12-02 03:42:28 -07:00
|
|
|
if not self.hs.is_mine(observer_user):
|
2014-08-12 08:10:52 -06: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()
|
|
|
|
)
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
# TODO: Inform the remote that we've dropped the presence list.
|
2015-05-18 08:46:37 -06:00
|
|
|
|
2014-08-28 11:43:03 -06:00
|
|
|
@defer.inlineCallbacks
|
2016-02-15 10:10:40 -07:00
|
|
|
def is_visible(self, observed_user, observer_user):
|
2016-02-18 02:09:50 -07:00
|
|
|
"""Returns whether a user can see another user's presence.
|
|
|
|
"""
|
2016-02-15 10:10:40 -07:00
|
|
|
observer_rooms = yield self.store.get_rooms_for_user(observer_user.to_string())
|
|
|
|
observed_rooms = yield self.store.get_rooms_for_user(observed_user.to_string())
|
2014-08-13 12:18:55 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
observer_room_ids = set(r.room_id for r in observer_rooms)
|
|
|
|
observed_room_ids = set(r.room_id for r in observed_rooms)
|
2014-08-28 11:43:03 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
if observer_room_ids & observed_room_ids:
|
|
|
|
defer.returnValue(True)
|
2014-08-28 11:43:03 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
accepted_observers = yield self.store.get_presence_list_observers_accepted(
|
|
|
|
observed_user.to_string()
|
2014-08-12 08:10:52 -06:00
|
|
|
)
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
defer.returnValue(observer_user.to_string() in accepted_observers)
|
2014-08-28 11:43:03 -06:00
|
|
|
|
2016-03-01 07:49:41 -07:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def get_all_presence_updates(self, last_id, current_id):
|
|
|
|
"""
|
|
|
|
Gets a list of presence update rows from between the given stream ids.
|
|
|
|
Each row has:
|
|
|
|
- stream_id(str)
|
|
|
|
- user_id(str)
|
|
|
|
- state(str)
|
|
|
|
- last_active_ts(int)
|
|
|
|
- last_federation_update_ts(int)
|
|
|
|
- last_user_sync_ts(int)
|
|
|
|
- status_msg(int)
|
|
|
|
- currently_active(int)
|
|
|
|
"""
|
|
|
|
# TODO(markjh): replicate the unpersisted changes.
|
|
|
|
# This could use the in-memory stores for recent changes.
|
|
|
|
rows = yield self.store.get_all_presence_updates(last_id, current_id)
|
|
|
|
defer.returnValue(rows)
|
|
|
|
|
2014-08-28 11:43:03 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
def should_notify(old_state, new_state):
|
|
|
|
"""Decides if a presence state change should be sent to interested parties.
|
|
|
|
"""
|
|
|
|
if old_state.status_msg != new_state.status_msg:
|
|
|
|
return True
|
2014-08-28 11:43:03 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
if old_state.state == PresenceState.ONLINE:
|
|
|
|
if new_state.state != PresenceState.ONLINE:
|
|
|
|
# Always notify for online -> anything
|
|
|
|
return True
|
2014-08-28 11:43:03 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
if new_state.currently_active != old_state.currently_active:
|
|
|
|
return True
|
2015-05-14 10:48:12 -06:00
|
|
|
|
2016-02-18 03:11:43 -07:00
|
|
|
if new_state.last_active_ts - old_state.last_active_ts > LAST_ACTIVE_GRANULARITY:
|
2016-02-15 10:10:40 -07:00
|
|
|
# Always notify for a transition where last active gets bumped.
|
|
|
|
return True
|
2014-08-28 10:43:15 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
if old_state.state != new_state.state:
|
|
|
|
return True
|
2015-05-14 10:48:12 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
return False
|
2015-05-14 08:29:58 -06:00
|
|
|
|
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
def _format_user_presence_state(state, now):
|
|
|
|
"""Convert UserPresenceState to a format that can be sent down to clients
|
|
|
|
and to other servers.
|
|
|
|
"""
|
|
|
|
content = {
|
|
|
|
"presence": state.state,
|
|
|
|
"user_id": state.user_id,
|
|
|
|
}
|
2016-02-18 03:11:43 -07:00
|
|
|
if state.last_active_ts:
|
|
|
|
content["last_active_ago"] = now - state.last_active_ts
|
2016-02-15 10:10:40 -07:00
|
|
|
if state.status_msg and state.state != PresenceState.OFFLINE:
|
|
|
|
content["status_msg"] = state.status_msg
|
|
|
|
if state.state == PresenceState.ONLINE:
|
|
|
|
content["currently_active"] = state.currently_active
|
2015-05-14 08:29:58 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
return content
|
2015-05-14 08:29:58 -06:00
|
|
|
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2014-08-29 10:09:15 -06:00
|
|
|
class PresenceEventSource(object):
|
|
|
|
def __init__(self, hs):
|
|
|
|
self.hs = hs
|
|
|
|
self.clock = hs.get_clock()
|
2016-02-15 10:10:40 -07:00
|
|
|
self.store = hs.get_datastore()
|
2014-08-29 10:09:15 -06:00
|
|
|
|
2014-09-02 09:29:04 -06:00
|
|
|
@defer.inlineCallbacks
|
2014-12-03 12:48:14 -07:00
|
|
|
@log_function
|
2016-02-15 10:10:40 -07:00
|
|
|
def get_new_events(self, user, from_key, room_ids=None, include_offline=True,
|
|
|
|
**kwargs):
|
|
|
|
# The process for getting presence events are:
|
|
|
|
# 1. Get the rooms the user is in.
|
|
|
|
# 2. Get the list of user in the rooms.
|
|
|
|
# 3. Get the list of users that are in the user's presence list.
|
|
|
|
# 4. If there is a from_key set, cross reference the list of users
|
|
|
|
# with the `presence_stream_cache` to see which ones we actually
|
|
|
|
# need to check.
|
|
|
|
# 5. Load current state for the users.
|
|
|
|
#
|
|
|
|
# We don't try and limit the presence updates by the current token, as
|
|
|
|
# sending down the rare duplicate is not a concern.
|
|
|
|
|
2016-02-23 07:03:46 -07:00
|
|
|
with Measure(self.clock, "presence.get_new_events"):
|
2016-02-23 06:49:16 -07:00
|
|
|
user_id = user.to_string()
|
|
|
|
if from_key is not None:
|
|
|
|
from_key = int(from_key)
|
|
|
|
room_ids = room_ids or []
|
2014-08-29 10:09:15 -06:00
|
|
|
|
2016-02-23 06:49:16 -07:00
|
|
|
presence = self.hs.get_handlers().presence_handler
|
2016-02-23 07:48:23 -07:00
|
|
|
stream_change_cache = self.store.presence_stream_cache
|
2015-05-18 09:21:51 -06:00
|
|
|
|
2016-02-23 06:49:16 -07:00
|
|
|
if not room_ids:
|
|
|
|
rooms = yield self.store.get_rooms_for_user(user_id)
|
|
|
|
room_ids = set(e.room_id for e in rooms)
|
2016-02-23 07:48:23 -07:00
|
|
|
else:
|
|
|
|
room_ids = set(room_ids)
|
2014-08-29 10:09:15 -06:00
|
|
|
|
2016-02-23 08:47:37 -07:00
|
|
|
max_token = self.store.get_current_presence_token()
|
|
|
|
|
2016-02-23 06:49:16 -07:00
|
|
|
plist = yield self.store.get_presence_list_accepted(user.localpart)
|
2016-02-23 07:48:23 -07:00
|
|
|
friends = set(row["observed_user_id"] for row in plist)
|
|
|
|
friends.add(user_id) # So that we receive our own presence
|
|
|
|
|
|
|
|
user_ids_changed = set()
|
2016-02-23 08:05:37 -07:00
|
|
|
changed = None
|
2016-02-23 08:47:37 -07:00
|
|
|
if from_key and max_token - from_key < 100:
|
2016-02-23 07:57:45 -07:00
|
|
|
# For small deltas, its quicker to get all changes and then
|
|
|
|
# work out if we share a room or they're in our presence list
|
2016-02-23 07:48:23 -07:00
|
|
|
changed = stream_change_cache.get_all_entities_changed(from_key)
|
|
|
|
|
2016-02-23 08:05:37 -07:00
|
|
|
# get_all_entities_changed can return None
|
|
|
|
if changed is not None:
|
2016-02-23 07:48:23 -07:00
|
|
|
for other_user_id in changed:
|
|
|
|
if other_user_id in friends:
|
|
|
|
user_ids_changed.add(other_user_id)
|
|
|
|
continue
|
|
|
|
other_rooms = yield self.store.get_rooms_for_user(other_user_id)
|
|
|
|
if room_ids.intersection(e.room_id for e in other_rooms):
|
|
|
|
user_ids_changed.add(other_user_id)
|
|
|
|
continue
|
2016-02-23 06:49:16 -07:00
|
|
|
else:
|
2016-02-23 07:57:45 -07:00
|
|
|
# Too many possible updates. Find all users we can see and check
|
|
|
|
# if any of them have changed.
|
2016-02-23 07:48:23 -07:00
|
|
|
user_ids_to_check = set()
|
|
|
|
for room_id in room_ids:
|
|
|
|
users = yield self.store.get_users_in_room(room_id)
|
|
|
|
user_ids_to_check.update(users)
|
2014-08-29 10:09:15 -06:00
|
|
|
|
2016-02-23 08:47:37 -07:00
|
|
|
user_ids_to_check.update(friends)
|
2014-08-29 10:09:15 -06:00
|
|
|
|
2016-02-23 07:48:23 -07:00
|
|
|
# Always include yourself. Only really matters for when the user is
|
|
|
|
# not in any rooms, but still.
|
|
|
|
user_ids_to_check.add(user_id)
|
2014-08-29 10:09:15 -06:00
|
|
|
|
2016-02-23 07:48:23 -07:00
|
|
|
if from_key:
|
|
|
|
user_ids_changed = stream_change_cache.get_entities_changed(
|
|
|
|
user_ids_to_check, from_key,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
user_ids_changed = user_ids_to_check
|
2014-08-29 10:09:15 -06:00
|
|
|
|
2016-02-23 06:49:16 -07:00
|
|
|
updates = yield presence.current_state_for_users(user_ids_changed)
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-23 07:48:23 -07:00
|
|
|
now = self.clock.time_msec()
|
2014-08-13 12:18:55 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
defer.returnValue(([
|
|
|
|
{
|
|
|
|
"type": "m.presence",
|
|
|
|
"content": _format_user_presence_state(s, now),
|
|
|
|
}
|
|
|
|
for s in updates.values()
|
|
|
|
if include_offline or s.state != PresenceState.OFFLINE
|
|
|
|
], max_token))
|
2014-08-12 08:10:52 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
def get_current_key(self):
|
|
|
|
return self.store.get_current_presence_token()
|
2014-08-13 12:18:55 -06:00
|
|
|
|
2016-02-15 10:10:40 -07:00
|
|
|
def get_pagination_rows(self, user, pagination_config, key):
|
|
|
|
return self.get_new_events(user, from_key=None, include_offline=False)
|
2016-02-18 04:52:33 -07:00
|
|
|
|
|
|
|
|
|
|
|
def handle_timeouts(user_states, is_mine_fn, user_to_num_current_syncs, now):
|
|
|
|
"""Checks the presence of users that have timed out and updates as
|
|
|
|
appropriate.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
user_states(list): List of UserPresenceState's to check.
|
|
|
|
is_mine_fn (fn): Function that returns if a user_id is ours
|
|
|
|
user_to_num_current_syncs (dict): Mapping of user_id to number of currently
|
|
|
|
active syncs.
|
|
|
|
now (int): Current time in ms.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
List of UserPresenceState updates
|
|
|
|
"""
|
|
|
|
changes = {} # Actual changes we need to notify people about
|
|
|
|
|
|
|
|
for state in user_states:
|
|
|
|
is_mine = is_mine_fn(state.user_id)
|
|
|
|
|
|
|
|
new_state = handle_timeout(state, is_mine, user_to_num_current_syncs, now)
|
|
|
|
if new_state:
|
|
|
|
changes[state.user_id] = new_state
|
|
|
|
|
|
|
|
return changes.values()
|
|
|
|
|
|
|
|
|
|
|
|
def handle_timeout(state, is_mine, user_to_num_current_syncs, now):
|
|
|
|
"""Checks the presence of the user to see if any of the timers have elapsed
|
|
|
|
|
|
|
|
Args:
|
|
|
|
state (UserPresenceState)
|
|
|
|
is_mine (bool): Whether the user is ours
|
|
|
|
user_to_num_current_syncs (dict): Mapping of user_id to number of currently
|
|
|
|
active syncs.
|
|
|
|
now (int): Current time in ms.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A UserPresenceState update or None if no update.
|
|
|
|
"""
|
|
|
|
if state.state == PresenceState.OFFLINE:
|
|
|
|
# No timeouts are associated with offline states.
|
|
|
|
return None
|
|
|
|
|
|
|
|
changed = False
|
|
|
|
user_id = state.user_id
|
|
|
|
|
|
|
|
if is_mine:
|
|
|
|
if state.state == PresenceState.ONLINE:
|
|
|
|
if now - state.last_active_ts > IDLE_TIMER:
|
|
|
|
# Currently online, but last activity ages ago so auto
|
|
|
|
# idle
|
|
|
|
state = state.copy_and_replace(
|
|
|
|
state=PresenceState.UNAVAILABLE,
|
|
|
|
)
|
|
|
|
changed = True
|
|
|
|
elif now - state.last_active_ts > LAST_ACTIVE_GRANULARITY:
|
|
|
|
# So that we send down a notification that we've
|
|
|
|
# stopped updating.
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
if now - state.last_federation_update_ts > FEDERATION_PING_INTERVAL:
|
|
|
|
# Need to send ping to other servers to ensure they don't
|
|
|
|
# timeout and set us to offline
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
# If there are have been no sync for a while (and none ongoing),
|
|
|
|
# set presence to offline
|
|
|
|
if not user_to_num_current_syncs.get(user_id, 0):
|
|
|
|
if now - state.last_user_sync_ts > SYNC_ONLINE_TIMEOUT:
|
|
|
|
state = state.copy_and_replace(
|
|
|
|
state=PresenceState.OFFLINE,
|
|
|
|
status_msg=None,
|
|
|
|
)
|
|
|
|
changed = True
|
|
|
|
else:
|
|
|
|
# We expect to be poked occaisonally by the other side.
|
|
|
|
# This is to protect against forgetful/buggy servers, so that
|
|
|
|
# no one gets stuck online forever.
|
|
|
|
if now - state.last_federation_update_ts > FEDERATION_TIMEOUT:
|
|
|
|
# The other side seems to have disappeared.
|
|
|
|
state = state.copy_and_replace(
|
|
|
|
state=PresenceState.OFFLINE,
|
|
|
|
status_msg=None,
|
|
|
|
)
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
return state if changed else None
|
|
|
|
|
|
|
|
|
|
|
|
def handle_update(prev_state, new_state, is_mine, wheel_timer, now):
|
|
|
|
"""Given a presence update:
|
|
|
|
1. Add any appropriate timers.
|
|
|
|
2. Check if we should notify anyone.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
prev_state (UserPresenceState)
|
|
|
|
new_state (UserPresenceState)
|
|
|
|
is_mine (bool): Whether the user is ours
|
|
|
|
wheel_timer (WheelTimer)
|
|
|
|
now (int): Time now in ms
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
3-tuple: `(new_state, persist_and_notify, federation_ping)` where:
|
|
|
|
- new_state: is the state to actually persist
|
|
|
|
- persist_and_notify (bool): whether to persist and notify people
|
|
|
|
- federation_ping (bool): whether we should send a ping over federation
|
|
|
|
"""
|
|
|
|
user_id = new_state.user_id
|
|
|
|
|
|
|
|
persist_and_notify = False
|
|
|
|
federation_ping = False
|
|
|
|
|
|
|
|
# If the users are ours then we want to set up a bunch of timers
|
|
|
|
# to time things out.
|
|
|
|
if is_mine:
|
|
|
|
if new_state.state == PresenceState.ONLINE:
|
|
|
|
# Idle timer
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_active_ts + IDLE_TIMER
|
|
|
|
)
|
|
|
|
|
2016-02-19 03:58:27 -07:00
|
|
|
active = now - new_state.last_active_ts < LAST_ACTIVE_GRANULARITY
|
|
|
|
new_state = new_state.copy_and_replace(
|
|
|
|
currently_active=active,
|
|
|
|
)
|
|
|
|
|
|
|
|
if active:
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_active_ts + LAST_ACTIVE_GRANULARITY
|
|
|
|
)
|
|
|
|
|
2016-02-18 04:52:33 -07:00
|
|
|
if new_state.state != PresenceState.OFFLINE:
|
|
|
|
# User has stopped syncing
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_user_sync_ts + SYNC_ONLINE_TIMEOUT
|
|
|
|
)
|
|
|
|
|
|
|
|
last_federate = new_state.last_federation_update_ts
|
|
|
|
if now - last_federate > FEDERATION_PING_INTERVAL:
|
|
|
|
# Been a while since we've poked remote servers
|
|
|
|
new_state = new_state.copy_and_replace(
|
|
|
|
last_federation_update_ts=now,
|
|
|
|
)
|
|
|
|
federation_ping = True
|
|
|
|
|
|
|
|
else:
|
|
|
|
wheel_timer.insert(
|
|
|
|
now=now,
|
|
|
|
obj=user_id,
|
|
|
|
then=new_state.last_federation_update_ts + FEDERATION_TIMEOUT
|
|
|
|
)
|
|
|
|
|
|
|
|
# Check whether the change was something worth notifying about
|
|
|
|
if should_notify(prev_state, new_state):
|
|
|
|
new_state = new_state.copy_and_replace(
|
|
|
|
last_federation_update_ts=now,
|
|
|
|
)
|
|
|
|
persist_and_notify = True
|
|
|
|
|
|
|
|
return new_state, persist_and_notify, federation_ping
|