'Returns a context manager that should surround any stream requests
from the user.
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.
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.'

@defer.inlineCallbacks
def user_syncing(self, user_id, affect_presence=True):

    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):
            (yield self._update_states([prev_state.copy_and_replace(state=PresenceState.ONLINE, last_active_ts=self.clock.time_msec(), last_user_sync_ts=self.clock.time_msec())]))
        else:
            (yield self._update_states([prev_state.copy_and_replace(last_user_sync_ts=self.clock.time_msec())]))
    @defer.inlineCallbacks
    def _end():
        if affect_presence:
            self.user_to_num_current_syncs[user_id] -= 1
            prev_state = (yield self.current_state_for_user(user_id))
            (yield self._update_states([prev_state.copy_and_replace(last_user_sync_ts=self.clock.time_msec())]))
    @contextmanager
    def _user_syncing():
        try:
            (yield)
        finally:
            preserve_fn(_end)()
    defer.returnValue(_user_syncing())
