@ -1,39 +1,50 @@
import ' dart:convert ' ;
import ' dart:developer ' ;
import ' package:flutter/foundation.dart ' ;
import ' package:get_storage/get_storage.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/utils/error_handler.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_response.dart ' ;
import ' package:fluffychat/widgets/matrix.dart ' ;
import ' ../common/config/environment.dart ' ;
import ' ../common/network/requests.dart ' ;
class LemmaInfoRepo {
static final GetStorage _lemmaStorage = GetStorage ( ' lemma_storage ' ) ;
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 ( ) ) ;
}
static Future < LemmaInfoResponse > get (
LemmaInfoRequest request , [
String ? feedback ,
bool useExpireAt = false ,
] ) async {
final cachedJson = _lemmaStorage . read ( request . storageKey ) ;
if ( cachedJson ! = null ) {
final cached = LemmaInfoResponse . fromJson ( cachedJson ) ;
final cached =
cachedJson = = null ? null : LemmaInfoResponse . fromJson ( cachedJson ) ;
if ( cached ! = null ) {
if ( feedback = = null ) {
/ / in this case , we just return the cached response
return cached ;
/ / at this point we have a cache without feedback
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 {
/ / we ' re adding this within the service to avoid needing to have the widgets
/ / save state including the bad response
@ -42,16 +53,6 @@ class LemmaInfoRepo {
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 (