diff --git a/lib/app/features/chat/providers/user_chat_relays_provider.c.dart b/lib/app/features/chat/providers/user_chat_relays_provider.c.dart index def7c8465..06fda0b93 100644 --- a/lib/app/features/chat/providers/user_chat_relays_provider.c.dart +++ b/lib/app/features/chat/providers/user_chat_relays_provider.c.dart @@ -1,6 +1,5 @@ // SPDX-License-Identifier: ice License 1.0 -import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:ion/app/exceptions/exceptions.dart'; import 'package:ion/app/features/auth/providers/auth_provider.c.dart'; import 'package:ion/app/features/nostr/model/action_source.dart'; @@ -19,59 +18,55 @@ part 'user_chat_relays_provider.c.g.dart'; /// It is different from the [UserRelays] provider because it is used to fetch the user chat relays /// @riverpod -Future userChatRelays( - Ref ref, - String pubkey, -) async { - final entity = ref.watch( - nostrCacheProvider.select( - cacheSelector(UserChatRelaysEntity.cacheKeyBuilder(pubkey: pubkey)), - ), - ); - if (entity != null) { - return entity; - } - - final requestMessage = RequestMessage() - ..addFilter( - RequestFilter(kinds: const [UserChatRelaysEntity.kind], authors: [pubkey]), +class UserChatRelays extends _$UserChatRelays { + @override + Future build(String pubkey) async { + final entity = ref.watch( + nostrCacheProvider.select( + cacheSelector(UserChatRelaysEntity.cacheKeyBuilder(pubkey: pubkey)), + ), ); + if (entity != null) { + return entity; + } - return ref - .watch(nostrNotifierProvider.notifier) - .requestEntity(requestMessage, actionSource: ActionSourceUser(pubkey)); -} - -/// -/// This provider is used to set the user chat relays for a given pubkey. -/// If the user chat relays are already set, it will not set them again. -/// If the user's relays have been updated, it will update the chat relays to match. -/// -@riverpod -Future setUserChatRelays(Ref ref) async { - final pubkey = ref.watch(currentPubkeySelectorProvider); + final requestMessage = RequestMessage() + ..addFilter( + RequestFilter(kinds: const [UserChatRelaysEntity.kind], authors: [pubkey]), + ); - if (pubkey == null) { - throw UserMasterPubkeyNotFoundException(); + return ref.watch(nostrNotifierProvider.notifier).requestEntity( + requestMessage, + actionSource: ActionSourceUser(pubkey), + ); } - final userChatRelays = await ref.watch(userChatRelaysProvider(pubkey).future); - final userRelays = await ref.watch(userRelaysManagerProvider.notifier).fetch([pubkey]); - final relayUrls = userRelays.first.data.list.map((e) => e.url).toList(); + Future setCurrentUserChatRelays() async { + final pubkey = ref.watch(currentPubkeySelectorProvider); - if (userChatRelays != null) { - final chatRelays = userChatRelays.data.list.map((e) => e.url).toList(); - if (chatRelays == relayUrls) { - return; + if (pubkey == null) { + throw UserMasterPubkeyNotFoundException(); } - } - final chatRelays = UserChatRelaysData( - list: relayUrls.map((url) => UserRelay(url: url)).toList(), - ); + final userRelays = await ref.watch(userRelaysManagerProvider.notifier).fetch([pubkey]); + final relayUrls = userRelays.first.data.list.map((e) => e.url).toList(); + + final userChatRelays = await ref.watch(userChatRelaysProvider(pubkey).future); - final chatRelaysEvent = await ref.read(nostrNotifierProvider.notifier).sign(chatRelays); + if (userChatRelays != null) { + final chatRelays = userChatRelays.data.list.map((e) => e.url).toList(); + if (chatRelays.toSet().containsAll(relayUrls) && relayUrls.toSet().containsAll(chatRelays)) { + return; + } + } - await ref.read(nostrNotifierProvider.notifier).sendEvents([chatRelaysEvent]); - ref.invalidate(userChatRelaysProvider(pubkey)); + final chatRelays = UserChatRelaysData( + list: relayUrls.map((url) => UserRelay(url: url)).toList(), + ); + + final chatRelaysEvent = await ref.read(nostrNotifierProvider.notifier).sign(chatRelays); + + await ref.read(nostrNotifierProvider.notifier).sendEvents([chatRelaysEvent]); + ref.invalidateSelf(); + } } diff --git a/lib/app/router/main_tabs/main_tab_navigation.dart b/lib/app/router/main_tabs/main_tab_navigation.dart index e4b595ddd..7bf9a4d5a 100644 --- a/lib/app/router/main_tabs/main_tab_navigation.dart +++ b/lib/app/router/main_tabs/main_tab_navigation.dart @@ -6,6 +6,7 @@ import 'package:flutter_hooks/flutter_hooks.dart'; import 'package:go_router/go_router.dart'; import 'package:hooks_riverpod/hooks_riverpod.dart'; import 'package:ion/app/extensions/extensions.dart'; +import 'package:ion/app/features/auth/providers/auth_provider.c.dart'; import 'package:ion/app/features/chat/providers/user_chat_relays_provider.c.dart'; import 'package:ion/app/features/chat/recent_chats/providers/conversations_edit_mode_provider.c.dart'; import 'package:ion/app/features/chat/recent_chats/views/components/conversation_edit_bottom_bar/conversation_edit_bottom_bar.dart'; @@ -28,7 +29,10 @@ class MainTabNavigation extends HookConsumerWidget { final currentTab = TabItem.fromNavigationIndex(shell.currentIndex); useOnInit(() { - ref.read(setUserChatRelaysProvider); + final pubkey = ref.watch(currentPubkeySelectorProvider); + if (pubkey != null) { + ref.read(userChatRelaysProvider(pubkey).notifier).setCurrentUserChatRelays(); + } }); return Scaffold(