import 'dart:convert'; import 'dart:developer'; import 'package:fluffychat/pangea/constants/model_keys.dart'; import 'package:http/http.dart'; import '../../widgets/matrix.dart'; import '../models/user_model.dart'; import '../models/user_profile_search_model.dart'; import '../network/requests.dart'; import '../network/urls.dart'; class PUserRepo { static Future repoCreatePangeaUser({ required String userID, required String dob, required fullName, required String matrixAccessToken, }) async { final Requests req = Requests( baseUrl: PApiUrls.baseAPI, matrixAccessToken: matrixAccessToken, ); final Map body = { ModelKey.userFullName: fullName, ModelKey.userPangeaUserId: userID, ModelKey.userDateOfBirth: dob, }; final Response res = await req.post( url: PApiUrls.createUser, body: body, ); return PUserModel.fromJson(jsonDecode(res.body)); } static Future fetchPangeaUserInfo({ required String userID, required String matrixAccessToken, }) async { Response res; try { final Requests req = Requests( baseUrl: PApiUrls.baseAPI, matrixAccessToken: matrixAccessToken, ); res = await req.get( url: PApiUrls.userDetails, objectId: userID, ); return PUserModel.fromJson(jsonDecode(res.body)); } catch (err) { //status code should be 400 - PTODO - check ffor this. log("Most likely a first signup and needs to make an account"); return null; } } //notes for jordan - only replace non-null fields, return whole profile //Jordan - should return pangeaUserId as well static Future updateUserProfile( Profile userProfile, String accessToken, ) async { final Requests req = Requests( baseUrl: PApiUrls.baseAPI, accessToken: accessToken, ); final Response res = await req.put( url: PApiUrls.updateUserProfile, body: userProfile.toJson(), ); //temp fix final content = jsonDecode(res.body); //PTODO - try taking this out and see where bug occurs if (content[ModelKey.userPangeaUserId] == null) { content[ModelKey.userPangeaUserId] = MatrixState.pangeaController.matrixState.client.userID; } return Profile.fromJson(content); } static Future searchUserProfiles({ // List? interests, String? targetLanguage, String? sourceLanguage, String? country, // String? speaks, String? pageNumber, required String accessToken, required int limit, }) async { final Requests req = Requests( baseUrl: PApiUrls.baseAPI, accessToken: accessToken, ); final Map body = {}; // if (interests != null) body[ModelKey.userInterests] = interests.toString(); if (targetLanguage != null) { body[ModelKey.userTargetLanguage] = targetLanguage; } if (sourceLanguage != null) { body[ModelKey.userSourceLanguage] = sourceLanguage; } if (country != null) body[ModelKey.userCountry] = country; final String searchUrl = "${PApiUrls.searchUserProfiles}?limit=$limit${pageNumber != null ? '&page=$pageNumber' : ''}"; final Response res = await req.post( url: searchUrl, body: body, ); //PTODO - implement paginiation - make another call with next url return UserProfileSearchResponse.fromJson(jsonDecode(res.body)); } }