make word meaning activity refetch new definition if cache expires (#1779)

* make word meaning activity refetch new definition if cache expires

* generated

* bake expire at into response json

* generated

* add expire at in to and from json method

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: ggurdin <46800240+ggurdin@users.noreply.github.com>
pull/1688/head
Wilson 9 months ago committed by GitHub
parent 91ab045365
commit 6af450efb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,39 +1,50 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:get_storage/get_storage.dart'; import 'package:get_storage/get_storage.dart';
import 'package:http/http.dart'; import 'package:http/http.dart';
import 'package:fluffychat/pangea/common/config/environment.dart';
import 'package:fluffychat/pangea/common/network/requests.dart';
import 'package:fluffychat/pangea/common/network/urls.dart'; import 'package:fluffychat/pangea/common/network/urls.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/events/models/content_feedback.dart'; import 'package:fluffychat/pangea/events/models/content_feedback.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_request.dart'; import 'package:fluffychat/pangea/lemmas/lemma_info_request.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_response.dart'; import 'package:fluffychat/pangea/lemmas/lemma_info_response.dart';
import 'package:fluffychat/widgets/matrix.dart'; import 'package:fluffychat/widgets/matrix.dart';
import '../common/config/environment.dart';
import '../common/network/requests.dart';
class LemmaInfoRepo { class LemmaInfoRepo {
static final GetStorage _lemmaStorage = GetStorage('lemma_storage'); static final GetStorage _lemmaStorage = GetStorage('lemma_storage');
static void set(LemmaInfoRequest request, LemmaInfoResponse response) { static void set(LemmaInfoRequest request, LemmaInfoResponse response) {
// set expireAt if not set
response.expireAt ??= DateTime.now().add(const Duration(minutes: 1));
_lemmaStorage.write(request.storageKey, response.toJson()); _lemmaStorage.write(request.storageKey, response.toJson());
} }
static Future<LemmaInfoResponse> get( static Future<LemmaInfoResponse> get(
LemmaInfoRequest request, [ LemmaInfoRequest request, [
String? feedback, String? feedback,
bool useExpireAt = false,
]) async { ]) async {
final cachedJson = _lemmaStorage.read(request.storageKey); final cachedJson = _lemmaStorage.read(request.storageKey);
if (cachedJson != null) { final cached =
final cached = LemmaInfoResponse.fromJson(cachedJson); cachedJson == null ? null : LemmaInfoResponse.fromJson(cachedJson);
if (cached != null) {
if (feedback == null) { if (feedback == null) {
// in this case, we just return the cached response // at this point we have a cache without feedback
return cached; if (!useExpireAt) {
// return cache as is if we're not using expireAt
return cached;
} else if (cached.expireAt != null) {
if (DateTime.now().isBefore(cached.expireAt!)) {
// return cache as is if we're using expireAt and it's set but not expired
return cached;
}
}
// we intentionally do not handle the case of expired at not set because
// old caches won't have them set, and we want to trigger a new
// choreo call
} else { } else {
// we're adding this within the service to avoid needing to have the widgets // we're adding this within the service to avoid needing to have the widgets
// save state including the bad response // save state including the bad response
@ -42,16 +53,6 @@ class LemmaInfoRepo {
feedback, feedback,
); );
} }
} else if (feedback != null) {
// the cache should have the request in order for the user to provide feedback
// this would be a strange situation and indicate some error in our logic
debugger(when: kDebugMode);
ErrorHandler.logError(
m: 'Feedback provided for a non-cached request',
data: request.toJson(),
);
} else {
debugPrint('No cached response for lemma ${request.lemma}, calling API');
} }
final Requests req = Requests( final Requests req = Requests(

@ -3,16 +3,21 @@ import 'package:fluffychat/pangea/events/models/content_feedback.dart';
class LemmaInfoResponse implements JsonSerializable { class LemmaInfoResponse implements JsonSerializable {
final List<String> emoji; final List<String> emoji;
final String meaning; final String meaning;
DateTime? expireAt;
LemmaInfoResponse({ LemmaInfoResponse({
required this.emoji, required this.emoji,
required this.meaning, required this.meaning,
this.expireAt,
}); });
factory LemmaInfoResponse.fromJson(Map<String, dynamic> json) { factory LemmaInfoResponse.fromJson(Map<String, dynamic> json) {
return LemmaInfoResponse( return LemmaInfoResponse(
emoji: (json['emoji'] as List<dynamic>).map((e) => e as String).toList(), emoji: (json['emoji'] as List<dynamic>).map((e) => e as String).toList(),
meaning: json['meaning'] as String, meaning: json['meaning'] as String,
expireAt: json['expireAt'] == null
? null
: DateTime.parse(json['expireAt'] as String),
); );
} }
@ -21,6 +26,7 @@ class LemmaInfoResponse implements JsonSerializable {
return { return {
'emoji': emoji, 'emoji': emoji,
'meaning': meaning, 'meaning': meaning,
'expireAt': expireAt?.toIso8601String(),
}; };
} }

@ -38,7 +38,7 @@ class LemmaMeaningActivityGenerator {
userL1: req.userL1, userL1: req.userL1,
); );
final res = await LemmaInfoRepo.get(lemmaDefReq); final res = await LemmaInfoRepo.get(lemmaDefReq, null, true);
final choices = await getDistractorMeanings(lemmaDefReq, 3); final choices = await getDistractorMeanings(lemmaDefReq, 3);

Loading…
Cancel
Save