2023-04-29 15:26:12 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-07-15 00:23:04 -04:00
|
|
|
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';
|
2023-04-29 15:26:12 -04:00
|
|
|
|
2023-09-29 07:17:17 -04:00
|
|
|
class ContactApi {
|
2023-05-16 07:01:42 -04:00
|
|
|
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;
|
2023-05-06 10:45:26 -04:00
|
|
|
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
|
|
|
}
|