@ -10,7 +10,6 @@ import 'package:fluffychat/pangea/extensions/pangea_room_extension/pangea_room_e
import ' package:fluffychat/pangea/models/space_model.dart ' ;
import ' package:fluffychat/pangea/utils/error_handler.dart ' ;
import ' package:fluffychat/pangea/utils/firebase_analytics.dart ' ;
import ' package:fluffychat/pangea/utils/space_code.dart ' ;
import ' package:fluffychat/widgets/matrix.dart ' ;
import ' package:flutter/foundation.dart ' ;
import ' package:flutter/material.dart ' ;
@ -63,110 +62,121 @@ class ClassController extends BaseController {
Future < void > joinClasswithCode ( BuildContext context , String classCode ) async {
final client = Matrix . of ( context ) . client ;
try {
final knockResponse = await client . httpClient . post (
Uri . parse (
' ${ client . homeserver } /_synapse/client/pangea/v1/knock_with_code ' ,
) ,
headers: {
' Content-Type ' : ' application/json ' ,
' Authorization ' : ' Bearer ${ client . accessToken } ' ,
} ,
body: jsonEncode ( { ' access_code ' : classCode } ) ,
) ;
if ( knockResponse . statusCode = = 429 ) {
SpaceCodeUtil . messageSnack (
context ,
L10n . of ( context ) ! . tooManyRequest ,
) ;
return ;
}
if ( knockResponse . statusCode ! = 200 ) {
SpaceCodeUtil . messageSnack (
context ,
L10n . of ( context ) ! . unableToFindClass ,
) ;
return ;
}
final knockResult = jsonDecode ( knockResponse . body ) ;
final foundClasses = List < String > . from ( knockResult [ ' rooms ' ] ) ;
final alreadyJoined = List < String > . from ( knockResult [ ' already_joined ' ] ) ;
if ( alreadyJoined . isNotEmpty ) {
SpaceCodeUtil . messageSnack (
context ,
L10n . of ( context ) ! . alreadyInClass ,
) ;
return ;
}
if ( foundClasses . isEmpty ) {
SpaceCodeUtil . messageSnack (
context ,
L10n . of ( context ) ! . unableToFindClass ,
) ;
return ;
}
final chosenClassId = foundClasses . first ;
if ( _pangeaController . matrixState . client . rooms
. any ( ( room ) = > room . id = = chosenClassId ) ) {
setActiveSpaceIdInChatListController ( chosenClassId ) ;
SpaceCodeUtil . messageSnack ( context , L10n . of ( context ) ! . alreadyInClass ) ;
return ;
} else {
await _pangeaController . pStoreService . save (
PLocalKey . justInputtedCode ,
classCode ,
isAccountData: false ,
await showFutureLoadingDialog (
context: context ,
future: ( ) async {
final knockResponse = await client . httpClient . post (
Uri . parse (
' ${ client . homeserver } /_synapse/client/pangea/v1/knock_with_code ' ,
) ,
headers: {
' Content-Type ' : ' application/json ' ,
' Authorization ' : ' Bearer ${ client . accessToken } ' ,
} ,
body: jsonEncode ( { ' access_code ' : classCode } ) ,
) ;
await client . joinRoomById ( chosenClassId ) ;
_pangeaController . pStoreService . delete ( PLocalKey . justInputtedCode ) ;
}
if ( _pangeaController . matrixState . client . getRoomById ( chosenClassId ) = =
null ) {
await _pangeaController . matrixState . client . waitForRoomInSync (
chosenClassId ,
join: true ,
if ( knockResponse . statusCode = = 429 ) {
await showFutureLoadingDialog (
context: context ,
future: ( ) async {
throw L10n . of ( context ) ! . tooManyRequest ;
} ,
) ;
return ;
}
if ( knockResponse . statusCode ! = 200 ) {
await showFutureLoadingDialog (
context: context ,
future: ( ) async {
throw L10n . of ( context ) ! . unableToFindClass ;
} ,
) ;
return ;
}
final knockResult = jsonDecode ( knockResponse . body ) ;
final foundClasses = List < String > . from ( knockResult [ ' rooms ' ] ) ;
final alreadyJoined = List < String > . from ( knockResult [ ' already_joined ' ] ) ;
if ( alreadyJoined . isNotEmpty ) {
await showFutureLoadingDialog (
context: context ,
future: ( ) async {
throw L10n . of ( context ) ! . alreadyInClass ;
} ,
) ;
return ;
}
if ( foundClasses . isEmpty ) {
await showFutureLoadingDialog (
context: context ,
future: ( ) async {
throw L10n . of ( context ) ! . unableToFindClass ;
} ,
) ;
return ;
}
final chosenClassId = foundClasses . first ;
if ( _pangeaController . matrixState . client . rooms
. any ( ( room ) = > room . id = = chosenClassId ) ) {
setActiveSpaceIdInChatListController ( chosenClassId ) ;
await showFutureLoadingDialog (
context: context ,
future: ( ) async {
throw L10n . of ( context ) ! . alreadyInClass ;
} ,
) ;
return ;
} else {
await _pangeaController . pStoreService . save (
PLocalKey . justInputtedCode ,
classCode ,
isAccountData: false ,
) ;
await client . joinRoomById ( chosenClassId ) ;
_pangeaController . pStoreService . delete ( PLocalKey . justInputtedCode ) ;
}
if ( _pangeaController . matrixState . client . getRoomById ( chosenClassId ) = =
null ) {
await _pangeaController . matrixState . client . waitForRoomInSync (
chosenClassId ,
join: true ,
) ;
}
/ / If the room is full , leave
final room =
_pangeaController . matrixState . client . getRoomById ( chosenClassId ) ;
if ( room = = null ) {
return ;
}
final joinResult = await showFutureLoadingDialog (
context: context ,
future: ( ) async {
if ( await room . leaveIfFull ( ) ) {
throw L10n . of ( context ) ! . roomFull ;
}
} ,
) ;
}
if ( joinResult . error ! = null ) {
return ;
}
/ / If the room is full , leave
final room =
_pangeaController . matrixState . client . getRoomById ( chosenClassId ) ;
if ( room = = null ) {
return ;
}
final joinResult = await showFutureLoadingDialog (
context: context ,
future: ( ) async {
if ( await room . leaveIfFull ( ) ) {
throw L10n . of ( context ) ! . roomFull ;
}
} ,
) ;
if ( joinResult . error ! = null ) {
return ;
}
setActiveSpaceIdInChatListController ( chosenClassId ) ;
setActiveSpaceIdInChatListController ( chosenClassId ) ;
/ / add the user ' s analytics room to this joined space
/ / so their teachers can join them via the space hierarchy
final Room ? joinedSpace =
_pangeaController . matrixState . client . getRoomById ( chosenClassId ) ;
/ / add the user ' s analytics room to this joined space
/ / so their teachers can join them via the space hierarchy
final Room ? joinedSpace =
_pangeaController . matrixState . client . getRoomById ( chosenClassId ) ;
/ / when possible , add user ' s analytics room the to space they joined
joinedSpace ? . addAnalyticsRoomsToSpace ( ) ;
/ / when possible , add user ' s analytics room the to space they joined
joinedSpace ? . addAnalyticsRoomsToSpace ( ) ;
/ / and invite the space ' s teachers to the user ' s analytics rooms
joinedSpace ? . inviteSpaceTeachersToAnalyticsRooms ( ) ;
GoogleAnalytics . joinClass ( classCode ) ;
return ;
} catch ( err ) {
SpaceCodeUtil . messageSnack (
context ,
ErrorCopy ( context , err ) . body ,
) ;
}
/ / and invite the space ' s teachers to the user ' s analytics rooms
joinedSpace ? . inviteSpaceTeachersToAnalyticsRooms ( ) ;
GoogleAnalytics . joinClass ( classCode ) ;
return ;
} ,
) ;
/ / P - EPIC
/ / prereq - server needs ability to invite to private room . how ?
/ / does server api have ability with admin token ?