spec compliance around filenames

This commit is contained in:
Henry Hiles 2026-12-04 16:48:09 -05:00
commit 3cfe6a3681
Signed by: Henry-Hiles
SSH key fingerprint: SHA256:VKQUdS31Q90KvX7EkKMHMBpUspcmItAh86a+v7PGiIs
4 changed files with 31 additions and 10 deletions

View file

@ -43,11 +43,10 @@ class ComposerDraftController(final ComposerParams composer)
final (:commands, :text) = TextCommand.parse(
event?.localContent?.editSource ??
switch (content) {
TextMessageContent(:final body) ||
ImageMessageContent(:final body) ||
VideoMessageContent(:final body) ||
AudioMessageContent(:final body) ||
FileMessageContent(:final body) => body,
TextMessageContent(:final body) => body,
// Media's body is its filename unless it has a caption
final MessageContent content when content.isMedia =>
content.caption ?? "",
_ => "",
},
);
@ -75,7 +74,7 @@ class ComposerDraftController(final ComposerParams composer)
)
.set(
content is MessageContent && content.isMedia
? (content.filename ?? content.body ?? "Attachment", content)
? (content.displayFilename ?? "Attachment", content)
: null,
);
_botCommand.clear();

View file

@ -1,4 +1,5 @@
import "package:nexus/models/content/message.dart";
import "package:nexus/helpers/extensions/is_media.dart";
extension GetFilename on MessageContent {
String? get filename => switch (this) {
@ -8,4 +9,16 @@ extension GetFilename on MessageContent {
FileMessageContent(:final filename) => filename,
_ => null,
};
/// The file's name. Per the spec, the body is the filename if there's no
/// separate filename.
String? get displayFilename => isMedia ? filename ?? body : null;
/// The caption of media, or the body of other messages. Per the spec, the
/// body of media is only a caption if there's a different filename.
String? get caption => switch (filename) {
_ when !isMedia => body,
final filename? when filename != body => body,
_ => null,
};
}

View file

@ -113,6 +113,9 @@ class const MessageRenderer(
maxLines: maxLines,
overflow: maxLines == null ? null : .ellipsis,
)
// Media without a caption has its filename as its body
: content.caption == null
? SizedBox.shrink()
: format == .html && formattedBody != null
? Html(
roomId: event.roomId,
@ -120,7 +123,7 @@ class const MessageRenderer(
formattedBody.linkifiedHtml,
)
: LinkifiedText(
body.isEmpty ? content.filename ?? "" : body,
body,
roomId: event.roomId,
style: textStyle,
maxLines: maxLines,
@ -136,7 +139,7 @@ class const MessageRenderer(
if (event.lastEditRowId != 0)
Text("(edited)", style: theme.textTheme.labelSmall),
if (linkify(body)
if (linkify(content.caption ?? "")
.firstWhereOrNull((element) => element is UrlElement)
case final UrlElement link?)
if (Uri.tryParse(link.url) case final Uri url?) UrlPreview(url),

View file

@ -35,8 +35,14 @@ final class const MessageMedia(final MessageContent content, {super.key})
encrypted: file != null,
filename: filename ?? body,
),
FileMessageContent(:final url, :final info, :final filename, :final file) =>
FileCard(url, info, filename: filename, encrypted: file != null),
FileMessageContent(
:final url,
:final info,
:final file,
:final filename,
:final body,
) =>
FileCard(url, info, filename: filename ?? body, encrypted: file != null),
ImageMessageContent(
:final url,
:final info,