'Set the presence state of the user.'

@defer.inlineCallbacks
def set_state(self, target_user, state, ignore_status_msg=False):

    status_msg = state.get('status_msg', None)
    presence = state['presence']
    valid_presence = (PresenceState.ONLINE, PresenceState.UNAVAILABLE, PresenceState.OFFLINE)
    if (presence not in valid_presence):
        raise SynapseError(400, 'Invalid    presence    state')
    user_id = target_user.to_string()
    prev_state = (yield self.current_state_for_user(user_id))
    new_fields = {'state': presence}
    if (not ignore_status_msg):
        msg = (status_msg if (presence != PresenceState.OFFLINE) else None)
        new_fields['status_msg'] = msg
    if (presence == PresenceState.ONLINE):
        new_fields['last_active_ts'] = self.clock.time_msec()
    (yield self._update_states([prev_state.copy_and_replace(**new_fields)]))
