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

View file

@ -1,4 +1,5 @@
import "package:nexus/models/content/message.dart"; import "package:nexus/models/content/message.dart";
import "package:nexus/helpers/extensions/is_media.dart";
extension GetFilename on MessageContent { extension GetFilename on MessageContent {
String? get filename => switch (this) { String? get filename => switch (this) {
@ -8,4 +9,16 @@ extension GetFilename on MessageContent {
FileMessageContent(:final filename) => filename, FileMessageContent(:final filename) => filename,
_ => null, _ => 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, maxLines: maxLines,
overflow: maxLines == null ? null : .ellipsis, overflow: maxLines == null ? null : .ellipsis,
) )
// Media without a caption has its filename as its body
: content.caption == null
? SizedBox.shrink()
: format == .html && formattedBody != null : format == .html && formattedBody != null
? Html( ? Html(
roomId: event.roomId, roomId: event.roomId,
@ -120,7 +123,7 @@ class const MessageRenderer(
formattedBody.linkifiedHtml, formattedBody.linkifiedHtml,
) )
: LinkifiedText( : LinkifiedText(
body.isEmpty ? content.filename ?? "" : body, body,
roomId: event.roomId, roomId: event.roomId,
style: textStyle, style: textStyle,
maxLines: maxLines, maxLines: maxLines,
@ -136,7 +139,7 @@ class const MessageRenderer(
if (event.lastEditRowId != 0) if (event.lastEditRowId != 0)
Text("(edited)", style: theme.textTheme.labelSmall), Text("(edited)", style: theme.textTheme.labelSmall),
if (linkify(body) if (linkify(content.caption ?? "")
.firstWhereOrNull((element) => element is UrlElement) .firstWhereOrNull((element) => element is UrlElement)
case final UrlElement link?) case final UrlElement link?)
if (Uri.tryParse(link.url) case final Uri url?) UrlPreview(url), 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, encrypted: file != null,
filename: filename ?? body, filename: filename ?? body,
), ),
FileMessageContent(:final url, :final info, :final filename, :final file) => FileMessageContent(
FileCard(url, info, filename: filename, encrypted: file != null), :final url,
:final info,
:final file,
:final filename,
:final body,
) =>
FileCard(url, info, filename: filename ?? body, encrypted: file != null),
ImageMessageContent( ImageMessageContent(
:final url, :final url,
:final info, :final info,