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
6 changed files with 1477 additions and 577 deletions

View file

@ -2,6 +2,7 @@ import "package:collection/collection.dart";
import "package:fast_immutable_collections/fast_immutable_collections.dart";
import "package:freezed_annotation/freezed_annotation.dart";
import "package:nexus/models/bot_command.dart";
import "package:nexus/models/composer_params.dart";
import "package:nexus/models/content/bot_command.dart";
import "package:nexus/models/content/bot_command_description.dart";
@ -10,7 +11,7 @@ part "bot_command_draft.freezed.dart";
@Freezed(toJson: false, fromJson: false)
class const BotCommandDraft({
required final BotCommand command,
required final String roomId,
required final ComposerParams composer,
final IMap<String, String> texts = const .empty(),
// The servers to join rooms picked from suggestions through, by room ID
@ -33,8 +34,9 @@ class const BotCommandDraft({
BotParameter? get focusedParameter => command.description.parameters
.firstWhereOrNull((parameter) => parameter.key == focused);
({Object? value, String? error}) parse(BotParameter parameter) =>
parameter.schema.parseText(texts[parameter.key] ?? "", roomId: roomId);
({Object? value, String? error}) parse(BotParameter parameter) => parameter
.schema
.parseText(texts[parameter.key] ?? "", roomId: composer.roomId);
IMap<String, Object> get arguments => .fromEntries(
shown

View file

@ -0,0 +1 @@
typedef ComposerParams = ({String roomId, String? threadRoot});

View file

@ -29,6 +29,8 @@ class const Event({
final String? sendError,
final IMap<String, int> reactions = const .empty(),
@JsonKey(name: "last_edit_rowid") final int lastEditRowId = 0,
final int threadCount = 0,
@JsonKey(name: "thread_latest_rowid") final int threadLatestRowId = 0,
final UnreadType? unreadType,
final Profile? pmp,
required final Content content,
@ -36,6 +38,8 @@ class const Event({
}) with _$Event {
Map<String, Object?> toJson() => _$EventToJson(this);
String? get threadRoot => relationType == "m.thread" ? relatesTo : null;
static String typeJsonFromJson(Map<dynamic, dynamic> json, _) =>
json["decrypted_type"] ?? json["type"];
@ -47,7 +51,15 @@ class const Event({
static String? replyToFromJson(Map<dynamic, dynamic> json) {
try {
return json["m.relates_to"]?["m.in_reply_to"]?["event_id"];
final relatesTo = json["m.relates_to"];
// Thread fallbacks are only for clients without thread support
if (relatesTo?["rel_type"] == "m.thread" &&
relatesTo?["is_falling_back"] == true) {
return null;
}
return relatesTo?["m.in_reply_to"]?["event_id"];
} catch (_) {
return null;
}

View file

@ -0,0 +1,17 @@
import "package:freezed_annotation/freezed_annotation.dart";
part "get_threads.freezed.dart";
part "get_threads.g.dart";
@Freezed(toJson: false, fromJson: false)
@JsonSerializable()
class const GetThreadsRequest({
required final String roomId,
final String? since,
final int limit = 20,
}) with _$GetThreadsRequest {
Map<String, Object?> toJson() => _$GetThreadsRequestToJson(this);
factory GetThreadsRequest.fromJson(Map<String, Object?> json) =>
_$GetThreadsRequestFromJson(json);
}

View file

@ -14,7 +14,7 @@ class const SendEventRequest({
// Merged into the content by toJson, as gomuks doesn't take these
@JsonKey(includeToJson: false) final String? relatesTo,
@JsonKey(includeToJson: false) final String? relationType,
@JsonKey(includeToJson: false) final String? replyTo,
@JsonKey(includeToJson: false) final Relation? reply,
@JsonKey(includeToJson: false) final Mentions? mentions,
final bool synchronous = false,
final bool disableEncryption = false,
@ -27,14 +27,14 @@ class const SendEventRequest({
"content": {
...content,
"m.mentions": ?mentions?.toJson(),
if (relatesTo != null || replyTo != null)
if (relatesTo != null || reply != null)
"m.relates_to": {
...((content["m.relates_to"] as Map<String, dynamic>?) ?? {}),
if (relatesTo != null) ...{
"event_id": relatesTo,
"rel_type": relationType,
},
if (replyTo != null) "m.in_reply_to": {"event_id": replyTo},
...?reply?.toJson(),
},
},
};

View file

@ -38,11 +38,18 @@ class const Mentions({
class const Relation({
required final String eventId,
required final RelationType relationType,
final String? threadRoot,
final bool isFallingBack = false,
}) with _$Relation {
Map<String, dynamic> toJson() {
switch (relationType) {
case RelationType.reply:
return {
if (threadRoot != null) ...{
"rel_type": "m.thread",
"event_id": threadRoot,
"is_falling_back": isFallingBack,
},
"m.in_reply_to": {"event_id": eventId},
};