Skip to content

Commit

Permalink
feat: create user chat relay manager class to sync chat relays with u…
Browse files Browse the repository at this point in the history
…ser relays
  • Loading branch information
ice-kreios committed Dec 31, 2024
1 parent d69cf0a commit 8ee4c98
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
53 changes: 27 additions & 26 deletions lib/app/features/chat/providers/user_chat_relays_provider.c.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// 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 @@ -13,37 +14,38 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'user_chat_relays_provider.c.g.dart';

///
/// This provider is used to fetch the user chat relays for a given pubkey.
/// It is different from the [UserRelays] provider because it is used to fetch the user chat relays
///
@riverpod
class UserChatRelays extends _$UserChatRelays {
@override
Future<UserChatRelaysEntity?> build(String pubkey) async {
final entity = ref.watch(
nostrCacheProvider.select<UserChatRelaysEntity?>(
cacheSelector(UserChatRelaysEntity.cacheKeyBuilder(pubkey: pubkey)),
),
Future<UserChatRelaysEntity?> userChatRelays(Ref ref, String pubkey) async {
final cached = ref.watch(
nostrCacheProvider.select<UserChatRelaysEntity?>(
cacheSelector(UserChatRelaysEntity.cacheKeyBuilder(pubkey: pubkey)),
),
);
if (cached != null) return cached;

final requestMessage = RequestMessage()
..addFilter(
RequestFilter(kinds: const [UserChatRelaysEntity.kind], authors: [pubkey]),
);
if (entity != null) {
return entity;
}

final requestMessage = RequestMessage()
..addFilter(
RequestFilter(kinds: const [UserChatRelaysEntity.kind], authors: [pubkey]),
return ref.watch(nostrNotifierProvider.notifier).requestEntity<UserChatRelaysEntity>(
requestMessage,
actionSource: ActionSourceUser(pubkey),
);
}

return ref.watch(nostrNotifierProvider.notifier).requestEntity<UserChatRelaysEntity>(
requestMessage,
actionSource: ActionSourceUser(pubkey),
);
}
@riverpod
class UserChatRelaysManager extends _$UserChatRelaysManager {
@override
FutureOr<void> build() {}

Future<void> setCurrentUserChatRelays() async {
///
/// Fetches user relays and sets them as chat relays if they differ
/// If chat relays already match user relays, does nothing
/// Signs and broadcasts new chat relay list if an update is needed
///
Future<void> set() async {
final pubkey = ref.watch(currentPubkeySelectorProvider);

if (pubkey == null) {
throw UserMasterPubkeyNotFoundException();
}
Expand All @@ -52,7 +54,6 @@ class UserChatRelays extends _$UserChatRelays {
final relayUrls = userRelays.first.data.list.map((e) => e.url).toList();

final userChatRelays = await ref.watch(userChatRelaysProvider(pubkey).future);

if (userChatRelays != null) {
final chatRelays = userChatRelays.data.list.map((e) => e.url).toList();
if (chatRelays.toSet().containsAll(relayUrls) && relayUrls.toSet().containsAll(chatRelays)) {
Expand All @@ -67,6 +68,6 @@ class UserChatRelays extends _$UserChatRelays {
final chatRelaysEvent = await ref.read(nostrNotifierProvider.notifier).sign(chatRelays);

await ref.read(nostrNotifierProvider.notifier).sendEvents([chatRelaysEvent]);
ref.invalidateSelf();
ref.invalidate(userChatRelaysProvider(pubkey));
}
}
2 changes: 1 addition & 1 deletion lib/app/router/main_tabs/main_tab_navigation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MainTabNavigation extends HookConsumerWidget {
useOnInit(() {
final pubkey = ref.watch(currentPubkeySelectorProvider);
if (pubkey != null) {
ref.read(userChatRelaysProvider(pubkey).notifier).setCurrentUserChatRelays();
ref.read(userChatRelaysManagerProvider.notifier).set();
}
});

Expand Down

0 comments on commit 8ee4c98

Please sign in to comment.