Skip to content

Commit

Permalink
feat: 5741 - respecting the "search products" access limitations (ope…
Browse files Browse the repository at this point in the history
…nfoodfacts#5810)

* feat: 5741 - respecting the "search products" access limitations

New file:
* `search_products_manager.dart`: Management of "search products" access limitations.

Impacted files:
* `background_task_download_products.dart`: refactored using `SearchProductsManager`
* `background_task_language_refresh.dart`: refactored using `SearchProductsManager`
* `background_task_top_barcodes.dart`: refactored using `SearchProductsManager`
* `lazy_counter.dart`: refactored using `SearchProductsManager`
* `paged_product_query.dart`: refactored using `SearchProductsManager`
* `product_list_page.dart`: refactored using `SearchProductsManager`
* `product_refresher.dart`: refactored using `SearchProductsManager`
* `pubspec.lock`: wtf

* fix: 5618 - category comparison for OxF

Impacted files:
* `background_task_language_refresh.dart`: explicitly setting the product_type
* `dao_product.dart`: made `productType` mandatory and not null for `put` and `putAll`
* `new_product_footer.dart`: fixed case with unexploitable `comparedToCategory` field
* `product_list_page.dart`: explicitly setting the product_type
* `product_refresher.dart`: explicitly setting the product_type
* `pubspec.lock`: wtf
* `query_product_list_supplier.dart`: explicitly setting the product_type

* fix: 5617 - share link to correct Oxf server

* fix: 5617 - share link to correct Oxf server

Impacted files:
* `app_en.arb`: added labels for sharing an Oxf product
* `new_product_footer.dart`: now displaying a correct label for product sharing - in addition to correct url
* `product_query.dart`: added labels for sharing an Oxf product
* `pubspec.lock`: wtf
* `pubspec.yaml`: downgraded sentry_flutter from problematic version

---------

Co-authored-by: Edouard Marquez <[email protected]>
  • Loading branch information
monsieurtanuki and g123k authored Nov 9, 2024
1 parent 4316432 commit 4d48671
Show file tree
Hide file tree
Showing 15 changed files with 181 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_work_barcode.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/query/search_products_manager.dart';

/// Background progressing task about downloading products.
class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
Expand Down Expand Up @@ -123,7 +124,8 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
fields.remove(ProductField.KNOWLEDGE_PANELS);
}
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
final SearchResult searchResult =
await SearchProductsManager.searchProducts(
ProductQuery.getReadUser(),
ProductSearchQueryConfiguration(
fields: fields,
Expand All @@ -137,6 +139,7 @@ class BackgroundTaskDownloadProducts extends BackgroundTaskProgressing {
version: ProductQuery.productQueryVersion,
),
uriHelper: uriProductHelper,
type: SearchProductsType.background,
);
final List<Product>? downloadedProducts = searchResult.products;
if (downloadedProducts == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/query/search_products_manager.dart';

/// Background task about downloading products to translate.
class BackgroundTaskLanguageRefresh extends BackgroundTask {
Expand Down Expand Up @@ -127,7 +128,8 @@ class BackgroundTaskLanguageRefresh extends BackgroundTask {
if (barcodes.isEmpty) {
return;
}
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
final SearchResult searchResult =
await SearchProductsManager.searchProducts(
getUser(),
ProductSearchQueryConfiguration(
fields: ProductQuery.fields,
Expand All @@ -141,13 +143,18 @@ class BackgroundTaskLanguageRefresh extends BackgroundTask {
version: ProductQuery.productQueryVersion,
),
uriHelper: _uriProductHelper,
type: SearchProductsType.background,
);
if (searchResult.products == null || searchResult.count == null) {
throw Exception('Cannot refresh language');
}

// save into database and refresh all visible products.
await daoProduct.putAll(searchResult.products!, language);
await daoProduct.putAll(
searchResult.products!,
language,
productType: productType,
);
localDatabase.upToDate.setLatestDownloadedProducts(searchResult.products!);

// Next page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:smooth_app/background/operation_type.dart';
import 'package:smooth_app/database/dao_work_barcode.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/query/search_products_manager.dart';

/// Background progressing task about downloading top n barcodes.
class BackgroundTaskTopBarcodes extends BackgroundTaskProgressing {
Expand Down Expand Up @@ -101,7 +102,8 @@ class BackgroundTaskTopBarcodes extends BackgroundTaskProgressing {

@override
Future<void> execute(final LocalDatabase localDatabase) async {
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
final SearchResult searchResult =
await SearchProductsManager.searchProducts(
ProductQuery.getReadUser(),
ProductSearchQueryConfiguration(
fields: <ProductField>[ProductField.BARCODE],
Expand All @@ -115,6 +117,7 @@ class BackgroundTaskTopBarcodes extends BackgroundTaskProgressing {
version: ProductQuery.productQueryVersion,
),
uriHelper: uriProductHelper,
type: SearchProductsType.background,
);
if (searchResult.products == null || searchResult.count == null) {
throw Exception('Cannot download top barcodes');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class QueryProductListSupplier extends ProductListSupplier {
await DaoProduct(localDatabase).putAll(
searchResult.products!,
productQuery.language,
productType: productQuery.productType,
);
}
await DaoProductList(localDatabase).put(productList);
Expand Down
10 changes: 4 additions & 6 deletions packages/smooth_app/lib/database/dao_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class DaoProduct extends AbstractSqlDao implements BulkDeletable {
Future<void> put(
final Product product,
final OpenFoodFactsLanguage language, {
final ProductType? productType,
required final ProductType productType,
}) async =>
putAll(
<Product>[product],
Expand All @@ -166,12 +166,10 @@ class DaoProduct extends AbstractSqlDao implements BulkDeletable {
Future<void> putAll(
final Iterable<Product> products,
final OpenFoodFactsLanguage language, {
final ProductType? productType,
required final ProductType productType,
}) async {
if (productType != null) {
for (final Product product in products) {
product.productType = productType;
}
for (final Product product in products) {
product.productType = productType;
}
await localDatabase.database.transaction(
(final Transaction transaction) async => _bulkReplaceLoop(
Expand Down
29 changes: 28 additions & 1 deletion packages/smooth_app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -2311,7 +2311,34 @@
},
"share_product_text": "Have a look at this product on Open Food Facts: {url}",
"@share_product_text": {
"description": "The content which is send, when sharing a product",
"description": "The content which is send, when sharing a 'food' product",
"placeholders": {
"url": {
"type": "String"
}
}
},
"share_product_text_beauty": "Have a look at this product on Open Beauty Facts: {url}",
"@share_product_text_beauty": {
"description": "The content which is send, when sharing a 'beauty' product",
"placeholders": {
"url": {
"type": "String"
}
}
},
"share_product_text_pet_food": "Have a look at this product on Open PetFood Facts: {url}",
"@share_product_text_pet_food": {
"description": "The content which is send, when sharing a 'pet food' product",
"placeholders": {
"url": {
"type": "String"
}
}
},
"share_product_text_product": "Have a look at this product on Open Products Facts: {url}",
"@share_product_text_product": {
"description": "The content which is send, when sharing a 'products' product",
"placeholders": {
"url": {
"type": "String"
Expand Down
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/pages/preferences/lazy_counter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/query/paged_user_product_query.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/query/search_products_manager.dart';
import 'package:smooth_app/services/smooth_services.dart';

/// Lazy Counter, with a cached value stored locally, and a call to the server.
Expand Down Expand Up @@ -81,12 +82,13 @@ class LazyCounterUserSearch extends LazyCounter {
);

try {
final SearchResult result = await OpenFoodAPIClient.searchProducts(
final SearchResult result = await SearchProductsManager.searchProducts(
user,
configuration,
uriHelper: ProductQuery.getUriProductHelper(
productType: ProductType.food,
),
type: SearchProductsType.count,
);
return result.count;
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import 'package:smooth_app/pages/product/common/product_refresher.dart';
import 'package:smooth_app/pages/product_list_user_dialog_helper.dart';
import 'package:smooth_app/pages/scan/carousel/scan_carousel_manager.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/query/search_products_manager.dart';
import 'package:smooth_app/resources/app_icons.dart' as icons;
import 'package:smooth_app/themes/theme_provider.dart';
import 'package:smooth_app/widgets/smooth_app_bar.dart';
Expand Down Expand Up @@ -455,19 +456,24 @@ class _ProductListPageState extends State<ProductListPage>
for (final MapEntry<ProductType, List<String>> entry
in productTypes.entries) {
final SearchResult searchResult =
await OpenFoodAPIClient.searchProducts(
await SearchProductsManager.searchProducts(
ProductQuery.getReadUser(),
ProductRefresher().getBarcodeListQueryConfiguration(
entry.value,
language,
),
uriHelper: ProductQuery.getUriProductHelper(productType: entry.key),
type: SearchProductsType.live,
);
final List<Product>? freshProducts = searchResult.products;
if (freshProducts == null) {
fresh = false;
} else {
await DaoProduct(localDatabase).putAll(freshProducts, language);
await DaoProduct(localDatabase).putAll(
freshProducts,
language,
productType: entry.key,
);
localDatabase.upToDate.setLatestDownloadedProducts(freshProducts);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:smooth_app/generic_lib/loading_dialog.dart';
import 'package:smooth_app/generic_lib/widgets/smooth_snackbar.dart';
import 'package:smooth_app/pages/user_management/login_page.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/query/search_products_manager.dart';
import 'package:smooth_app/services/smooth_services.dart';
import 'package:smooth_app/themes/smooth_theme_colors.dart';

Expand Down Expand Up @@ -263,15 +264,21 @@ class ProductRefresher {
) async {
try {
final OpenFoodFactsLanguage language = ProductQuery.getLanguage();
final SearchResult searchResult = await OpenFoodAPIClient.searchProducts(
final SearchResult searchResult =
await SearchProductsManager.searchProducts(
ProductQuery.getReadUser(),
getBarcodeListQueryConfiguration(barcodes, language),
uriHelper: ProductQuery.getUriProductHelper(productType: productType),
type: SearchProductsType.live,
);
if (searchResult.products == null) {
return null;
}
await DaoProduct(localDatabase).putAll(searchResult.products!, language);
await DaoProduct(localDatabase).putAll(
searchResult.products!,
language,
productType: productType,
);
localDatabase.upToDate
.setLatestDownloadedProducts(searchResult.products!);
return searchResult.products!.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,22 +227,26 @@ class _ProductCompareButton extends StatelessWidget {
tags.isNotEmpty &&
tags.length == labels.length) {
categoryTag = product.comparedToCategory;
if (categoryTag == null || blackListedCategories.contains(categoryTag)) {
if (categoryTag != null) {
for (int i = 0; i < tags.length; i++) {
if (categoryTag == tags[i]) {
categoryLabel = labels[i];
break;
}
}
}
if (categoryLabel == null ||
blackListedCategories.contains(categoryTag)) {
// fallback algorithm
int index = tags.length - 1;
// cf. https://github.com/openfoodfacts/openfoodfacts-dart/pull/474
// looking for the most detailed non blacklisted category
categoryTag = tags[index];
categoryLabel = labels[index];
while (blackListedCategories.contains(categoryTag) && index > 0) {
index--;
categoryTag = tags[index];
}
}
if (categoryTag != null) {
for (int i = 0; i < tags.length; i++) {
if (categoryTag == tags[i]) {
categoryLabel = labels[i];
}
categoryLabel = labels[index];
}
}
}
Expand Down Expand Up @@ -329,6 +333,7 @@ class _ProductShareButton extends StatelessWidget {
}

Future<void> _shareProduct(BuildContext context, Product product) async {
final ProductType productType = product.productType ?? ProductType.food;
AnalyticsHelper.trackEvent(
AnalyticsEvent.shareProduct,
barcode: product.barcode,
Expand All @@ -337,10 +342,10 @@ class _ProductShareButton extends StatelessWidget {
// We need to provide a sharePositionOrigin to make the plugin work on ipad
final RenderBox? box = context.findRenderObject() as RenderBox?;
final String url = 'https://'
'${ProductQuery.getCountry().offTag}.openfoodfacts.org'
'${ProductQuery.getCountry().offTag}.${productType.getDomain()}.org'
'/product/${product.barcode}';
Share.share(
appLocalizations.share_product_text(url),
productType.getShareProductLabel(appLocalizations, url),
sharePositionOrigin:
box == null ? null : box.localToGlobal(Offset.zero) & box.size,
);
Expand Down
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/query/paged_product_query.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/query/product_query.dart';
import 'package:smooth_app/query/search_products_manager.dart';

/// Paged product query (with [pageSize] and [pageNumber]).
abstract class PagedProductQuery {
Expand Down Expand Up @@ -39,10 +40,11 @@ abstract class PagedProductQuery {
void toTopPage() => _pageNumber = _startPageNumber;

Future<SearchResult> getSearchResult() async =>
OpenFoodAPIClient.searchProducts(
SearchProductsManager.searchProducts(
ProductQuery.getReadUser(),
getQueryConfiguration(),
uriHelper: ProductQuery.getUriProductHelper(productType: productType),
type: SearchProductsType.live,
);

AbstractQueryConfiguration getQueryConfiguration();
Expand Down
19 changes: 19 additions & 0 deletions packages/smooth_app/lib/query/product_query.dart
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,23 @@ extension ProductTypeExtension on ProductType {
ProductType.product =>
appLocalizations.hey_incomplete_product_message_product,
};

String getShareProductLabel(
final AppLocalizations appLocalizations,
final String url,
) =>
switch (this) {
ProductType.food => appLocalizations.share_product_text(
url,
),
ProductType.beauty => appLocalizations.share_product_text_beauty(
url,
),
ProductType.petFood => appLocalizations.share_product_text_pet_food(
url,
),
ProductType.product => appLocalizations.share_product_text_product(
url,
),
};
}
Loading

0 comments on commit 4d48671

Please sign in to comment.