createEventAcknowledge method

dynamic createEventAcknowledge(
  1. EventNotificationModel acknowledgedEvent,
  2. String? atKey,
  3. String? fromAtSign
)

Updates data of members of an event

Implementation

// ignore: always_declare_return_types
createEventAcknowledge(EventNotificationModel acknowledgedEvent,
    String? atKey, String? fromAtSign) async {
  try {
    var eventId =
        acknowledgedEvent.key!.split('createevent-')[1].split('@')[0];

    if ((atClientInstance!.preference != null) &&
        (atClientInstance!.preference!.namespace != null)) {
      eventId = eventId.replaceAll(
          '.${atClientInstance!.preference!.namespace!}', '');
    }

    late EventNotificationModel presentEventData;
    allEventNotifications.forEach((element) {
      if (element.key!.contains('createevent-$eventId')) {
        presentEventData = EventNotificationModel.fromJson(jsonDecode(
            EventNotificationModel.convertEventNotificationToJson(
                element.eventNotificationModel!)));
      }
    });

    /// Old approach
    var response = await atClientInstance!.getKeys(
      regex: 'createevent-$eventId',
    );

    var key = EventService().getAtKey(response[0]);

    /// New approach

    // var key = EventService().getAtKey(presentEventData.key);

    Map<dynamic, dynamic>? tags;

    presentEventData.group!.members!.forEach((presentGroupMember) {
      acknowledgedEvent.group!.members!.forEach((acknowledgedGroupMember) {
        if (acknowledgedGroupMember.atSign![0] != '@') {
          acknowledgedGroupMember.atSign =
              '@' + acknowledgedGroupMember.atSign!;
        }

        if (presentGroupMember.atSign![0] != '@') {
          presentGroupMember.atSign = '@' + presentGroupMember.atSign!;
        }

        if (fromAtSign![0] != '@') fromAtSign = '@' + fromAtSign!;

        // print(
        //     'acknowledgedGroupMember.atSign ${acknowledgedGroupMember.atSign}, presentGroupMember.atSign ${presentGroupMember.atSign}, fromAtSign $fromAtSign');

        if (acknowledgedGroupMember.atSign!.toLowerCase() ==
                presentGroupMember.atSign!.toLowerCase() &&
            acknowledgedGroupMember.atSign!.toLowerCase() ==
                fromAtSign!.toLowerCase()) {
          // print(
          //     'acknowledgedGroupMember.tags ${acknowledgedGroupMember.tags}');
          presentGroupMember.tags = acknowledgedGroupMember.tags;
          tags = presentGroupMember.tags;
        }
      });
      // print('presentGroupMember.tags ${presentGroupMember.tags}');
    });

    presentEventData.isUpdate = true;
    var allAtsignList = <String?>[];
    presentEventData.group!.members!.forEach((element) {
      allAtsignList.add(element.atSign);
    });

    var notification = EventNotificationModel.convertEventNotificationToJson(
        presentEventData);

    // print('notification $notification');

    var result = await atClientInstance!
        .put(key, notification, isDedicated: MixedConstants.isDedicated);

    key.sharedWith = jsonEncode(allAtsignList);

    await SyncSecondary().callSyncSecondary(
      SyncOperation.notifyAll,
      atKey: key,
      notification: notification,
      operation: OperationEnum.update,
      isDedicated: MixedConstants.isDedicated,
    );

    /// Dont sync as notifyAll is called

    if (result is bool && result) {
      //   mapUpdatedDataToWidget(
      //       convertEventToHybrid(NotificationType.Event,
      //           eventNotificationModel: presentEventData),
      //       tags: tags,
      //       tagOfAtsign: fromAtSign);

      mapUpdatedEventDataToWidget(presentEventData,
          tags: tags, tagOfAtsign: fromAtSign);
      // print('acknowledgement for $fromAtSign completed');
    }
  } catch (e) {
    print('error in event acknowledgement: $e');
  }
}