Add Supa account and search changes

This commit is contained in:
Fabian Freund
2026-05-22 18:10:22 +02:00
parent 3a19865b2e
commit 51289f1266
374 changed files with 54061 additions and 5013 deletions
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:weblibre/features/account/domain/repositories/account_auth.dart';
import 'package:weblibre/features/search_credits/data/models/search_credits_status.dart';
part 'search_credits_repository.g.dart';
@Riverpod(keepAlive: true)
class SearchCreditsRepository extends _$SearchCreditsRepository {
@override
Future<SearchCreditsStatus> build() async {
final authState = ref.watch(accountAuthRepositoryProvider).value;
if (authState == null || !authState.isSignedIn) {
// Signed-out is not an error; it's a known zero-credit state.
return SearchCreditsStatus.empty;
}
// Don't swallow RPC failures as `SearchCreditsStatus.empty` — that
// renders identically to a real zero balance and would prompt the
// user to "buy more" when the actual fix is to retry. Let errors
// propagate so the AsyncValue carries them and consumers can show an
// error state with a retry CTA.
final client = authState.client!;
final response = await client.rpc('get_my_search_balance').single();
return SearchCreditsStatus.fromJson(response);
}
Future<void> refresh() async {
ref.invalidateSelf();
// Swallow the propagated error — `refresh()` is fire-and-forget from
// pull-to-refresh / lifecycle hooks. The new state (success or error)
// is what consumers re-render from via `ref.watch`.
try {
await future;
} catch (_) {
// intentionally ignored — see comment above
}
}
}
@@ -0,0 +1,58 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'search_credits_repository.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
@ProviderFor(SearchCreditsRepository)
final searchCreditsRepositoryProvider = SearchCreditsRepositoryProvider._();
final class SearchCreditsRepositoryProvider
extends
$AsyncNotifierProvider<SearchCreditsRepository, SearchCreditsStatus> {
SearchCreditsRepositoryProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'searchCreditsRepositoryProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$searchCreditsRepositoryHash();
@$internal
@override
SearchCreditsRepository create() => SearchCreditsRepository();
}
String _$searchCreditsRepositoryHash() =>
r'e351ac000fa2da31e513f5a9817ac81897199df5';
abstract class _$SearchCreditsRepository
extends $AsyncNotifier<SearchCreditsStatus> {
FutureOr<SearchCreditsStatus> build();
@$mustCallSuper
@override
void runBuild() {
final ref =
this.ref as $Ref<AsyncValue<SearchCreditsStatus>, SearchCreditsStatus>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<AsyncValue<SearchCreditsStatus>, SearchCreditsStatus>,
AsyncValue<SearchCreditsStatus>,
Object?,
Object?
>;
element.handleCreate(ref, build);
}
}
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:weblibre/features/search_credits/data/token_stash.dart';
import 'package:weblibre/features/user/data/providers.dart';
part 'search_token_stash_repository.g.dart';
/// Read-only-shaped stash for the search session: only reserve / commit /
/// release / count / take / clear are safe to call. The issuance controller
/// constructs its own stash with the freshly-fetched `key.version` before
/// calling `stash.add` so token rows are tagged with the exact key the
/// issuer signed against — see [searchTokenIssuanceControllerProvider].
@Riverpod(keepAlive: true)
DriftTokenStash searchTokenStash(Ref ref) {
final db = ref.watch(userDatabaseProvider);
return DriftTokenStash(db.searchTokensDao);
}
@Riverpod(keepAlive: true)
Stream<int> searchTokenStashCount(Ref ref) {
final db = ref.watch(userDatabaseProvider);
return db.searchTokensDao.watchCount();
}
@@ -0,0 +1,102 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'search_token_stash_repository.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
/// Read-only-shaped stash for the search session: only reserve / commit /
/// release / count / take / clear are safe to call. The issuance controller
/// constructs its own stash with the freshly-fetched `key.version` before
/// calling `stash.add` so token rows are tagged with the exact key the
/// issuer signed against — see [searchTokenIssuanceControllerProvider].
@ProviderFor(searchTokenStash)
final searchTokenStashProvider = SearchTokenStashProvider._();
/// Read-only-shaped stash for the search session: only reserve / commit /
/// release / count / take / clear are safe to call. The issuance controller
/// constructs its own stash with the freshly-fetched `key.version` before
/// calling `stash.add` so token rows are tagged with the exact key the
/// issuer signed against — see [searchTokenIssuanceControllerProvider].
final class SearchTokenStashProvider
extends
$FunctionalProvider<DriftTokenStash, DriftTokenStash, DriftTokenStash>
with $Provider<DriftTokenStash> {
/// Read-only-shaped stash for the search session: only reserve / commit /
/// release / count / take / clear are safe to call. The issuance controller
/// constructs its own stash with the freshly-fetched `key.version` before
/// calling `stash.add` so token rows are tagged with the exact key the
/// issuer signed against — see [searchTokenIssuanceControllerProvider].
SearchTokenStashProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'searchTokenStashProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$searchTokenStashHash();
@$internal
@override
$ProviderElement<DriftTokenStash> $createElement($ProviderPointer pointer) =>
$ProviderElement(pointer);
@override
DriftTokenStash create(Ref ref) {
return searchTokenStash(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(DriftTokenStash value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<DriftTokenStash>(value),
);
}
}
String _$searchTokenStashHash() => r'e761b254b778f7c4cc7475e0034aa7bec8e126de';
@ProviderFor(searchTokenStashCount)
final searchTokenStashCountProvider = SearchTokenStashCountProvider._();
final class SearchTokenStashCountProvider
extends $FunctionalProvider<AsyncValue<int>, int, Stream<int>>
with $FutureModifier<int>, $StreamProvider<int> {
SearchTokenStashCountProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'searchTokenStashCountProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$searchTokenStashCountHash();
@$internal
@override
$StreamProviderElement<int> $createElement($ProviderPointer pointer) =>
$StreamProviderElement(pointer);
@override
Stream<int> create(Ref ref) {
return searchTokenStashCount(ref);
}
}
String _$searchTokenStashCountHash() =>
r'0cef046aad0dc7e0a6e0f717fe1896f8f546d852';
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:riverpod/experimental/persist.dart';
import 'package:riverpod_annotation/experimental/json_persist.dart';
import 'package:riverpod_annotation/experimental/persist.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:search_backend/search_backend.dart';
import 'package:weblibre/features/search_credits/data/models/web_search_settings.dart';
import 'package:weblibre/features/user/data/providers.dart';
part 'web_search_settings.g.dart';
@Riverpod(keepAlive: true)
@JsonPersist()
class WebSearchSettingsController extends _$WebSearchSettingsController {
void setRouteThroughTor(bool value) {
state = state.copyWith(routeThroughTor: value);
}
void setSearchMode(SearchMode mode) {
state = state.copyWith(searchMode: mode);
}
void setLanguage(String? language) {
state = state.copyWith(language: language);
}
void setRegion(String? region) {
state = state.copyWith(region: region);
}
void setSafeSearch(SafeSearch? safeSearch) {
state = state.copyWith(safeSearch: safeSearch);
}
void setTimeRange(TimeRange? timeRange) {
state = state.copyWith(timeRange: timeRange);
}
@override
WebSearchSettings build() {
persist(
ref.watch(riverpodDatabaseStorageProvider),
key: 'WebSearchSettings',
);
return stateOrNull ?? WebSearchSettings.withDefaults();
}
}
@@ -0,0 +1,106 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'web_search_settings.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
@ProviderFor(WebSearchSettingsController)
@JsonPersist()
final webSearchSettingsControllerProvider =
WebSearchSettingsControllerProvider._();
@JsonPersist()
final class WebSearchSettingsControllerProvider
extends $NotifierProvider<WebSearchSettingsController, WebSearchSettings> {
WebSearchSettingsControllerProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'webSearchSettingsControllerProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$webSearchSettingsControllerHash();
@$internal
@override
WebSearchSettingsController create() => WebSearchSettingsController();
/// {@macro riverpod.override_with_value}
Override overrideWithValue(WebSearchSettings value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<WebSearchSettings>(value),
);
}
}
String _$webSearchSettingsControllerHash() =>
r'f21a6203fdb8b21c28b0ddd23f1b48768673138e';
@JsonPersist()
abstract class _$WebSearchSettingsControllerBase
extends $Notifier<WebSearchSettings> {
WebSearchSettings build();
@$mustCallSuper
@override
void runBuild() {
final ref = this.ref as $Ref<WebSearchSettings, WebSearchSettings>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<WebSearchSettings, WebSearchSettings>,
WebSearchSettings,
Object?,
Object?
>;
element.handleCreate(ref, build);
}
}
// **************************************************************************
// JsonGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
abstract class _$WebSearchSettingsController
extends _$WebSearchSettingsControllerBase {
/// The default key used by [persist].
String get key {
const resolvedKey = "WebSearchSettingsController";
return resolvedKey;
}
/// A variant of [persist], for JSON-specific encoding.
///
/// You can override [key] to customize the key used for storage.
PersistResult persist(
FutureOr<Storage<String, String>> storage, {
String? key,
String Function(WebSearchSettings state)? encode,
WebSearchSettings Function(String encoded)? decode,
StorageOptions options = const StorageOptions(),
}) {
return NotifierPersistX(this).persist<String, String>(
storage,
key: key ?? this.key,
encode: encode ?? $jsonCodex.encode,
decode:
decode ??
(encoded) {
final e = $jsonCodex.decode(encoded);
return WebSearchSettings.fromJson(e as Map<String, Object?>);
},
options: options,
);
}
}