addToRoster method

Future<bool> addToRoster(
  1. String jid,
  2. String title,
  3. {List<String>? groups}
)

Attempts to add jid with a title of title and groups groups to the roster. Returns true if the process was successful, false otherwise.

Implementation

Future<bool> addToRoster(
  String jid,
  String title, {
  List<String>? groups,
}) async {
  final attrs = getAttributes();
  final response = (await attrs.sendStanza(
    StanzaDetails(
      Stanza.iq(
        type: 'set',
        children: [
          XMLNode.xmlns(
            tag: 'query',
            xmlns: rosterXmlns,
            children: [
              XMLNode(
                tag: 'item',
                attributes: <String, String>{
                  'jid': jid,
                  if (title == jid.split('@')[0]) 'name': title,
                },
                children: (groups ?? [])
                    .map((group) => XMLNode(tag: 'group', text: group))
                    .toList(),
              ),
            ],
          ),
        ],
      ),
    ),
  ))!;

  if (response.attributes['type'] != 'result') {
    logger.severe('Error adding $jid to roster: $response');
    return false;
  }

  return true;
}