feat(market): host and publish the cover photo with each offer

Wire the Blossom MediaTransport into publishing: shareable lots carry
their varietyId, publishLots uploads the lot's cover photo and puts the
returned URL on the offer (best-effort — a hosting failure still publishes
the offer, just without a photo). Add a configurable media server (Comunes
default, empty to opt out) to settings and the market advanced config.
This commit is contained in:
vjrj 2026-07-10 21:12:36 +02:00
parent 5bc1715637
commit fa53295439
17 changed files with 280 additions and 22 deletions

View file

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:commons_core/commons_core.dart';
import 'package:flutter_test/flutter_test.dart';
@ -40,6 +41,24 @@ class FakeOfferTransport implements OfferTransport {
Future<void> close() async {}
}
/// In-memory [MediaTransport]: hands back a fixed URL, or throws when [fail] is
/// set (to prove the publish step degrades to no-image instead of erroring).
class FakeMediaTransport implements MediaTransport {
FakeMediaTransport({this.fail = false});
final bool fail;
int uploads = 0;
@override
Future<Uri> upload(Uint8List bytes, {required String mimeType}) async {
uploads++;
if (fail) throw const MediaUploadException('boom', 500);
return Uri.parse('https://media.test/hosted.jpg');
}
@override
Future<void> close() async {}
}
/// Emits every published offer TWICE on discover the way a relay legitimately
/// resends an addressable event (a stored copy plus a live echo). Used to prove
/// the cubit dedups instead of doubling the listing.
@ -269,12 +288,14 @@ void main() {
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
category: 'Solanaceae',
),
ShareableLot(
lotId: 'lot-2',
varietyId: 'var-2',
summary: 'Judía',
offerStatus: OfferStatus.exchange,
),
@ -291,6 +312,85 @@ void main() {
await cubit.close();
});
test('publishLots hosts the cover photo and puts its URL on the offer',
() async {
final transport = FakeOfferTransport();
final media = FakeMediaTransport();
final cubit = OffersCubit(
transport,
media: media,
coverPhoto: (id) async => Uint8List.fromList([1, 2, 3]),
);
await cubit.publishLots(
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',
);
await cubit.discover('sp3');
await pumpEventQueue();
expect(media.uploads, 1);
expect(cubit.state.offers.single.imageUrl, 'https://media.test/hosted.jpg');
await cubit.close();
});
test('publishLots still publishes (without image) when hosting fails',
() async {
final transport = FakeOfferTransport();
final cubit = OffersCubit(
transport,
media: FakeMediaTransport(fail: true),
coverPhoto: (id) async => Uint8List.fromList([1, 2, 3]),
);
final count = await cubit.publishLots(
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',
);
expect(count, 1); // the offer still went out
await cubit.discover('sp3');
await pumpEventQueue();
expect(cubit.state.offers.single.imageUrl, isNull);
await cubit.close();
});
test('publishLots without a media server publishes no image', () async {
final transport = FakeOfferTransport();
final cubit = OffersCubit(transport); // no media, no coverPhoto
await cubit.publishLots(
const [
ShareableLot(
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared,
),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',
);
await cubit.discover('sp3');
await pumpEventQueue();
expect(cubit.state.offers.single.imageUrl, isNull);
await cubit.close();
});
test('publishLots is a no-op offline or with no area', () async {
final offline = OffersCubit(null);
expect(
@ -298,6 +398,7 @@ void main() {
const [
ShareableLot(
lotId: 'x',
varietyId: 'vx',
summary: 'x',
offerStatus: OfferStatus.shared,
),
@ -315,6 +416,7 @@ void main() {
const [
ShareableLot(
lotId: 'x',
varietyId: 'vx',
summary: 'x',
offerStatus: OfferStatus.shared,
),
@ -336,13 +438,20 @@ void main() {
cubit: cubit,
shareableLots: const [
ShareableLot(
lotId: 'lot-1', summary: 'Tomate', offerStatus: OfferStatus.shared),
lotId: 'lot-1',
varietyId: 'var-1',
summary: 'Tomate',
offerStatus: OfferStatus.shared),
ShareableLot(
lotId: 'lot-2',
varietyId: 'var-2',
summary: 'Judía',
offerStatus: OfferStatus.exchange),
ShareableLot(
lotId: 'lot-3', summary: 'Otro', offerStatus: OfferStatus.shared),
lotId: 'lot-3',
varietyId: 'var-3',
summary: 'Otro',
offerStatus: OfferStatus.shared),
],
authorPubkeyHex: 'ab' * 32,
areaGeohash: 'sp3e9',