switch to using matrix profile to store DOB
This commit is contained in:
parent
477da85ed6
commit
ea3689529b
7 changed files with 83 additions and 16 deletions
|
|
@ -1,5 +1,6 @@
|
|||
class PLocalKey {
|
||||
static const String user = 'user';
|
||||
static const String matrixProfile = 'matrixProfile';
|
||||
|
||||
static const String classes = 'classes';
|
||||
|
||||
|
|
|
|||
|
|
@ -16,4 +16,6 @@ class PangeaEventTypes {
|
|||
|
||||
static const audio = "p.audio";
|
||||
static const botOptions = "pangea.bot_options";
|
||||
|
||||
static const userAge = "pangea.user_age";
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,3 @@
|
|||
import 'package:matrix/matrix.dart';
|
||||
|
||||
import 'package:fluffychat/pangea/constants/age_limits.dart';
|
||||
import 'package:fluffychat/pangea/constants/pangea_event_types.dart';
|
||||
import 'package:fluffychat/pangea/controllers/base_controller.dart';
|
||||
|
|
@ -7,6 +5,7 @@ import 'package:fluffychat/pangea/controllers/pangea_controller.dart';
|
|||
import 'package:fluffychat/pangea/extensions/pangea_room_extension.dart';
|
||||
import 'package:fluffychat/pangea/models/class_model.dart';
|
||||
import 'package:fluffychat/pangea/utils/p_extension.dart';
|
||||
import 'package:matrix/matrix.dart';
|
||||
|
||||
class PermissionsController extends BaseController {
|
||||
late PangeaController _pangeaController;
|
||||
|
|
@ -32,8 +31,7 @@ class PermissionsController extends BaseController {
|
|||
|
||||
/// Returns false if user is null
|
||||
bool isUser18() {
|
||||
final dob =
|
||||
_pangeaController.userController.userModel?.profile?.dateOfBirth;
|
||||
final dob = _pangeaController.userController.matrixProfile?.dateOfBirth;
|
||||
return dob != null
|
||||
? DateTime.parse(dob).isAtLeastYearsOld(AgeLimits.toAccessFeatures)
|
||||
: false;
|
||||
|
|
|
|||
|
|
@ -2,14 +2,15 @@ import 'dart:async';
|
|||
import 'dart:developer';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:jwt_decode/jwt_decode.dart';
|
||||
import 'package:matrix/matrix.dart' as matrix;
|
||||
|
||||
import 'package:fluffychat/pangea/constants/language_keys.dart';
|
||||
import 'package:fluffychat/pangea/constants/model_keys.dart';
|
||||
import 'package:fluffychat/pangea/constants/pangea_event_types.dart';
|
||||
import 'package:fluffychat/pangea/controllers/base_controller.dart';
|
||||
import 'package:fluffychat/pangea/controllers/pangea_controller.dart';
|
||||
import 'package:fluffychat/widgets/fluffy_chat_app.dart';
|
||||
import 'package:jwt_decode/jwt_decode.dart';
|
||||
import 'package:matrix/matrix.dart' as matrix;
|
||||
|
||||
import '../constants/local.key.dart';
|
||||
import '../models/user_model.dart';
|
||||
import '../repo/user_repo.dart';
|
||||
|
|
@ -35,6 +36,11 @@ class UserController extends BaseController {
|
|||
|
||||
if (newUserModel != null) {
|
||||
_savePUserModel(newUserModel);
|
||||
if (newUserModel.profile!.dateOfBirth != null) {
|
||||
await setMatrixProfile(newUserModel.profile!.dateOfBirth!);
|
||||
}
|
||||
final MatrixProfile? matrixProfile = await getMatrixProfile();
|
||||
_saveMatrixProfile(matrixProfile);
|
||||
}
|
||||
_completeCompleter();
|
||||
|
||||
|
|
@ -45,6 +51,29 @@ class UserController extends BaseController {
|
|||
}
|
||||
}
|
||||
|
||||
Future<void> setMatrixProfile(String dob) async {
|
||||
await _pangeaController.matrixState.client.setAccountData(
|
||||
userId!,
|
||||
PangeaEventTypes.userAge,
|
||||
{ModelKey.userDateOfBirth: dob},
|
||||
);
|
||||
final MatrixProfile? matrixProfile = await getMatrixProfile();
|
||||
_saveMatrixProfile(matrixProfile);
|
||||
}
|
||||
|
||||
Future<MatrixProfile?> getMatrixProfile() async {
|
||||
try {
|
||||
final Map<String, dynamic> accountData =
|
||||
await _pangeaController.matrixState.client.getAccountData(
|
||||
userId!,
|
||||
PangeaEventTypes.userAge,
|
||||
);
|
||||
return MatrixProfile.fromJson(accountData);
|
||||
} catch (_) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
void _completeCompleter() {
|
||||
if (!_completer.isCompleted) {
|
||||
_completer.complete(null);
|
||||
|
|
@ -91,6 +120,11 @@ class UserController extends BaseController {
|
|||
return data != null ? PUserModel.fromJson(data) : null;
|
||||
}
|
||||
|
||||
MatrixProfile? get matrixProfile {
|
||||
final data = _pangeaController.pStoreService.read(PLocalKey.matrixProfile);
|
||||
return data != null ? MatrixProfile.fromJson(data) : null;
|
||||
}
|
||||
|
||||
Future<bool> get isPUserDataAvailable async {
|
||||
try {
|
||||
final PUserModel? toCheck = userModel ?? (await fetchUserModel());
|
||||
|
|
@ -102,8 +136,10 @@ class UserController extends BaseController {
|
|||
|
||||
Future<bool> get isUserDataAvailableAndDateOfBirthSet async {
|
||||
try {
|
||||
final PUserModel? toCheck = userModel ?? (await fetchUserModel());
|
||||
return toCheck?.profile?.dateOfBirth != null ? true : false;
|
||||
if (matrixProfile == null) {
|
||||
await fetchUserModel();
|
||||
}
|
||||
return matrixProfile?.dateOfBirth != null ? true : false;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -147,6 +183,16 @@ class UserController extends BaseController {
|
|||
FluffyChatApp.router.go("/rooms/user_age");
|
||||
}
|
||||
|
||||
_saveMatrixProfile(MatrixProfile? matrixProfile) {
|
||||
if (matrixProfile != null) {
|
||||
_pangeaController.pStoreService.save(
|
||||
PLocalKey.matrixProfile,
|
||||
matrixProfile.toJson(),
|
||||
);
|
||||
setState(data: matrixProfile);
|
||||
}
|
||||
}
|
||||
|
||||
_savePUserModel(PUserModel? pUserModel) {
|
||||
final jsonUser = pUserModel!.toJson();
|
||||
_pangeaController.pStoreService.save(PLocalKey.user, jsonUser);
|
||||
|
|
@ -199,16 +245,21 @@ class UserController extends BaseController {
|
|||
profile: updatedUserProfile,
|
||||
),
|
||||
);
|
||||
|
||||
if (dateOfBirth != null) {
|
||||
await setMatrixProfile(dateOfBirth);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> createPangeaUser({required String dob}) async {
|
||||
final PUserModel newUserModel = await PUserRepo.repoCreatePangeaUser(
|
||||
userID: userId!,
|
||||
dateOfBirth: dob,
|
||||
fullName: fullname,
|
||||
matrixAccessToken: _matrixAccessToken!,
|
||||
);
|
||||
await _savePUserModel(newUserModel);
|
||||
|
||||
await setMatrixProfile(dob);
|
||||
}
|
||||
|
||||
String? get _matrixAccessToken =>
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:country_picker/country_picker.dart';
|
||||
import 'package:fluffychat/pangea/constants/model_keys.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
|
||||
import 'package:fluffychat/pangea/constants/model_keys.dart';
|
||||
import '../constants/language_keys.dart';
|
||||
import 'language_model.dart';
|
||||
|
||||
|
|
@ -40,6 +39,24 @@ class PUserModel {
|
|||
}
|
||||
}
|
||||
|
||||
class MatrixProfile {
|
||||
String dateOfBirth;
|
||||
|
||||
MatrixProfile({
|
||||
required this.dateOfBirth,
|
||||
});
|
||||
|
||||
factory MatrixProfile.fromJson(Map<String, dynamic> json) => MatrixProfile(
|
||||
dateOfBirth: json[ModelKey.userDateOfBirth],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = <String, dynamic>{};
|
||||
data[ModelKey.userDateOfBirth] = dateOfBirth;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Profile {
|
||||
// i'm considering removing this field because it's duplicating info in the
|
||||
// matrix database
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ class PUserAgeController extends State<PUserAge> {
|
|||
loading = true;
|
||||
});
|
||||
|
||||
final String date = DateFormat('MM-dd-yyyy').format(selectedDate!);
|
||||
final String date = DateFormat('yyyy-MM-dd').format(selectedDate!);
|
||||
|
||||
if (pangeaController.userController.userModel?.access == null) {
|
||||
await pangeaController.userController.createPangeaUser(dob: date);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ import '../network/urls.dart';
|
|||
class PUserRepo {
|
||||
static Future<PUserModel> repoCreatePangeaUser({
|
||||
required String userID,
|
||||
required String dateOfBirth,
|
||||
required fullName,
|
||||
required String matrixAccessToken,
|
||||
}) async {
|
||||
|
|
@ -25,7 +24,6 @@ class PUserRepo {
|
|||
final Map<String, dynamic> body = {
|
||||
ModelKey.userFullName: fullName,
|
||||
ModelKey.userPangeaUserId: userID,
|
||||
ModelKey.userDateOfBirth: dateOfBirth,
|
||||
};
|
||||
final Response res = await req.post(
|
||||
url: PApiUrls.createUser,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue