From 08029051d5802277cc806d48c22008a6cc135de6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20R=C3=A1=C5=BE?= Date: Wed, 18 Nov 2026 00:25:35 +0100 Subject: [PATCH] fix(media): open the attachment that was tapped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The viewer and the chat-info gallery both build their list from `viewableMedia`, which required `downloadStatus == ready`. Attachments that carry a stale failed/expired flag (left behind by the old re-download bug) were therefore missing from both lists, while the bubbles drew them from their local file anyway. Tapping such a bubble looked up an index in a list that did not contain it, fell back to 0 and opened the newest attachment in the conversation — often a video. The same filter is why those attachments never showed up under shared media. A local file is what makes an attachment viewable now; the status only decides what to show while there is none. That also covers album items. The viewer stops guessing as well: when the tapped attachment is not in the list it shows that attachment alone instead of page 0. Verified on the running debug build: the chat-info grid went from one tile to seven for the same conversation, an image tile opens an image and the video tile opens the video player. --- lib/pages/media_viewer_page.dart | 4 +++- lib/utils/message_notifier.dart | 17 +++++++------- test/chat_info_media_test.dart | 38 +++++++++++++++++++++++++++++--- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/lib/pages/media_viewer_page.dart b/lib/pages/media_viewer_page.dart index 82ee077..f728d90 100644 --- a/lib/pages/media_viewer_page.dart +++ b/lib/pages/media_viewer_page.dart @@ -83,7 +83,9 @@ class _MediaViewerPageState extends State { return; } setState(() { - _media = media.isEmpty ? [widget.message] : media; + // Never fall back to the first page when the tapped attachment is not in + // the list: that opens a different attachment than the one clicked. + _media = index < 0 ? [widget.message] : media; _index = index < 0 ? 0 : index; _controller = PageController(initialPage: _index); }); diff --git a/lib/utils/message_notifier.dart b/lib/utils/message_notifier.dart index a589018..709a918 100644 --- a/lib/utils/message_notifier.dart +++ b/lib/utils/message_notifier.dart @@ -27,22 +27,23 @@ class MessageNotifier extends ChangeNotifier { /// items are expanded into one entry per attachment. static List viewableMedia(List messages) { final media = []; + // A local file is what makes an attachment viewable, not its status: a + // stale failed/expired flag (an old redelivery) used to hide attachments + // that the bubbles and the viewer can still open. + bool viewable(PrivateMessage message) => + message.localPath != null && + message.mediaType != null && + message.mediaType != MediaType.voice; for (final message in messages) { if (message.isAlbum) { for (final item in albumMedia(message)) { - if (item.localPath != null && - item.mediaType != null && - item.mediaType != MediaType.voice && - item.downloadStatus == MediaDownloadStatus.ready) { + if (viewable(item)) { media.add(item); } } continue; } - if (message.localPath != null && - message.mediaType != null && - message.mediaType != MediaType.voice && - message.downloadStatus == MediaDownloadStatus.ready) { + if (viewable(message)) { media.add(message); } } diff --git a/test/chat_info_media_test.dart b/test/chat_info_media_test.dart index e888c2c..f1b79f3 100644 --- a/test/chat_info_media_test.dart +++ b/test/chat_info_media_test.dart @@ -1,6 +1,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:whisper/models/private_message.dart'; import 'package:whisper/utils/message_notifier.dart'; +import 'package:whisper_websocket_client_dart/models/media_type.dart'; PrivateMessage message(String? type, String? path, DateTime sentAt, {int? downloadStatus}) { @@ -10,8 +11,22 @@ PrivateMessage message(String? type, String? path, DateTime sentAt, downloadStatus: downloadStatus ?? MediaDownloadStatus.ready); } +PrivateMessage album(DateTime sentAt, String path, MediaType type, + {int status = MediaDownloadStatus.ready}) { + final result = PrivateMessage('1', '', sentAt, sentAt, false); + result.setAlbum([ + AlbumItem( + mediaId: 'a', + type: type.name, + size: 1, + localPath: path, + status: status), + ]); + return result; +} + void main() { - test('shared media keeps only ready, viewable media with a local file', () { + test('shared media needs a local file, whatever the status says', () { final now = DateTime(2026, 11, 12, 12); final media = MessageNotifier.viewableMedia([ message('image', '/tmp/a.jpg', now), @@ -21,12 +36,29 @@ void main() { // Not downloaded yet message('image', null, now.subtract(const Duration(minutes: 3))), // Still downloading - message('image', '/tmp/d.jpg', now.subtract(const Duration(minutes: 4)), + message('image', null, now.subtract(const Duration(minutes: 4)), downloadStatus: MediaDownloadStatus.pending), + // A stale failure flag must not hide a file that is right there + message('image', '/tmp/e.jpg', now.subtract(const Duration(minutes: 5)), + downloadStatus: MediaDownloadStatus.expired), // Plain text - message(null, null, now.subtract(const Duration(minutes: 5))), + message(null, null, now.subtract(const Duration(minutes: 6))), + ]); + + expect(media.map((m) => m.localPath), + ['/tmp/a.jpg', '/tmp/b.mp4', '/tmp/e.jpg']); + }); + + test('an album contributes each of its attachments', () { + final now = DateTime(2026, 11, 12, 12); + final media = MessageNotifier.viewableMedia([ + album(now, '/tmp/a.jpg', MediaType.image), + album(now.subtract(const Duration(minutes: 1)), '/tmp/b.mp4', + MediaType.video, + status: MediaDownloadStatus.failed), ]); expect(media.map((m) => m.localPath), ['/tmp/a.jpg', '/tmp/b.mp4']); + expect(media.first.mediaType, MediaType.image); }); }