newThread method

Future<List<ModeratorEntry>?> newThread(
  1. String _baseUrl,
  2. String text,
  3. Uint8List attachement,
  4. String _nick,
  5. String _avatar,
  6. String shortLinkOfPrevious,
  7. String DEPcid,
  8. String mimeType,
  9. String _xxHash,
  10. String blurHash,
)

Implementation

Future<List<ModeratorEntry>?> newThread(
    String _baseUrl,
    String text,
    Uint8List attachement,
    String _nick,
    String _avatar,
    String shortLinkOfPrevious,
    String DEPcid,
    String mimeType,
    String _xxHash,
    String blurHash) async {
  if (_latestCapsule == null) return null;
  if (_latestCapsule!.katamari.isEmpty) {
    return null;
  }
  if (_nick.isEmpty) {
    _nick = currentNick;
  }
  if (shortLinkOfPrevious.isEmpty) {
    shortLinkOfPrevious =
        messages.all.where((element) => element.flags.isPost).first.shortLink;
  }
  Uint8List nt;
  //assume net is live here
  if (text.contains('https://')) {
    bool bcHash = text.contains('#bandcamp');
    bool tagBc = false;
    if (!text.startsWith('[')) {
      final ali = text.split('https://');
      final alib = ali[1].split(' ');
      final scresu = await scrapeBClink('https://' + alib[0]);
      if (scresu.isNotEmpty) {
        //pull the image as attachement

        if (scresu.last is Uint8List && attachement.isEmpty) {
          attachement = scresu.last;
          //patch url title pair
          text = '[' + scresu[0] + '](' + scresu[1] + ') ';
        } else {
          text = scresu[2];
        }

        if (!bcHash && scresu.contains('bandcamp')) tagBc = true;
        bool sk = false;
        alib.forEach((element) {
          if (!sk) {
            sk = true;
            return;
          }
          text += ' ' + element;
        });

        if (!bcHash && tagBc) text += ' #bandcamp';
      }
    }
  } //dont touch markdown links

  if (mimeType.isEmpty) {
    return await _io!
        .newThread(
            baseUrl,
            text,
            shortLinkOfPrevious,
            _latestCapsule != null ? _latestCapsule!.katamari : Uint8List(0),
            _nick,
            _avatar,
            Uint8List(0))
        .then((value) {
      if (value.isEmpty) return null;
      final na = updateLatestCapsule(value, false);
      if (na != null && na.isNotEmpty) {
        _dirty = true;
        currentPendingAsModeratorEntries;
      }
      return na;
    });
  }

  var attParamsToPass = Uint8List(0);
  if (_xxHash.isNotEmpty) {
    final atty = await _xxCache.pullLocalXX(_xxHash.toString());
    if (atty != null && atty.isNotEmpty) {
      final cid = await _xxCache.CIDforXX(_xxHash);
      if (cid.isEmpty) {
        //dont submit without ipfs
        //retry pushing
        _ipfsQ.pushToPinataIPFS('', '', atty).then((_cid) {
          if (_cid.isEmpty) {
            print('pushAttachment:pushToPinataIPFS failed');
            return;
          }
          xxC.mapCIDtoXX(_cid, _xxHash);
        });
        return [];
      }
      final rds = redressTextAndAttachment(
          text, atty.lengthInBytes, _xxHash, mimeType, cid);
      if (rds.isEmpty) {
        return null;
      }
      text = rds[0];
      attParamsToPass = rds[1];
    } else {
      print('newThread reported xx attacment not found, deep trouble.');
      return null;
    }
    if (blurHash.isNotEmpty) {
      text += ' !|' + blurHash + ' ';
    }
  } else {
    //xxhash not galculated yet
    print('noxx');
  }
  /*try {
      final image = img.decodeImage(attachement);
      final blurHash = BlurHash.encode(image!, numCompX: 4, numCompY: 3);
      text += ' !|' + blurHash.hash + ' ';
      print('blurhashed');
    } catch (e) {}*/

  return await _io!
      .newThread(baseUrl, text, shortLinkOfPrevious, _latestCapsule!.katamari,
          _nick, _avatar, attParamsToPass)
      .then((value) {
    if (value.isEmpty) return null;
    final na = updateLatestCapsule(value, false);
    if (na != null && na.isNotEmpty) {
      _dirty = true;
      currentPendingAsModeratorEntries;
    }
    return na;
  });
}