From ecbe421611cecc0b93f91728b3a5de9395ff388e Mon Sep 17 00:00:00 2001 From: William Jordan-Cooley Date: Tue, 30 Jul 2024 11:34:14 -0400 Subject: [PATCH 1/5] if bot, don't invite people to your analytics room --- .../extensions/client_extension/client_analytics_extension.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pangea/extensions/client_extension/client_analytics_extension.dart b/lib/pangea/extensions/client_extension/client_analytics_extension.dart index 396e09f8d..797850662 100644 --- a/lib/pangea/extensions/client_extension/client_analytics_extension.dart +++ b/lib/pangea/extensions/client_extension/client_analytics_extension.dart @@ -100,6 +100,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(); } From 1abbb8ee3f7ec6e9273bd574d6df5857dd523bf5 Mon Sep 17 00:00:00 2001 From: ggurdin Date: Tue, 30 Jul 2024 12:45:31 -0400 Subject: [PATCH 2/5] on profile create, also create pangea profile. If there's no pangea profile when fetching access token, create one. --- lib/pangea/controllers/user_controller.dart | 27 ++++++++++++++++--- lib/pangea/repo/user_repo.dart | 29 +++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) 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/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, From d9f272e38999d650883e2ac3f7e505c1946a55fc Mon Sep 17 00:00:00 2001 From: ggurdin Date: Tue, 30 Jul 2024 12:52:23 -0400 Subject: [PATCH 3/5] don't have analytics rooms for bot user --- lib/pangea/controllers/my_analytics_controller.dart | 8 ++++---- .../client_extension/client_analytics_extension.dart | 8 ++++++-- .../extensions/client_extension/client_extension.dart | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/pangea/controllers/my_analytics_controller.dart b/lib/pangea/controllers/my_analytics_controller.dart index 12baeb689..4c592a6fc 100644 --- a/lib/pangea/controllers/my_analytics_controller.dart +++ b/lib/pangea/controllers/my_analytics_controller.dart @@ -233,11 +233,11 @@ class MyAnalyticsController { 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( PangeaEventTypes.summaryAnalytics, _client.userID!, ); @@ -307,7 +307,7 @@ class MyAnalyticsController { // if there's new content to be sent, or if lastUpdated hasn't been // set yet for this room, send the analytics events if (summaryContent.isNotEmpty || l2AnalyticsLastUpdated == null) { - await analyticsRoom.sendSummaryAnalyticsEvent( + await analyticsRoom?.sendSummaryAnalyticsEvent( summaryContent, ); } @@ -347,7 +347,7 @@ class MyAnalyticsController { // ); if (recentConstructUses.isNotEmpty) { - await analyticsRoom.sendConstructsEvent( + await analyticsRoom?.sendConstructsEvent( recentConstructUses, ); } diff --git a/lib/pangea/extensions/client_extension/client_analytics_extension.dart b/lib/pangea/extensions/client_extension/client_analytics_extension.dart index 396e09f8d..af43a7ba2 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, 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 From 398cac35ffe095212ec92424c113e97fb6b31d81 Mon Sep 17 00:00:00 2001 From: ggurdin Date: Tue, 30 Jul 2024 12:55:42 -0400 Subject: [PATCH 4/5] don't add analytics rooms to spaces if you are the bot --- .../extensions/client_extension/client_analytics_extension.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/pangea/extensions/client_extension/client_analytics_extension.dart b/lib/pangea/extensions/client_extension/client_analytics_extension.dart index b694a02ca..381bbba4c 100644 --- a/lib/pangea/extensions/client_extension/client_analytics_extension.dart +++ b/lib/pangea/extensions/client_extension/client_analytics_extension.dart @@ -78,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); @@ -95,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(); } From bc913e4d7aad04f0a7e12610c74d3fc0b363743e Mon Sep 17 00:00:00 2001 From: ggurdin Date: Tue, 30 Jul 2024 13:54:05 -0400 Subject: [PATCH 5/5] bump version number --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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"