Try out posting in a thread.
This commit is contained in:
parent
0e33113516
commit
12f1cb7985
|
@ -72,22 +72,32 @@ Future<void> _postOnMastodon(reqJson) async {
|
||||||
// The 'height' and 'width' fields are the height and width of the photo.
|
// The 'height' and 'width' fields are the height and width of the photo.
|
||||||
// We download all pictures with dio into a temporary directory.
|
// 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 attachments = reqJson['object']['attachments'] ?? [];
|
||||||
|
|
||||||
final photos =
|
var photos = attachments.where((attachment) => attachment['type'] == 'photo');
|
||||||
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 = [];
|
List<String> photoUrls = [];
|
||||||
|
|
||||||
for (final photo in photos) {
|
for (final photo in photos) {
|
||||||
final photoUrl = photo['photo']['sizes'].last['url'];
|
final photoUrl = photo['photo']['sizes'].last['url'];
|
||||||
// Generate a random filename.
|
// Generate a random filename.
|
||||||
// Add the extension '.jpg'.
|
// Add the extension '.jpg'.
|
||||||
final photoFilename = '${DateTime.now().millisecondsSinceEpoch}-${photoUrl.split('/').last.split('?').first.substring(0, 10)}.jpg';
|
final photoFilename =
|
||||||
final photoPath =
|
'${DateTime.now().millisecondsSinceEpoch}-${photoUrl.split('/').last.split('?').first.substring(0, 10)}.jpg';
|
||||||
'${Directory.systemTemp.path}/$photoFilename';
|
final photoPath = '${Directory.systemTemp.path}/$photoFilename';
|
||||||
await dio.download(photoUrl, photoPath);
|
await dio.download(photoUrl, photoPath);
|
||||||
photoUrls.add(photoPath);
|
photoUrls.add(photoPath);
|
||||||
}
|
}
|
||||||
|
@ -115,18 +125,50 @@ Future<void> _postOnMastodon(reqJson) async {
|
||||||
|
|
||||||
for (final photoUrl in photoUrls) {
|
for (final photoUrl in photoUrls) {
|
||||||
final resp = await dio.post('$instanceUrl/api/v1/media',
|
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:
|
options:
|
||||||
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
||||||
mediaIds.add(resp.data['id']);
|
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},
|
data: {'status': postText, 'media_ids': mediaIds},
|
||||||
options:
|
options:
|
||||||
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
|
||||||
|
|
||||||
await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete()));
|
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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue