fix: move shouldDoActivity function to pangeaMessageEvent to prevent giving activities for messages not in user's l2 (#1832)

This commit is contained in:
ggurdin 2025-02-18 09:57:48 -05:00 committed by GitHub
parent cd51e68474
commit b1b5e998ce
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 50 additions and 56 deletions

View file

@ -253,7 +253,8 @@ class HtmlMessage extends StatelessWidget {
? isSelected!.call(token)
: false;
final shouldDo = token?.shouldDoActivity(
final shouldDo = pangeaMessageEvent?.shouldDoActivity(
token: token,
a: ActivityTypeEnum.wordMeaning,
feature: null,
tag: null,

View file

@ -71,6 +71,8 @@ class MessageAnalyticsEntry {
late final bool _includeHiddenWordActivities;
late final PangeaMessageEvent _pangeaMessageEvent;
final List<TargetTokensAndActivityType> _activityQueue = [];
final int _maxQueueLength = 3;
@ -78,9 +80,11 @@ class MessageAnalyticsEntry {
MessageAnalyticsEntry({
required List<PangeaToken> tokens,
required bool includeHiddenWordActivities,
required PangeaMessageEvent pangeaMessageEvent,
}) {
_tokens = tokens;
_includeHiddenWordActivities = includeHiddenWordActivities;
_pangeaMessageEvent = pangeaMessageEvent;
setActivityQueue();
}
@ -171,7 +175,8 @@ class MessageAnalyticsEntry {
final List<List<PangeaToken>> sequences = [];
List<PangeaToken> currentSequence = [];
for (final token in _tokens) {
if (token.shouldDoActivity(
if (_pangeaMessageEvent.shouldDoActivity(
token: token,
a: ActivityTypeEnum.hiddenWordListening,
feature: null,
tag: null,
@ -257,6 +262,7 @@ class MessageAnalyticsController {
_cache[key] = MessageAnalyticsEntry(
tokens: tokens,
includeHiddenWordActivities: includeHiddenWordActivities,
pangeaMessageEvent: pangeaMessageEvent,
);
clean();

View file

@ -13,6 +13,7 @@ import 'package:fluffychat/pangea/choreographer/repo/full_text_translation_repo.
import 'package:fluffychat/pangea/common/constants/model_keys.dart';
import 'package:fluffychat/pangea/events/event_wrappers/pangea_representation_event.dart';
import 'package:fluffychat/pangea/events/extensions/pangea_event_extension.dart';
import 'package:fluffychat/pangea/events/models/pangea_token_model.dart';
import 'package:fluffychat/pangea/events/models/representation_content_model.dart';
import 'package:fluffychat/pangea/events/models/tokens_event_content_model.dart';
import 'package:fluffychat/pangea/spaces/models/space_model.dart';
@ -578,7 +579,8 @@ class PangeaMessageEvent {
if (eligibleTokens.isEmpty) return 1;
final alreadyDid = eligibleTokens.where(
(token) => !token.shouldDoActivity(
(token) => !shouldDoActivity(
token: token,
a: ActivityTypeEnum.wordMeaning,
feature: null,
tag: null,
@ -697,4 +699,21 @@ class PangeaMessageEvent {
l2Code == null ? [] : practiceActivitiesByLangCode(l2Code!);
bool get shouldShowToolbar => !event.isActivityMessage;
bool shouldDoActivity({
required PangeaToken? token,
required ActivityTypeEnum a,
required String? feature,
required String? tag,
}) {
if (!messageDisplayLangIsL2 || token == null) {
return false;
}
return token.shouldDoActivity(
a: a,
feature: feature,
tag: tag,
);
}
}

View file

@ -388,16 +388,6 @@ class PangeaToken {
}
}
bool get shouldDoPosActivity => shouldDoMorphActivity("pos");
bool shouldDoMorphActivity(String feature) {
return shouldDoActivity(
a: ActivityTypeEnum.morphId,
feature: feature,
tag: getMorphTag(feature),
);
}
/// Safely get morph tag for a given feature without regard for case
String? getMorphTag(String feature) {
if (morph.containsKey(feature)) return morph[feature];
@ -463,22 +453,6 @@ class PangeaToken {
_isActivityProbablyLevelAppropriate(a, feature, tag);
}
List<ActivityTypeEnum> get eligibleActivityTypes {
final List<ActivityTypeEnum> eligibleActivityTypes = [];
if (!lemma.saveVocab) {
return eligibleActivityTypes;
}
for (final type in ActivityTypeEnum.values) {
if (shouldDoActivity(a: type, feature: null, tag: null)) {
eligibleActivityTypes.add(type);
}
}
return eligibleActivityTypes;
}
ConstructUses get vocabConstruct =>
MatrixState.pangeaController.getAnalytics.constructListModel
.getConstructUses(
@ -577,14 +551,6 @@ class PangeaToken {
.cast<ConstructUses>()
.toList();
Map<String, dynamic> toServerChoiceTokenWithXP() {
return {
'token': toJson(),
'constructs_with_xp': constructs.map((e) => e.toJson()).toList(),
'target_types': eligibleActivityTypes.map((e) => e.string).toList(),
};
}
Future<List<String>> getEmojiChoices() => LemmaInfoRepo.get(
LemmaInfoRequest(
lemma: lemma.text,

View file

@ -197,12 +197,12 @@ class MessageTextWidget extends StatelessWidget {
text: TextSpan(
children:
tokenPositions.mapIndexed((int i, TokenPosition tokenPosition) {
final shouldDo = tokenPosition.token?.shouldDoActivity(
a: ActivityTypeEnum.wordMeaning,
feature: null,
tag: null,
) ??
false;
final shouldDo = pangeaMessageEvent.shouldDoActivity(
token: tokenPosition.token,
a: ActivityTypeEnum.wordMeaning,
feature: null,
tag: null,
);
final didMeaningActivity =
tokenPosition.token?.didActivitySuccessfully(

View file

@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:fluffychat/pangea/events/models/pangea_token_model.dart';
import 'package:fluffychat/pangea/toolbar/enums/activity_type_enum.dart';
import 'package:fluffychat/pangea/toolbar/widgets/practice_activity/word_zoom_activity_button.dart';
class EmojiPracticeButton extends StatelessWidget {
@ -16,22 +15,14 @@ class EmojiPracticeButton extends StatelessWidget {
super.key,
});
bool get _shouldDoActivity => token.shouldDoActivity(
a: ActivityTypeEnum.emoji,
feature: null,
tag: null,
);
@override
Widget build(BuildContext context) {
final emoji = token.getEmoji();
return _shouldDoActivity || emoji != null
return emoji != null
? SizedBox(
width: 40,
child: WordZoomActivityButton(
icon: emoji == null
? const Icon(Icons.add_reaction_outlined)
: Text(emoji),
icon: Text(emoji),
isSelected: isSelected,
onPressed: onPressed,
),

View file

@ -1,6 +1,8 @@
import 'package:flutter/material.dart';
import 'package:fluffychat/pangea/events/event_wrappers/pangea_message_event.dart';
import 'package:fluffychat/pangea/events/models/pangea_token_model.dart';
import 'package:fluffychat/pangea/toolbar/enums/activity_type_enum.dart';
import 'package:fluffychat/pangea/toolbar/widgets/word_zoom/morphs/morphological_list_item.dart';
class ActivityMorph {
@ -16,12 +18,14 @@ class ActivityMorph {
}
class MorphologicalListWidget extends StatelessWidget {
final PangeaMessageEvent pangeaMessageEvent;
final PangeaToken token;
final String? selectedMorphFeature;
final Function(String?) setMorphFeature;
const MorphologicalListWidget({
super.key,
required this.pangeaMessageEvent,
required this.selectedMorphFeature,
required this.token,
required this.setMorphFeature,
@ -32,7 +36,12 @@ class MorphologicalListWidget extends StatelessWidget {
return ActivityMorph(
morphFeature: entry.key,
morphTag: entry.value,
revealed: !token.shouldDoMorphActivity(entry.key),
revealed: !pangeaMessageEvent.shouldDoActivity(
token: token,
a: ActivityTypeEnum.morphId,
feature: entry.key,
tag: token.getMorphTag(entry.key),
),
);
}).toList();

View file

@ -176,7 +176,8 @@ class WordZoomWidgetState extends State<WordZoomWidget> {
}
bool _shouldShowActivity(WordZoomSelection selection) {
final shouldDo = widget.token.shouldDoActivity(
final shouldDo = widget.messageEvent.shouldDoActivity(
token: widget.token,
a: selection.activityType,
feature: _selectedMorphFeature,
tag: _selectedMorphFeature == null
@ -272,6 +273,7 @@ class WordZoomWidgetState extends State<WordZoomWidget> {
wordDetailsController: this,
),
MorphologicalListWidget(
pangeaMessageEvent: widget.messageEvent,
token: widget.token,
setMorphFeature: (feature) => _setSelectionType(
WordZoomSelection.morph,