From db74a8865ea5d7f25b606fa6852cf695a4dacbc2 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Sun, 8 Nov 2026 16:22:18 +0100 Subject: [PATCH] feat(preview): post previews as Open Graph notes --- README.md | 6 ++-- test/preview.spec.ts | 65 +++++++++++++++++++++++++++++--------------- wasm/Cargo.lock | 2 +- wasm/Cargo.toml | 2 +- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 7f58307..cb997c3 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,13 @@ You need to generate a matrix id, using the algorithm as shown above. ## Link previews -`GET /_soliditas/preview_url?url=[url]` answers in the shape of the Matrix `/preview_url` endpoint. Missing `og:` tags are filled from `twitter:` tags, ``, `<meta name="description">` and oEmbed. Twitter/X links are read from the [FxTwitter API](https://github.com/FxEmbed/FxEmbed), Reddit and TikTok titles from their oEmbed endpoints, Tumblr posts without an image get the [fxtumblr](https://github.com/knuxify/fxtumblr) render, and Misskey-family notes show their images from the ActivityPub object unless marked sensitive, instead of the author's avatar. +`GET /_soliditas/preview_url?url=[url]` answers in the shape of the Matrix `/preview_url` endpoint. Missing `og:` tags are filled from `twitter:` tags, `<title>`, `<meta name="description">` and oEmbed. Twitter/X links are read from the [FxTwitter API](https://github.com/FxEmbed/FxEmbed), Bluesky posts from their author's PDS, Reddit and TikTok titles from their oEmbed endpoints, Tumblr posts without an image get the [fxtumblr](https://github.com/knuxify/fxtumblr) render, and fediverse posts are read from their ActivityPub object. The scraper is the Rust crate `sable-link-preview`, which the Sable client also runs natively. `wasm/` compiles it for the worker, and `pnpm build:wasm` (run before `test`, `dev` and `deploy`) needs Rust with the `wasm32-unknown-unknown` target and `wasm-bindgen-cli` 0.2.130. -`og:image` is returned as `mxc://[servername]/og_[base64url of the image url]`. When a post has more than one image, `com.sable.images` lists up to ten of them as `{ url, width, height }`, and `og:image` stays the single image other clients show (the FxTwitter mosaic for tweets). +Posts from X, Bluesky, the fediverse, Tumblr, Reddit and TikTok, and pages that declare `og:type` `note`, are answered with `og:type` `note` and Sable's `note:` Open Graph keys: the author, text, counts, content warning, every image and video (`og:image`, then `og:image:1`, `og:image:2`...) and the quoted post under `related:1:`, described in the `sable-link-preview` README. + +`og:image` is returned as `mxc://[servername]/og_[base64url of the image url]`. When a page has more than one image, `com.sable.images` lists up to ten of them as `{ url, width, height }`, and `og:image` stays the single image other clients show. ## License diff --git a/test/preview.spec.ts b/test/preview.spec.ts index b15645c..8eb5ae7 100644 --- a/test/preview.spec.ts +++ b/test/preview.spec.ts @@ -287,6 +287,8 @@ describe('preview_url fallbacks', () => { 'og:title': 'From the pics community on Reddit', 'og:description': 'test post please ignore', 'og:site_name': 'reddit', + 'og:type': 'note', + 'note:content': 'test post please ignore', }); }); @@ -300,6 +302,9 @@ describe('preview_url fallbacks', () => { 'og:description': 'caption', 'og:site_name': 'TikTok', 'com.sable.author_name': 'Scout', + 'og:type': 'note', + 'note:author': 'Scout', + 'note:content': 'caption', }); }); }); @@ -326,11 +331,17 @@ describe('preview_url for tweets', () => { 'og:image': `mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/media/a.jpg', 'og_')}`, 'og:image:width': 3000, 'og:image:height': 2000, + 'og:type': 'note', + 'note:author': 'NASA', + 'note:author:handle': 'NASA', + 'note:author:image': `mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/avatar.jpg', 'og_')}`, + 'note:author:url': 'https://x.com/NASA', + 'note:content': 'Liftoff', }); expect(fetchSpy).toHaveBeenCalledTimes(1); }); - it('uses the mosaic as the main image and lists every photo', async () => { + it('numbers every photo after the first and keeps the avatar out of og:image', async () => { serveRoutes({ 'https://api.fxtwitter.com/status/1': () => json({ @@ -338,7 +349,6 @@ describe('preview_url for tweets', () => { author, media: { photos: [{ url: 'https://pbs.twimg.com/a.jpg', width: 10, height: 20 }, { url: 'https://pbs.twimg.com/b.jpg' }], - mosaic: { formats: { jpeg: 'https://mosaic.example/1' } }, }, }, }), @@ -346,15 +356,14 @@ describe('preview_url for tweets', () => { }); expect((await (await preview('https://twitter.com/NASA/status/1')).json()) as object).toMatchObject({ - 'og:image': `mxc://gifs.example/${toMatrixID('https://mosaic.example/1', 'og_')}`, - 'com.sable.images': [ - { url: `mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/a.jpg', 'og_')}`, width: 10, height: 20 }, - { url: `mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/b.jpg', 'og_')}` }, - ], + 'og:image': `mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/a.jpg', 'og_')}`, + 'og:image:width': 10, + 'og:image:height': 20, + 'og:image:1': `mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/b.jpg', 'og_')}`, }); const single = (await (await preview('https://fixupx.com/NASA/status/2')).json()) as Record<string, unknown>; - expect(single['og:image']).toBe(`mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/avatar.jpg', 'og_')}`); - expect(single).not.toHaveProperty('com.sable.images'); + expect(single).not.toHaveProperty('og:image'); + expect(single['note:author:image']).toBe(`mxc://gifs.example/${toMatrixID('https://pbs.twimg.com/avatar.jpg', 'og_')}`); }); it('falls back to the page when the api has nothing', async () => { @@ -374,33 +383,39 @@ describe('preview_url for fediverse posts', () => { }); } - it('takes the image of a misskey note from its activity', async () => { + it('reads a misskey note from its activity', async () => { serveNote({ + type: 'Note', + content: '<p>hi</p>', attachment: [ { mediaType: 'image/webp', url: 'https://files.example/a.webp', width: 800, height: 600 }, - { mediaType: 'video/mp4', url: 'https://files.example/v.mp4' }, + { mediaType: 'audio/mpeg', url: 'https://files.example/a.mp3' }, { mediaType: 'image/png', url: 'https://files.example/b.png' }, ], }); - const a = `mxc://gifs.example/${toMatrixID('https://files.example/a.webp', 'og_')}`; expect(await (await preview('https://misskey.example/notes/n1')).json()).toEqual({ 'og:title': 'Ann (@ann)', - 'og:image': a, + 'og:description': 'hi', + 'og:type': 'note', + 'note:content': 'hi', + 'og:image': `mxc://gifs.example/${toMatrixID('https://files.example/a.webp', 'og_')}`, + 'og:image:type': 'image/webp', 'og:image:width': 800, 'og:image:height': 600, - 'com.sable.images': [ - { url: a, width: 800, height: 600 }, - { url: `mxc://gifs.example/${toMatrixID('https://files.example/b.png', 'og_')}` }, - ], + 'og:image:1': `mxc://gifs.example/${toMatrixID('https://files.example/b.png', 'og_')}`, + 'og:image:1:type': 'image/png', }); }); - it('keeps the avatar for a sensitive note', async () => { - serveNote({ sensitive: true, attachment: [{ mediaType: 'image/webp', url: 'https://files.example/a.webp' }] }); + it('keeps the media of a sensitive note out of og:image', async () => { + serveNote({ type: 'Note', sensitive: true, attachment: [{ mediaType: 'image/webp', url: 'https://files.example/a.webp' }] }); - expect((await (await preview('https://misskey.example/notes/n1')).json()) as object).toMatchObject({ - 'og:image': `mxc://gifs.example/${toMatrixID('https://misskey.example/avatar.webp', 'og_')}`, + const answer = (await (await preview('https://misskey.example/notes/n1')).json()) as Record<string, unknown>; + expect(answer).not.toHaveProperty('og:image'); + expect(answer).toMatchObject({ + 'note:sensitivity': 'inappropriate', + 'og:image:1': `mxc://gifs.example/${toMatrixID('https://files.example/a.webp', 'og_')}`, }); }); @@ -423,6 +438,12 @@ describe('preview_url for tumblr posts', () => { expect(await (await preview('https://www.tumblr.com/staff/42/some-slug')).json()).toEqual({ 'og:title': 'Reblog by @staff', 'og:description': 'text', + 'og:site_name': 'Tumblr', + 'og:type': 'note', + 'note:author': 'staff', + 'note:author:handle': 'staff', + 'note:author:url': 'https://staff.tumblr.com', + 'note:content': 'text', 'og:image': `mxc://gifs.example/${toMatrixID(render, 'og_')}`, }); }); @@ -442,7 +463,7 @@ describe('preview_url for tumblr posts', () => { it('answers from fxtumblr when tumblr is unreachable', async () => { serveRoutes({ 'https://tpmblr.com/staff/42': () => html(`<meta property="og:title" content="staff"><meta property="og:image" content="${render}">`) }); - expect(await (await preview('https://tumblr.com/staff/42')).json()).toEqual({ + expect((await (await preview('https://tumblr.com/staff/42')).json()) as object).toMatchObject({ 'og:title': 'staff', 'og:image': `mxc://gifs.example/${toMatrixID(render, 'og_')}`, }); diff --git a/wasm/Cargo.lock b/wasm/Cargo.lock index 6fbc576..bb9f855 100644 --- a/wasm/Cargo.lock +++ b/wasm/Cargo.lock @@ -768,7 +768,7 @@ checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" [[package]] name = "sable-link-preview" version = "0.1.0" -source = "git+https://git.sable.moe/SableClient/link-preview.git?rev=931c614c21534edb570fafcee6c4e1ec0f502fac#931c614c21534edb570fafcee6c4e1ec0f502fac" +source = "git+https://git.sable.moe/SableClient/link-preview.git?rev=46c32b3872bf242721587f249679e37932de69b8#46c32b3872bf242721587f249679e37932de69b8" dependencies = [ "futures-util", "lol_html", diff --git a/wasm/Cargo.toml b/wasm/Cargo.toml index 102fbd8..1c549a4 100644 --- a/wasm/Cargo.toml +++ b/wasm/Cargo.toml @@ -9,7 +9,7 @@ publish = false crate-type = ["cdylib"] [dependencies] -sable-link-preview = { git = "https://git.sable.moe/SableClient/link-preview.git", rev = "931c614c21534edb570fafcee6c4e1ec0f502fac", features = ["scrape"] } +sable-link-preview = { git = "https://git.sable.moe/SableClient/link-preview.git", rev = "46c32b3872bf242721587f249679e37932de69b8", features = ["scrape"] } serde_json = "1" url = "2" wasm-bindgen = "=0.2.130"