Try out posting in a thread.

This commit is contained in:
Inex Code 2022-04-21 07:21:05 +00:00
parent 0e33113516
commit 12f1cb7985
1 changed files with 51 additions and 9 deletions

View File

@ -72,22 +72,32 @@ Future<void> _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<String> 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<void> _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()));
}
}
}