From 7b1cccf78bc20fb65b97a0c8702394e07628b1b6 Mon Sep 17 00:00:00 2001 From: ggurdin <46800240+ggurdin@users.noreply.github.com> Date: Wed, 8 Jan 2025 09:05:19 -0500 Subject: [PATCH] fix: In lemma definition request, use lemma.form if no lemma.text and log error to sentry (#1376) --- lib/pangea/models/pangea_token_model.dart | 2 +- lib/pangea/repo/lemma_definition_repo.dart | 27 ++++++++++++------- .../word_meaning_activity_generator.dart | 2 +- .../contextual_translation_widget.dart | 2 +- .../word_zoom/lemma_definition_widget.dart | 2 +- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/pangea/models/pangea_token_model.dart b/lib/pangea/models/pangea_token_model.dart index 0a5961780..8d0920e55 100644 --- a/lib/pangea/models/pangea_token_model.dart +++ b/lib/pangea/models/pangea_token_model.dart @@ -524,7 +524,7 @@ class PangeaToken { Future> getEmojiChoices() => LemmaDictionaryRepo.get( LemmaDefinitionRequest( - lemma: lemma.text, + lemma: lemma, partOfSpeech: pos, lemmaLang: MatrixState .pangeaController.languageController.userL2?.langCode ?? diff --git a/lib/pangea/repo/lemma_definition_repo.dart b/lib/pangea/repo/lemma_definition_repo.dart index 78ed1e489..a1fe83226 100644 --- a/lib/pangea/repo/lemma_definition_repo.dart +++ b/lib/pangea/repo/lemma_definition_repo.dart @@ -2,31 +2,40 @@ import 'dart:convert'; import 'package:http/http.dart'; +import 'package:fluffychat/pangea/models/lemma.dart'; import 'package:fluffychat/pangea/network/urls.dart'; +import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/widgets/matrix.dart'; import '../config/environment.dart'; import '../network/requests.dart'; class LemmaDefinitionRequest { - final String lemma; + final Lemma _lemma; final String partOfSpeech; final String lemmaLang; final String userL1; LemmaDefinitionRequest({ - required this.lemma, required this.partOfSpeech, required this.lemmaLang, required this.userL1, - }); + required Lemma lemma, + }) : _lemma = lemma; - factory LemmaDefinitionRequest.fromJson(Map json) { - return LemmaDefinitionRequest( - lemma: json['lemma'] as String, - partOfSpeech: json['part_of_speech'] as String, - lemmaLang: json['lemma_lang'] as String, - userL1: json['user_l1'] as String, + String get lemma { + if (_lemma.text.isNotEmpty) { + return _lemma.text; + } + ErrorHandler.logError( + e: "Found lemma with empty text", + data: { + 'lemma': _lemma, + 'part_of_speech': partOfSpeech, + 'lemma_lang': lemmaLang, + 'user_l1': userL1, + }, ); + return _lemma.form; } Map toJson() { diff --git a/lib/pangea/repo/practice/word_meaning_activity_generator.dart b/lib/pangea/repo/practice/word_meaning_activity_generator.dart index 94ef28233..2c002c6c6 100644 --- a/lib/pangea/repo/practice/word_meaning_activity_generator.dart +++ b/lib/pangea/repo/practice/word_meaning_activity_generator.dart @@ -21,7 +21,7 @@ class WordMeaningActivityGenerator { ); final LemmaDefinitionRequest lemmaDefReq = LemmaDefinitionRequest( - lemma: lemmaId.lemma, + lemma: req.targetTokens[0].lemma, partOfSpeech: lemmaId.category, /// This assumes that the user's L2 is the language of the lemma diff --git a/lib/pangea/widgets/word_zoom/contextual_translation_widget.dart b/lib/pangea/widgets/word_zoom/contextual_translation_widget.dart index a3281552c..a2f1741a1 100644 --- a/lib/pangea/widgets/word_zoom/contextual_translation_widget.dart +++ b/lib/pangea/widgets/word_zoom/contextual_translation_widget.dart @@ -20,7 +20,7 @@ class ContextualTranslationWidget extends StatelessWidget { Future _fetchDefinition() async { final LemmaDefinitionRequest lemmaDefReq = LemmaDefinitionRequest( - lemma: token.lemma.text, + lemma: token.lemma, partOfSpeech: token.pos, /// This assumes that the user's L2 is the language of the lemma diff --git a/lib/pangea/widgets/word_zoom/lemma_definition_widget.dart b/lib/pangea/widgets/word_zoom/lemma_definition_widget.dart index 780de5ea7..e2772c5bf 100644 --- a/lib/pangea/widgets/word_zoom/lemma_definition_widget.dart +++ b/lib/pangea/widgets/word_zoom/lemma_definition_widget.dart @@ -36,7 +36,7 @@ class LemmaDefinitionWidgetState extends State { } else { final res = await LemmaDictionaryRepo.get( LemmaDefinitionRequest( - lemma: widget.token.lemma.text, + lemma: widget.token.lemma, partOfSpeech: widget.token.pos, lemmaLang: widget.tokenLang, userL1: MatrixState