openvibers/openvibe.markets serverviews.js

views.js

main 7.7 KB · 98 lines · javascript

1'use strict';
2/**
3 * Markets' pages, server-rendered in the network's shell (openvibe-shared/shell, kit.css) with markets.css. Every value
4 * that reaches markup goes through esc(). The search works without script.
5 */
6const shell = require('openvibe-shared/shell');
7const serve = require('openvibe-shared/serve');
8const { PLACES } = require('./elsewhere');
9
10// Our own files by their content (a release's change is a new address, so neither a browser nor Cloudflare keeps an old
11// one for the hour they may cache it): /x.css?v=<first 10 hex of its sha256>, read once at boot.
12const asset = (() => {
13 const fs = require('node:fs');
14 const crypto = require('node:crypto');
15 const kept = new Map();
16 return (name) => {
17 if (!kept.has(name)) {
18 let v = 'dev';
19 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 */ }
20 kept.set(name, `/${name}?v=${v}`);
21 }
22 return kept.get(name);
23 };
24})();
25
26const SITE = 'https://openvibe.markets';
27const esc = (v) => String(v == null ? '' : v).replace(/[&<>"']/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));
28const money = (n, currency = 'USD') => {
29 try { return new Intl.NumberFormat('en-US', { style: 'currency', currency }).format(n); } catch { return `${Number(n).toFixed(2)} ${currency}`; }
30};
31/** "as of 3 hours ago": a price is an observation, never "now". */
32function asOf(iso, now = Date.now()) {
33 const t = Date.parse(iso || '');
34 if (!Number.isFinite(t)) return 'time not stated';
35 const m = Math.max(0, Math.round((now - t) / 60000));
36 if (m < 60) return `as of ${m <= 1 ? 'a minute' : `${m} minutes`} ago`;
37 const h = Math.round(m / 60);
38 if (h < 48) return `as of ${h} hour${h === 1 ? '' : 's'} ago`;
39 return `as of ${new Date(t).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric', timeZone: 'UTC' })}`;
40}
41
42function 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' }) {
43 return shell.page({
44 name: 'OpenVibe.Markets', siteName: 'OpenVibe.Markets', title, description, canonical: `${SITE}${path}`, robots,
45 navbar: { service: 'markets', apiBase: 'https://openvibe.network', links: [{ label: 'Search', href: '/' }, { label: 'Deals', href: 'https://openvibe.deals/' }] },
46 footer: { service: 'markets', variant: 'full', mount: '#ov-footer' },
47 head: `<link rel="stylesheet" href="${serve.url('kit.css')}"><link rel="stylesheet" href="${asset('markets.css')}"><script src="${asset('markets.js')}" defer></script>`,
48 body: `<div id="navbar-mount"></div><main id="main" class="ovk-page mk">${body}</main>`,
49 });
50}
51
52const searchForm = (q = '') => `<form class="mk-search" method="get" action="/search" role="search">
53<label class="mk-search-box"><span class="mk-sr">What are you looking for?</span><input type="search" name="q" value="${esc(q)}" placeholder="Headphones, a sofa, a used road bike…" maxlength="100" autocomplete="off" required data-ov-trending="own"></label>
54<button class="mk-btn mk-btn--primary" type="submit">Search</button></form>`;
55
56/** Search by photo (v0.2): the photo is shrunk in the browser and sent once for OpenVibe Vision to name. */
57const photoForm = () => `<form class="mk-photo" method="post" action="/photo">
58<input type="hidden" name="image" value="" data-mk-image>
59<label class="mk-btn mk-photo-btn"><input type="file" accept="image/*" capture="environment" data-mk-photo class="mk-sr"><span>Search by photo</span></label>
60<span class="mk-photo-said" role="status" data-mk-photo-said>A photo of the thing itself works best.</span></form>`;
61
62function home({ photo = false, error = null } = {}) {
63 return page({
64 title: 'Every marketplace, one search', body: `
65<section class="mk-hero"><h1>Every marketplace, one search</h1>
66<p class="mk-lede">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.</p>
67${error ? `<p class="mk-error" role="alert">${esc(error)}</p>` : ''}${searchForm()}${photo ? photoForm() : ''}</section>
68<section class="mk-sec"><h2>Where it looks</h2><ul class="mk-places">${PLACES.map((p) => `<li><b>${esc(p.name)}</b><small>${esc(p.kind)}</small></li>`).join('')}</ul></section>
69<ul class="mk-promises">
70<li><b>Neutral.</b> A commission never raises a result. Every result says where it is from and when its price was seen.</li>
71<li><b>A price is an observation.</b> It is shown as of its time, never as "now".</li>
72<li><b>Nothing about you.</b> Searching needs no account, and your words are not kept. A photo is sent once to OpenVibe Vision to name what it shows; neither keeps the photo.</li>
73</ul>` });
74}
75
76function results({ q, deals, links, now = Date.now(), fromPhoto = false }) {
77 // A deal without a picture takes the whole width: no empty square.
78 const card = (r) => `<li class="mk-card${r.image ? '' : ' mk-card--text'}">${r.image ? `<img class="mk-card-img" src="${esc(r.image)}" alt="" loading="lazy" width="72" height="72">` : ''}
79<span class="mk-card-body"><a class="mk-card-title" href="${esc(r.url)}">${esc(r.title)}</a>
80<small>${esc(r.store || 'A store')} · ${esc(r.source)}${r.condition && r.condition !== 'new' ? ` · ${esc(r.condition)}` : ''}</small></span>
81<span class="mk-card-price">${r.price != null ? `<b>${esc(money(r.price, r.currency || 'USD'))}</b>${r.was != null && r.was > r.price ? `<s>${esc(money(r.was, r.currency || 'USD'))}</s>` : ''}<small>${esc(asOf(r.observed_at, now))}</small>
82<a class="mk-afford" href="https://openvibe.finance/ahead?cost=${encodeURIComponent(r.price.toFixed(2))}">Can I afford it?</a>` : '<small>price not stated</small>'}</span></li>`;
83 const found = deals.ok ? (deals.results.length
84 ? `<p class="mk-count">${deals.total} deal${deals.total === 1 ? '' : 's'} on OpenVibe Deals${deals.total > deals.results.length ? `, the newest ${deals.results.length} here (<a href="https://openvibe.deals/search?q=${encodeURIComponent(q)}">all of them</a>)` : ''}</p><ul class="mk-cards">${deals.results.map(card).join('')}</ul>`
85 : '<p class="mk-empty">No deal on OpenVibe Deals matches these words yet. The marketplaces below search their own listings.</p>')
86 : `<p class="mk-empty">${esc(deals.error || 'Deals could not be searched just now.')} The marketplaces below still work.</p>`;
87 return page({
88 title: `${q}: every marketplace`, path: `/search?q=${encodeURIComponent(q)}`, robots: 'noindex, follow', body: `
89<header class="mk-head">${searchForm(q)}${fromPhoto ? `<p class="mk-from-photo">From your photo: <b>${esc(q)}</b>. Not quite it? Change the words above.</p>` : ''}</header>
90<section class="mk-sec" aria-labelledby="mk-deals-h"><h2 id="mk-deals-h">Deals</h2>${found}
91<p class="mk-watch"><a href="https://openvibe.deals/watches?query=${encodeURIComponent(q)}">Alert me about new deals for “${esc(q)}”</a> <small>on OpenVibe Deals, when you are signed in</small></p></section>
92<section class="mk-sec" aria-labelledby="mk-else-h"><h2 id="mk-else-h">Search it on</h2>
93<ul class="mk-elsewhere">${links.map((l) => `<li><a href="${esc(l.url)}" target="_blank" rel="noopener noreferrer"><b>${esc(l.name)}</b><small>${esc(l.kind)}</small></a></li>`).join('')}</ul>
94<p class="mk-note">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.</p></section>` });
95}
96
97module.exports = { page, home, results, esc, money, asOf };
98