OpenContacts/lib/apis/contact_api.dart
2024-07-15 00:23:04 -04:00

37 lines
No EOL
1.6 KiB
Dart

import 'dart:convert';
import 'package:OpenContacts/clients/api_client.dart';
import 'package:OpenContacts/models/users/friend.dart';
import 'package:OpenContacts/models/users/friend_status.dart';
import 'package:OpenContacts/models/users/user.dart';
import 'package:OpenContacts/models/users/user_profile.dart';
import 'package:OpenContacts/models/users/user_status.dart';
class ContactApi {
static Future<List<Friend>> getFriendsList(ApiClient client, {DateTime? lastStatusUpdate}) async {
final response = await client.get("/users/${client.userId}/contacts${lastStatusUpdate != null ? "?lastStatusUpdate=${lastStatusUpdate.toUtc().toIso8601String()}" : ""}");
client.checkResponse(response);
final data = jsonDecode(response.body) as List;
return data.map((e) => Friend.fromMap(e)).toList();
}
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(),
contactStatus: FriendStatus.accepted,
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);
}
}