Solian/lib/providers/relation.dart

81 lines
2.5 KiB
Dart
Raw Normal View History

2024-07-23 17:17:41 +00:00
import 'package:get/get.dart';
import 'package:solian/exceptions/request.dart';
import 'package:solian/models/account.dart';
2024-07-23 17:17:41 +00:00
import 'package:solian/models/relations.dart';
import 'package:solian/providers/auth.dart';
class RelationshipProvider extends GetxController {
2024-08-02 15:15:28 +00:00
final RxInt friendRequestCount = 0.obs;
final RxList<Relationship> _friends = RxList.empty(growable: true);
2024-08-02 15:15:28 +00:00
Future<void> refreshRelativeList() async {
final resp = await listRelation();
final List<Relationship> result = resp.body
.map((e) => Relationship.fromJson(e))
.toList()
.cast<Relationship>();
2024-08-02 15:15:28 +00:00
_friends.value = result.where((x) => x.status == 1).toList();
_friends.refresh();
2024-08-02 15:15:28 +00:00
friendRequestCount.value = result.where((x) => x.status == 0).length;
}
2024-07-23 17:17:41 +00:00
bool hasFriend(Account account) {
final auth = Get.find<AuthProvider>();
if (auth.userProfile.value!['id'] == account.id) return true;
return _friends.any((x) => x.relatedId == account.id);
2024-07-23 17:17:41 +00:00
}
2024-09-16 03:57:16 +00:00
Future<Response> listRelation() async {
final AuthProvider auth = Get.find();
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('auth');
return client.get('/users/me/relations');
}
2024-07-23 17:17:41 +00:00
2024-09-16 03:57:16 +00:00
Future<Response> listRelationWithStatus(int status) async {
final AuthProvider auth = Get.find();
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('auth');
return client.get('/users/me/relations?status=$status');
}
2024-07-23 17:17:41 +00:00
Future<Response> makeFriend(String username) async {
final AuthProvider auth = Get.find();
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('auth');
final resp = await client.post('/users/me/relations?related=$username', {});
2024-07-23 17:17:41 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-07-23 17:17:41 +00:00
}
return resp;
}
Future<Response> handleRelation(
Relationship relationship, bool doAccept) async {
final AuthProvider auth = Get.find();
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('auth');
2024-07-23 17:17:41 +00:00
final resp = await client.post(
'/users/me/relations/${relationship.relatedId}/${doAccept ? 'accept' : 'decline'}',
{},
);
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-07-23 17:17:41 +00:00
}
return resp;
}
Future<Response> editRelation(Relationship relationship, int status) async {
final AuthProvider auth = Get.find();
2024-09-16 03:57:16 +00:00
final client = await auth.configureClient('auth');
final resp = await client.patch(
'/users/me/relations/${relationship.relatedId}',
{'status': status},
);
2024-07-23 17:17:41 +00:00
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-07-23 17:17:41 +00:00
}
return resp;
}
}