def handle_timeout(state, is_mine, user_to_num_current_syncs, now):
    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
Return an IP address for a client connection given the server host.
dataset/ETHPy150Open matrix-org/synapse/synapse/handlers/presence.py/handle_timeout--crossover with--dataset/ETHPy150Open AppScale/appscale/AppServer/lib/cherrypy/cherrypy/test/webtest.py/interface