updateWelcomeMessage method

  1. @override
Future<void> updateWelcomeMessage({
  1. ChatMessageText? text,
  2. List<Attachment>? attachments,
})
override

Updates the WelcomeMessage of the authenticated MyUser.

Implementation

@override
Future<void> updateWelcomeMessage({
  ChatMessageText? text,
  List<Attachment>? attachments,
}) async {
  Log.debug(
    'updateWelcomeMessage(text: $text, attachments: $attachments)',
    '$runtimeType',
  );

  final bool reset =
      text?.val.isEmpty == true && attachments?.isEmpty == true;

  final WelcomeMessage? previous = myUser.value?.welcomeMessage;

  myUser.update(
    (u) => u
      ?..welcomeMessage = reset
          ? null
          : WelcomeMessage(
              text: text,
              attachments: attachments ?? [],
              at: PreciseDateTime.now(),
            ),
  );

  try {
    final List<Future>? uploads = attachments
        ?.mapIndexed((i, e) {
          if (e is LocalAttachment) {
            return e.upload.value?.future.then(
              (a) {
                attachments[i] = a;
              },
              onError: (_) {
                // No-op, as failed upload attempts are handled below.
              },
            );
          }
        })
        .nonNulls
        .toList();

    await Future.wait(uploads ?? []);

    if (attachments?.whereType<LocalAttachment>().isNotEmpty == true) {
      throw const ConnectionException(
        UpdateWelcomeMessageException(
          UpdateWelcomeMessageErrorCode.unknownAttachment,
        ),
      );
    }

    await _graphQlProvider.updateWelcomeMessage(
      reset
          ? null
          : WelcomeMessageInput(
              text: text == null
                  ? null
                  : ChatMessageTextInput(
                      kw$new: text.val.isEmpty ? null : text,
                    ),
              attachments: attachments == null
                  ? null
                  : ChatMessageAttachmentsInput(
                      kw$new: attachments.map((e) => e.id).toList(),
                    ),
            ),
    );
  } catch (_) {
    myUser.update((u) => u?..welcomeMessage = previous);
    rethrow;
  }
}