OpenContacts/lib/apis/contact_api.dart

37 lines
1.5 KiB
Dart
Raw Normal View History

2023-04-29 15:26:12 -04:00
import 'dart:convert';
import 'package:recon/clients/api_client.dart';
import 'package:recon/models/users/friend.dart';
import 'package:recon/models/users/friend_status.dart';
import 'package:recon/models/users/user.dart';
import 'package:recon/models/users/user_profile.dart';
import 'package:recon/models/users/user_status.dart';
2023-04-29 15:26:12 -04:00
2023-09-29 07:17:17 -04:00
class ContactApi {
static Future<List<Friend>> getFriendsList(ApiClient client, {DateTime? lastStatusUpdate}) async {
2023-09-29 03:51:46 -04:00
final response = await client.get("/users/${client.userId}/contacts${lastStatusUpdate != null ? "?lastStatusUpdate=${lastStatusUpdate.toUtc().toIso8601String()}" : ""}");
2023-05-25 09:50:38 -04:00
client.checkResponse(response);
2023-04-29 15:26:12 -04:00
final data = jsonDecode(response.body) as List;
return data.map((e) => Friend.fromMap(e)).toList();
2023-04-29 15:26:12 -04:00
}
2023-09-29 03:51:46 -04:00
static Future<void> addUserAsFriend(ApiClient client, {required User user}) async {
final friend = Friend(
id: user.id,
username: user.username,
ownerId: client.userId,
userStatus: UserStatus.empty(),
userProfile: UserProfile.empty(),
2023-09-29 09:33:43 -04:00
contactStatus: FriendStatus.accepted,
2023-09-29 03:51:46 -04:00
latestMessageTime: DateTime.now(),
);
final body = jsonEncode(friend.toMap(shallow: true));
final response = await client.put("/users/${client.userId}/contacts/${user.id}", body: body);
client.checkResponse(response);
}
static Future<void> removeUserAsFriend(ApiClient client, {required User user}) async {
final response = await client.delete("/users/${client.userId}/friends/${user.id}");
client.checkResponse(response);
}
2023-04-29 15:26:12 -04:00
}