Skip to content

Commit

Permalink
Merge pull request #78 from pangeachat/decouple-translation
Browse files Browse the repository at this point in the history
Decouple sending representation event from showing translation
  • Loading branch information
wcjord authored Feb 21, 2024
2 parents 7a38608 + f6444d4 commit 4a2e60d
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 154 deletions.
1 change: 0 additions & 1 deletion lib/pages/chat/chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1560,7 +1560,6 @@ class ChatController extends State<ChatPageWithRoom>
timeline: timeline!,
ownMessage: event.senderId == room.client.userID,
);
_pangeaMessageEvents[eventId]!.setDisplayRepresentation(context);
}

void setToolbarDisplayController(String eventId) {
Expand Down
81 changes: 43 additions & 38 deletions lib/pangea/controllers/message_data_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,45 +108,26 @@ class MessageDataController extends BaseController {
/////// translation ////////
/// make representation (originalSent and originalWritten always false)
Future<Event?> _getRepresentationMatrixEvent({
required BuildContext context,
Future<Event?> _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()}),
);
Expand All @@ -155,15 +136,48 @@ class MessageDataController extends BaseController {
}
}

/// make representation (originalSent and originalWritten always false)
Future<Event?> getRepresentationMatrixEvent({
required BuildContext context,
required String messageEventId,
Future<PangeaRepresentation?> 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<Event?> 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;
Expand All @@ -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,
),
),
);
Expand Down
78 changes: 27 additions & 51 deletions lib/pangea/models/pangea_message_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ class PangeaMessageEvent {
return rep;
}

Future<RepresentationEvent?> representationByLanguageGlobal({
Future<PangeaRepresentation?> representationByLanguageGlobal({
required BuildContext context,
required String langCode,
}) async {
Expand All @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -388,36 +394,6 @@ class PangeaMessageEvent {
return langCode ?? LanguageKeys.unknownLanguage;
}

RepresentationEvent? _displayRepresentation;

RepresentationEvent? displayRepresentation(String langCode) =>
_displayRepresentation;

Future<void> 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<SpanData> 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
Expand Down
109 changes: 55 additions & 54 deletions lib/pangea/utils/get_chat_list_item_subtitle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}
}
}
Loading

0 comments on commit 4a2e60d

Please sign in to comment.