mirror of
https://github.com/block/buzz.git
synced 2026-12-01 13:42:45 +00:00
mobile: keep a thumbnail fallback apart from a direct load of the same URL (#8913)
## Summary Follow-up to [#8896](https://github.com/block/buzz/pull/8896), from its review threads. **A fallback and a direct load of the same thumbnail no longer share a download.** `MediaImageProvider.isThumbnailFallback` decides what a 404 means: for a thumbnail `MediaImage` picked in place of an original, it records a missing thumbnail so the next build loads the original; for any other URL it starts the 30s failure cooldown. It was left out of equality and the in-flight download key, so when a profile URL that is itself a thumbnail and a fallback to that same thumbnail were on screen together, both joined one download and only one of them ran its own 404 handling. Now each keeps its own provider and download, and each gets its own handling. The failure cooldown is keyed the same way. Before, a direct 404 of a thumbnail put its URL on cooldown (30s, up to 5 min with Retry-After), and an avatar that fell back to that thumbnail during the cooldown hit the cooldown error instead of a 404, so it showed initials instead of loading the original. Also: - Reworded the `preferThumbnail` doc so it no longer names community icons, which never set it. - Removed the community-icon avatar test, which fed a URL production never produces. ## Testing - New test: a direct thumbnail URL and a fallback to it, shown together, make two requests; after a 404 the direct URL is in cooldown and the fallback's original loads (not the initials). It fails with the fetch key change reverted. - New test: after a direct thumbnail 404, an avatar that falls back to the same thumbnail still loads the original. It fails with the cooldown key change reverted. - `just mobile-check` and `just mobile-test` pass. 🤖 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>
This commit is contained in:
@@ -48,8 +48,10 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
/// Set when [url] is a thumbnail [MediaImage] chose in place of an
|
||||
/// original: its 404 then records a missing thumbnail, so later builds load
|
||||
/// the original, rather than a failure cooldown. Any other 404, including a
|
||||
/// profile URL that is itself a thumbnail, keeps the cooldown. Excluded from
|
||||
/// equality: it changes how a failure is handled, not the bytes.
|
||||
/// profile URL that is itself a thumbnail, keeps the cooldown. Part of
|
||||
/// equality, the shared download's key and the cooldown's key, so a
|
||||
/// fallback and a direct load of the same URL each keep their own failure
|
||||
/// handling.
|
||||
final bool isThumbnailFallback;
|
||||
|
||||
/// Excluded from equality: transport, not identity.
|
||||
@@ -80,8 +82,12 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
|
||||
static const _defaultCooldown = Duration(seconds: 30);
|
||||
static const _maxRetryAfter = Duration(minutes: 5);
|
||||
static final Map<String, DateTime> _cooldownUntil = {};
|
||||
static final Map<(String, MediaGetAuthService), _SharedFetch> _inFlight = {};
|
||||
|
||||
/// Keyed like [_inFlight]: a direct load's failure must not stop a
|
||||
/// fallback to the same URL from recording a missing thumbnail.
|
||||
static final Map<(String, bool), DateTime> _cooldownUntil = {};
|
||||
static final Map<(String, MediaGetAuthService, bool), _SharedFetch>
|
||||
_inFlight = {};
|
||||
|
||||
/// Injectable clock for cooldown tests.
|
||||
@visibleForTesting
|
||||
@@ -91,7 +97,10 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
static void debugResetCooldowns() => _cooldownUntil.clear();
|
||||
|
||||
@visibleForTesting
|
||||
static Iterable<String> get debugCooldownUrls => _cooldownUntil.keys;
|
||||
static Iterable<String> get debugCooldownUrls =>
|
||||
_cooldownUntil.keys.map((key) => key.$1);
|
||||
|
||||
(String, bool) get _cooldownKey => (url, isThumbnailFallback);
|
||||
|
||||
@override
|
||||
Future<MediaImageProvider> obtainKey(ImageConfiguration configuration) {
|
||||
@@ -127,7 +136,7 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
// Each decode size is its own cache entry (see [ResizeImage]), so one
|
||||
// URL shown at two sizes resolves twice. Share the download instead of
|
||||
// fetching the same bytes once per size.
|
||||
final fetchKey = (url, auth);
|
||||
final fetchKey = (url, auth, isThumbnailFallback);
|
||||
final fetch = _inFlight[fetchKey] ??= _SharedFetch(
|
||||
_fetchBytes,
|
||||
onFinished: (fetch) {
|
||||
@@ -176,12 +185,12 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
/// Downloads [url]. Completing [abort] means every load waiting for this
|
||||
/// download was abandoned, so the URL is not put on cooldown.
|
||||
Future<Uint8List> _fetchBytes(Future<void> abort) async {
|
||||
final until = _cooldownUntil[url];
|
||||
final until = _cooldownUntil[_cooldownKey];
|
||||
if (until != null) {
|
||||
if (debugNow().isBefore(until)) {
|
||||
throw MediaImageCooldownException(url: url, until: until);
|
||||
}
|
||||
_cooldownUntil.remove(url);
|
||||
_cooldownUntil.remove(_cooldownKey);
|
||||
}
|
||||
|
||||
final uri = Uri.parse(url);
|
||||
@@ -244,7 +253,7 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
} catch (_) {
|
||||
// Nobody wanted the image any more; the URL itself did not fail.
|
||||
if (!wasAbandoned) {
|
||||
_cooldownUntil[url] = debugNow().add(_defaultCooldown);
|
||||
_cooldownUntil[_cooldownKey] = debugNow().add(_defaultCooldown);
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
@@ -257,7 +266,7 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
throw NetworkImageLoadException(statusCode: 404, uri: uri);
|
||||
}
|
||||
if (response.statusCode != 200) {
|
||||
_cooldownUntil[url] = debugNow().add(_cooldownFor(response));
|
||||
_cooldownUntil[_cooldownKey] = debugNow().add(_cooldownFor(response));
|
||||
throw NetworkImageLoadException(
|
||||
statusCode: response.statusCode,
|
||||
uri: uri,
|
||||
@@ -265,7 +274,7 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
}
|
||||
final bytes = response.bodyBytes;
|
||||
if (bytes.isEmpty) {
|
||||
_cooldownUntil[url] = debugNow().add(_defaultCooldown);
|
||||
_cooldownUntil[_cooldownKey] = debugNow().add(_defaultCooldown);
|
||||
throw NetworkImageLoadException(statusCode: 200, uri: uri);
|
||||
}
|
||||
return bytes;
|
||||
@@ -286,11 +295,12 @@ class MediaImageProvider extends ImageProvider<MediaImageProvider> {
|
||||
return other is MediaImageProvider &&
|
||||
other.url == url &&
|
||||
other.scale == scale &&
|
||||
other.auth == auth;
|
||||
other.auth == auth &&
|
||||
other.isThumbnailFallback == isThumbnailFallback;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => Object.hash(url, scale, auth);
|
||||
int get hashCode => Object.hash(url, scale, auth, isThumbnailFallback);
|
||||
|
||||
@override
|
||||
String toString() =>
|
||||
|
||||
@@ -80,8 +80,8 @@ class AvatarImageContent extends ConsumerStatefulWidget {
|
||||
|
||||
/// Load the relay's small JPEG thumbnail for a small relay-hosted image
|
||||
/// (see [MediaImage.preferThumbnail]). People and agent avatars set this,
|
||||
/// matching desktop. Leave it off for images that may be transparent, such
|
||||
/// as community icons: the thumbnail renders transparent areas black.
|
||||
/// matching desktop. Leave it off for images that may be transparent: the
|
||||
/// thumbnail renders transparent areas black.
|
||||
final bool preferThumbnail;
|
||||
|
||||
const AvatarImageContent({
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:buzz/shared/community/community_avatar.dart';
|
||||
import 'package:buzz/shared/community/community_icon_provider.dart';
|
||||
import 'package:buzz/shared/relay/media_auth.dart';
|
||||
import 'package:buzz/shared/relay/media_image.dart';
|
||||
import 'package:buzz/shared/widgets/avatar_shape.dart';
|
||||
@@ -355,25 +353,97 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('community icons load the original, which keeps transparency', (
|
||||
tester,
|
||||
) async {
|
||||
final icon = '$relay/media/${'34' * 32}.png';
|
||||
await mount(
|
||||
tester,
|
||||
overrides: [
|
||||
communityIconPresentationProvider(relay).overrideWithValue(icon),
|
||||
],
|
||||
);
|
||||
testWidgets(
|
||||
'a direct thumbnail URL and a fallback to it keep their own 404 handling',
|
||||
(tester) async {
|
||||
final original = '$relay/media/${'9a' * 32}.png';
|
||||
final thumbnail = '$relay/media/${'9a' * 32}.thumb.jpg';
|
||||
final slow404 = Completer<http.Response>();
|
||||
respond = (url) async => url == thumbnail
|
||||
? slow404.future
|
||||
: http.Response.bytes(_pngBytes, 200);
|
||||
await mount(tester);
|
||||
|
||||
// The channels header size, well within the thumbnail.
|
||||
await show(
|
||||
tester,
|
||||
const CommunityAvatar(name: 'Buzz', relayUrl: relay, size: 36),
|
||||
);
|
||||
expect(requested, [icon]);
|
||||
expect(shownUrl(tester), icon);
|
||||
});
|
||||
// One profile stores the thumbnail URL itself; another stores the
|
||||
// original, so its avatar substitutes the same thumbnail. Both are
|
||||
// waiting on the relay when its 404 arrives.
|
||||
await show(
|
||||
tester,
|
||||
Column(
|
||||
children: [
|
||||
AvatarImage(
|
||||
imageUrl: thumbnail,
|
||||
radius: 20,
|
||||
fallback: const Text('D'),
|
||||
),
|
||||
AvatarImage(
|
||||
imageUrl: original,
|
||||
radius: 20,
|
||||
fallback: const Text('F'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
expect(requested.where((url) => url == thumbnail), hasLength(2));
|
||||
slow404.complete(http.Response('', 404));
|
||||
await show(tester, const SizedBox());
|
||||
|
||||
// The direct URL is cooling down, and the fallback recorded the
|
||||
// missing thumbnail, so the original avatar shown again at another
|
||||
// size loads the original instead of showing initials.
|
||||
expect(MediaImageProvider.debugCooldownUrls, [thumbnail]);
|
||||
await show(
|
||||
tester,
|
||||
AvatarImage(
|
||||
imageUrl: original,
|
||||
radius: 16,
|
||||
fallback: const Text('F'),
|
||||
),
|
||||
);
|
||||
expect(shownUrl(tester), original);
|
||||
expect(find.text('F'), findsNothing);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'a fallback after a direct thumbnail 404 still loads the original',
|
||||
(tester) async {
|
||||
final original = '$relay/media/${'9b' * 32}.png';
|
||||
final thumbnail = '$relay/media/${'9b' * 32}.thumb.jpg';
|
||||
respond = (url) async => url == thumbnail
|
||||
? http.Response('', 404)
|
||||
: http.Response.bytes(_pngBytes, 200);
|
||||
await mount(tester);
|
||||
|
||||
// A profile that stores the thumbnail URL itself fails first and
|
||||
// puts that URL on cooldown.
|
||||
await show(
|
||||
tester,
|
||||
AvatarImage(
|
||||
imageUrl: thumbnail,
|
||||
radius: 20,
|
||||
fallback: const Text('D'),
|
||||
),
|
||||
);
|
||||
await show(tester, const SizedBox());
|
||||
expect(MediaImageProvider.debugCooldownUrls, [thumbnail]);
|
||||
|
||||
// During that cooldown, an avatar for the original falls back to the
|
||||
// same thumbnail. Its own request records the thumbnail as missing,
|
||||
// so the avatar ends up on the original rather than initials.
|
||||
await show(
|
||||
tester,
|
||||
AvatarImage(
|
||||
imageUrl: original,
|
||||
radius: 20,
|
||||
fallback: const Text('F'),
|
||||
),
|
||||
);
|
||||
expect(requested.where((url) => url == thumbnail), hasLength(2));
|
||||
expect(requested, contains(original));
|
||||
expect(find.text('F'), findsNothing);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('clips agents to the desktop squircle', (tester) async {
|
||||
|
||||
Reference in New Issue
Block a user