106 lines
2.4 KiB
Dart
106 lines
2.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:localization/localization.dart';
|
|
import 'package:loris/business_logic/network_tools/get_post_from_url.dart';
|
|
import 'package:loris/global.dart' as global;
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../timeline/timeline.dart';
|
|
|
|
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";
|
|
}
|
|
}
|
|
|
|
String get name {
|
|
switch (this) {
|
|
case InteractionType.favorite:
|
|
return "like".i18n();
|
|
case InteractionType.reblog:
|
|
return "reblog".i18n();
|
|
}
|
|
}
|
|
}
|
|
|
|
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<int, PostModel?> post = await getPostFromUrl(id, posturl);
|
|
return post.keys.first == 200
|
|
? await makeInteractionFromId(id, post[post.keys.first]!.id, type)
|
|
: post.keys.first;
|
|
}
|
|
|
|
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);
|
|
}
|