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

@ -174,6 +174,7 @@ class SeedLabelEntry extends Equatable {
class ShareableLot extends Equatable {
const ShareableLot({
required this.lotId,
required this.varietyId,
required this.summary,
required this.offerStatus,
this.category,
@ -183,6 +184,10 @@ class ShareableLot extends Equatable {
final String lotId;
/// The lot's variety, so the publish step can fetch its cover photo to host
/// as the offer image. Not published itself never leaves the device.
final String varietyId;
/// What to publish: the variety label (never the full inventory).
final String summary;
@ -197,7 +202,7 @@ class ShareableLot extends Equatable {
@override
List<Object?> get props =>
[lotId, summary, offerStatus, category, harvestYear, isOrganic];
[lotId, varietyId, summary, offerStatus, category, harvestYear, isOrganic];
}
/// One germination test on a lot; [rate] is derived (0..1), null when it can't
@ -727,6 +732,12 @@ class VarietyRepository {
return byVariety;
}
/// The cover photo (lowest `sortOrder`) for a single variety, or null when it
/// has none. Used by the publish step to host an offer's image; reuses the
/// same "first photo" rule as the inventory avatar.
Future<Uint8List?> coverPhotoFor(String varietyId) async =>
(await _firstPhotosFor([varietyId]))[varietyId];
/// Maps each of [speciesIds] to its scientific name (one query).
Future<Map<String, String>> _scientificNamesFor(
Set<String> speciesIds,
@ -1461,6 +1472,7 @@ class VarietyRepository {
if (byId[l.varietyId] case final v?)
ShareableLot(
lotId: l.id,
varietyId: v.id,
summary: v.label,
offerStatus: l.offerStatus,
category: v.category,

View file

@ -483,7 +483,9 @@
"noProfile": "Esta persona entá nun compartió'l so perfil",
"copyId": "Copiar códigu",
"idCopied": "Códigu copiáu",
"photo": "Semeya"
"photo": "Semeya",
"mediaServerLabel": "Sirvidor de semeyes",
"mediaServerHelp": "Ónde s'agospien les semeyes de les ufiertes pa que otres persones les vean. Déxalo baleru pa compartir ensin semeyes."
},
"profile": {
"title": "El to perfil",

View file

@ -486,7 +486,9 @@
"noProfile": "This person hasn't shared a profile yet",
"copyId": "Copy code",
"idCopied": "Code copied",
"photo": "Photo"
"photo": "Photo",
"mediaServerLabel": "Photo server",
"mediaServerHelp": "Where offer photos are hosted so others can see them. Leave empty to share without photos."
},
"profile": {
"title": "Your profile",

View file

@ -485,7 +485,9 @@
"noProfile": "Esta persona aún no ha compartido su perfil",
"copyId": "Copiar código",
"idCopied": "Código copiado",
"photo": "Foto"
"photo": "Foto",
"mediaServerLabel": "Servidor de fotos",
"mediaServerHelp": "Dónde se alojan las fotos de las ofertas para que otras personas las vean. Déjalo vacío para compartir sin fotos."
},
"profile": {
"title": "Tu perfil",

View file

@ -482,7 +482,9 @@
"noProfile": "Esta pessoa ainda não partilhou o seu perfil",
"copyId": "Copiar código",
"idCopied": "Código copiado",
"photo": "Foto"
"photo": "Foto",
"mediaServerLabel": "Servidor de fotos",
"mediaServerHelp": "Onde as fotos das ofertas ficam alojadas para que outras pessoas as vejam. Deixe vazio para partilhar sem fotos."
},
"profile": {
"title": "O teu perfil",

View file

@ -4,9 +4,9 @@
/// To regenerate, run: `dart run slang`
///
/// Locales: 4
/// Strings: 1552 (388 per locale)
/// Strings: 1560 (390 per locale)
///
/// Built on 2026-07-10 at 18:55 UTC
/// Built on 2026-07-10 at 19:08 UTC
// coverage:ignore-file
// ignore_for_file: type=lint, unused_import

View file

@ -692,6 +692,8 @@ class _Translations$market$ast extends Translations$market$en {
@override String get copyId => 'Copiar códigu';
@override String get idCopied => 'Códigu copiáu';
@override String get photo => 'Semeya';
@override String get mediaServerLabel => 'Sirvidor de semeyes';
@override String get mediaServerHelp => 'Ónde s\'agospien les semeyes de les ufiertes pa que otres persones les vean. Déxalo baleru pa compartir ensin semeyes.';
}
// Path: profile
@ -1458,6 +1460,8 @@ extension on TranslationsAst {
'market.copyId' => 'Copiar códigu',
'market.idCopied' => 'Códigu copiáu',
'market.photo' => 'Semeya',
'market.mediaServerLabel' => 'Sirvidor de semeyes',
'market.mediaServerHelp' => 'Ónde s\'agospien les semeyes de les ufiertes pa que otres persones les vean. Déxalo baleru pa compartir ensin semeyes.',
'profile.title' => 'El to perfil',
'profile.name' => 'Nome',
'profile.nameHint' => 'Cómo te ven los demás',

View file

@ -1285,6 +1285,12 @@ class Translations$market$en {
/// en: 'Photo'
String get photo => 'Photo';
/// en: 'Photo server'
String get mediaServerLabel => 'Photo server';
/// en: 'Where offer photos are hosted so others can see them. Leave empty to share without photos.'
String get mediaServerHelp => 'Where offer photos are hosted so others can see them. Leave empty to share without photos.';
}
// Path: profile
@ -2224,6 +2230,8 @@ extension on Translations {
'market.copyId' => 'Copy code',
'market.idCopied' => 'Code copied',
'market.photo' => 'Photo',
'market.mediaServerLabel' => 'Photo server',
'market.mediaServerHelp' => 'Where offer photos are hosted so others can see them. Leave empty to share without photos.',
'profile.title' => 'Your profile',
'profile.name' => 'Display name',
'profile.nameHint' => 'How others see you',

View file

@ -694,6 +694,8 @@ class _Translations$market$es extends Translations$market$en {
@override String get copyId => 'Copiar código';
@override String get idCopied => 'Código copiado';
@override String get photo => 'Foto';
@override String get mediaServerLabel => 'Servidor de fotos';
@override String get mediaServerHelp => 'Dónde se alojan las fotos de las ofertas para que otras personas las vean. Déjalo vacío para compartir sin fotos.';
}
// Path: profile
@ -1462,6 +1464,8 @@ extension on TranslationsEs {
'market.copyId' => 'Copiar código',
'market.idCopied' => 'Código copiado',
'market.photo' => 'Foto',
'market.mediaServerLabel' => 'Servidor de fotos',
'market.mediaServerHelp' => 'Dónde se alojan las fotos de las ofertas para que otras personas las vean. Déjalo vacío para compartir sin fotos.',
'profile.title' => 'Tu perfil',
'profile.name' => 'Nombre',
'profile.nameHint' => 'Cómo te ven los demás',

View file

@ -691,6 +691,8 @@ class _Translations$market$pt extends Translations$market$en {
@override String get copyId => 'Copiar código';
@override String get idCopied => 'Código copiado';
@override String get photo => 'Foto';
@override String get mediaServerLabel => 'Servidor de fotos';
@override String get mediaServerHelp => 'Onde as fotos das ofertas ficam alojadas para que outras pessoas as vejam. Deixe vazio para partilhar sem fotos.';
}
// Path: profile
@ -1456,6 +1458,8 @@ extension on TranslationsPt {
'market.copyId' => 'Copiar código',
'market.idCopied' => 'Código copiado',
'market.photo' => 'Foto',
'market.mediaServerLabel' => 'Servidor de fotos',
'market.mediaServerHelp' => 'Onde as fotos das ofertas ficam alojadas para que outras pessoas as vejam. Deixe vazio para partilhar sem fotos.',
'profile.title' => 'O teu perfil',
'profile.name' => 'Nome',
'profile.nameHint' => 'Como os outros te veem',

View file

@ -39,20 +39,34 @@ class SocialService {
/// Opens a session against [relayUrls]: one fault-tolerant pool carrying all
/// three transports (offers, messaging, trust). Relays that are down are
/// skipped; throws only when none are reachable. Caller closes it.
Future<SocialSession> openSession(List<String> relayUrls) async {
///
/// [mediaServerUrl] (a Blossom endpoint) enables offer photos: when set, the
/// session exposes a [MediaTransport] to host images. It rides HTTP, not the
/// relay pool, so it's independent of relay reachability.
Future<SocialSession> openSession(
List<String> relayUrls, {
String? mediaServerUrl,
}) async {
final pool = await RelayPool.connect(relayUrls, identity: identity);
return SocialSession(pool);
return SocialSession(pool, mediaServerUrl: mediaServerUrl);
}
}
/// One relay channel with the three transports on top the
/// "one channel, three interfaces" shape, at the app layer.
/// "one channel, three interfaces" shape, at the app layer. Media rides its own
/// HTTP endpoint (Blossom), so it's a fourth transport but not on the channel.
class SocialSession {
SocialSession(this._channel)
SocialSession(this._channel, {String? mediaServerUrl})
: offers = NostrOfferTransport(_channel),
messages = NostrMessageTransport(_channel),
trust = NostrTrustTransport(_channel),
profile = NostrProfileTransport(_channel);
profile = NostrProfileTransport(_channel),
media = (mediaServerUrl != null && mediaServerUrl.trim().isNotEmpty)
? BlossomMediaTransport(
Uri.parse(mediaServerUrl.trim()),
_channel.privateKeyHex,
)
: null;
final NostrChannel _channel;
@ -61,7 +75,14 @@ class SocialSession {
final TrustTransport trust;
final ProfileTransport profile;
Future<void> close() => _channel.close();
/// Hosts offer photos, or null when no media server is configured (offers then
/// publish without images).
final MediaTransport? media;
Future<void> close() async {
await media?.close();
await _channel.close();
}
}
Uint8List _hexToBytes(String hex) {

View file

@ -17,6 +17,7 @@ class SocialSettings {
static const _areaKey = 'tane.social.area_geohash';
static const _relaysKey = 'tane.social.relays';
static const _searchPrecisionKey = 'tane.social.search_precision';
static const _mediaServerKey = 'tane.social.media_server';
/// How wide "your zone" searches, as a geohash prefix length: 5 ±2.4 km
/// ("very close"), 4 ±20 km ("around here"), 3 ±78 km ("my region").
@ -39,6 +40,11 @@ class SocialSettings {
'wss://relay.primal.net',
];
/// Blossom media server used to host offer photos so peers can see them. The
/// Comunes-run server is the non-commercial default; the user can point this
/// at any Blossom server, or clear it to publish offers without photos.
static const String defaultMediaServer = 'https://blossom.comunes.org';
/// The user's coarse area as a low-precision geohash (may be empty). Set
/// manually or from device location; the transport coarsens it further.
Future<String> areaGeohash() async => (await _store.read(_areaKey)) ?? '';
@ -73,6 +79,18 @@ class SocialSettings {
Future<void> setSearchPrecision(int precision) =>
_store.write(_searchPrecisionKey, '${_clampPrecision(precision)}');
/// The Blossom media server for offer photos. Falls back to
/// [defaultMediaServer] until the user configures one; an explicitly saved
/// empty value turns photo hosting off (offers still publish, without images).
Future<String> mediaServerUrl() async {
final raw = await _store.read(_mediaServerKey);
if (raw == null) return defaultMediaServer; // never configured
return raw.trim();
}
Future<void> setMediaServerUrl(String url) =>
_store.write(_mediaServerKey, url.trim());
int _clampPrecision(int p) => p < minSearchPrecision
? minSearchPrecision
: (p > maxSearchPrecision ? maxSearchPrecision : p);

View file

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:typed_data';
import 'package:commons_core/commons_core.dart';
import 'package:equatable/equatable.dart';
@ -134,12 +135,26 @@ class OffersState extends Equatable {
/// UI stays offline-tolerant (a null transport = the social layer is unavailable
/// and the screen degrades gracefully).
class OffersCubit extends Cubit<OffersState> {
OffersCubit(this._transport, {Future<void> Function()? onDispose})
: _onDispose = onDispose,
OffersCubit(
this._transport, {
MediaTransport? media,
Future<Uint8List?> Function(String varietyId)? coverPhoto,
Future<void> Function()? onDispose,
}) : _media = media,
_coverPhoto = coverPhoto,
_onDispose = onDispose,
super(const OffersState());
final OfferTransport? _transport;
/// Hosts an offer's cover photo so peers can see it; null when no media server
/// is configured, in which case offers publish without images.
final MediaTransport? _media;
/// Fetches a variety's cover photo bytes (for hosting); null in tests or when
/// no inventory repo is wired.
final Future<Uint8List?> Function(String varietyId)? _coverPhoto;
/// Closes the owning [SocialSession]/connection when the cubit is disposed
/// (null in tests, where the transport is a fake with nothing to close).
final Future<void> Function()? _onDispose;
@ -269,6 +284,7 @@ class OffersCubit extends Cubit<OffersState> {
areaGeohash: areaGeohash,
category: lot.category,
isOrganic: lot.isOrganic,
imageUrl: await _hostCoverPhoto(lot.varietyId),
);
final result = await transport.publish(offer);
if (result.accepted) accepted++;
@ -281,6 +297,23 @@ class OffersCubit extends Cubit<OffersState> {
return accepted;
}
/// Uploads the lot's cover photo to the media server and returns its URL, or
/// null when there's no media server, no photo, or the upload fails. Hosting
/// is best-effort: a missing image never blocks publishing the offer.
Future<String?> _hostCoverPhoto(String varietyId) async {
final media = _media;
final coverPhoto = _coverPhoto;
if (media == null || coverPhoto == null) return null;
try {
final Uint8List? bytes = await coverPhoto(varietyId);
if (bytes == null || bytes.isEmpty) return null;
final url = await media.upload(bytes, mimeType: 'image/jpeg');
return url.toString();
} catch (_) {
return null; // degrade: publish the offer without a photo
}
}
@override
Future<void> close() async {
_searchTimeout?.cancel();
@ -295,13 +328,21 @@ class OffersCubit extends Cubit<OffersState> {
/// fails), the cubit is created with a null transport and the screen still opens.
Future<OffersCubit> createOffersCubit(
SocialService social,
SocialSettings settings,
) async {
SocialSettings settings, {
VarietyRepository? repository,
}) async {
final relays = await settings.relayUrls();
if (relays.isEmpty) return OffersCubit(null);
try {
final session = await social.openSession(relays);
return OffersCubit(session.offers, onDispose: session.close);
final mediaServer = await settings.mediaServerUrl();
final session =
await social.openSession(relays, mediaServerUrl: mediaServer);
return OffersCubit(
session.offers,
media: session.media,
coverPhoto: repository?.coverPhotoFor,
onDispose: session.close,
);
} catch (_) {
return OffersCubit(null); // offline / no relay reachable
}

View file

@ -58,7 +58,11 @@ class _MarketScreenState extends State<MarketScreen> {
final repo =
widget.outbox != null ? context.read<VarietyRepository>() : null;
setState(() => _loading = true);
final cubit = await createOffersCubit(widget.social, widget.settings);
final cubit = await createOffersCubit(
widget.social,
widget.settings,
repository: repo,
);
final area = await widget.settings.areaGeohash();
if (!mounted) {
await cubit.close();
@ -599,6 +603,7 @@ class _ConfigSheet extends StatefulWidget {
class _ConfigSheetState extends State<_ConfigSheet> {
final _area = TextEditingController();
final _servers = TextEditingController();
final _mediaServer = TextEditingController();
bool _loading = true;
bool _locationBusy = false;
String? _locationError;
@ -613,6 +618,7 @@ class _ConfigSheetState extends State<_ConfigSheet> {
Future<void> _load() async {
_area.text = await widget.settings.areaGeohash();
_servers.text = (await widget.settings.relayUrls()).join('\n');
_mediaServer.text = await widget.settings.mediaServerUrl();
_precision = await widget.settings.searchPrecision();
if (mounted) setState(() => _loading = false);
}
@ -620,6 +626,7 @@ class _ConfigSheetState extends State<_ConfigSheet> {
Future<void> _save() async {
await widget.settings.setAreaGeohash(_area.text);
await widget.settings.setRelayUrls(_servers.text.split('\n'));
await widget.settings.setMediaServerUrl(_mediaServer.text);
await widget.settings.setSearchPrecision(_precision);
if (mounted) Navigator.of(context).pop(true);
}
@ -649,6 +656,7 @@ class _ConfigSheetState extends State<_ConfigSheet> {
void dispose() {
_area.dispose();
_servers.dispose();
_mediaServer.dispose();
super.dispose();
}
@ -788,6 +796,16 @@ class _ConfigSheetState extends State<_ConfigSheet> {
helperMaxLines: 2,
),
),
const SizedBox(height: 14),
TextField(
key: const Key('market.mediaServer'),
controller: _mediaServer,
decoration: InputDecoration(
labelText: t.market.mediaServerLabel,
helperText: t.market.mediaServerHelp,
helperMaxLines: 2,
),
),
],
),
),

View file

@ -27,6 +27,15 @@ void main() {
expect(await settings.relayUrls(), isEmpty);
});
test('media server defaults, is configurable, and can be turned off',
() async {
expect(await settings.mediaServerUrl(), SocialSettings.defaultMediaServer);
await settings.setMediaServerUrl(' https://blossom.example ');
expect(await settings.mediaServerUrl(), 'https://blossom.example');
await settings.setMediaServerUrl(''); // explicit empty = no photo hosting
expect(await settings.mediaServerUrl(), '');
});
test('stores and normalises the area geohash (trim + lowercase)', () async {
await settings.setAreaGeohash(' SP3E9 ');
expect(await settings.areaGeohash(), 'sp3e9');

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',