feat(market): embed offer photos inline instead of a media server

Replace the Blossom media-server approach with a fully decentralized one:
generate a small (~320px) JPEG thumbnail from the lot's cover photo and
embed it in the offer event as a data: URI, so photos ride the relays with
no server and no IP leak. The full photo stays in the encrypted inventory.

- offer_thumbnail.dart: downscale-until-it-fits data-URI builder + decoder
- market_widgets: render data: URIs from memory, http(s) URLs from network
- offers_cubit: publish a thumbnail (best-effort) in place of an upload
- drop MediaTransport/Blossom from commons_core (http/crypto deps) and the
  media-server setting; parked on branch parked/offer-photos-blossom

Relay event-size limits cap the image to a thumbnail; higher-res sharing
would need the parked Blossom path.
This commit is contained in:
vjrj 2026-07-10 21:27:59 +02:00
parent fa53295439
commit 9109581be0
24 changed files with 230 additions and 411 deletions

View file

@ -0,0 +1,56 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:image/image.dart' as img;
/// Builds a tiny `data:image/jpeg;base64,` thumbnail from a full photo, small
/// enough to ride *inside* a Nostr offer event no media server, fully
/// decentralized. The full-resolution photo stays in the encrypted local
/// inventory; only this shrunken preview is published so peers can see the seed.
///
/// Relays cap event size, so we downscale (longest edge) and re-encode until the
/// base64 fits under [maxBytes]. Returns null when the bytes aren't a decodable
/// image or no size small enough is reachable the offer then publishes without
/// a photo (graceful, never a hard failure).
String? offerThumbnailDataUri(
Uint8List bytes, {
int maxBytes = 40000,
List<int> edges = const [320, 256, 192, 128],
int quality = 68,
}) {
try {
final decoded = img.decodeImage(bytes);
if (decoded == null) return null;
for (final edge in edges) {
final resized = _fitWithin(decoded, edge);
final jpeg = img.encodeJpg(resized, quality: quality);
final b64 = base64.encode(jpeg);
if (b64.length <= maxBytes) return 'data:image/jpeg;base64,$b64';
}
return null; // even the smallest edge didn't fit — skip the image
} catch (_) {
return null; // undecodable/corrupt bytes publish without a photo
}
}
/// Extracts the raw bytes from a `data:...;base64,` URI, or null when [uri] is
/// not a base64 data URI. Used by the UI to render an inline thumbnail with
/// `Image.memory` instead of a network fetch.
Uint8List? decodeDataUri(String uri) {
if (!uri.startsWith('data:')) return null;
try {
return Uri.parse(uri).data?.contentAsBytes();
} on FormatException {
return null;
}
}
/// Resizes [image] so its longest edge is at most [edge], preserving aspect
/// ratio. Never upscales a photo that's already smaller.
img.Image _fitWithin(img.Image image, int edge) {
if (image.width <= edge && image.height <= edge) return image;
return image.width >= image.height
? img.copyResize(image, width: edge)
: img.copyResize(image, height: edge);
}

View file

@ -39,34 +39,20 @@ 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.
///
/// [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 {
Future<SocialSession> openSession(List<String> relayUrls) async {
final pool = await RelayPool.connect(relayUrls, identity: identity);
return SocialSession(pool, mediaServerUrl: mediaServerUrl);
return SocialSession(pool);
}
}
/// One relay channel with the three transports on top the
/// "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.
/// "one channel, three interfaces" shape, at the app layer.
class SocialSession {
SocialSession(this._channel, {String? mediaServerUrl})
SocialSession(this._channel)
: offers = NostrOfferTransport(_channel),
messages = NostrMessageTransport(_channel),
trust = NostrTrustTransport(_channel),
profile = NostrProfileTransport(_channel),
media = (mediaServerUrl != null && mediaServerUrl.trim().isNotEmpty)
? BlossomMediaTransport(
Uri.parse(mediaServerUrl.trim()),
_channel.privateKeyHex,
)
: null;
profile = NostrProfileTransport(_channel);
final NostrChannel _channel;
@ -75,14 +61,7 @@ class SocialSession {
final TrustTransport trust;
final ProfileTransport profile;
/// 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();
}
Future<void> close() => _channel.close();
}
Uint8List _hexToBytes(String hex) {

View file

@ -17,7 +17,6 @@ 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").
@ -40,11 +39,6 @@ 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)) ?? '';
@ -79,18 +73,6 @@ 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);