|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:flutter_hooks/flutter_hooks.dart'; |
| 4 | +import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 5 | + |
| 6 | +import '../../shared/channels/saved_message_store.dart'; |
| 7 | +import '../../shared/profile/user_cache_provider.dart'; |
| 8 | +import '../../shared/profile/verified_profile_owner.dart'; |
| 9 | +import '../../shared/relay/relay.dart'; |
| 10 | +import '../../shared/relay/relay_author_provider.dart'; |
| 11 | +import 'timeline_message.dart'; |
| 12 | + |
| 13 | +/// The signed evidence [formatTimeline] judges cross-signer edits with: |
| 14 | +/// verified owners by author, and the relay's trusted signing key. |
| 15 | +typedef EditAuthority = ({Map<String, String> owners, String? relayKey}); |
| 16 | + |
| 17 | +/// Watches the evidence the cross-signer edits in [events] need, and asks |
| 18 | +/// the user cache for the profiles that supply it. Call from a hook build. |
| 19 | +/// |
| 20 | +/// The profiles are asked for on each connect while the edits show, so a |
| 21 | +/// request that failed is made again. The cache skips authors it already |
| 22 | +/// has a current answer for, with or without an attestation. |
| 23 | +/// |
| 24 | +/// Nothing is watched for a timeline without such edits, so the relay is not |
| 25 | +/// asked for its key and unrelated profile changes do not reformat it. |
| 26 | +EditAuthority useEditAuthority(WidgetRef ref, List<NostrEvent> events) { |
| 27 | + final needs = useMemoized(() => editAuthorityNeeds(events), [events]); |
| 28 | + final authors = needs.owners.join(','); |
| 29 | + final connected = |
| 30 | + authors.isNotEmpty && |
| 31 | + ref.watch( |
| 32 | + relaySessionProvider.select( |
| 33 | + (session) => session.status == SessionStatus.connected, |
| 34 | + ), |
| 35 | + ); |
| 36 | + useEffect(() { |
| 37 | + if (connected) { |
| 38 | + ref.read(userCacheProvider.notifier).requestLinked(needs.owners); |
| 39 | + } |
| 40 | + return null; |
| 41 | + }, [authors, connected]); |
| 42 | + return ( |
| 43 | + owners: authors.isEmpty |
| 44 | + ? const <String, String>{} |
| 45 | + : ref.watch(_editOwnersProvider(authors)), |
| 46 | + relayKey: needs.relayKey ? ref.watch(editRelayKeyProvider) : null, |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +/// [useEditAuthority] for a notifier that formats once: reads the evidence |
| 51 | +/// held now, requests missing profiles without waiting for them, and starts |
| 52 | +/// no relay key lookup. |
| 53 | +EditAuthority readEditAuthority(Ref ref, List<NostrEvent> events) { |
| 54 | + final needs = editAuthorityNeeds(events); |
| 55 | + if (needs.owners.isNotEmpty) { |
| 56 | + unawaited(ref.read(userCacheProvider.notifier).preload(needs.owners)); |
| 57 | + } |
| 58 | + final profiles = ref.read(userCacheProvider); |
| 59 | + return ( |
| 60 | + owners: { |
| 61 | + for (final author in needs.owners) |
| 62 | + if (profiles[author]?.ownerEvidence case final evidence?) |
| 63 | + author: ?verifiedEvidenceOwner(evidence, author), |
| 64 | + }, |
| 65 | + relayKey: needs.relayKey |
| 66 | + ? ref.read(_learnedRelayKeyProvider).value ?? |
| 67 | + ref.read(savedRelayAuthorProvider).value |
| 68 | + : null, |
| 69 | + ); |
| 70 | +} |
| 71 | + |
| 72 | +/// Checks ownership for the Edit and Delete actions. |
| 73 | +/// Returns true if [viewer] is [author], or if [author]'s signed profile |
| 74 | +/// verifies [viewer] as its owner. |
| 75 | +/// If a restored profile names [viewer] as owner and lacks a signed copy, |
| 76 | +/// requests the profile again and returns false. |
| 77 | +/// Call when actions open so the check reads the latest cached profile. |
| 78 | +bool readCanManageMessage(WidgetRef ref, String author, String? viewer) { |
| 79 | + final key = author.toLowerCase(); |
| 80 | + final me = viewer?.toLowerCase(); |
| 81 | + if (me == null) return false; |
| 82 | + if (key == me) return true; |
| 83 | + final profile = ref.read(userCacheProvider)[key]; |
| 84 | + final evidence = profile?.ownerEvidence; |
| 85 | + if (evidence == null) { |
| 86 | + // A restored profile without its signed copy names this viewer: fetch |
| 87 | + // the profile again so a later menu can verify the owner. |
| 88 | + if (profile?.ownerPubkey == me) { |
| 89 | + ref.read(userCacheProvider.notifier).get(key); |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + return verifiedEvidenceOwner(evidence, key) == me; |
| 94 | +} |
| 95 | + |
| 96 | +/// Verified owners for comma-joined [authors]. Rebuilt only when one of |
| 97 | +/// their verified owners changes, so a timeline reformats only then. |
| 98 | +final _editOwnersProvider = Provider.autoDispose |
| 99 | + .family<Map<String, String>, String>( |
| 100 | + (ref, authors) => { |
| 101 | + for (final author in authors.split(',')) |
| 102 | + author: ?ref.watch(verifiedProfileOwnerProvider(author)), |
| 103 | + }, |
| 104 | + ); |
| 105 | + |
| 106 | +/// The relay key last read live for this identity and relay. |
| 107 | +class _LearnedRelayKey { |
| 108 | + String? value; |
| 109 | +} |
| 110 | + |
| 111 | +/// One per identity and relay, so a key never outlives its community. |
| 112 | +final _learnedRelayKeyProvider = Provider<_LearnedRelayKey>((ref) { |
| 113 | + ref.watch( |
| 114 | + relayConfigProvider.select((config) => (config.baseUrl, config.nsec)), |
| 115 | + ); |
| 116 | + return _LearnedRelayKey(); |
| 117 | +}); |
| 118 | + |
| 119 | +/// The relay signing key trusted for edit attribution. |
| 120 | +/// |
| 121 | +/// While connected, asks the relay (NIP-11); a key it returns replaces any |
| 122 | +/// earlier one at once, as the saved-message store does on rotation. While |
| 123 | +/// offline, reconnecting or reloading, keeps the key last read for this |
| 124 | +/// identity and relay, else the key the saved-message store holds. Watch it |
| 125 | +/// only when an edit needs it: watching while connected costs a request. |
| 126 | +final editRelayKeyProvider = Provider.autoDispose<String?>((ref) { |
| 127 | + final learned = ref.watch(_learnedRelayKeyProvider); |
| 128 | + final saved = ref.watch(savedRelayAuthorProvider).value; |
| 129 | + final connected = ref.watch( |
| 130 | + relaySessionProvider.select( |
| 131 | + (session) => session.status == SessionStatus.connected, |
| 132 | + ), |
| 133 | + ); |
| 134 | + if (connected) { |
| 135 | + final relay = ref.watch( |
| 136 | + relayConfigProvider.select((config) => config.baseUrl), |
| 137 | + ); |
| 138 | + final live = ref.watch(relayAuthorProvider(relay)).value; |
| 139 | + if (live != null) learned.value = live; |
| 140 | + } |
| 141 | + return learned.value ?? saved; |
| 142 | +}); |
0 commit comments