diff --git a/lib/pangea/controllers/my_analytics_controller.dart b/lib/pangea/controllers/my_analytics_controller.dart index 591b7a372..bf58019f1 100644 --- a/lib/pangea/controllers/my_analytics_controller.dart +++ b/lib/pangea/controllers/my_analytics_controller.dart @@ -198,11 +198,11 @@ class MyAnalyticsController extends BaseController { if (userL2 == null || _client.userID == null) return; // analytics room for the user and current target language - final Room analyticsRoom = await _client.getMyAnalyticsRoom(userL2!); + final Room? analyticsRoom = await _client.getMyAnalyticsRoom(userL2!); // get the last time analytics were updated for this room final DateTime? l2AnalyticsLastUpdated = - await analyticsRoom.analyticsLastUpdated( + await analyticsRoom?.analyticsLastUpdated( _client.userID!, ); @@ -301,7 +301,7 @@ class MyAnalyticsController extends BaseController { // ); if (recentConstructUses.isNotEmpty || l2AnalyticsLastUpdated == null) { - await analyticsRoom.sendConstructsEvent( + await analyticsRoom?.sendConstructsEvent( recentConstructUses, ); } diff --git a/lib/pangea/controllers/user_controller.dart b/lib/pangea/controllers/user_controller.dart index 2ef0227e5..bf8ce3c11 100644 --- a/lib/pangea/controllers/user_controller.dart +++ b/lib/pangea/controllers/user_controller.dart @@ -83,6 +83,15 @@ class UserController extends BaseController { createdAt: DateTime.now(), ); final newProfile = Profile(userSettings: userSettings); + + // we don't use the pangea profile anymore, but we still need + // it to get access token for the choreographer, so create one + await PUserRepo.repoCreatePangeaUser( + userID: userId!, + dob: dob.toIso8601String(), + fullName: fullname!, + matrixAccessToken: _matrixAccessToken!, + ); await newProfile.saveProfileData(waitForDataInSync: true); } @@ -155,13 +164,25 @@ class UserController extends BaseController { _pangeaController.pStoreService.read(PLocalKey.access); if (localAccessToken == null || needNewJWT(localAccessToken)) { - final PangeaProfileResponse? userModel = - await PUserRepo.fetchPangeaUserInfo( + PangeaProfileResponse? userModel = await PUserRepo.fetchPangeaUserInfo( userID: userId!, matrixAccessToken: _matrixAccessToken!, ); + // Oops, some accounts were made without creating pangea profiles, so they + // don't have access to an access token yet. In that case, create a pangea profile. if (userModel?.access == null) { - throw ("Trying to get accessToken with null userModel"); + final dob = profile.userSettings.dateOfBirth; + if (dob != null) { + userModel = await PUserRepo.repoCreatePangeaUser( + userID: userId!, + dob: dob.toIso8601String(), + fullName: fullname!, + matrixAccessToken: _matrixAccessToken!, + ); + if (userModel?.access == null) { + throw ("Trying to get accessToken with null userModel"); + } + } } _pangeaController.pStoreService.save( PLocalKey.access, diff --git a/lib/pangea/extensions/client_extension/client_analytics_extension.dart b/lib/pangea/extensions/client_extension/client_analytics_extension.dart index 2d2e19bc3..82f1b621b 100644 --- a/lib/pangea/extensions/client_extension/client_analytics_extension.dart +++ b/lib/pangea/extensions/client_extension/client_analytics_extension.dart @@ -3,7 +3,7 @@ part of "client_extension.dart"; extension AnalyticsClientExtension on Client { /// Get the logged in user's analytics room matching /// a given langCode. If not present, create it. - Future _getMyAnalyticsRoom(String langCode) async { + Future _getMyAnalyticsRoom(String langCode) async { final Room? analyticsRoom = _analyticsRoomLocal(langCode); if (analyticsRoom != null) return analyticsRoom; return _makeAnalyticsRoom(langCode); @@ -35,7 +35,11 @@ extension AnalyticsClientExtension on Client { /// /// If the room does not appear immediately after creation, this method waits for it to appear in sync. /// Returns the created [Room] object. - Future _makeAnalyticsRoom(String langCode) async { + Future _makeAnalyticsRoom(String langCode) async { + if (userID == null || userID == BotName.byEnvironment) { + return null; + } + final String roomID = await createRoom( creationContent: { 'type': PangeaRoomTypes.analytics, @@ -74,6 +78,7 @@ extension AnalyticsClientExtension on Client { // migration function to change analytics rooms' vsibility to public // so they will appear in the space hierarchy Future _updateAnalyticsRoomVisibility() async { + if (userID == null || userID == BotName.byEnvironment) return; await Future.wait( allMyAnalyticsRooms.map((room) async { final visability = await getRoomVisibilityOnDirectory(room.id); @@ -91,6 +96,7 @@ extension AnalyticsClientExtension on Client { /// so teachers can join them via space hierarchy. /// Allows teachers to join analytics rooms without being invited. void _addAnalyticsRoomsToAllSpaces() { + if (userID == null || userID == BotName.byEnvironment) return; for (final Room room in allMyAnalyticsRooms) { room.addAnalyticsRoomToSpaces(); } @@ -100,6 +106,7 @@ extension AnalyticsClientExtension on Client { /// Handles case when students cannot add analytics room to space(s) /// so teacher is still able to get analytics data for this student void _inviteAllTeachersToAllAnalyticsRooms() { + if (userID == null || userID == BotName.byEnvironment) return; for (final Room room in allMyAnalyticsRooms) { room.inviteTeachersToAnalyticsRoom(); } diff --git a/lib/pangea/extensions/client_extension/client_extension.dart b/lib/pangea/extensions/client_extension/client_extension.dart index af66c7d1f..7af50d501 100644 --- a/lib/pangea/extensions/client_extension/client_extension.dart +++ b/lib/pangea/extensions/client_extension/client_extension.dart @@ -21,7 +21,7 @@ extension PangeaClient on Client { /// Get the logged in user's analytics room matching /// a given langCode. If not present, create it. - Future getMyAnalyticsRoom(String langCode) async => + Future getMyAnalyticsRoom(String langCode) async => await _getMyAnalyticsRoom(langCode); /// Get local analytics room for a given langCode and diff --git a/lib/pangea/repo/user_repo.dart b/lib/pangea/repo/user_repo.dart index 47caaab0b..244e21c54 100644 --- a/lib/pangea/repo/user_repo.dart +++ b/lib/pangea/repo/user_repo.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:developer'; import 'package:fluffychat/pangea/constants/model_keys.dart'; +import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:http/http.dart'; import '../models/user_model.dart'; @@ -10,6 +11,34 @@ 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 { + try { + final Requests req = Requests( + baseUrl: PApiUrls.baseAPI, + matrixAccessToken: matrixAccessToken, + ); + + final Map body = { + ModelKey.userFullName: fullName, + ModelKey.userPangeaUserId: userID, + ModelKey.userDateOfBirth: dob, + }; + final resp = await req.post( + url: PApiUrls.createUser, + body: body, + ); + return PangeaProfileResponse.fromJson(jsonDecode(resp.body)); + } catch (err, s) { + ErrorHandler.logError(e: err, s: s); + return null; + } + } + static Future fetchPangeaUserInfo({ required String userID, required String matrixAccessToken, diff --git a/pubspec.yaml b/pubspec.yaml index 1e75fc4b5..2f2d47699 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -6,7 +6,7 @@ description: Learn a language while texting your friends. # Pangea# publish_to: none # On version bump also increase the build number for F-Droid -version: 1.21.1+3533 +version: 1.21.2+3534 environment: sdk: ">=3.0.0 <4.0.0"