publishBundle method

Future<Result<OmemoError, bool>> publishBundle(
  1. OmemoBundle bundle
)

Attempts to publish a device bundle to the device list and device bundle PubSub nodes.

On success, returns true. On failure, returns an OmemoError.

Implementation

Future<Result<OmemoError, bool>> publishBundle(
  omemo.OmemoBundle bundle,
) async {
  final attrs = getAttributes();
  final pm = attrs.getManagerById<PubSubManager>(pubsubManager)!;
  final bareJid = attrs.getFullJID().toBare();

  XMLNode? deviceList;
  final deviceListRaw = await _retrieveDeviceListPayload(bareJid);
  if (!deviceListRaw.isType<OmemoError>()) {
    deviceList = deviceListRaw.get<XMLNode>();
  }

  deviceList ??= XMLNode.xmlns(
    tag: 'devices',
    xmlns: omemoDevicesXmlns,
  );

  final ids = deviceList.children
      .map((child) => int.parse(child.attributes['id']! as String));

  if (!ids.contains(bundle.id)) {
    // Only update the device list if the device Id is not there
    final newDeviceList = XMLNode.xmlns(
      tag: 'devices',
      xmlns: omemoDevicesXmlns,
      children: [
        ...deviceList.children,
        XMLNode(
          tag: 'device',
          attributes: <String, String>{
            'id': '${bundle.id}',
          },
        ),
      ],
    );

    final deviceListPublish = await pm.publish(
      bareJid,
      omemoDevicesXmlns,
      newDeviceList,
      id: 'current',
      options: const PubSubPublishOptions(
        accessModel: 'open',
      ),
    );
    if (deviceListPublish.isType<PubSubError>()) return const Result(false);
  }

  final deviceBundlePublish = await pm.publish(
    bareJid,
    omemoBundlesXmlns,
    bundleToXML(bundle),
    id: '${bundle.id}',
    options: const PubSubPublishOptions(
      accessModel: 'open',
      maxItems: 'max',
    ),
  );

  return Result(deviceBundlePublish.isType<PubSubError>());
}