implemented content blocking

This commit is contained in:
Fabian Freund
2024-06-19 21:08:18 +02:00
parent 2f6ae2684d
commit 2bacbbffd2
18 changed files with 1301 additions and 0 deletions
@@ -0,0 +1,24 @@
import 'package:bang_navigator/features/content_block/data/models/host.dart';
import 'package:bang_navigator/features/content_block/domain/repositories/host.dart';
import 'package:bang_navigator/features/content_block/domain/repositories/sync.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'providers.g.dart';
@Riverpod()
Stream<DateTime?> lastSyncOfSource(
LastSyncOfSourceRef ref,
HostSource source,
) {
final repository = ref.watch(hostSyncRepositoryProvider.notifier);
return repository.watchLastSyncOfSource(source);
}
@Riverpod()
Stream<int> hostCountOfSource(
HostCountOfSourceRef ref,
HostSource source,
) {
final repository = ref.watch(hostRepositoryProvider.notifier);
return repository.watchHostCount(source);
}
@@ -0,0 +1,287 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'providers.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$lastSyncOfSourceHash() => r'0f96dc6c5ffc44654734bd4be187fd0c4d34a310';
/// Copied from Dart SDK
class _SystemHash {
_SystemHash._();
static int combine(int hash, int value) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + value);
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x0007ffff & hash) << 10));
return hash ^ (hash >> 6);
}
static int finish(int hash) {
// ignore: parameter_assignments
hash = 0x1fffffff & (hash + ((0x03ffffff & hash) << 3));
// ignore: parameter_assignments
hash = hash ^ (hash >> 11);
return 0x1fffffff & (hash + ((0x00003fff & hash) << 15));
}
}
/// See also [lastSyncOfSource].
@ProviderFor(lastSyncOfSource)
const lastSyncOfSourceProvider = LastSyncOfSourceFamily();
/// See also [lastSyncOfSource].
class LastSyncOfSourceFamily extends Family<AsyncValue<DateTime?>> {
/// See also [lastSyncOfSource].
const LastSyncOfSourceFamily();
/// See also [lastSyncOfSource].
LastSyncOfSourceProvider call(
HostSource source,
) {
return LastSyncOfSourceProvider(
source,
);
}
@override
LastSyncOfSourceProvider getProviderOverride(
covariant LastSyncOfSourceProvider provider,
) {
return call(
provider.source,
);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'lastSyncOfSourceProvider';
}
/// See also [lastSyncOfSource].
class LastSyncOfSourceProvider extends AutoDisposeStreamProvider<DateTime?> {
/// See also [lastSyncOfSource].
LastSyncOfSourceProvider(
HostSource source,
) : this._internal(
(ref) => lastSyncOfSource(
ref as LastSyncOfSourceRef,
source,
),
from: lastSyncOfSourceProvider,
name: r'lastSyncOfSourceProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$lastSyncOfSourceHash,
dependencies: LastSyncOfSourceFamily._dependencies,
allTransitiveDependencies:
LastSyncOfSourceFamily._allTransitiveDependencies,
source: source,
);
LastSyncOfSourceProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.source,
}) : super.internal();
final HostSource source;
@override
Override overrideWith(
Stream<DateTime?> Function(LastSyncOfSourceRef provider) create,
) {
return ProviderOverride(
origin: this,
override: LastSyncOfSourceProvider._internal(
(ref) => create(ref as LastSyncOfSourceRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
source: source,
),
);
}
@override
AutoDisposeStreamProviderElement<DateTime?> createElement() {
return _LastSyncOfSourceProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is LastSyncOfSourceProvider && other.source == source;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, source.hashCode);
return _SystemHash.finish(hash);
}
}
mixin LastSyncOfSourceRef on AutoDisposeStreamProviderRef<DateTime?> {
/// The parameter `source` of this provider.
HostSource get source;
}
class _LastSyncOfSourceProviderElement
extends AutoDisposeStreamProviderElement<DateTime?>
with LastSyncOfSourceRef {
_LastSyncOfSourceProviderElement(super.provider);
@override
HostSource get source => (origin as LastSyncOfSourceProvider).source;
}
String _$hostCountOfSourceHash() => r'3bd118037e5762fd5134838ac12ed6d0d2283e76';
/// See also [hostCountOfSource].
@ProviderFor(hostCountOfSource)
const hostCountOfSourceProvider = HostCountOfSourceFamily();
/// See also [hostCountOfSource].
class HostCountOfSourceFamily extends Family<AsyncValue<int>> {
/// See also [hostCountOfSource].
const HostCountOfSourceFamily();
/// See also [hostCountOfSource].
HostCountOfSourceProvider call(
HostSource source,
) {
return HostCountOfSourceProvider(
source,
);
}
@override
HostCountOfSourceProvider getProviderOverride(
covariant HostCountOfSourceProvider provider,
) {
return call(
provider.source,
);
}
static const Iterable<ProviderOrFamily>? _dependencies = null;
@override
Iterable<ProviderOrFamily>? get dependencies => _dependencies;
static const Iterable<ProviderOrFamily>? _allTransitiveDependencies = null;
@override
Iterable<ProviderOrFamily>? get allTransitiveDependencies =>
_allTransitiveDependencies;
@override
String? get name => r'hostCountOfSourceProvider';
}
/// See also [hostCountOfSource].
class HostCountOfSourceProvider extends AutoDisposeStreamProvider<int> {
/// See also [hostCountOfSource].
HostCountOfSourceProvider(
HostSource source,
) : this._internal(
(ref) => hostCountOfSource(
ref as HostCountOfSourceRef,
source,
),
from: hostCountOfSourceProvider,
name: r'hostCountOfSourceProvider',
debugGetCreateSourceHash:
const bool.fromEnvironment('dart.vm.product')
? null
: _$hostCountOfSourceHash,
dependencies: HostCountOfSourceFamily._dependencies,
allTransitiveDependencies:
HostCountOfSourceFamily._allTransitiveDependencies,
source: source,
);
HostCountOfSourceProvider._internal(
super._createNotifier, {
required super.name,
required super.dependencies,
required super.allTransitiveDependencies,
required super.debugGetCreateSourceHash,
required super.from,
required this.source,
}) : super.internal();
final HostSource source;
@override
Override overrideWith(
Stream<int> Function(HostCountOfSourceRef provider) create,
) {
return ProviderOverride(
origin: this,
override: HostCountOfSourceProvider._internal(
(ref) => create(ref as HostCountOfSourceRef),
from: from,
name: null,
dependencies: null,
allTransitiveDependencies: null,
debugGetCreateSourceHash: null,
source: source,
),
);
}
@override
AutoDisposeStreamProviderElement<int> createElement() {
return _HostCountOfSourceProviderElement(this);
}
@override
bool operator ==(Object other) {
return other is HostCountOfSourceProvider && other.source == source;
}
@override
int get hashCode {
var hash = _SystemHash.combine(0, runtimeType.hashCode);
hash = _SystemHash.combine(hash, source.hashCode);
return _SystemHash.finish(hash);
}
}
mixin HostCountOfSourceRef on AutoDisposeStreamProviderRef<int> {
/// The parameter `source` of this provider.
HostSource get source;
}
class _HostCountOfSourceProviderElement
extends AutoDisposeStreamProviderElement<int> with HostCountOfSourceRef {
_HostCountOfSourceProviderElement(super.provider);
@override
HostSource get source => (origin as HostCountOfSourceProvider).source;
}
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
@@ -0,0 +1,29 @@
import 'package:bang_navigator/features/content_block/data/database/database.dart';
import 'package:bang_navigator/features/content_block/data/models/host.dart';
import 'package:bang_navigator/features/content_block/data/providers.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'host.g.dart';
@Riverpod(keepAlive: true)
class HostRepository extends _$HostRepository {
late HostDatabase _db;
@override
void build() {
_db = ref.watch(hostDatabaseProvider);
}
Stream<List<String>> watchHosts({Iterable<HostSource>? sources}) {
return _db.hostDao
.getHostList(sources: sources)
.map(
(hostData) => hostData.hostname,
)
.watch();
}
Stream<int> watchHostCount(HostSource source) {
return _db.hostDao.getHostCount(sources: [source]).watchSingle();
}
}
@@ -0,0 +1,25 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'host.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$hostRepositoryHash() => r'b28362146b75391f55fb353fc6fde72dc1d2fcb9';
/// See also [HostRepository].
@ProviderFor(HostRepository)
final hostRepositoryProvider = NotifierProvider<HostRepository, void>.internal(
HostRepository.new,
name: r'hostRepositoryProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$hostRepositoryHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$HostRepository = Notifier<void>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member
@@ -0,0 +1,105 @@
import 'package:bang_navigator/features/content_block/data/database/database.dart';
import 'package:bang_navigator/features/content_block/data/models/host.dart';
import 'package:bang_navigator/features/content_block/data/providers.dart';
import 'package:bang_navigator/features/content_block/data/services/source.dart';
import 'package:drift/isolate.dart';
import 'package:exceptions/exceptions.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'sync.g.dart';
@Riverpod(keepAlive: true)
class HostSyncRepository extends _$HostSyncRepository {
late HostDatabase _db;
HostSyncRepository();
static Future<Result<void>> _fetchAndSync({
required HostSourceService sourceService,
required HostDatabase db,
required Uri url,
required HostSource source,
required Duration? syncInterval,
}) async {
if (syncInterval != null) {
final lastSync =
await db.syncDao.lastSyncOfSource(source).getSingleOrNull();
if (lastSync != null &&
lastSync.difference(DateTime.now()) < syncInterval) {
return Result.success(null);
}
}
final result = await sourceService.getHosts(url);
return result.flatMapAsync(
(remoteHosts) async {
await db.syncDao.syncHosts(
source: source,
remoteHosts: remoteHosts,
syncTime: DateTime.now(),
);
},
);
}
Future<Result<void>> syncHostSource(
HostSource source,
Duration? syncInterval,
) async {
try {
return Result.success(
await _db.computeWithDatabase(
connect: HostDatabase.new,
computation: (db) async {
final ref = ProviderContainer();
final result = await _fetchAndSync(
sourceService: ref.read(hostSourceServiceProvider.notifier),
db: db,
url: Uri.parse(source.url),
source: source,
syncInterval: syncInterval,
);
//Throw if necessary
return result.value;
},
),
);
} catch (e) {
return Result.failure(
ErrorMessage(
message: "Failed to sync Hosts (${source.name})",
source: 'HostSync',
details: e,
),
);
}
}
Stream<DateTime?> watchLastSyncOfSource(HostSource source) {
return _db.syncDao.lastSyncOfSource(source).watchSingleOrNull();
}
Future<Map<HostSource, Result<void>>> syncHostSources({
Set<HostSource>? sources,
Duration? syncInterval,
}) async {
//Default to all sources
sources ??= HostSource.values.toSet();
//Run isolated operations
final futures = sources.map(
(source) => syncHostSource(source, syncInterval)
.then((result) => MapEntry(source, result)),
);
return Map.fromEntries(await Future.wait(futures));
}
@override
void build() {
_db = ref.watch(hostDatabaseProvider);
}
}
@@ -0,0 +1,27 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'sync.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
String _$hostSyncRepositoryHash() =>
r'22b89d3687c415441f99caa4d77dabaeb4969534';
/// See also [HostSyncRepository].
@ProviderFor(HostSyncRepository)
final hostSyncRepositoryProvider =
NotifierProvider<HostSyncRepository, void>.internal(
HostSyncRepository.new,
name: r'hostSyncRepositoryProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$hostSyncRepositoryHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef _$HostSyncRepository = Notifier<void>;
// ignore_for_file: type=lint
// ignore_for_file: subtype_of_sealed_class, invalid_use_of_internal_member, invalid_use_of_visible_for_testing_member