aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/messages.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-10-13 05:47:23 +0000
committerAdam Soltys <[email protected]>2026-10-13 05:47:23 +0000
commit4e1cd666a06c4ee852586986d0301278d680a614 (patch)
tree73c8081423bc6e53e1f2d205e62d16d834fc8d90 /src/features/messages.js
parenta6ed8151a08f540a68fd5362884e4f9b888bca69 (diff)
downloadcoinosv3-4e1cd666a06c4ee852586986d0301278d680a614.tar.gz
coinosv3-4e1cd666a06c4ee852586986d0301278d680a614.zip
Feed: NIP-71 video events are posts, with their clips. Adam asked whether a feed could ask for just the posts with videos; for notes it can't (a relay filters by kind, author, time and one-letter tags, never by what a post says, and imeta isn't indexed), but NIP-71 gives clips kinds of their own. Of the 900 people he follows, 58 have posted 665 of them (191 kind 21, 33 kind 22, 170 kind 34235, 271 kind 34236 — divine.video's), 12 in the last fortnight next to 220 notes linking a video. Those kinds are now asked for with the notes (FEED_KINDS: feeds, profiles, topics, live), and drawn as posts: the title, the words, and the clip from its imeta — of the versions on offer, the one nearest 1080 on its short side (not the 4K original), never an HLS playlist, its url tag as a fallback — with the imeta's poster and size. A file whose name doesn't say it's a clip (divine.video's have no extension) plays when an imeta types it video/* or it's a video event's file, in a note as much as a video event. An addressable clip (34235/34236) shows once, not once per version. The clips are in the pager and the media grid's Videos (their captions: title and words, no file link); replies are NIP-22 comments, which threads already fetch. tools/video-events-test.js (the kinds asked for; a kind 21 as a post with its 720p version and poster, not "kind 21"; a 34236 with an extensionless file and its older version once; a note's imeta-typed file; the pager over all three with the caption).
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01KPw21NEpWeTGu6swVASmBt
Diffstat (limited to 'src/features/messages.js')
-rw-r--r--src/features/messages.js79
1 files changed, 63 insertions, 16 deletions
diff --git a/src/features/messages.js b/src/features/messages.js
index f4e01b7..42e2839 100644
--- a/src/features/messages.js
+++ b/src/features/messages.js
@@ -536,8 +536,13 @@ export function messagesFeature(ctx) {
// copies go there too, so other clients reading that list see them.
let ownInbox = [];
const toOwnInbox = (wrap) => { if (ownInbox.length) publishOn(ownInbox, wrap, { onauth: relayAuth }); };
- // what a feed or a profile lists: notes, and polls (NIP-88)
- const FEED_KINDS = [1, 1068];
+ // NIP-71 video events: a clip posted as itself — normal (21) and short,
+ // vertical (22), and the addressable 34235/34236 that divine.video and
+ // others publish. The file is in an imeta tag, not the text.
+ const VIDEO_KINDS = [21, 22, 34235, 34236];
+ const isVideoEvent = (ev) => !!ev && VIDEO_KINDS.includes(ev.kind);
+ // what a feed or a profile lists: notes, polls (NIP-88) and video events
+ const FEED_KINDS = [1, 1068, ...VIDEO_KINDS];
// A repost (NIP-18 kind 6) of someone's note shows in a feed of the
// people who reposted it, as the note itself under "<name> reposted".
// Only reposts that carry the note (every major client embeds it) and
@@ -4091,7 +4096,7 @@ export function messagesFeature(ctx) {
const editOf = (ev) => (ev && ev.id && ev.pubkey && (ev.kind === 1 || ev.kind === 1111)
? postEdits().ed.get(editKey(ev.id, ev.pubkey)) : undefined);
// what a post says now, and the emoji its words may use
- const noteText = (ev) => { const e = editOf(ev); return e ? e.c : ev.content; };
+ const noteText = (ev) => { if (isVideoEvent(ev)) return videoEventText(ev); const e = editOf(ev); return e ? e.c : ev.content; };
const noteEmojiTags = (ev) => { const e = editOf(ev); return emojiTagMap(e ? [...ev.tags, ...e.tags] : ev.tags); };
function notePostEdit(ev) {
const ids = (ev.tags || []).filter((x) => x[0] === 'e' && /^[0-9a-f]{64}$/.test(x[1] || '')).map((x) => x[1]);
@@ -6283,6 +6288,43 @@ export function messagesFeature(ctx) {
// moov-first file — with a short cap, since it gates the row. Remembered
// on disk so a clip seen once is never probed again.
const VIDEO_RE = /\.(mp4|webm|mov|m4v)(\?[^\s]*)?$/i;
+ // files known to be clips without saying so in their name (divine.video's
+ // have no extension): an imeta tag's video/* type, or a video event's file
+ const videoUrls = new Set();
+ const isVideoUrl = (url) => VIDEO_RE.test(url) || videoUrls.has(url);
+ const imetaFields = (tag) => { const f = {}; for (const p of tag.slice(1)) { const i = typeof p === 'string' ? p.indexOf(' ') : -1; if (i > 0) f[p.slice(0, i)] = p.slice(i + 1); } return f; };
+ // A video event's file: of the versions its imeta tags offer, the one
+ // nearest 1080 on its short side (a phone's screen; not the 4K original),
+ // never an HLS playlist; else its url tag.
+ function videoEventFile(ev) {
+ let best = null;
+ for (const tag of ev.tags || []) {
+ if (tag[0] !== 'imeta') continue;
+ const f = imetaFields(tag);
+ if (!/^https?:\/\//.test(f.url || '') || /mpegurl/i.test(f.m || '') || /\.m3u8(\?|$)/i.test(f.url)) continue;
+ if (f.m && !/^video\//.test(f.m)) continue;
+ const dm = /^(\d+)(?:\.0+)?x(\d+)/.exec(f.dim || '');
+ const edge = dm ? Math.min(+dm[1], +dm[2]) : 0;
+ const score = !edge ? -400 : -Math.abs(edge - 1080) - (edge > 1080 ? 1 : 0); // nearest 1080; the smaller on a tie
+ if (!best || score > best.score) best = { url: f.url, image: f.image, score };
+ }
+ if (!best) { const u = (ev.tags.find((x) => x[0] === 'url') || [])[1]; if (/^https?:\/\//.test(u || '')) best = { url: u }; }
+ if (best) videoUrls.add(best.url);
+ return best;
+ }
+ // what a video event reads as: its title, its words, and its file (drawn
+ // as the clip it is)
+ function videoEventText(ev) {
+ learnVideoMeta(ev);
+ const f = videoEventFile(ev);
+ const title = String((ev.tags.find((x) => x[0] === 'title') || [])[1] || '').trim();
+ const words = String(ev.content || '').trim();
+ const parts = [];
+ if (title && title !== words && !words.startsWith(title)) parts.push(title);
+ if (words) parts.push(words);
+ if (f && !words.includes(f.url)) parts.push(f.url);
+ return parts.join('\n\n');
+ }
const AUDIO_RE = /\.(mp3|m4a|aac|ogg|oga|opus|wav|flac)(\?[^\s]*)?$/i;
const VIDEO_PROBE_MS = 3000, VIDEO_DIMS_KEEP = 400, VIDEO_DIMS_KEY = 'coinos-video-dims-v2'; // v2: turned clips stand upright
const videoDims = new Map(); // url -> { width, height }
@@ -6338,9 +6380,9 @@ export function messagesFeature(ctx) {
function learnVideoMeta(ev) {
for (const tag of ev?.tags || []) {
if (tag[0] !== 'imeta') continue;
- const f = {};
- for (const p of tag.slice(1)) { const i = typeof p === 'string' ? p.indexOf(' ') : -1; if (i > 0) f[p.slice(0, i)] = p.slice(i + 1); }
- if (!f.url || !(VIDEO_RE.test(f.url) || /^video\//.test(f.m || ''))) continue;
+ const f = imetaFields(tag);
+ if (!f.url || !(VIDEO_RE.test(f.url) || /^video\//.test(f.m || '') || (isVideoEvent(ev) && !f.m))) continue;
+ if (!VIDEO_RE.test(f.url)) videoUrls.add(f.url);
const have = videoInfo.get(f.url) || {};
const d = {};
if (!have.size && +f.size > 0) d.size = +f.size;
@@ -6608,7 +6650,7 @@ export function messagesFeature(ctx) {
for (const part of String(content || '').split(NOTE_SPLIT)) {
const md = part && MD_PARTS.exec(part);
const url = md ? md[3] : (/^https?:\/\//i.test(part || '') ? part : null);
- if (url && !(md && md[1]) && VIDEO_RE.test(url)) urls.push(url);
+ if (url && !(md && md[1]) && isVideoUrl(url)) urls.push(url);
}
return urls;
}
@@ -6784,10 +6826,13 @@ export function messagesFeature(ctx) {
// (`staged` holds the note's id too, for a repost)
const noteOf = (e) => (e.kind === REPOST ? repostInner(e) : e);
const notesIn = new Set([...c.notes, ...(c.deferred || [])].map((e) => noteOf(e)?.id).filter(Boolean));
+ // an addressable post (a 34235/34236 clip) once, however many versions of it come
+ for (const e of [...c.notes, ...(c.deferred || [])]) { const a = noteOf(e) && addrOf(noteOf(e)); if (a) notesIn.add(a); }
+ const addrNew = (e) => { const n = noteOf(e); const a = n && addrOf(n); return !a || (!notesIn.has(a) && !staged.has(a) && !!staged.add(a)); };
const fresh = (e) => { const n = noteOf(e); return !!n && !notesIn.has(n.id) && !staged.has(n.id); };
- const unstage = (e) => { staged.delete(e.id); const n = noteOf(e); if (n) staged.delete(n.id); };
+ const unstage = (e) => { staged.delete(e.id); const n = noteOf(e); if (n) { staged.delete(n.id); const a = addrOf(n); if (a) staged.delete(a); } };
let add = (evs || []).filter((e) => (e.kind === REPOST ? !!repostInner(e) && !hidden(repostInner(e)) && !reboosted(e) : FEED_KINDS.includes(e.kind) && !isReply(e)) && !hidden(e)
- && fresh(e) && !known.has(e.id) && !staged.has(e.id) && known.add(e.id) && staged.add(e.id) && staged.add(noteOf(e).id));
+ && fresh(e) && !known.has(e.id) && !staged.has(e.id) && addrNew(e) && known.add(e.id) && staged.add(e.id) && staged.add(noteOf(e).id));
if (!add.length) return false;
const def = feedDef(c.id);
if (def && def.curated) {
@@ -8080,7 +8125,7 @@ export function messagesFeature(ctx) {
return x >= 0 && x <= w && y >= 0 && y <= hh;
}
function urlNode(url, { label = null, isImage = false } = {}) {
- if (VIDEO_RE.test(url)) {
+ if (isVideoUrl(url)) {
return videoNode(url, { stable: !!feedPaint });
}
if (AUDIO_RE.test(url)) return audioNode(url, label);
@@ -9856,7 +9901,7 @@ export function messagesFeature(ctx) {
// An event of a kind this app has no renderer for (a chess game, kind
// 64, reached through a reaction to it) is shown as what its author's
// client said it is (NIP-31 alt), never as raw content dressed as a post.
- const foreign = ev.kind !== 1 && ev.kind !== ARTICLE && ev.kind !== POLL && ev.kind !== COMMENT && !zapRec;
+ const foreign = ev.kind !== 1 && ev.kind !== ARTICLE && ev.kind !== POLL && ev.kind !== COMMENT && !isVideoEvent(ev) && !zapRec;
const altText = foreign ? ((ev.tags.find((x) => x[0] === 'alt') || [])[1] || t('noteForeignKind', { kind: ev.kind })) : null;
const isReply = ev.kind === COMMENT || (!foreign && !zapRec && ev.tags.some((x) => x[0] === 'e'));
const canZap = canZapPk(pk);
@@ -11346,7 +11391,9 @@ export function messagesFeature(ctx) {
// for more picks up where it stopped instead of re-reading the relays.
const mediaIdx = new Map(); // key -> { key, images: Map, videos: Map, checked: Set, until, end, loaded }
const mediaIdxKey = (feedId) => (mePk() || 'visitor') + '|' + feedId;
- const hasMediaOf = (ev, kind) => ev.kind === REPOST ? false : kind === 'videos' ? noteVideoUrls(ev.content).length > 0
+ // a post's clips: a video event's file, or the clips linked in a note
+ const clipUrlsOf = (ev) => (isVideoEvent(ev) ? [videoEventFile(ev)?.url].filter(Boolean) : noteVideoUrls(ev.content));
+ const hasMediaOf = (ev, kind) => ev.kind === REPOST ? false : kind === 'videos' ? clipUrlsOf(ev).length > 0
: noteMediaUrls(ev.content).some((u) => !u.startsWith('https://i.ytimg.com/'));
function mediaIndex(feedId) {
const key = mediaIdxKey(feedId);
@@ -11723,7 +11770,7 @@ export function messagesFeature(ctx) {
// a grid shows the picture itself — no warning to fold it behind
if (curated ? notForPublic(ev) : modOn('sensitive') && sensitiveWhy(ev) !== null && !isMe(ev.pubkey)) continue;
if (kind === 'videos') learnVideoMeta(ev);
- const urls = kind === 'videos' ? noteVideoUrls(ev.content).filter(verticalClip)
+ const urls = kind === 'videos' ? clipUrlsOf(ev).filter(verticalClip)
: noteMediaUrls(ev.content).filter((u) => !u.startsWith('https://i.ytimg.com/'));
for (const url of urls) {
const it = { ev, url };
@@ -11745,7 +11792,7 @@ export function messagesFeature(ctx) {
const ev = row.kind === REPOST ? repostInner(row) || row : row;
if (modOn('sensitive') && sensitiveWhy(ev) !== null && !isMe(ev.pubkey)) continue;
if (kind === 'videos') learnVideoMeta(ev);
- const urls = kind === 'videos' ? noteVideoUrls(ev.content)
+ const urls = kind === 'videos' ? clipUrlsOf(ev)
: noteMediaUrls(ev.content).filter((u) => !u.startsWith('https://i.ytimg.com/'));
for (const url of urls) out.push({ ev, url, row });
}
@@ -11832,9 +11879,9 @@ export function messagesFeature(ctx) {
}
// The words of the post, without the links to the media it carries.
function mediaCaption(ev) {
- let s = String(ev.content || '');
+ let s = isVideoEvent(ev) ? videoEventText(ev) : String(ev.content || '');
s = s.replace(new RegExp(MD_LINK, 'gi'), (m) => (MD_PARTS.exec(m)?.[1] ? '' : m));
- for (const u of [...noteMediaUrls(ev.content), ...noteVideoUrls(ev.content)]) s = s.split(u).join('');
+ for (const u of [...noteMediaUrls(s), ...noteVideoUrls(s)]) s = s.split(u).join('');
return plainExcerpt(s);
}
function setMediaMode(m) {