Files
buzz/mobile/lib/features/channels/message_gallery.dart
Tom Brow 85c7156085 perf(mobile): stop downloading whole videos for posterless stills (#9367)
Follow-up to [#9356 gallery video
selection](https://github.com/block/buzz/pull/9356), merged as
[ef57fec4d](ef57fec4d2).
It makes two changes that came out of review.

### 1. Remove automatic stills for videos without poster metadata

#9356 generated a still for every rendered video bubble or stack card
that had no NIP-71 poster. To do that it downloaded the **whole video**
(up to the 100 MB limit), grabbed a frame and threw the file away.
Desktop (buzz-app) never writes `image`/`thumb` on video attachments, so
this covered every desktop-sent video, not only old mobile messages.
Before #9356 those bubbles were a static placeholder that used no
network.

The cost on a real timeline:
- A four-card stack could pull up to about 400 MB just for stills,
before anyone pressed play.
- When the user did open a video, playback started its own separate
download of the same file, and the poster download kept competing for
bandwidth.
- A failed, cancelled or stalled download saved nothing, so every
remount (scroll back, swipe back in a stack) started the full GET again.
Cached stills from non-relay URLs were also dropped and fetched again
after the cache's seven-day expiry.

This PR deletes that path: `video_poster.dart` and its test, plus the
watchers and `generatedPoster` branches in `video_preview.dart` and
`media_stack.dart`. Posterless videos are back to base behavior: "Video
attachment" in a bubble, the plain surface on a stack card, and the
source snapshot in the native viewer. The rest of #9356 stays: queued
local stills (`prepareVideoPoster` on the local file, no network), the
remote-poster preview in the native viewer, the picker changes and swipe
dismissal.

`NativeVideoFiles._cancelTimeouts` was added for poster disposal. No
remaining test depends on it, so it goes back to base too.

If stills for desktop-sent videos are still wanted, a bounded follow-up
is to generate and cache the still from the file that playback already
downloaded, after the user opens it. That adds no network.


### 2. Route `.3gpp` and `.3g2` picks as videos

The Video row is gone from the attachment menu, so `_isPickedVideo` is
now the only thing that classifies path-only picker results. Its
extension list missed `3gpp` and `3g2`. The recent-media grid returns
the original library file, and PHPicker exports 3GPP2 as `.3g2`, so
those videos were queued as images and then rejected by `uploadImage`.
Before #9356, the explicit Video action uploaded them through
`uploadVideo`, which transcodes to MP4.

### Pareto

The change only removes work: no extra network or decoding for
posterless videos, and no change for videos that carry a poster. The one
thing we give up is the still on posterless videos that #9356 added and
base never had.

### Validation

- Added test rows: `clip.3gpp` and `clip.3g2` in the existing
mixed-gallery composer journey. Both now queue as video.
- Full mobile `flutter test` passes on
[518af3a0d](518af3a0d2):
4,688 passed, 71 skipped. `flutter analyze` is clean.
- I didn't run an app journey for this fix. It brings the posterless
paths back to base code, and #9356's own validation covers the rest.

Review report:
`buzz://message?channel=e71f9a2a-ac46-473d-a24e-5e16fd8cb7b1&id=bcae52d59c255add1a927e0135441a1c58115b6aa6cda8d9ff7e58d47a627a87`

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Signed-off-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 03:18:29 +00:00

95 lines
2.8 KiB
Dart

import 'package:flutter/foundation.dart';
import 'message_media.dart';
import 'message_content/link_normalizer.dart';
import 'message_content/markdown_escapes.dart';
/// Attachment metadata shared by carousel and stack rendering.
@immutable
class MessageGalleryItem {
final String url;
final String semanticLabel;
final double? aspectRatio;
/// Declared media type, used when selecting the native gallery.
final String? mimeType;
/// Whether this item opens video playback rather than an image decoder.
final bool isVideo;
/// Optional video poster; absent posters use a static video placeholder.
final String? posterUrl;
const MessageGalleryItem({
required this.url,
required this.semanticLabel,
required this.aspectRatio,
this.mimeType,
this.isVideo = false,
this.posterUrl,
});
}
/// A trailing run of media embeds and its preceding message text.
@immutable
class TrailingImageGallery {
final String content;
final List<MessageGalleryItem> items;
const TrailingImageGallery({required this.content, required this.items});
}
/// Groups trailing images, optionally including videos for message stacks.
TrailingImageGallery? extractTrailingImageGallery(
String content,
Map<String, ImetaEntry> imetaByUrl, {
bool includeVideos = false,
}) {
final lines = content.split('\n');
var cursor = lines.length - 1;
while (cursor >= 0 && lines[cursor].trim().isEmpty) {
cursor -= 1;
}
final items = <MessageGalleryItem>[];
final imagePattern = RegExp(r'^!\[([^\]]*)\]\((https?://[^)\s]+)\)$');
while (cursor >= 0) {
final match = imagePattern.firstMatch(lines[cursor].trim());
if (match == null) break;
// CommonMark unescapes link destinations, as it does labels below.
final url = unshieldMarkdownEscapes(shieldMarkdownEscapes(match.group(2)!));
final imeta = imetaByUrl[url];
final mediaKind = classifyMediaUrl(url, imeta: imeta);
if (mediaKind != MessageMediaKind.image &&
!(includeVideos && mediaKind == MessageMediaKind.video)) {
break;
}
final markdownLabel = decodeMarkdownLabelSyntax(
unshieldMarkdownEscapes(shieldMarkdownEscapes(match.group(1)!)),
).trim();
items.add(
MessageGalleryItem(
url: url,
semanticLabel:
imeta?.alt ??
(markdownLabel.isNotEmpty
? markdownLabel
: mediaKind == MessageMediaKind.video
? 'Video'
: 'Message image'),
aspectRatio: imeta?.aspectRatio,
mimeType: imeta?.mimeType,
isVideo: mediaKind == MessageMediaKind.video,
posterUrl: imeta?.posterUrl,
),
);
cursor -= 1;
}
if (items.length < 2) return null;
return TrailingImageGallery(
content: lines.take(cursor + 1).join('\n').trimRight(),
items: items.reversed.toList(),
);
}