Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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, `<title>`, `<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

Expand Down
65 changes: 43 additions & 22 deletions test/preview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
});

Expand All @@ -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',
});
});
});
Expand All @@ -326,35 +331,39 @@ 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({
tweet: {
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' } },
},
},
}),
'https://api.fxtwitter.com/status/2': () => json({ tweet: { author, text: 'hi' } }),
});

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 () => {
Expand All @@ -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_')}`,
});
});

Expand All @@ -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_')}`,
});
});
Expand All @@ -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_')}`,
});
Expand Down
2 changes: 1 addition & 1 deletion wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading