setPresence method

Future<void> setPresence(
  1. String userId,
  2. PresenceType presence, {
  3. String? statusMsg,
})

This API sets the given user's presence state. When setting the status, the activity time is updated to reflect that activity; the client does not need to specify the last_active_ago field. You cannot set the presence state of another user.

userId The user whose presence state to update.

presence The new presence state.

statusMsg The status message to attach to this state.

Implementation

Future<void> setPresence(String userId, PresenceType presence,
    {String? statusMsg}) async {
  final requestUri = Uri(
      path:
          '_matrix/client/v3/presence/${Uri.encodeComponent(userId)}/status');
  final request = Request('PUT', baseUri!.resolveUri(requestUri));
  request.headers['authorization'] = 'Bearer ${bearerToken!}';
  request.headers['content-type'] = 'application/json';
  request.bodyBytes = utf8.encode(jsonEncode({
    'presence': presence.name,
    if (statusMsg != null) 'status_msg': statusMsg,
  }));
  final response = await httpClient.send(request);
  final responseBody = await response.stream.toBytes();
  if (response.statusCode != 200) unexpectedResponse(response, responseBody);
  final responseString = utf8.decode(responseBody);
  final json = jsonDecode(responseString);
  return ignore(json);
}