add thread support

Fixes #129
This commit is contained in:
Henry Hiles 2026-11-05 19:14:46 -05:00
commit 7f7bdbf525
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
5 changed files with 1477 additions and 577 deletions

View file

@ -26,6 +26,7 @@ extension BuildEventOptions on Event {
required String roomId,
required String userId,
required void Function(int, RelationType) onRelation,
void Function(String threadRoot)? onStartThread,
}) {
final theme = Theme.of(context);
final danger = theme.colorScheme.error;
@ -34,11 +35,15 @@ extension BuildEventOptions on Event {
final notifier = RoomChatController.provider((
roomId: roomId,
contextualEvent: null,
threadRoot: null,
)).notifier;
final isPinned = ref
.read(PinnedIdsController.provider(roomId))
.contains(eventId);
final canSend = ref.read(
PowerLevelController.provider(.new(eventType: .message, roomId: roomId)),
);
void showReasonSheet({
required String title,
@ -170,11 +175,7 @@ extension BuildEventOptions on Event {
),
),
if (ref.read(
PowerLevelController.provider(
.new(eventType: .message, roomId: roomId),
),
))
if (canSend)
PopupMenuItem(
onTap: () => onRelation(rowId, .reply),
child: const ListTile(
@ -183,6 +184,18 @@ extension BuildEventOptions on Event {
),
),
if (onStartThread != null &&
threadCount == 0 &&
canSend &&
relatesTo == null)
PopupMenuItem(
onTap: () => onStartThread(eventId),
child: const ListTile(
leading: Icon(Icons.forum),
title: Text("Start Thread"),
),
),
if (sender == userId &&
switch (content) {
LocationMessageContent() ||

View file

@ -32,7 +32,7 @@ extension OpenLinkInfo on WidgetRef {
: await read(
EventController.provider(
.new(roomId: summary.roomId, eventId: linkInfo.eventId!),
).selectAsync((data) => data?.rowId),
).future,
).onError((_, _) => null);
if (await focusRoom(summary.roomId, eventRowId) || !context.mounted) return;

View file

@ -0,0 +1,50 @@
import "package:collection/collection.dart";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:nexus/helpers/extensions/event_id_is_fake.dart";
import "package:nexus/models/event.dart";
import "package:nexus/models/room.dart";
typedef ThreadInfo = ({int count, Event? latest});
typedef Thread = ({Event root, ThreadInfo info});
extension ThreadEvents on Room {
/// The threads in the loaded events, most recently active first.
IList<Thread> get threads {
DateTime lastActive(Thread thread) =>
thread.info.latest?.timestamp ?? thread.root.timestamp;
return events.values
.where((event) => event.threadCount > 0)
.map((root) => (root: root, info: thread(root)))
.sorted((a, b) => lastActive(b).compareTo(lastActive(a)))
.toIList();
}
/// The events in the thread from [threadRoot], oldest first, without the
/// root itself.
IList<Event> threadEvents(String threadRoot) =>
(relations[threadRoot] ?? const .empty())
.map((rowId) => events[rowId])
.nonNulls
.where(
(event) =>
event.threadRoot == threadRoot &&
(!event.eventId.isFake || clientSticky.contains(event.rowId)),
)
.sorted((a, b) => a.timestamp.compareTo(b.timestamp))
.toIList();
/// How many replies the thread from [root] has, and its latest event, from
/// gomuks' aggregate on it
ThreadInfo thread(Event root) =>
(count: root.threadCount, latest: events[root.threadLatestRowId]);
/// The latest event in the thread from [threadRoot], if it's loaded.
Event? latestThreadEvent(String threadRoot) => switch (eventIds[threadRoot]) {
final rowId? => switch (events[rowId]) {
final root? => thread(root).latest,
null => null,
},
null => null,
};
}

View file

@ -0,0 +1,40 @@
import "package:flutter/services.dart";
import "package:flutter_hooks/flutter_hooks.dart";
import "package:material_ui/material_ui.dart";
import "package:nexus/models/relation_type.dart";
final class ChatComposer({
required final ValueNotifier<int?> relatedEvent,
required final ValueNotifier<RelationType> relationType,
required final FocusNode node,
}) {
factory use() {
final relatedEvent = useState<int?>(null);
final relationType = useState(RelationType.reply);
final node = useFocusNode(
onKeyEvent: (_, event) {
if (event is KeyDownEvent && event.logicalKey == .escape) {
relatedEvent.value = null;
return .handled;
}
return .ignored;
},
);
return .new(
relatedEvent: relatedEvent,
relationType: relationType,
node: node,
);
}
int? get repliedEventRowId =>
relationType.value == .reply ? relatedEvent.value : null;
void relate(int rowId, RelationType type) {
relatedEvent.value = rowId;
relationType.value = type;
node.requestFocus();
}
}

View file

@ -15,6 +15,8 @@ final class ChatScroll({
required final GlobalKey centerKey,
required final ScrollController scrollController,
required final bool atBottom,
required final int? highlightedEvent,
required final JumpToEvent jumpToEvent,
required final Future<void> Function() jumpToBottom,
required final GlobalKey Function(int eventRowId) keyFor,
@ -28,6 +30,8 @@ final class ChatScroll({
final anchorId = useState<int?>(null);
final atBottom = useState(true);
final pendingJump = useState<int?>(null);
final highlightedEvent = useState<int?>(null);
final hookContext = useContext();
final pendingJumpDone = useRef<Completer<void>?>(null);
void finishPendingJump() {
pendingJumpDone.value?.complete();
@ -95,15 +99,14 @@ final class ChatScroll({
final anchorIndex = items.indexOf(anchor);
if (anchorIndex == -1) return (history: const .empty(), live: items);
final historyLength = anchorIndex == -1 ? items.length : anchorIndex + 1;
return (
history: items.take(anchorIndex).toIList().reversed.toIList(),
live: items.skip(anchorIndex).toIList(),
history: items.take(historyLength).toIList().reversed.toIList(),
live: items.skip(historyLength).toIList(),
);
}, [controllerData, anchorId.value]);
final lastRowId = split.live.lastOrNull;
final lastRowId = controllerData.value?.timeline.lastOrNull;
final previousLastRowId = useRef<int?>(null);
useEffect(() {
@ -118,7 +121,8 @@ final class ChatScroll({
WidgetsBinding.instance.addPostFrameCallback((_) {
if (!scrollController.hasClients) return;
scrollController.jumpTo(scrollController.position.minScrollExtent);
anchorId.value = lastRowId;
scrollController.position.correctPixels(0);
});
return null;
@ -173,37 +177,49 @@ final class ChatScroll({
],
);
Future<void> scrollToEvent(int rowId) async {
if (!scrollController.hasClients) return;
final context = keyFor(rowId).currentContext;
if (context != null) {
await Scrollable.ensureVisible(
context,
alignment: 0.5,
duration: Durations.extralong1,
curve: Curves.easeInOut,
);
return;
}
finishPendingJump();
final done = pendingJumpDone.value = Completer();
pendingJump.value = rowId;
if (controllerData.value?.timeline.contains(rowId) ?? false) {
anchorId.value = rowId;
} else {
anchorId.value = null;
contextualEvent.value = rowId;
}
await done.future;
}
return .new(
historyRows: split.history,
liveRows: split.live,
centerKey: centerKey,
scrollController: scrollController,
atBottom: atBottom.value,
highlightedEvent: highlightedEvent.value,
keyFor: keyFor,
jumpToEvent: (int rowId) async {
if (!scrollController.hasClients) return;
highlightedEvent.value = rowId;
final context = keyFor(rowId).currentContext;
if (context != null) {
await Scrollable.ensureVisible(
context,
alignment: 0.5,
duration: Durations.extralong1,
curve: Curves.easeInOut,
);
return;
}
finishPendingJump();
final done = pendingJumpDone.value = Completer();
pendingJump.value = rowId;
if (controllerData.value?.timeline.contains(rowId) ?? false) {
anchorId.value = rowId;
} else {
anchorId.value = null;
contextualEvent.value = rowId;
}
await done.future;
await scrollToEvent(rowId);
await Future.delayed(Durations.extralong1, () {
if (hookContext.mounted && highlightedEvent.value == rowId) {
highlightedEvent.value = null;
}
});
},
jumpToBottom: () async {
finishPendingJump();