Skip to content

Commit

Permalink
fix: change provider to class based
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-kreios committed Dec 31, 2024
1 parent 256685d commit d69cf0a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
87 changes: 41 additions & 46 deletions lib/app/features/chat/providers/user_chat_relays_provider.c.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<UserChatRelaysEntity?> userChatRelays(
Ref ref,
String pubkey,
) async {
final entity = ref.watch(
nostrCacheProvider.select<UserChatRelaysEntity?>(
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<UserChatRelaysEntity?> build(String pubkey) async {
final entity = ref.watch(
nostrCacheProvider.select<UserChatRelaysEntity?>(
cacheSelector(UserChatRelaysEntity.cacheKeyBuilder(pubkey: pubkey)),
),
);
if (entity != null) {
return entity;
}

return ref
.watch(nostrNotifierProvider.notifier)
.requestEntity<UserChatRelaysEntity>(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<void> 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<UserChatRelaysEntity>(
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<void> 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();
}
}
6 changes: 5 additions & 1 deletion lib/app/router/main_tabs/main_tab_navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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(
Expand Down

0 comments on commit d69cf0a

Please sign in to comment.