mastodon_vk_reposter/bin/server.dart

143 lines
5.3 KiB
Dart
Raw Normal View History

2022-04-21 05:59:37 +00:00
import 'dart:convert';
import 'dart:io';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'package:shelf_router/shelf_router.dart';
2022-04-21 06:37:29 +00:00
import 'package:dio/dio.dart' as dio_lib;
var dio = dio_lib.Dio();
2022-04-21 05:59:37 +00:00
// Load confirmation token from env
final confirmationToken = Platform.environment['CONFIRMATION_TOKEN'];
// Load secret key from env
final secretKey = Platform.environment['SECRET_KEY'];
2022-04-21 06:37:29 +00:00
// Load mastodon instance url from env
final instanceUrl = Platform.environment['INSTANCE_URL'];
// Load mastodon auth token from env
final authToken = Platform.environment['MASTODON_TOKEN'];
2022-04-21 05:59:37 +00:00
// Configure routes.
final _router = Router()
..get('/', _rootHandler)
..post('/vk', _vkHandler);
Response _rootHandler(Request req) {
return Response.ok('Hello, World!\n');
}
Future<Response> _vkHandler(Request req) async {
// POST request should contain a JSON body.
// JSON must contain a 'type' field.
// The value of the 'type' field must be one of the following:
// 'confirmation'
// 'wall_post_new'
// If we recieve a 'confirmation' request, we should respond with a confirmationToken.
// If we recieve a 'wall_post_new' request, we should validate if 'secret' field equals
// secretKey and respond with an 'ok' string, printing request in the console.
final requestBody = await req.readAsString();
final requestJson = json.decode(requestBody);
final requestType = requestJson['type'];
if (requestType == 'confirmation') {
return Response.ok(confirmationToken);
} else if (requestType == 'wall_post_new') {
if (requestJson['secret'] == secretKey) {
print(requestBody);
2022-04-21 06:37:29 +00:00
_postOnMastodon(requestJson);
2022-04-21 05:59:37 +00:00
return Response.ok('ok');
} else {
return Response.ok('invalid secret');
}
} else {
return Response.ok('invalid request type');
}
}
2022-04-21 06:37:29 +00:00
Future<void> _postOnMastodon(reqJson) async {
// Called to repost the post on Mastodon on vk.
// reqJson contains the 'object' field of the VK post.
// This object has the 'text' field with the post text.
// Also it contains the 'attachments' field with the attachments.
// The attachments field is an array of objects.
// Each object has the 'type' field with the attachment type.
// We only want to post photos.
// The 'photo' type is 'photo'.
// The 'photo' type has the 'sizes' field with an array of objects.
// Each object has the 'url', 'height' and 'width' fields.
// We want to post the largest photo.
// The 'url' field is the url 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.
final postText = reqJson['object']['text'] ?? '';
2022-04-21 06:41:08 +00:00
final attachments = reqJson['object']['attachments'] ?? [];
2022-04-21 06:49:59 +00:00
2022-04-21 06:37:29 +00:00
final photos =
attachments.where((attachment) => attachment['type'] == 'photo');
2022-04-21 07:09:41 +00:00
final photoUrls = photos.map((photo) async {
2022-04-21 06:37:29 +00:00
final photoUrl = photo['photo']['sizes'].last['url'];
2022-04-21 07:08:28 +00:00
// Generate a random filename.
// Add the extension '.jpg'.
final photoFilename = '${DateTime.now().millisecondsSinceEpoch}-${photoUrl.split('/').last.split('?').first.substring(0, 10)}.jpg';
2022-04-21 06:37:29 +00:00
final photoPath =
2022-04-21 07:08:28 +00:00
'${Directory.systemTemp.path}/$photoFilename';
2022-04-21 07:09:41 +00:00
await dio.download(photoUrl, photoPath);
2022-04-21 06:37:29 +00:00
return photoPath;
});
// Post the post on Mastodon.
// First we upload all photos with dio.
// Then we post the post with the uploaded photos and the text.
// We delete the photos after posting.
// Photos are uploaded to POST <instanceUrl>/api/v1/media in the 'file' form data field.
// We take the 'id' field of the response and add it to the post's 'media_ids' field.
// The 'media_ids' field is an array of media ids.
// We post the post with the media ids and the text in the 'status' field.
// Endpoint: POST <instanceUrl>/api/v1/statuses
// Headers: Authorization: Bearer <authToken>
// We use auth on all requests.
// if there are no photos, we don't fetch them
if (photos.isEmpty) {
await dio.post('$instanceUrl/api/v1/statuses',
data: {'status': postText},
2022-04-21 06:49:59 +00:00
options:
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
2022-04-21 06:37:29 +00:00
} else {
2022-04-21 06:54:29 +00:00
var mediaIds = [];
for (final photoUrl in photoUrls) {
final resp = await dio.post('$instanceUrl/api/v1/media',
2022-04-21 06:57:41 +00:00
data: dio_lib.FormData.fromMap({'file': dio_lib.MultipartFile.fromFileSync(photoUrl)}),
2022-04-21 06:54:29 +00:00
options:
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
mediaIds.add(resp.data['id']);
}
2022-04-21 06:37:29 +00:00
await dio.post('$instanceUrl/api/v1/statuses',
2022-04-21 06:49:59 +00:00
data: {'status': postText, 'media_ids': mediaIds},
options:
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
2022-04-21 06:37:29 +00:00
await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete()));
}
}
2022-04-21 05:59:37 +00:00
void main(List<String> args) async {
// Use any available host or container IP (usually `0.0.0.0`).
final ip = InternetAddress.anyIPv4;
// Configure a pipeline that logs requests.
final _handler = Pipeline().addMiddleware(logRequests()).addHandler(_router);
// For running in containers, we respect the PORT environment variable.
final port = int.parse(Platform.environment['PORT'] ?? '8080');
final server = await serve(_handler, ip, port);
print('Server listening on port ${server.port}');
}