diff --git a/lib/pages/chat/chat.dart b/lib/pages/chat/chat.dart index a30e4e5fe1..f5e6f3b6b5 100644 --- a/lib/pages/chat/chat.dart +++ b/lib/pages/chat/chat.dart @@ -1560,7 +1560,6 @@ class ChatController extends State timeline: timeline!, ownMessage: event.senderId == room.client.userID, ); - _pangeaMessageEvents[eventId]!.setDisplayRepresentation(context); } void setToolbarDisplayController(String eventId) { diff --git a/lib/pangea/controllers/message_data_controller.dart b/lib/pangea/controllers/message_data_controller.dart index 98bf83245b..b0759b108c 100644 --- a/lib/pangea/controllers/message_data_controller.dart +++ b/lib/pangea/controllers/message_data_controller.dart @@ -108,45 +108,26 @@ class MessageDataController extends BaseController { /////// translation //////// /// make representation (originalSent and originalWritten always false) - Future _getRepresentationMatrixEvent({ - required BuildContext context, + Future _sendRepresentationMatrixEvent({ + required PangeaRepresentation representation, required String messageEventId, - required FullTextTranslationRequestModel req, required Room room, }) async { try { - final FullTextTranslationResponseModel res = - await FullTextTranslationRepo.translate( - accessToken: await _pangeaController.userController.accessToken, - request: req, - ); - - final PangeaRepresentation representation = PangeaRepresentation( - langCode: req.tgtLang, - text: res.bestTranslation, - originalSent: false, - originalWritten: false, - ); - final Event? repEvent = await room.sendPangeaEvent( content: representation.toJson(), parentEventId: messageEventId, type: PangeaEventTypes.representation, ); - // debugger(when: kDebugMode && repEvent == null); - return repEvent; } catch (err, stack) { Sentry.addBreadcrumb( Breadcrumb( message: - "err in _getRepresentationMatrixEvent with messageEventId $messageEventId", + "err in _sendRepresentationMatrixEvent with messageEventId $messageEventId", ), ); - Sentry.addBreadcrumb( - Breadcrumb.fromJson({"req": req.toJson()}), - ); Sentry.addBreadcrumb( Breadcrumb.fromJson({"room": room.toJson()}), ); @@ -155,15 +136,48 @@ class MessageDataController extends BaseController { } } - /// make representation (originalSent and originalWritten always false) - Future getRepresentationMatrixEvent({ - required BuildContext context, - required String messageEventId, + Future getPangeaRepresentation({ required String text, required String? source, required String target, required Room room, - }) { + }) async { + final req = FullTextTranslationRequestModel( + text: text, + tgtLang: target, + srcLang: source, + userL2: + _pangeaController.languageController.activeL2Code(roomID: room.id)!, + userL1: + _pangeaController.languageController.activeL1Code(roomID: room.id)!, + ); + + try { + final FullTextTranslationResponseModel res = + await FullTextTranslationRepo.translate( + accessToken: await _pangeaController.userController.accessToken, + request: req, + ); + + return PangeaRepresentation( + langCode: req.tgtLang, + text: res.bestTranslation, + originalSent: false, + originalWritten: false, + ); + } catch (err, stack) { + ErrorHandler.logError(e: err, s: stack); + return null; + } + } + + /// make representation (originalSent and originalWritten always false) + Future sendRepresentationMatrixEvent({ + required PangeaRepresentation representation, + required String messageEventId, + required Room room, + required String target, + }) async { final CacheItem? item = getItem(messageEventId, PangeaEventTypes.representation, target); if (item != null) return item.data; @@ -173,19 +187,10 @@ class MessageDataController extends BaseController { messageEventId, PangeaEventTypes.representation, target, - _getRepresentationMatrixEvent( - context: context, + _sendRepresentationMatrixEvent( messageEventId: messageEventId, - req: FullTextTranslationRequestModel( - text: text, - tgtLang: target, - srcLang: source, - userL2: _pangeaController.languageController - .activeL2Code(roomID: room.id)!, - userL1: _pangeaController.languageController - .activeL1Code(roomID: room.id)!, - ), room: room, + representation: representation, ), ), ); diff --git a/lib/pangea/models/pangea_message_event.dart b/lib/pangea/models/pangea_message_event.dart index 9e130766a4..0b15faa999 100644 --- a/lib/pangea/models/pangea_message_event.dart +++ b/lib/pangea/models/pangea_message_event.dart @@ -306,7 +306,7 @@ class PangeaMessageEvent { return rep; } - Future representationByLanguageGlobal({ + Future representationByLanguageGlobal({ required BuildContext context, required String langCode, }) async { @@ -316,7 +316,7 @@ class PangeaMessageEvent { if (repLocal != null || langCode == LanguageKeys.unknownLanguage || langCode == LanguageKeys.mixedLanguage || - langCode == LanguageKeys.multiLanguage) return repLocal; + langCode == LanguageKeys.multiLanguage) return repLocal?.content; if (eventId.contains("web")) return null; @@ -325,32 +325,38 @@ class PangeaMessageEvent { final PangeaRepresentation? basis = (originalWritten ?? originalSent)?.content; - final Event? repEvent = await MatrixState.pangeaController.messageData - .getRepresentationMatrixEvent( - context: context, - messageEventId: _latestEdit.eventId, + final PangeaRepresentation? pangeaRep = + await MatrixState.pangeaController.messageData.getPangeaRepresentation( text: basis?.text ?? _latestEdit.body, - target: langCode, source: basis?.langCode, + target: langCode, room: _latestEdit.room, ); - // PTODO - if res.source different from langCode, save rep for source + if (pangeaRep == null || + await _latestEdit.room.getEventById(_latestEdit.eventId) == null) { + return null; + } - return repEvent != null - ? RepresentationEvent( - event: repEvent, + MatrixState.pangeaController.messageData + .sendRepresentationMatrixEvent( + representation: pangeaRep, + messageEventId: _latestEdit.eventId, + room: _latestEdit.room, + target: langCode, + ) + .then( + (value) { + representations.add( + RepresentationEvent( + event: value, timeline: timeline, - ) - : null; - // } catch (err, s) { - // debugger(when: kDebugMode); - // ErrorHandler.logError( - // e: err, - // s: s, - // ); - // return null; - // } + ), + ); + }, + ); + + return pangeaRep; } RepresentationEvent? get originalSent => representations @@ -388,36 +394,6 @@ class PangeaMessageEvent { return langCode ?? LanguageKeys.unknownLanguage; } - RepresentationEvent? _displayRepresentation; - - RepresentationEvent? displayRepresentation(String langCode) => - _displayRepresentation; - - Future setDisplayRepresentation( - BuildContext context, - ) async { - if (messageDisplayLangCode == LanguageKeys.unknownLanguage || - _displayRepresentation != null) { - return; - } - _displayRepresentation = representationByLanguage(messageDisplayLangCode); - if (_displayRepresentation != null) return; - - try { - _displayRepresentation = await representationByLanguageGlobal( - context: context, - langCode: messageDisplayLangCode, - ); - return; - } catch (err, s) { - ErrorHandler.logError( - m: "error in getDisplayRepresentation", - e: err, - s: s, - ); - } - } - // List get activities => //each match is turned into an activity that other students can access //they're not told the answer but have to find it themselves diff --git a/lib/pangea/utils/get_chat_list_item_subtitle.dart b/lib/pangea/utils/get_chat_list_item_subtitle.dart index 006282fd36..47b4b6f751 100644 --- a/lib/pangea/utils/get_chat_list_item_subtitle.dart +++ b/lib/pangea/utils/get_chat_list_item_subtitle.dart @@ -3,6 +3,7 @@ import 'package:fluffychat/pangea/constants/model_keys.dart'; import 'package:fluffychat/pangea/controllers/pangea_controller.dart'; import 'package:fluffychat/pangea/models/class_model.dart'; import 'package:fluffychat/pangea/models/pangea_message_event.dart'; +import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:matrix/matrix.dart'; @@ -16,68 +17,68 @@ class GetChatListItemSubtitle { PangeaController pangeaController, ) async { if (event == null) return L10n.of(context)!.emptyChat; - // try { - if (event.type != EventTypes.Message || - !pangeaController.permissionsController - .isToolEnabled(ToolSetting.immersionMode, event.room) || - event.content.tryGet(ModelKey.transcription) != null) { - return event.calcLocalizedBody( - MatrixLocals(L10n.of(context)!), - hideReply: true, - hideEdit: true, - plaintextBody: true, - removeMarkdown: true, - withSenderNamePrefix: !event.room.isDirectChat || - event.room.directChatMatrixID != event.room.lastEvent?.senderId, - ); - } + try { + if (event.type != EventTypes.Message || + !pangeaController.permissionsController + .isToolEnabled(ToolSetting.immersionMode, event.room) || + event.content.tryGet(ModelKey.transcription) != null) { + return event.calcLocalizedBody( + MatrixLocals(L10n.of(context)!), + hideReply: true, + hideEdit: true, + plaintextBody: true, + removeMarkdown: true, + withSenderNamePrefix: !event.room.isDirectChat || + event.room.directChatMatrixID != event.room.lastEvent?.senderId, + ); + } - String? eventContextId = event.eventId; - if (!event.eventId.isValidMatrixId || event.eventId.sigil != '\$') { - eventContextId = null; - } - final Timeline timeline = - await event.room.getTimeline(eventContextId: eventContextId); + String? eventContextId = event.eventId; + if (!event.eventId.isValidMatrixId || event.eventId.sigil != '\$') { + eventContextId = null; + } + final Timeline timeline = + await event.room.getTimeline(eventContextId: eventContextId); - final PangeaMessageEvent pangeaMessageEvent = PangeaMessageEvent( - event: event, - timeline: timeline, - ownMessage: false, - ); - final l2Code = - pangeaController.languageController.activeL2Code(roomID: event.roomId); + final PangeaMessageEvent pangeaMessageEvent = PangeaMessageEvent( + event: event, + timeline: timeline, + ownMessage: false, + ); + final l2Code = pangeaController.languageController + .activeL2Code(roomID: event.roomId); - if (l2Code == null || l2Code == LanguageKeys.unknownLanguage) { - return event.body; - } + if (l2Code == null || l2Code == LanguageKeys.unknownLanguage) { + return event.body; + } - final String? text = - (await pangeaMessageEvent.representationByLanguageGlobal( - context: context, - langCode: l2Code, - )) - ?.text; + final String? text = + (await pangeaMessageEvent.representationByLanguageGlobal( + context: context, + langCode: l2Code, + )) + ?.text; - final i18n = MatrixLocals(L10n.of(context)!); + final i18n = MatrixLocals(L10n.of(context)!); - if (text == null) return L10n.of(context)!.emptyChat; + if (text == null) return L10n.of(context)!.emptyChat; - if (!event.room.isDirectChat || - event.room.directChatMatrixID != event.room.lastEvent?.senderId) { - final senderNameOrYou = event.senderId == event.room.client.userID - ? i18n.you - : event.room - .unsafeGetUserFromMemoryOrFallback(event.senderId) - .calcDisplayname(i18n: i18n); + if (!event.room.isDirectChat || + event.room.directChatMatrixID != event.room.lastEvent?.senderId) { + final senderNameOrYou = event.senderId == event.room.client.userID + ? i18n.you + : event.room + .unsafeGetUserFromMemoryOrFallback(event.senderId) + .calcDisplayname(i18n: i18n); - return "$senderNameOrYou: $text"; - } + return "$senderNameOrYou: $text"; + } - return text; - // } catch (e, s) { - // debugger(when: kDebugMode); - // ErrorHandler.logError(e: e, s: s); - // return event.body; - // } + return text; + } catch (e, s) { + // debugger(when: kDebugMode); + ErrorHandler.logError(e: e, s: s); + return event.body; + } } } diff --git a/lib/pangea/widgets/chat/message_translation_card.dart b/lib/pangea/widgets/chat/message_translation_card.dart index 50f6ff3714..5014d83f33 100644 --- a/lib/pangea/widgets/chat/message_translation_card.dart +++ b/lib/pangea/widgets/chat/message_translation_card.dart @@ -1,5 +1,5 @@ +import 'package:fluffychat/pangea/models/message_data_models.dart'; import 'package:fluffychat/pangea/models/pangea_message_event.dart'; -import 'package:fluffychat/pangea/models/pangea_representation_event.dart'; import 'package:fluffychat/pangea/repo/full_text_translation_repo.dart'; import 'package:fluffychat/pangea/utils/bot_style.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; @@ -25,7 +25,7 @@ class MessageTranslationCard extends StatefulWidget { } class MessageTranslationCardState extends State { - RepresentationEvent? repEvent; + PangeaRepresentation? repEvent; String? selectionTranslation; String? oldSelectedText; String? l1Code; @@ -43,9 +43,11 @@ class MessageTranslationCardState extends State { final String? langCode = translationLangCode(); if (langCode == null) return; - repEvent = widget.messageEvent.representationByLanguage( - langCode, - ); + repEvent = widget.messageEvent + .representationByLanguage( + langCode, + ) + ?.content; if (repEvent == null && mounted) { repEvent = await widget.messageEvent.representationByLanguageGlobal( diff --git a/lib/pangea/widgets/igc/pangea_rich_text.dart b/lib/pangea/widgets/igc/pangea_rich_text.dart index 37124175dd..342993daaa 100644 --- a/lib/pangea/widgets/igc/pangea_rich_text.dart +++ b/lib/pangea/widgets/igc/pangea_rich_text.dart @@ -4,8 +4,8 @@ import 'dart:ui'; import 'package:fluffychat/config/app_config.dart'; import 'package:fluffychat/pangea/constants/language_keys.dart'; import 'package:fluffychat/pangea/controllers/pangea_controller.dart'; +import 'package:fluffychat/pangea/models/message_data_models.dart'; import 'package:fluffychat/pangea/models/pangea_message_event.dart'; -import 'package:fluffychat/pangea/models/pangea_representation_event.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/widgets/chat/message_context_menu.dart'; import 'package:fluffychat/pangea/widgets/chat/message_toolbar.dart'; @@ -38,7 +38,7 @@ class PangeaRichTextState extends State { bool _fetchingRepresentation = false; double get blur => _fetchingRepresentation && widget.immersionMode ? 5 : 0; String textSpan = ""; - RepresentationEvent? repEvent; + PangeaRepresentation? repEvent; @override void initState() { @@ -71,9 +71,11 @@ class PangeaRichTextState extends State { debugger(when: kDebugMode); } - repEvent = widget.pangeaMessageEvent.representationByLanguage( - widget.pangeaMessageEvent.messageDisplayLangCode, - ); + repEvent = widget.pangeaMessageEvent + .representationByLanguage( + widget.pangeaMessageEvent.messageDisplayLangCode, + ) + ?.content; if (repEvent == null) { setState(() => _fetchingRepresentation = true);