createScheduledFileMessage method

Future<ScheduledFileMessage> createScheduledFileMessage(
  1. ScheduledFileMessageParams fileMessageParams, {
  2. OnScheduledMessageCallback<ScheduledFileMessage>? callback,
})

Creates scheduled file message

Implementation

Future<ScheduledFileMessage> createScheduledFileMessage(
  ScheduledFileMessageParams fileMessageParams, {
  OnScheduledMessageCallback<ScheduledFileMessage>? callback,
}) async {
  UploadResponse? upload;

  if (fileMessageParams.uploadFile.hasBinary) {
    try {
      upload = await _sdk.api
          .send<UploadResponse>(
        ChannelScheduledFileUploadRequest(
          channelUrl: channelUrl,
          params: fileMessageParams,
        ),
      )
          .timeout(
        Duration(seconds: _sdk.options.fileTransferTimeout),
        onTimeout: () {
          logger.e('upload timeout');
          if (callback != null) {
            callback(null, SBError(code: ErrorCode.fileUploadTimeout));
          }
          throw SBError(code: ErrorCode.fileUploadTimeout);
        },
      );
      fileMessageParams.uploadFile
        ..fileSize = upload.fileSize
        ..url = upload.url;
    } catch (e) {
      if (callback != null) {
        final error = SBError(message: 'Failed Sending Request');
        callback(null, error);
      }
      rethrow;
    }
  }

  try {
    final result = await _sdk.api.send<ScheduledFileMessage>(
      GroupChannelScheduledFileMessageSendRequest(
        channelUrl: channelUrl,
        params: fileMessageParams,
      ),
    );

    if (callback != null) {
      callback(result, null);
    }
    return result;
  } catch (e) {
    if (callback != null) {
      final error = SBError(message: 'Failed Sending Request');
      callback(null, error);
    }
    rethrow;
  }
}