neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
Rich links on posts: staff attach up to five, shown as cards
7ce364f · gmorell · 2026-12-03 06:23
Message
{commit_body(@commit)}
Files changed
modified
Cargo.lock
+74
−0
| 873 | 873 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 874 | 874 | checksum = "11119743ad110e8c1bdccd930d7f5c30c99e5fc76a7b63ec9807e84eef0c5f59" |
| 875 | 875 | dependencies = [ |
| 876 | + "cssparser-macros", | |
| 876 | 877 | "dtoa-short", |
| 877 | 878 | "itoa", |
| 879 | + "phf", | |
| 878 | 880 | "smallvec", |
| 879 | 881 | ] |
| 880 | 882 | |
| 883 | +[[package]] | |
| 884 | +name = "cssparser-macros" | |
| 885 | +version = "0.7.1" | |
| 886 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 887 | +checksum = "d045de693cb712d0b22c6a64be5b953f67b3ce00ab5ad3dd5d8b441886ab8e1a" | |
| 888 | +dependencies = [ | |
| 889 | + "quote", | |
| 890 | + "syn 3.0.10", | |
| 891 | +] | |
| 892 | + | |
| 881 | 893 | [[package]] |
| 882 | 894 | name = "ctr" |
| 883 | 895 | version = "0.9.2" |
| 1153 | 1165 | "zeroize", |
| 1154 | 1166 | ] |
| 1155 | 1167 | |
| 1168 | +[[package]] | |
| 1169 | +name = "ego-tree" | |
| 1170 | +version = "0.11.1" | |
| 1171 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 1172 | +checksum = "7bd50d24b1b75aa817bb0339cb9f2e0e664bdfab1e160a69a0fda9145b77fab2" | |
| 1173 | + | |
| 1156 | 1174 | [[package]] |
| 1157 | 1175 | name = "either" |
| 1158 | 1176 | version = "1.19.0" |
| 2783 | 2801 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 2784 | 2802 | checksum = "010378780309880b08997fae13be7834dba947d36393bd372f2b1556deb2a2f6" |
| 2785 | 2803 | dependencies = [ |
| 2804 | + "phf_macros", | |
| 2786 | 2805 | "phf_shared", |
| 2787 | 2806 | "serde", |
| 2788 | 2807 | ] |
| 2807 | 2826 | "phf_shared", |
| 2808 | 2827 | ] |
| 2809 | 2828 | |
| 2829 | +[[package]] | |
| 2830 | +name = "phf_macros" | |
| 2831 | +version = "0.14.0" | |
| 2832 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 2833 | +checksum = "5fa8d0ca26d424d27630da600c6624696e7dec8bf7b3b492b383c5dc49e5e085" | |
| 2834 | +dependencies = [ | |
| 2835 | + "phf_generator", | |
| 2836 | + "phf_shared", | |
| 2837 | + "proc-macro2", | |
| 2838 | + "quote", | |
| 2839 | + "syn 2.0.119", | |
| 2840 | +] | |
| 2841 | + | |
| 2810 | 2842 | [[package]] |
| 2811 | 2843 | name = "phf_shared" |
| 2812 | 2844 | version = "0.14.0" |
| 3286 | 3318 | "neiam-auth", |
| 3287 | 3319 | "pulldown-cmark", |
| 3288 | 3320 | "qrcode", |
| 3321 | + "reqwest", | |
| 3322 | + "scraper", | |
| 3289 | 3323 | "sea-orm", |
| 3290 | 3324 | "serde", |
| 3291 | 3325 | "serde_json", |
| 3581 | 3615 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 3582 | 3616 | checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" |
| 3583 | 3617 | |
| 3618 | +[[package]] | |
| 3619 | +name = "scraper" | |
| 3620 | +version = "0.29.0" | |
| 3621 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 3622 | +checksum = "6844b5f49a5579f12ba4f89d9f7eec6d71889e4aa0bc7181821040ce66f08fff" | |
| 3623 | +dependencies = [ | |
| 3624 | + "cssparser", | |
| 3625 | + "ego-tree", | |
| 3626 | + "getopts", | |
| 3627 | + "html5ever", | |
| 3628 | + "precomputed-hash", | |
| 3629 | + "selectors", | |
| 3630 | +] | |
| 3631 | + | |
| 3584 | 3632 | [[package]] |
| 3585 | 3633 | name = "sea-bae" |
| 3586 | 3634 | version = "0.2.2" |
| 3759 | 3807 | "zeroize", |
| 3760 | 3808 | ] |
| 3761 | 3809 | |
| 3810 | +[[package]] | |
| 3811 | +name = "selectors" | |
| 3812 | +version = "0.41.0" | |
| 3813 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 3814 | +checksum = "8322aebea94a6cc288ff884e8a49bdbceef60206c400c6c46d22e62fc4fd91a6" | |
| 3815 | +dependencies = [ | |
| 3816 | + "bitflags", | |
| 3817 | + "cssparser", | |
| 3818 | + "derive_more", | |
| 3819 | + "log", | |
| 3820 | + "new_debug_unreachable", | |
| 3821 | + "precomputed-hash", | |
| 3822 | + "rustc-hash", | |
| 3823 | + "servo_arc", | |
| 3824 | + "smallvec", | |
| 3825 | +] | |
| 3826 | + | |
| 3762 | 3827 | [[package]] |
| 3763 | 3828 | name = "semver" |
| 3764 | 3829 | version = "1.0.28" |
| 3894 | 3959 | "serde", |
| 3895 | 3960 | ] |
| 3896 | 3961 | |
| 3962 | +[[package]] | |
| 3963 | +name = "servo_arc" | |
| 3964 | +version = "0.5.0" | |
| 3965 | +source = "registry+https://github.com/rust-lang/crates.io-index" | |
| 3966 | +checksum = "b4eceb519ec34d8290aaf6e46f435102f2bfa63f22b05a144fb16ab266d3b198" | |
| 3967 | +dependencies = [ | |
| 3968 | + "stable_deref_trait", | |
| 3969 | +] | |
| 3970 | + | |
| 3897 | 3971 | [[package]] |
| 3898 | 3972 | name = "sha1" |
| 3899 | 3973 | version = "0.10.7" |
modified
Cargo.toml
+5
−0
| 67 | 67 | idna = "1" |
| 68 | 68 | # The lameness filter's compression signal (zlib; pure-Rust backend). |
| 69 | 69 | flate2 = "1" |
| 70 | +# Rich links on posts (src/link_cards.rs, src/fetch.rs): the background | |
| 71 | +# fetch of a staff-chosen page, and a real HTML parser for its <head> | |
| 72 | +# (party-slut's). Already in the tree via neiam-auth / mail-prism-client. | |
| 73 | +reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] } | |
| 74 | +scraper = "0.29" | |
| 70 | 75 | |
| 71 | 76 | # CLI (manage binary) |
| 72 | 77 | clap = { version = "4.5", features = ["derive"] } |
modified
README.md
+63
−0
| 214 | 214 | - Off-site images still load directly from their hosts. A caching image |
| 215 | 215 | proxy would hide readers' addresses from them; it isn't built. |
| 216 | 216 | |
| 217 | +### Rich links on posts | |
| 218 | + | |
| 219 | +Staff can attach up to 5 links to any post. Each one shows under the body as | |
| 220 | +a card: picture, title, description, site name and host, in a responsive row | |
| 221 | +on the Classic topic page, the Focus topic page and the Focus reader | |
| 222 | +(`src/link_cards.rs`, `templates/forum/_post_links.html`; ported from | |
| 223 | +party-slut's links on a part of an event). Only staff can do this. A post's | |
| 224 | +author can't, unless they're staff. | |
| 225 | + | |
| 226 | +- **Managing them.** A post's staff controls have a **Links** button. It | |
| 227 | + opens an inline panel (htmx) with the post's links, each with its status, | |
| 228 | + Refetch and Remove, and a field to attach another. A bare `example.com` | |
| 229 | + becomes `https://example.com/`. Only http(s) is accepted, and not with a | |
| 230 | + user name or password in it. The same address twice on one post is | |
| 231 | + refused, however it's written. Attaching and removing are in the | |
| 232 | + moderation log ("Attached link" / "Removed link", with the URL). | |
| 233 | +- **Fetching.** The page is never fetched while a page is shown, or in the | |
| 234 | + request that attached it. A background task reads it once (at most 2 at a | |
| 235 | + time across the process), and the row stays `pending` until that finishes. | |
| 236 | + On startup, any rows still `pending` (because a restart interrupted them) | |
| 237 | + are fetched. The task reads OpenGraph, then Twitter's tags, then `<title>` | |
| 238 | + and `meta description`, with a real HTML parser. The text is collapsed and | |
| 239 | + cut short. Favicons aren't fetched. A page that can't be read, or has | |
| 240 | + nothing to say, is `failed`, and its card shows the address and host. | |
| 241 | + While the panel is open with a pending link, it re-asks every 3 s, so the | |
| 242 | + card appears without a reload. Nothing is pushed to other readers; they | |
| 243 | + see the card on their next load. | |
| 244 | +- **The fence** (`src/fetch.rs`): | |
| 245 | + - Requests may only go to http(s) addresses on the public internet. | |
| 246 | + Loopback, private, link-local (cloud metadata), CGNAT, unique-local IPv6, | |
| 247 | + documentation and reserved ranges are refused, and so is IPv4 written as | |
| 248 | + IPv6. The check applies to literal addresses and to every address a name | |
| 249 | + resolves to. One private answer among public ones is enough to refuse. | |
| 250 | + - The connection goes to the addresses that were checked, so a name can't | |
| 251 | + answer one way to the check and another way to the connect (DNS | |
| 252 | + rebinding). | |
| 253 | + - Redirects are followed by hand, at most 4, and every hop is checked | |
| 254 | + again. | |
| 255 | + - Proxy settings from the environment are ignored. | |
| 256 | + - Each hop has a 10 s timeout. | |
| 257 | + - The size cap is enforced while reading: 1 MB of a page (cut there) and | |
| 258 | + the avatar limit for a picture (refused if larger). | |
| 259 | + - It sends a `recourse-link-preview/1.0 (+PUBLIC_URL)` User-Agent. | |
| 260 | +- **Pictures** are copied into Postgres beside the link, as avatars are, and | |
| 261 | + served from `/links/<id>/image`. They're never hotlinked. The accepted | |
| 262 | + types are the avatar ones (PNG, JPEG, GIF, WebP). The type is decided from | |
| 263 | + the bytes, not the other server's `Content-Type`, so SVG is never | |
| 264 | + accepted. Pictures aren't re-encoded or resized: the avatar path doesn't | |
| 265 | + do that either. The route sets `nosniff` and a sandboxing CSP. | |
| 266 | +- **Cards follow their post.** A deleted, hidden or held post has no cards | |
| 267 | + for anyone, on any layout or in the API, and its picture route answers | |
| 268 | + 404. Staff still see its links in the panel, with a note saying why | |
| 269 | + there's no card. Card links go through `/out` with | |
| 270 | + `rel="noopener noreferrer nofollow ugc"`. Cards sit on `base-100` inside | |
| 271 | + the post card, so they stay opaque on every theme and on the sea-glass | |
| 272 | + panels. | |
| 273 | +- **API / app.** Every post in `/api/v1` has a read-only `links` array with | |
| 274 | + `url`, `href` (the absolute `/out` link), `host`, `status`, `title`, | |
| 275 | + `description`, `site_name` and `image_url` (server-relative). The Android | |
| 276 | + post view shows them as cards. A tap goes through its "Leaving" dialog, and | |
| 277 | + it asks for the picture with the device token, since the route follows the | |
| 278 | + post's visibility. | |
| 279 | + | |
| 217 | 280 | ### Client addresses |
| 218 | 281 | |
| 219 | 282 | **What is collected:** the address an account used when it: |
modified
android/app/src/main/java/org/neiam/recourse/app/data/Models.kt
+20
−0
| 198 | 198 | @SerialName("can_react") val canReact: Boolean = true, |
| 199 | 199 | @SerialName("can_flag") val canFlag: Boolean = false, |
| 200 | 200 | val flagged: Boolean = false, |
| 201 | + /** Rich links staff attached, as cards (read-only; none on a hidden post). */ | |
| 202 | + val links: List<PostLink> = emptyList(), | |
| 203 | +) | |
| 204 | + | |
| 205 | +/** | |
| 206 | + * A rich link on a post (src/link_cards.rs). `status` is `pending` (not read | |
| 207 | + * yet), `ok`, or `failed` (show the URL and host). `imageUrl` is the | |
| 208 | + * server's own copy of the picture, server-relative like `avatar_url`. | |
| 209 | + */ | |
| 210 | +@Serializable | |
| 211 | +data class PostLink( | |
| 212 | + val id: String, | |
| 213 | + val url: String, | |
| 214 | + val href: String = url, | |
| 215 | + val host: String = "", | |
| 216 | + val status: String = "pending", | |
| 217 | + val title: String? = null, | |
| 218 | + val description: String? = null, | |
| 219 | + @SerialName("site_name") val siteName: String? = null, | |
| 220 | + @SerialName("image_url") val imageUrl: String? = null, | |
| 201 | 221 | ) |
| 202 | 222 | |
| 203 | 223 | @Serializable |
modified
android/app/src/main/java/org/neiam/recourse/app/ui/ReaderScreen.kt
+76
−0
| 23 | 23 | import androidx.compose.runtime.* |
| 24 | 24 | import androidx.compose.ui.Alignment |
| 25 | 25 | import androidx.compose.ui.Modifier |
| 26 | +import androidx.compose.ui.layout.ContentScale | |
| 26 | 27 | import androidx.compose.ui.platform.LocalContext |
| 27 | 28 | import androidx.compose.ui.text.font.FontWeight |
| 28 | 29 | import androidx.compose.ui.text.style.TextOverflow |
| 29 | 30 | import androidx.compose.ui.unit.dp |
| 31 | +import coil.compose.AsyncImage | |
| 32 | +import coil.request.ImageRequest | |
| 30 | 33 | import kotlinx.coroutines.launch |
| 31 | 34 | import org.neiam.recourse.app.data.* |
| 32 | 35 |
| 289 | 292 | } else { |
| 290 | 293 | HtmlText(post.cooked, Modifier.fillMaxWidth()) |
| 291 | 294 | } |
| 295 | + if (!post.hidden && post.links.isNotEmpty()) LinkCards(post.links) | |
| 292 | 296 | FlowRow(horizontalArrangement = Arrangement.spacedBy(6.dp)) { |
| 293 | 297 | post.reactions.filter { mayReact || it.count > 0 }.forEach { r -> |
| 294 | 298 | FilterChip( |
| 318 | 322 | } |
| 319 | 323 | } |
| 320 | 324 | |
| 325 | +/** | |
| 326 | + * A post's rich links, as the web's cards: picture (our server's copy), | |
| 327 | + * title, description, site and host. A tap goes through the same | |
| 328 | + * "Leaving recourse" dialog as a link in the body. | |
| 329 | + */ | |
| 330 | +@Composable | |
| 331 | +private fun LinkCards(links: List<PostLink>) { | |
| 332 | + val session = LocalSession.current | |
| 333 | + val context = LocalContext.current | |
| 334 | + var leaving by remember { mutableStateOf<LinkTarget.External?>(null) } | |
| 335 | + val open = { url: String -> | |
| 336 | + runCatching { context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url))) } | |
| 337 | + Unit | |
| 338 | + } | |
| 339 | + Column(verticalArrangement = Arrangement.spacedBy(8.dp)) { | |
| 340 | + links.forEach { link -> | |
| 341 | + OutlinedCard( | |
| 342 | + onClick = { | |
| 343 | + when (val t = Leaving.classify(link.url, session.creds.baseUrl)) { | |
| 344 | + is LinkTarget.External -> leaving = t | |
| 345 | + is LinkTarget.Internal -> open(t.url) | |
| 346 | + is LinkTarget.Other -> open(t.url) | |
| 347 | + } | |
| 348 | + }, | |
| 349 | + modifier = Modifier.fillMaxWidth(), | |
| 350 | + ) { | |
| 351 | + link.imageUrl?.let { path -> | |
| 352 | + AsyncImage( | |
| 353 | + // The picture route follows the post's visibility, | |
| 354 | + // so it is asked for as this device. | |
| 355 | + model = ImageRequest.Builder(context) | |
| 356 | + .data(session.url(path)) | |
| 357 | + .addHeader("Authorization", "Bearer ${session.creds.token}") | |
| 358 | + .build(), | |
| 359 | + imageLoader = session.imageLoader, | |
| 360 | + contentDescription = null, | |
| 361 | + contentScale = ContentScale.Crop, | |
| 362 | + modifier = Modifier.fillMaxWidth().height(120.dp), | |
| 363 | + ) | |
| 364 | + } | |
| 365 | + Column(Modifier.padding(12.dp), verticalArrangement = Arrangement.spacedBy(4.dp)) { | |
| 366 | + Text( | |
| 367 | + link.title ?: link.host.ifBlank { link.url }, | |
| 368 | + style = MaterialTheme.typography.titleSmall, | |
| 369 | + maxLines = 2, | |
| 370 | + overflow = TextOverflow.Ellipsis, | |
| 371 | + ) | |
| 372 | + val description = link.description ?: if (link.title == null) link.url else null | |
| 373 | + description?.let { | |
| 374 | + Text( | |
| 375 | + it, | |
| 376 | + style = MaterialTheme.typography.bodySmall, | |
| 377 | + color = MaterialTheme.colorScheme.onSurfaceVariant, | |
| 378 | + maxLines = 3, | |
| 379 | + overflow = TextOverflow.Ellipsis, | |
| 380 | + ) | |
| 381 | + } | |
| 382 | + Text( | |
| 383 | + listOfNotNull(link.siteName, link.host.ifBlank { null }).joinToString(" · ") + | |
| 384 | + if (link.status == "pending") " · reading…" else "", | |
| 385 | + style = MaterialTheme.typography.labelSmall, | |
| 386 | + color = MaterialTheme.colorScheme.onSurfaceVariant, | |
| 387 | + maxLines = 1, | |
| 388 | + overflow = TextOverflow.Ellipsis, | |
| 389 | + ) | |
| 390 | + } | |
| 391 | + } | |
| 392 | + } | |
| 393 | + } | |
| 394 | + leaving?.let { t -> LeavingDialog(t, onDismiss = { leaving = null }) { leaving = null; open(t.url) } } | |
| 395 | +} | |
| 396 | + | |
| 321 | 397 | /** The web's flag reasons (src/policy.rs FLAG_REASONS). */ |
| 322 | 398 | private val FLAG_REASONS = listOf( |
| 323 | 399 | "spam" to "Spam", |
modified
android/app/src/test/java/org/neiam/recourse/app/data/ContractTest.kt
+11
−0
Click to load diff…
modified
android/app/src/test/resources/api/bookmarks.json
+3
−3
Click to load diff…
modified
android/app/src/test/resources/api/categories.json
+277
−3904
Click to load diff…
modified
android/app/src/test/resources/api/category.json
+29
−29
Click to load diff…
modified
android/app/src/test/resources/api/category_staff_only.json
+13
−13
Click to load diff…
modified
android/app/src/test/resources/api/devices.json
+4
−4
Click to load diff…
modified
android/app/src/test/resources/api/error_silenced.json
+1
−1
Click to load diff…
modified
android/app/src/test/resources/api/me.json
+5
−5
Click to load diff…
modified
android/app/src/test/resources/api/me_silenced.json
+8
−8
Click to load diff…
modified
android/app/src/test/resources/api/notifications.json
+6
−6
Click to load diff…
modified
android/app/src/test/resources/api/post_held.json
+6
−5
Click to load diff…
modified
android/app/src/test/resources/api/reply.json
+6
−5
Click to load diff…
modified
android/app/src/test/resources/api/search.json
+48
−622
Click to load diff…
modified
android/app/src/test/resources/api/tag.json
+18
−18
Click to load diff…
modified
android/app/src/test/resources/api/tags.json
+4
−39
Click to load diff…
modified
android/app/src/test/resources/api/topic.json
+45
−20
Click to load diff…
modified
android/app/src/test/resources/api/topic_locked.json
+29
−26
Click to load diff…
modified
android/app/src/test/resources/api/topic_no_reactions.json
+14
−13
Click to load diff…
modified
migration/src/lib.rs
+2
−0
Click to load diff…
added
migration/src/m20240101_000026_post_links.rs
+51
−0
Click to load diff…
modified
src/api/forum.rs
+9
−2
Click to load diff…
modified
src/api/views.rs
+39
−0
Click to load diff…
modified
src/entities/mod.rs
+1
−0
Click to load diff…
added
src/entities/post_links.rs
+47
−0
Click to load diff…
added
src/fetch.rs
+482
−0
Click to load diff…
modified
src/handlers/forum.rs
+18
−0
Click to load diff…
modified
src/handlers/mod.rs
+1
−0
Click to load diff…
modified
src/handlers/moderation.rs
+2
−0
Click to load diff…
added
src/handlers/post_links.rs
+187
−0
Click to load diff…
modified
src/lib.rs
+2
−0
Click to load diff…
added
src/link_cards.rs
+668
−0
Click to load diff…
modified
src/main.rs
+2
−0
Click to load diff…
modified
src/moderation.rs
+3
−3
Click to load diff…
modified
src/web.rs
+6
−0
Click to load diff…
modified
templates/forum/_post.html
+3
−0
Click to load diff…
added
templates/forum/_post_links.html
+77
−0
Click to load diff…
modified
tests/api_db.rs
+25
−0
Click to load diff…
modified
tests/moderation_db.rs
+272
−0
Click to load diff…
modified
tests/templates.rs
+18
−0
Click to load diff…
Parents: 84bfe6e