Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/pages/media_viewer_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ class _MediaViewerPageState extends State<MediaViewerPage> {
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);
});
Expand Down
17 changes: 9 additions & 8 deletions lib/utils/message_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@ class MessageNotifier extends ChangeNotifier {
/// items are expanded into one entry per attachment.
static List<PrivateMessage> viewableMedia(List<PrivateMessage> messages) {
final media = <PrivateMessage>[];
// 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);
}
}
Expand Down
38 changes: 35 additions & 3 deletions test/chat_info_media_test.dart
Original file line number Diff line number Diff line change
@@ -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}) {
Expand All @@ -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),
Expand All @@ -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);
});
}
Loading