52 lines
1.3 KiB
Dart
52 lines
1.3 KiB
Dart
|
import 'dart:convert';
|
||
|
|
||
|
import 'package:loris/business_logic/timeline/timeline.dart';
|
||
|
import 'package:loris/global.dart' as global;
|
||
|
import 'package:http/http.dart' as http;
|
||
|
|
||
|
Future<Map<int, PostModel?>> getPostFromUrl(
|
||
|
String id,
|
||
|
String posturl,
|
||
|
) async {
|
||
|
Map<String, String> headers =
|
||
|
global.settings!.identities[id]!.getAuthHeaders();
|
||
|
headers.addAll(global.defaultHeaders);
|
||
|
|
||
|
final uriv1 = Uri(
|
||
|
scheme: "https",
|
||
|
host: global.settings!.identities[id]!.instanceUrl,
|
||
|
path: "api/v1/search",
|
||
|
queryParameters: {
|
||
|
"resolve": "true",
|
||
|
"type": "statuses",
|
||
|
"q": posturl,
|
||
|
},
|
||
|
);
|
||
|
|
||
|
http.Response response = await http.get(uriv1, headers: headers);
|
||
|
if (response.statusCode != 200) {
|
||
|
final uriv2 = Uri(
|
||
|
scheme: "https",
|
||
|
host: global.settings!.identities[id]!.instanceUrl,
|
||
|
path: "api/v2/search",
|
||
|
queryParameters: {
|
||
|
"type": "statuses",
|
||
|
"q": posturl,
|
||
|
"resolve": "true",
|
||
|
},
|
||
|
);
|
||
|
response = await http.get(uriv2, headers: headers);
|
||
|
}
|
||
|
|
||
|
if (response.statusCode != 200) {
|
||
|
return {response.statusCode: null};
|
||
|
}
|
||
|
|
||
|
final Map<String, dynamic> json = jsonDecode(response.body);
|
||
|
final List<dynamic> statuses = json["statuses"];
|
||
|
if (statuses.isEmpty) {
|
||
|
return {404: null};
|
||
|
}
|
||
|
return {200: PostModel.fromJson(statuses[0], id)};
|
||
|
}
|