Files
buzz/mobile/lib/features/channels/send_message_provider.dart
klopez4212 8cc7e94392 Refine mobile Messages sections, Duo navigation, and chat typography (#9885)
## Summary

- Organize mobile Messages into configurable flat sections. See All
keeps the list and its search position when opening a conversation, and
supports side-by-side browsing on an unfolded Duo.
- Use the same 16-point Messages gutter in booklet and open Duo layouts.
The regular iPhone uses 20 points. Align navigation actions and use
native inline titles on See All.
- Update mobile typography and spacing across chat rows, message
bubbles, drafts, and the composer. Render code proportionally to the
17-point message body.

## Device review

Compare the leading edges of the section headings and channel icons in
Messages on the open Duo and in booklet mode. Open See All and check
that its rows follow the same column.

---------

Signed-off-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
Signed-off-by: kenny lopez <klopez4212@gmail.com>
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
2026-11-23 13:14:05 -08:00

308 lines
11 KiB
Dart

import 'dart:async';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import '../../shared/mentions/mention_tags.dart';
import '../../shared/relay/relay.dart';
import '../channels/channel_management_provider.dart';
import '../../shared/profile/user_cache_provider.dart';
import '../../shared/profile/user_profile.dart';
import 'channel.dart';
import 'composer_send_origin.dart';
import 'me/me_messages_provider.dart';
import 'channel_messages_provider.dart';
import 'message_mention_pubkeys.dart';
import 'local_message_send_animation_provider.dart';
/// Sends messages by signing an event with the user's nsec and publishing it
/// over the relay's NIP-42-authenticated WebSocket session.
class SendMessage {
final SignedEventRelay _signedEventRelay;
final Future<List<ChannelMember>> Function(String channelId) _fetchMembers;
final Map<String, UserProfile> Function() _readUserCache;
final void Function(String channelId, NostrEvent event) _addLocalMessage;
final void Function(String channelId, String eventId)
_markLocalMessageForAnimation;
final void Function(String channelId, String eventId) _completeLocalMessage;
final void Function(String channelId, String eventId) _removeLocalMessage;
final bool Function()? _isDeliveryValid;
SendMessage({
required SignedEventRelay signedEventRelay,
required Future<List<ChannelMember>> Function(String channelId)
fetchMembers,
required Map<String, UserProfile> Function() readUserCache,
required void Function(String channelId, NostrEvent event) addLocalMessage,
void Function(String channelId, String eventId)?
markLocalMessageForAnimation,
required void Function(String channelId, String eventId)
completeLocalMessage,
required void Function(String channelId, String eventId) removeLocalMessage,
bool Function()? isDeliveryValid,
}) : _signedEventRelay = signedEventRelay,
_fetchMembers = fetchMembers,
_readUserCache = readUserCache,
_addLocalMessage = addLocalMessage,
_markLocalMessageForAnimation =
markLocalMessageForAnimation ?? ((_, _) {}),
_completeLocalMessage = completeLocalMessage,
_removeLocalMessage = removeLocalMessage,
_isDeliveryValid = isDeliveryValid;
/// Send a text message to a channel.
///
/// For thread replies, pass [parentEventId] and optionally [rootEventId].
/// If [rootEventId] is null it defaults to [parentEventId] (direct reply to
/// thread head). Tags are built to match the desktop's `buildReplyTags`
/// convention with `root` / `reply` markers. Pass [mediaTags] to append
/// relay-validated `imeta` tags and NIP-30 `emoji` tags. [mayPublish] is
/// asked after every wait, once the event is signed and shown locally and
/// right before it is published; when it says no, nothing is published,
/// the local message is removed and this throws.
Future<void> call({
required String channelId,
required String content,
String? parentEventId,
String? rootEventId,
List<String>? mentionPubkeys,
Channel? channel,
List<List<String>> mediaTags = const [],
FutureOr<void> Function(NostrEvent)? beforePublish,
void Function()? validateDelivery,
Future<bool> Function()? mayPublish,
}) async {
_ensureDeliveryValid();
// Use explicitly passed pubkeys, or resolve @mentions against
// channel members to avoid matching the wrong user.
final explicitMentions =
mentionPubkeys ?? await _resolveMentions(content, channelId);
final authorPubkey = _signedEventRelay.pubkey;
final dmRecipientPubkeys = channel?.isDm == true
? await _fetchDmRecipientPubkeys(channelId, channel!, authorPubkey)
: null;
final resolvedMentions = dmRecipientPubkeys != null
? messageMentionPubkeys(
channel: channel!,
senderPubkey: authorPubkey,
explicitMentions: explicitMentions,
dmRecipientPubkeys: dmRecipientPubkeys,
)
: explicitMentions;
// Normalize mentions: lowercase, deduplicate, exclude self (matching
// the desktop's normalizeMentionPubkeys).
final selfLower = authorPubkey?.toLowerCase();
final seenMentions = <String>{?selfLower};
final normalizedMentions = <String>[
for (final pk in resolvedMentions)
if (seenMentions.add(pk.toLowerCase())) pk,
];
// Two-field `mention` tags are references; they go through the same
// writer as recipients so a bad key rejects the message.
bool isReference(List<String> tag) =>
tag.length == 2 && tag[0] == 'mention';
final references = [
for (final tag in mediaTags)
if (isReference(tag)) tag[1],
];
final mentionTags = writeMentionTags(
recipients: normalizedMentions,
references: references,
members: normalizedMentions.isEmpty
? const {}
: await _recipientMembers(channelId, dmRecipientPubkeys, selfLower),
);
final tags = <List<String>>[
['h', channelId],
if (parentEventId != null) ..._buildReplyTags(parentEventId, rootEventId),
...mentionTags,
for (final tag in mediaTags)
if (!isReference(tag)) tag,
];
_ensureDeliveryValid();
NostrEvent? localMessage;
try {
await _signedEventRelay.submit(
kind: EventKind.streamMessage,
content: content,
tags: tags,
onSigned: (event) async {
await beforePublish?.call(event);
_ensureDeliveryValid();
validateDelivery?.call();
localMessage = event;
_markLocalMessageForAnimation(channelId, event.id);
_addLocalMessage(channelId, event);
ComposerSendOrigin.didInsertLocalMessage();
},
mayPublish: mayPublish,
);
final event = localMessage;
if (event != null) _completeLocalMessage(channelId, event.id);
} catch (_) {
final event = localMessage;
if (event != null) _removeLocalMessage(channelId, event.id);
rethrow;
}
}
/// Resolve every identity that is actually a current member of this DM.
///
/// Membership is authoritative for delivery. The channel metadata's `p`
/// tags can lag membership changes, so they are only used when the membership
/// snapshot is unavailable.
Future<Set<String>> _fetchDmRecipientPubkeys(
String channelId,
Channel channel,
String? authorPubkey,
) async {
List<ChannelMember>? members;
try {
members = await _fetchMembers(channelId);
} catch (_) {
// Fall back to metadata below so an unavailable membership query does
// not block ordinary DM sends.
}
final author = authorPubkey?.toLowerCase();
return {
for (final participant in dmParticipantPubkeys(channel, members))
if (participant.trim().isNotEmpty &&
participant.toLowerCase() != author)
participant.toLowerCase(),
};
}
/// Keys that can be recipients: DM participants, or the channel's current
/// members. The sender always counts as a member.
Future<Set<String>> _recipientMembers(
String channelId,
Set<String>? dmRecipientPubkeys,
String? self,
) async {
final members =
dmRecipientPubkeys ??
{
for (final member in await _fetchMembers(channelId))
member.pubkey.toLowerCase(),
};
return {...members, ?self};
}
void _ensureDeliveryValid() {
if (_isDeliveryValid?.call() == false) {
throw ActiveCommunityChangedError(
'Message delivery cancelled because the active community changed',
);
}
}
/// Resolve @mentions to pubkeys, scoped to channel members.
///
/// Fetches channel members from the relay and matches @names only
/// against members of that channel. Falls back to the full user cache
/// if the member fetch fails.
Future<List<String>> _resolveMentions(
String content,
String channelId,
) async {
final mentionPattern = RegExp(r'@(\w+)');
final matches = mentionPattern.allMatches(content);
if (matches.isEmpty) return const [];
// Try to get channel member pubkeys for scoped resolution.
Set<String>? memberPubkeys;
try {
final members = await _fetchMembers(channelId);
memberPubkeys = {for (final m in members) m.pubkey.toLowerCase()};
} catch (_) {
// Non-fatal — fall through to unscoped cache lookup.
}
final cache = _readUserCache();
final pubkeys = <String>{};
for (final match in matches) {
final name = match.group(1)?.toLowerCase();
if (name == null || name.isEmpty) continue;
for (final profile in cache.values) {
final displayName = profile.displayName?.toLowerCase();
if (displayName == null) continue;
// Match against full display name or first word.
final firstName = displayName.split(RegExp(r'\s+')).first;
if (displayName != name && firstName != name) continue;
// If we have channel members, only match members of this channel.
if (memberPubkeys != null &&
!memberPubkeys.contains(profile.pubkey.toLowerCase())) {
continue;
}
pubkeys.add(profile.pubkey);
break;
}
}
return pubkeys.toList();
}
/// Build `e`-tags for a thread reply, matching the desktop convention:
/// - Direct reply to thread head: `["e", id, "", "reply"]`
/// - Nested reply: `["e", rootId, "", "root"]` + `["e", parentId, "", "reply"]`
static List<List<String>> _buildReplyTags(
String parentEventId,
String? rootEventId,
) {
final root = rootEventId ?? parentEventId;
if (parentEventId == root) {
return [
['e', root, '', 'reply'],
];
}
return [
['e', root, '', 'root'],
['e', parentEventId, '', 'reply'],
];
}
}
final sendMessageProvider = Provider<SendMessage>((ref) => _sender(ref));
/// Shares signing and delivery with channels, with Me's flat optimistic reader.
final meSendMessageProvider = Provider<SendMessage>(
(ref) => _sender(ref, personal: true),
);
SendMessage _sender(Ref ref, {bool personal = false}) {
final messages = personal ? meMessagesProvider : channelMessagesProvider;
final config = ref.watch(relayConfigProvider);
return SendMessage(
signedEventRelay: SignedEventRelay(
session: ref.read(relaySessionProvider.notifier),
nsec: config.nsec,
),
fetchMembers: (channelId) =>
ref.read(channelMembersProvider(channelId).future),
readUserCache: () => ref.read(userCacheProvider),
addLocalMessage: (channelId, event) =>
ref.read(messages(channelId).notifier).addLocalMessage(event),
markLocalMessageForAnimation: (channelId, eventId) => ref
.read(localMessageSendAnimationProvider(channelId).notifier)
.mark(eventId),
completeLocalMessage: (channelId, eventId) =>
ref.read(messages(channelId).notifier).completeLocalMessage(eventId),
removeLocalMessage: (channelId, eventId) =>
ref.read(messages(channelId).notifier).removeLocalMessage(eventId),
isDeliveryValid: () {
final currentConfig = ref.read(relayConfigProvider);
return currentConfig.baseUrl == config.baseUrl &&
currentConfig.nsec == config.nsec;
},
);
}