From 12f1cb7985b7d6adddfb71a5103a6d979a3089e4 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Thu, 21 Apr 2022 07:21:05 +0000 Subject: [PATCH] Try out posting in a thread. --- bin/server.dart | 60 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/bin/server.dart b/bin/server.dart index 24799e3..abf17ef 100644 --- a/bin/server.dart +++ b/bin/server.dart @@ -72,22 +72,32 @@ Future _postOnMastodon(reqJson) async { // The 'height' and 'width' fields are the height and width of the photo. // We download all pictures with dio into a temporary directory. - final postText = reqJson['object']['text'] ?? ''; + // Make sure post text is 500 characters or less. + String postText = reqJson['object']['text']; + if (postText.length > 500) { + postText.substring(0, 500); + } final attachments = reqJson['object']['attachments'] ?? []; - final photos = - attachments.where((attachment) => attachment['type'] == 'photo'); + var photos = attachments.where((attachment) => attachment['type'] == 'photo'); + + // If there are more than four photos, move other photos to another array to be posted in the thread. + var otherPhotos = []; + if (photos.length > 4) { + otherPhotos = photos.skip(4).toList(); + photos = photos.take(4).toList(); + } List photoUrls = []; - + for (final photo in photos) { final photoUrl = photo['photo']['sizes'].last['url']; // Generate a random filename. // Add the extension '.jpg'. - final photoFilename = '${DateTime.now().millisecondsSinceEpoch}-${photoUrl.split('/').last.split('?').first.substring(0, 10)}.jpg'; - final photoPath = - '${Directory.systemTemp.path}/$photoFilename'; + final photoFilename = + '${DateTime.now().millisecondsSinceEpoch}-${photoUrl.split('/').last.split('?').first.substring(0, 10)}.jpg'; + final photoPath = '${Directory.systemTemp.path}/$photoFilename'; await dio.download(photoUrl, photoPath); photoUrls.add(photoPath); } @@ -115,18 +125,50 @@ Future _postOnMastodon(reqJson) async { for (final photoUrl in photoUrls) { final resp = await dio.post('$instanceUrl/api/v1/media', - data: dio_lib.FormData.fromMap({'file': dio_lib.MultipartFile.fromFileSync(photoUrl)}), + data: dio_lib.FormData.fromMap( + {'file': dio_lib.MultipartFile.fromFileSync(photoUrl)}), options: dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})); mediaIds.add(resp.data['id']); } - await dio.post('$instanceUrl/api/v1/statuses', + var postResponse = await dio.post('$instanceUrl/api/v1/statuses', data: {'status': postText, 'media_ids': mediaIds}, options: dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})); await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete())); + + // If there are other photos, post them in a thread. + while (otherPhotos.isNotEmpty) { + mediaIds = []; + if (otherPhotos.length > 4) { + otherPhotos = otherPhotos.skip(4).toList(); + photos = otherPhotos.take(4).toList(); + } else { + photos = otherPhotos; + otherPhotos = []; + } + + for (final photoUrl in photoUrls) { + final resp = await dio.post('$instanceUrl/api/v1/media', + data: dio_lib.FormData.fromMap( + {'file': dio_lib.MultipartFile.fromFileSync(photoUrl)}), + options: dio_lib.Options( + headers: {'Authorization': 'Bearer $authToken'})); + mediaIds.add(resp.data['id']); + } + + postResponse = await dio.post('$instanceUrl/api/v1/statuses', + data: { + 'in_reply_to_id': postResponse.data['id'], + 'media_ids': mediaIds + }, + options: + dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})); + + await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete())); + } } }