59 lines
1.8 KiB
Dart
59 lines
1.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:loris/business_logic/account/account.dart';
|
|
import 'package:loris/global.dart' as global;
|
|
import 'package:http/http.dart' as http;
|
|
|
|
Future<List<AccountModel>> _getFollowRequest(String identityName) async {
|
|
final identity = global.settings!.identities[identityName]!;
|
|
final headers = {...identity.getAuthHeaders(), ...global.defaultHeaders};
|
|
final Uri uri = Uri(
|
|
scheme: "https",
|
|
host: identity.instanceUrl,
|
|
path: "/api/v1/follow_requests",
|
|
);
|
|
final result = await http.get(uri, headers: headers);
|
|
if (result.statusCode != 200) return [];
|
|
|
|
List<AccountModel> models = [];
|
|
for (var m in jsonDecode(result.body)) {
|
|
models.add(AccountModel.fromJson(m, identityName));
|
|
}
|
|
return models;
|
|
}
|
|
|
|
Future<List<AccountModel>> getFollowRequests() async {
|
|
List<Future<List<AccountModel>>> pending = [];
|
|
global.settings!.identities.forEach((key, value) {
|
|
pending.add(_getFollowRequest(key));
|
|
});
|
|
|
|
List<AccountModel> finished = [];
|
|
for (var m in pending) {
|
|
finished.addAll(await m);
|
|
}
|
|
return finished;
|
|
}
|
|
|
|
// accept follow request or deny
|
|
// request is accepted unless deny is true
|
|
Future<RelationshipModel?> handleFollowRequest(AccountModel account,
|
|
{deny = false}) async {
|
|
final identity = global.settings!.identities[account.identity]!;
|
|
final headers = {...identity.getAuthHeaders(), ...global.defaultHeaders};
|
|
final Uri uri = Uri(
|
|
scheme: "https",
|
|
host: identity.instanceUrl,
|
|
path:
|
|
"/api/v1/follow_requests/${account.id}/${deny ? "reject" : "authorize"}",
|
|
);
|
|
|
|
final result = await http.post(uri, headers: headers);
|
|
print(uri);
|
|
print(result.body);
|
|
if (result.statusCode != 200) {
|
|
return null;
|
|
}
|
|
return RelationshipModel.fromJson(jsonDecode(result.body), account.identity);
|
|
}
|