/*
* 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 .
*/
import 'package:fast_equatable/fast_equatable.dart';
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:weblibre/data/models/web_page_info.dart';
import 'package:weblibre/domain/services/generic_website.dart';
import 'package:weblibre/extensions/ref_cache.dart';
import 'package:weblibre/extensions/uri.dart';
import 'package:weblibre/features/geckoview/domain/entities/states/tab.dart';
import 'package:weblibre/features/geckoview/domain/providers/tab_state.dart';
import 'package:weblibre/features/geckoview/features/tabs/data/entities/tab_mode.dart';
import 'package:weblibre/features/geckoview/features/tabs/domain/repositories/tab.dart';
import 'package:weblibre/features/proxy/data/proxy_connection.dart';
import 'package:weblibre/features/tor/domain/services/tor_proxy.dart';
import 'package:weblibre/features/user/data/models/proxy_routing_settings.dart';
import 'package:weblibre/features/user/domain/repositories/proxy_routing_settings.dart';
part 'website_title.g.dart';
@Riverpod()
class CompletePageInfo extends _$CompletePageInfo {
@override
AsyncValue build(TabState cached) {
ref.cacheFor(const Duration(minutes: 2));
if (cached.isPageInfoComplete || !cached.url.isHttpOrHttps) {
return AsyncData(cached);
}
ref.listen(
fireImmediately: true,
tabStateProvider(cached.id).select((value) => value?.title),
(previous, next) {
if (next != null) {
final current = stateOrNull?.value ?? cached;
state = AsyncData(current.copyWith.title(next));
}
},
);
ref.listen(
fireImmediately: true,
tabStateProvider(cached.id).select((value) => value?.favicon),
(previous, next) {
if (next != null) {
final current = stateOrNull?.value ?? cached;
state = AsyncData(current.copyWith.favicon(next));
}
},
);
return AsyncData(cached);
}
}
@Riverpod()
Future pageInfo(
Ref ref,
Uri url, {
required bool isImageRequest,
}) async {
final link = ref.cacheFor(const Duration(minutes: 2));
final tabState = ref.read(selectedTabStateProvider);
int? proxyPort;
if (tabState?.id != null) {
final containerData = await ref
.read(tabDataRepositoryProvider.notifier)
.getTabContainerData(tabState!.id);
final proxyRoutingSettings = ref.read(
proxyRoutingSettingsWithDefaultsProvider,
);
if (containerData?.metadata.proxyConnectionId is TorProxyConnectionId ||
(tabState.tabMode is! PrivateTabMode &&
proxyRoutingSettings.regularTabsMode ==
ProxyRegularTabRoutingMode.all &&
proxyRoutingSettings.regularTabsProxyConnectionId
is TorProxyConnectionId) ||
(tabState.tabMode is PrivateTabMode &&
proxyRoutingSettings.privateTabsProxyConnectionId
is TorProxyConnectionId)) {
proxyPort = await ref.read(
torProxyServiceProvider.selectAsync((value) => value.socksPort),
);
if (proxyPort == null) {
throw Exception('Could not proxy request');
}
}
}
final result = await ref
.watch(genericWebsiteServiceProvider.notifier)
.fetchPageInfo(
url: url,
isImageRequest: isImageRequest,
proxyPort: proxyPort,
);
if (!result.isSuccess) {
link.close();
}
return result.value;
}
@Riverpod()
AsyncValue?>> websiteFeedProvider(
Ref ref,
String tabId,
) {
final tabState = ref.watch(tabStateProvider(tabId))!;
final feeds = ref.watch(
pageInfoProvider(
tabState.url,
isImageRequest: false,
).select((value) => value.whenData((data) => EquatableValue(data.feeds))),
);
return feeds;
}