2023-04-30 09:43:59 -04:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2023-05-03 15:55:34 -04:00
|
|
|
import 'package:contacts_plus_plus/clients/api_client.dart';
|
2023-05-04 14:57:16 -04:00
|
|
|
import 'package:contacts_plus_plus/models/friend.dart';
|
2023-05-05 09:05:06 -04:00
|
|
|
import 'package:contacts_plus_plus/models/personal_profile.dart';
|
2023-05-01 13:13:40 -04:00
|
|
|
import 'package:contacts_plus_plus/models/user.dart';
|
2023-05-04 14:57:16 -04:00
|
|
|
import 'package:contacts_plus_plus/models/user_profile.dart';
|
2023-05-06 05:18:00 -04:00
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
2023-04-30 09:43:59 -04:00
|
|
|
|
|
|
|
class UserApi {
|
|
|
|
static Future<Iterable<User>> searchUsers(ApiClient client, {required String needle}) async {
|
|
|
|
final response = await client.get("/users?name=$needle");
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
final data = jsonDecode(response.body) as List;
|
|
|
|
return data.map((e) => User.fromMap(e));
|
|
|
|
}
|
2023-05-05 05:29:54 -04:00
|
|
|
|
|
|
|
static Future<User> getUser(ApiClient client, {required String userId}) async {
|
|
|
|
final response = await client.get("/users/$userId/");
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
final data = jsonDecode(response.body);
|
|
|
|
return User.fromMap(data);
|
|
|
|
}
|
2023-05-04 14:57:16 -04:00
|
|
|
|
2023-05-05 10:39:40 -04:00
|
|
|
static Future<UserStatus> getUserStatus(ApiClient client, {required String userId}) async {
|
|
|
|
final response = await client.get("/users/$userId/status");
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
final data = jsonDecode(response.body);
|
|
|
|
return UserStatus.fromMap(data);
|
|
|
|
}
|
2023-05-06 12:34:14 -04:00
|
|
|
|
|
|
|
static Future<void> notifyOnlineInstance(ApiClient client) async {
|
|
|
|
final response = await client.post("/stats/instanceOnline/${client.authenticationData.secretMachineId.hashCode}");
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
}
|
2023-05-05 10:39:40 -04:00
|
|
|
|
|
|
|
static Future<void> setStatus(ApiClient client, {required UserStatus status}) async {
|
2023-05-06 05:18:00 -04:00
|
|
|
final pkginfo = await PackageInfo.fromPlatform();
|
|
|
|
status = status.copyWith(
|
|
|
|
neosVersion: "${pkginfo.version} of ${pkginfo.appName}",
|
2023-05-17 02:07:10 -04:00
|
|
|
isMobile: true,
|
2023-05-06 05:18:00 -04:00
|
|
|
);
|
2023-05-05 10:39:40 -04:00
|
|
|
final body = jsonEncode(status.toMap(shallow: true));
|
|
|
|
final response = await client.put("/users/${client.userId}/status", body: body);
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
}
|
|
|
|
|
2023-05-05 09:05:06 -04:00
|
|
|
static Future<PersonalProfile> getPersonalProfile(ApiClient client) async {
|
|
|
|
final response = await client.get("/users/${client.userId}");
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
final data = jsonDecode(response.body);
|
|
|
|
return PersonalProfile.fromMap(data);
|
|
|
|
}
|
|
|
|
|
2023-05-04 14:57:16 -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(),
|
|
|
|
friendStatus: FriendStatus.accepted,
|
2023-05-06 13:01:15 -04:00
|
|
|
latestMessageTime: DateTime.now(),
|
2023-05-04 14:57:16 -04:00
|
|
|
);
|
|
|
|
final body = jsonEncode(friend.toMap(shallow: true));
|
|
|
|
final response = await client.put("/users/${client.userId}/friends/${user.id}", body: body);
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Future<void> removeUserAsFriend(ApiClient client, {required User user}) async {
|
|
|
|
final response = await client.delete("/users/${client.userId}/friends/${user.id}");
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
}
|
2023-04-30 09:43:59 -04:00
|
|
|
}
|