133 lines
3.0 KiB
Dart
133 lines
3.0 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:loris/global.dart' as global;
|
|
import 'package:http/http.dart' as http;
|
|
|
|
enum InteractionType {
|
|
favorite,
|
|
reblog,
|
|
}
|
|
|
|
extension InteractionTypeExtension on InteractionType {
|
|
IconData get icon {
|
|
switch (this) {
|
|
case InteractionType.favorite:
|
|
return Icons.favorite_border;
|
|
case InteractionType.reblog:
|
|
return Icons.repeat_sharp;
|
|
}
|
|
}
|
|
|
|
IconData get revokeIcon {
|
|
switch (this) {
|
|
case InteractionType.favorite:
|
|
return Icons.favorite;
|
|
case InteractionType.reblog:
|
|
return Icons.repeat_on_sharp;
|
|
}
|
|
}
|
|
|
|
String get slug {
|
|
switch (this) {
|
|
case InteractionType.favorite:
|
|
return "favourite";
|
|
case InteractionType.reblog:
|
|
return "reblog";
|
|
}
|
|
}
|
|
|
|
String get revokeSlug {
|
|
switch (this) {
|
|
case InteractionType.favorite:
|
|
return "unfavourite";
|
|
case InteractionType.reblog:
|
|
return "unreblog";
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<int> makeInteractionFromId(
|
|
String id,
|
|
String postid,
|
|
InteractionType type, {
|
|
bool revoke = false,
|
|
}) async {
|
|
Map<String, String> headers =
|
|
global.settings!.identities[id]!.getAuthHeaders();
|
|
headers.addAll(global.defaultHeaders);
|
|
|
|
final uri = Uri(
|
|
scheme: "https",
|
|
path: "/api/v1/statuses/$postid/${revoke ? type.revokeSlug : type.slug}",
|
|
host: global.settings!.identities[id]!.instanceUrl,
|
|
);
|
|
|
|
final response = await http.post(uri, headers: headers);
|
|
return response.statusCode;
|
|
}
|
|
|
|
Future<int> makeInteractionFromUrl(
|
|
String id,
|
|
String posturl,
|
|
InteractionType type, {
|
|
bool revoke = false,
|
|
}) 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;
|
|
}
|
|
|
|
final Map<String, dynamic> json = jsonDecode(response.body);
|
|
final List<dynamic> statuses = json["statuses"];
|
|
if (statuses.isEmpty) {
|
|
return 404;
|
|
}
|
|
final String postid = statuses[0]["id"];
|
|
|
|
return await makeInteractionFromId(id, postid, type);
|
|
}
|
|
|
|
Future<int> makeFullInteraction(
|
|
String id,
|
|
String postid,
|
|
String posturl,
|
|
InteractionType type, {
|
|
bool revoke = false,
|
|
}) async {
|
|
int i = await makeInteractionFromId(id, postid, type);
|
|
if (i == 200) {
|
|
return i;
|
|
}
|
|
return await makeInteractionFromUrl(id, posturl, type);
|
|
}
|