forked from Nexus/nexus
parent
47645ea327
commit
7f7bdbf525
1 changed files with 1477 additions and 577 deletions
|
|
@ -7,10 +7,14 @@ import "package:flutter_riverpod/flutter_riverpod.dart";
|
|||
import "package:fluttertagger/fluttertagger.dart";
|
||||
import "package:nexus/controllers/attachment.dart";
|
||||
import "package:nexus/controllers/client.dart";
|
||||
import "package:nexus/controllers/event.dart";
|
||||
import "package:nexus/controllers/rooms.dart";
|
||||
import "package:nexus/controllers/thread_replies.dart";
|
||||
import "package:nexus/helpers/extensions/is_media.dart";
|
||||
import "package:nexus/helpers/extensions/send_failed.dart";
|
||||
import "package:nexus/helpers/extensions/thread_events.dart";
|
||||
import "package:nexus/models/bot_command_draft.dart";
|
||||
import "package:nexus/models/composer_params.dart";
|
||||
import "package:nexus/models/content/bot_command.dart";
|
||||
import "package:nexus/models/content/content.dart";
|
||||
import "package:nexus/models/content/image_pack.dart";
|
||||
|
|
@ -21,6 +25,7 @@ import "package:nexus/models/content/reaction.dart";
|
|||
import "package:nexus/models/content/sticker.dart";
|
||||
import "package:nexus/models/direction.dart";
|
||||
import "package:nexus/models/event.dart";
|
||||
import "package:nexus/models/paginate_manual.dart";
|
||||
import "package:nexus/models/requests/redact_event.dart";
|
||||
import "package:nexus/models/relation_type.dart";
|
||||
import "package:nexus/models/content/message.dart";
|
||||
|
|
@ -28,7 +33,11 @@ import "package:nexus/models/requests/send_message.dart";
|
|||
import "package:nexus/models/room.dart";
|
||||
import "package:nexus/models/room_chat.dart";
|
||||
|
||||
typedef ChatParams = ({String roomId, int? contextualEvent});
|
||||
typedef ChatParams = ({
|
||||
String roomId,
|
||||
int? contextualEvent,
|
||||
String? threadRoot,
|
||||
});
|
||||
|
||||
class RoomChatController(final ChatParams info)
|
||||
extends AsyncNotifier<RoomChat?> {
|
||||
|
|
@ -47,52 +56,173 @@ class RoomChatController(final ChatParams info)
|
|||
ref.read(RoomsController.provider.notifier).addState(info.roomId, state);
|
||||
}
|
||||
|
||||
final timeline = room.timeline
|
||||
.toEntryIList(compare: (a, b) => (a?.key ?? 0).compareTo(b?.key ?? 0))
|
||||
.map((e) => e.value)
|
||||
.toIList()
|
||||
.addAll(room.clientSticky)
|
||||
.nonNulls
|
||||
.toIList();
|
||||
if (info.contextualEvent == null ||
|
||||
timeline.contains(info.contextualEvent)) {
|
||||
ref.watch(
|
||||
RoomsController.provider.select(
|
||||
(rooms) =>
|
||||
(rooms[info.roomId]?.timeline, rooms[info.roomId]?.clientSticky),
|
||||
),
|
||||
if (info.threadRoot case final threadRoot?
|
||||
when info.contextualEvent == null) {
|
||||
final nextBatch = await ref.watch(
|
||||
ThreadRepliesController.provider((
|
||||
roomId: info.roomId,
|
||||
threadRoot: threadRoot,
|
||||
)).future,
|
||||
);
|
||||
|
||||
return .new(
|
||||
timeline: timeline,
|
||||
hasMoreBackward: room.hasMore,
|
||||
timeline: ref.watch(RoomsController.provider.select(threadTimeline)),
|
||||
hasMoreBackward: nextBatch != null,
|
||||
hasMoreForward: false,
|
||||
);
|
||||
} else {
|
||||
final context = await client.getEventContext(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
eventId: room.events[info.contextualEvent!]!.eventId,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
// `before` is in reverse chronological order
|
||||
final events = context.before.reversed
|
||||
if (info.threadRoot == null) {
|
||||
final timeline = room.timeline
|
||||
.toEntryIList(compare: (a, b) => (a?.key ?? 0).compareTo(b?.key ?? 0))
|
||||
.map((e) => e.value)
|
||||
.toIList()
|
||||
.add(context.event)
|
||||
.addAll(context.after);
|
||||
.addAll(room.clientSticky)
|
||||
.nonNulls
|
||||
// Threads are shown from their root instead
|
||||
.where((rowId) => room.events[rowId]?.threadRoot == null)
|
||||
.toIList();
|
||||
|
||||
if (info.contextualEvent == null ||
|
||||
timeline.contains(info.contextualEvent)) {
|
||||
ref.watch(
|
||||
RoomsController.provider.select(
|
||||
(rooms) => (
|
||||
rooms[info.roomId]?.timeline,
|
||||
rooms[info.roomId]?.clientSticky,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
return .new(
|
||||
timeline: timeline,
|
||||
hasMoreBackward: room.hasMore,
|
||||
hasMoreForward: false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
final context = await client.getEventContext(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
eventId: room.events[info.contextualEvent!]!.eventId,
|
||||
),
|
||||
);
|
||||
|
||||
final contextEvents = context.before.reversed
|
||||
.toIList()
|
||||
.add(context.event)
|
||||
.addAll(context.after);
|
||||
ref
|
||||
.read(RoomsController.provider.notifier)
|
||||
.storeEvents(contextEvents.addAll(context.relatedEvents));
|
||||
|
||||
if (info.threadRoot case final threadRoot?) {
|
||||
final (older, newer) = await (
|
||||
client.paginateManual(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
threadRoot: threadRoot,
|
||||
since: context.start,
|
||||
direction: .backward,
|
||||
),
|
||||
),
|
||||
client.paginateManual(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
threadRoot: threadRoot,
|
||||
since: context.end,
|
||||
direction: .forward,
|
||||
),
|
||||
),
|
||||
).wait;
|
||||
final rootRowId = await threadRootBefore(older);
|
||||
ref
|
||||
.read(RoomsController.provider.notifier)
|
||||
.storeEvents(events.addAll(context.relatedEvents));
|
||||
.storeEvents(
|
||||
older.events
|
||||
.addAll(older.relatedEvents)
|
||||
.addAll(newer.events)
|
||||
.addAll(newer.relatedEvents),
|
||||
);
|
||||
|
||||
return .new(
|
||||
timeline: .new(events.map((element) => element.rowId)),
|
||||
hasMoreBackward: true,
|
||||
hasMoreForward: true,
|
||||
historicalData: .new(start: context.start, end: context.end),
|
||||
timeline: .new([?rootRowId])
|
||||
.addAll(
|
||||
// Paginating backwards gives the newest first
|
||||
older.events.reversed
|
||||
.addAll(
|
||||
contextEvents.where(
|
||||
(event) => event.threadRoot == threadRoot,
|
||||
),
|
||||
)
|
||||
.addAll(newer.events)
|
||||
.map((event) => event.rowId),
|
||||
)
|
||||
.removeDuplicates(),
|
||||
hasMoreBackward: older.nextBatch != null,
|
||||
hasMoreForward: newer.nextBatch != null,
|
||||
historicalData: .new(
|
||||
start: older.nextBatch ?? "",
|
||||
end: newer.nextBatch ?? "",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return .new(
|
||||
timeline: .new(
|
||||
contextEvents
|
||||
.where((event) => event.threadRoot == null)
|
||||
.map((event) => event.rowId),
|
||||
),
|
||||
hasMoreBackward: true,
|
||||
hasMoreForward: true,
|
||||
historicalData: .new(start: context.start, end: context.end),
|
||||
);
|
||||
}
|
||||
|
||||
/// The thread's root, which comes before all of its events, if paginating
|
||||
/// backwards to [older] reached the start of the thread.
|
||||
Future<int?> threadRootBefore(PaginateManual older) async =>
|
||||
switch (info.threadRoot) {
|
||||
final threadRoot? when older.nextBatch == null => await ref.read(
|
||||
EventController.provider(
|
||||
.new(roomId: info.roomId, eventId: threadRoot),
|
||||
).future,
|
||||
),
|
||||
_ => null,
|
||||
};
|
||||
|
||||
/// The thread's root, then its events, if this is a thread's chat.
|
||||
IList<int> threadTimeline(IMap<String, Room> rooms) =>
|
||||
switch ((rooms[info.roomId], info.threadRoot)) {
|
||||
(final room?, final threadRoot?) => .new([
|
||||
?room.eventIds[threadRoot],
|
||||
...room.threadEvents(threadRoot).map((event) => event.rowId),
|
||||
]),
|
||||
_ => const .empty(),
|
||||
};
|
||||
|
||||
Relation? replyRelation(Event? replied) =>
|
||||
switch (info.threadRoot ?? replied?.threadRoot) {
|
||||
null =>
|
||||
replied == null
|
||||
? null
|
||||
: .new(eventId: replied.eventId, relationType: .reply),
|
||||
final threadRoot => .new(
|
||||
eventId:
|
||||
replied?.eventId ??
|
||||
ref
|
||||
.read(RoomsController.provider)[info.roomId]
|
||||
?.latestThreadEvent(threadRoot)
|
||||
?.eventId ??
|
||||
threadRoot,
|
||||
relationType: .reply,
|
||||
threadRoot: threadRoot,
|
||||
isFallingBack: replied == null,
|
||||
),
|
||||
};
|
||||
|
||||
Future<void> deleteMessage(Event event, {String? reason}) => ref
|
||||
.read(ClientController.provider.notifier)
|
||||
.redactEvent(
|
||||
|
|
@ -133,97 +263,128 @@ class RoomChatController(final ChatParams info)
|
|||
return;
|
||||
}
|
||||
|
||||
if ((info.threadRoot, chat) case (final threadRoot?, final chat?)
|
||||
when chat.historicalData == null) {
|
||||
return ref
|
||||
.read(
|
||||
ThreadRepliesController.provider((
|
||||
roomId: info.roomId,
|
||||
threadRoot: threadRoot,
|
||||
)).notifier,
|
||||
)
|
||||
.loadOlder();
|
||||
}
|
||||
|
||||
state = .loading();
|
||||
|
||||
final client = ref.read(ClientController.provider.notifier);
|
||||
|
||||
if (chat?.historicalData == null) {
|
||||
final timelineKeys = ref
|
||||
.read(RoomsController.provider.select((value) => value[info.roomId]))
|
||||
?.timeline
|
||||
.keys;
|
||||
final response = await client.paginate(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
maxTimelineId: timelineKeys?.isNotEmpty == true
|
||||
? timelineKeys?.reduce(min)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
try {
|
||||
if (chat?.historicalData == null) {
|
||||
final timelineKeys = ref
|
||||
.read(
|
||||
RoomsController.provider.select((value) => value[info.roomId]),
|
||||
)
|
||||
?.timeline
|
||||
.keys;
|
||||
final response = await client.paginate(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
maxTimelineId: timelineKeys?.isNotEmpty == true
|
||||
? timelineKeys?.reduce(min)
|
||||
: null,
|
||||
),
|
||||
);
|
||||
|
||||
if (response.events.isEmpty) {
|
||||
state = .data(chat);
|
||||
}
|
||||
if (response.events.isEmpty) {
|
||||
state = .data(chat);
|
||||
}
|
||||
|
||||
ref
|
||||
.read(RoomsController.provider.notifier)
|
||||
.update(
|
||||
IMap({
|
||||
info.roomId: Room(
|
||||
events: IMap.fromIterable(
|
||||
response.events.addAll(response.relatedEvents),
|
||||
keyMapper: (event) => event.rowId,
|
||||
valueMapper: (event) => event,
|
||||
),
|
||||
hasMore: response.hasMore,
|
||||
receipts: response.receipts,
|
||||
timeline: IMap.fromIterable(
|
||||
response.events,
|
||||
keyMapper: (event) => event.timelineRowId,
|
||||
valueMapper: (event) => event.rowId,
|
||||
ref
|
||||
.read(RoomsController.provider.notifier)
|
||||
.update(
|
||||
.new({
|
||||
info.roomId: .new(
|
||||
// ignore: unused_result - I am using it you donkey
|
||||
events: .fromIterable(
|
||||
response.events.addAll(response.relatedEvents),
|
||||
keyMapper: (event) => event.rowId,
|
||||
valueMapper: (event) => event,
|
||||
),
|
||||
hasMore: response.hasMore,
|
||||
receipts: response.receipts,
|
||||
// ignore: unused_result - I am using it you donkey
|
||||
timeline: .fromIterable(
|
||||
response.events,
|
||||
keyMapper: (event) => event.timelineRowId,
|
||||
valueMapper: (event) => event.rowId,
|
||||
),
|
||||
),
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
final paginationResponse = await client.paginateManual(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
threadRoot: info.threadRoot,
|
||||
direction: direction,
|
||||
since: direction == .forward
|
||||
? chat!.historicalData!.end
|
||||
: chat!.historicalData!.start,
|
||||
),
|
||||
);
|
||||
|
||||
ref
|
||||
.read(RoomsController.provider.notifier)
|
||||
.storeEvents(
|
||||
paginationResponse.events.addAll(
|
||||
paginationResponse.relatedEvents,
|
||||
),
|
||||
}),
|
||||
);
|
||||
} else {
|
||||
final paginationResponse = await client.paginateManual(
|
||||
.new(
|
||||
roomId: info.roomId,
|
||||
direction: direction,
|
||||
since: direction == .forward
|
||||
? chat!.historicalData!.end
|
||||
: chat!.historicalData!.start,
|
||||
),
|
||||
);
|
||||
);
|
||||
|
||||
ref
|
||||
.read(RoomsController.provider.notifier)
|
||||
.storeEvents(
|
||||
paginationResponse.events.addAll(paginationResponse.relatedEvents),
|
||||
);
|
||||
|
||||
final eventRowIds =
|
||||
(direction == .forward
|
||||
final rootRowId = direction == .backward
|
||||
? await threadRootBefore(paginationResponse)
|
||||
: null;
|
||||
final eventRowIds = [
|
||||
?rootRowId,
|
||||
...(direction == .forward
|
||||
? paginationResponse.events
|
||||
: paginationResponse.events.reversed)
|
||||
.map((element) => element.rowId)
|
||||
.toIList();
|
||||
.where((event) => event.threadRoot == info.threadRoot)
|
||||
.map((element) => element.rowId),
|
||||
].lock;
|
||||
|
||||
state = .data(
|
||||
.new(
|
||||
timeline: direction == .forward
|
||||
? chat.timeline.addAll(eventRowIds)
|
||||
: eventRowIds.addAll(chat.timeline),
|
||||
hasMoreForward:
|
||||
direction == .forward && paginationResponse.nextBatch == null
|
||||
? false
|
||||
: chat.hasMoreForward,
|
||||
hasMoreBackward:
|
||||
direction == .backward && paginationResponse.nextBatch == null
|
||||
? false
|
||||
: chat.hasMoreBackward,
|
||||
historicalData: chat.historicalData?.copyWith(
|
||||
start:
|
||||
(direction == .backward
|
||||
? paginationResponse.nextBatch
|
||||
: null) ??
|
||||
chat.historicalData!.start,
|
||||
end:
|
||||
(direction == .forward ? paginationResponse.nextBatch : null) ??
|
||||
chat.historicalData!.end,
|
||||
state = .data(
|
||||
.new(
|
||||
timeline: direction == .forward
|
||||
? chat.timeline.addAll(eventRowIds)
|
||||
: eventRowIds.addAll(chat.timeline),
|
||||
hasMoreForward:
|
||||
direction == .forward && paginationResponse.nextBatch == null
|
||||
? false
|
||||
: chat.hasMoreForward,
|
||||
hasMoreBackward:
|
||||
direction == .backward && paginationResponse.nextBatch == null
|
||||
? false
|
||||
: chat.hasMoreBackward,
|
||||
historicalData: chat.historicalData?.copyWith(
|
||||
start:
|
||||
(direction == .backward
|
||||
? paginationResponse.nextBatch
|
||||
: null) ??
|
||||
chat.historicalData!.start,
|
||||
end:
|
||||
(direction == .forward
|
||||
? paginationResponse.nextBatch
|
||||
: null) ??
|
||||
chat.historicalData!.end,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
);
|
||||
}
|
||||
} catch (_) {
|
||||
state = .data(chat);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -239,7 +400,11 @@ class RoomChatController(final ChatParams info)
|
|||
? null
|
||||
: events?[relatedEventRowId];
|
||||
|
||||
final attachmentProvider = AttachmentController.provider(info.roomId);
|
||||
final ComposerParams composer = (
|
||||
roomId: info.roomId,
|
||||
threadRoot: info.threadRoot,
|
||||
);
|
||||
final attachmentProvider = AttachmentController.provider(composer);
|
||||
final attachment = ref.read(attachmentProvider)?.$2;
|
||||
ref.invalidate(attachmentProvider);
|
||||
|
||||
|
|
@ -275,9 +440,9 @@ class RoomChatController(final ChatParams info)
|
|||
room: taggedMessage.contains("@room"),
|
||||
),
|
||||
text: taggedMessage,
|
||||
relation: relatedEvent == null
|
||||
? null
|
||||
: .new(eventId: relatedEvent.eventId, relationType: relationType),
|
||||
relation: relationType == .edit && relatedEvent != null
|
||||
? .new(eventId: relatedEvent.eventId, relationType: .edit)
|
||||
: replyRelation(relatedEvent),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
@ -384,7 +549,7 @@ class RoomChatController(final ChatParams info)
|
|||
if (shouldMention && repliedEvent != null) repliedEvent.sender,
|
||||
].toIList(),
|
||||
),
|
||||
replyTo: repliedEvent?.eventId,
|
||||
reply: replyRelation(repliedEvent),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
@ -451,9 +616,7 @@ class RoomChatController(final ChatParams info)
|
|||
command: draft.input,
|
||||
),
|
||||
mentions: .new(userIds: [draft.command.sender].lock),
|
||||
relation: repliedEvent == null
|
||||
? null
|
||||
: .new(eventId: repliedEvent.eventId, relationType: .reply),
|
||||
relation: replyRelation(repliedEvent),
|
||||
),
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue