Try out posting in a thread.
This commit is contained in:
parent
0e33113516
commit
12f1cb7985
|
@ -72,12 +72,22 @@ 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 = [];
|
||||
|
||||
|
@ -85,9 +95,9 @@ Future<void> _postOnMastodon(reqJson) async {
|
|||
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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue