'use strict'; /** * Markets' pages, server-rendered in the network's shell (openvibe-shared/shell, kit.css) with markets.css. Every value * that reaches markup goes through esc(). The search works without script. */ const shell = require('openvibe-shared/shell'); const serve = require('openvibe-shared/serve'); const { PLACES } = require('./elsewhere'); // Our own files by their content (a release's change is a new address, so neither a browser nor Cloudflare keeps an old // one for the hour they may cache it): /x.css?v=, read once at boot. const asset = (() => { const fs = require('node:fs'); const crypto = require('node:crypto'); const kept = new Map(); return (name) => { if (!kept.has(name)) { let v = 'dev'; try { v = crypto.createHash('sha256').update(fs.readFileSync(require('node:path').join(__dirname, '..', 'public', name))).digest('hex').slice(0, 10); } catch { /* missing in a test: unversioned */ } kept.set(name, `/${name}?v=${v}`); } return kept.get(name); }; })(); const SITE = 'https://openvibe.markets'; const esc = (v) => String(v == null ? '' : v).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); const money = (n, currency = 'USD') => { try { return new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(n); } catch { return `${Number(n).toFixed(2)} ${currency}`; } }; /** "as of 3 hours ago": a price is an observation, never "now". */ function asOf(iso, now = Date.now()) { const t = Date.parse(iso || ''); if (!Number.isFinite(t)) return 'time not stated'; const m = Math.max(0, Math.round((now - t) / 60000)); if (m < 60) return `as of ${m <= 1 ? 'a minute' : `${m} minutes`} ago`; const h = Math.round(m / 60); if (h < 48) return `as of ${h} hour${h === 1 ? '' : 's'} ago`; return `as of ${new Date(t).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' })}`; } function page({ title, body, path = '/', description = 'Every marketplace, one search: new, used, refurbished and local, with every price shown as of its time.', robots = 'index, follow' }) { return shell.page({ name: 'OpenVibe.Markets', siteName: 'OpenVibe.Markets', title, description, canonical: `${SITE}${path}`, robots, navbar: { service: 'markets', apiBase: 'https://openvibe.network', links: [{ label: 'Search', href: '/' }, { label: 'Deals', href: 'https://openvibe.deals/' }] }, footer: { service: 'markets', variant: 'full', mount: '#ov-footer' }, head: ``, body: `
${body}
`, }); } const searchForm = (q = '') => ``; /** Search by photo (v0.2): the photo is shrunk in the browser and sent once for OpenVibe Vision to name. */ const photoForm = () => `
A photo of the thing itself works best.
`; function home({ photo = false, error = null } = {}) { return page({ title: 'Every marketplace, one search', body: `

Every marketplace, one search

New, used, refurbished and local. Deals the community found, with every price shown as of when it was seen, and the big marketplaces one tap away with your search already in.

${error ? `` : ''}${searchForm()}${photo ? photoForm() : ''}

Where it looks

` }); } function results({ q, deals, links, now = Date.now(), fromPhoto = false }) { // A deal without a picture takes the whole width: no empty square. const card = (r) => `
  • ${r.image ? `` : ''} ${esc(r.title)} ${esc(r.store || 'A store')} · ${esc(r.source)}${r.condition && r.condition !== 'new' ? ` · ${esc(r.condition)}` : ''} ${r.price != null ? `${esc(money(r.price, r.currency || 'USD'))}${r.was != null && r.was > r.price ? `${esc(money(r.was, r.currency || 'USD'))}` : ''}${esc(asOf(r.observed_at, now))} Can I afford it?` : 'price not stated'}
  • `; const found = deals.ok ? (deals.results.length ? `

    ${deals.total} deal${deals.total === 1 ? '' : 's'} on OpenVibe Deals${deals.total > deals.results.length ? `, the newest ${deals.results.length} here (all of them)` : ''}

    ` : '

    No deal on OpenVibe Deals matches these words yet. The marketplaces below search their own listings.

    ') : `

    ${esc(deals.error || 'Deals could not be searched just now.')} The marketplaces below still work.

    `; return page({ title: `${q}: every marketplace`, path: `/search?q=${encodeURIComponent(q)}`, robots: 'noindex, follow', body: `
    ${searchForm(q)}${fromPhoto ? `

    From your photo: ${esc(q)}. Not quite it? Change the words above.

    ` : ''}

    Deals

    ${found}

    Alert me about new deals for “${esc(q)}” on OpenVibe Deals, when you are signed in

    Search it on

    These open each marketplace's own search with your words in it. Markets does not fetch from them or know what you do there; as a permitted feed comes for one, its listings join the results above.

    ` }); } module.exports = { page, home, results, esc, money, asOf };