0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 1 | use std::{ |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 2 | collections::{HashMap, VecDeque}, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 3 | sync::{ |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 4 | Arc, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 5 | atomic::{AtomicBool, Ordering}, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 6 | }, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 7 | time::Duration, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 8 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 9 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 10 | use futures_util::{FutureExt, future::WeakShared}; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 11 | use matrix_sdk::{ |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 12 | Client, HttpError, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 13 | ruma::api::{ |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 14 | Metadata, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 15 | client::{authenticated_media, media}, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 16 | }, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 17 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 18 | use ruma::{ |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 19 | api::error::ErrorKind as ApiErrorKind, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 20 | serde::{JsonObject, Raw}, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 21 | time::Instant, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 22 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 23 | use tokio::sync::Mutex; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 24 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 25 | use super::executor::EngineBoxFuture; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 26 | use crate::{Error, LinkPreview, normalise::server_preview}; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 27 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 28 | const HIT_FOR: Duration = Duration::from_secs(60 * 60); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 29 | const MISS_FOR: Duration = Duration::from_secs(10 * 60); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 30 | const REMEMBERED: usize = 512; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 31 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 32 | type Lookup = EngineBoxFuture<'static, Result<Option<LinkPreview>, Error>>; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 33 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 34 | /// the homeserver rate limits `preview_url` and a busy timeline repeats links, so answers are |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 35 | /// kept a while, misses too, and asks for one url at once share one request |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 36 | pub(crate) struct UrlPreviews(Arc<Inner>); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 37 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 38 | struct Inner { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 39 | client: Client, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 40 | /// a server that doesn't preview at all says so once and isn't asked again |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 41 | unsupported: AtomicBool, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 42 | cache: Mutex<Cache>, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 43 | lookups: Mutex<HashMap<String, WeakShared<Lookup>>>, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 44 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 45 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 46 | #[derive(Default)] |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 47 | struct Cache { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 48 | entries: HashMap<String, (Instant, Option<LinkPreview>)>, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 49 | order: VecDeque<String>, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 50 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 51 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 52 | impl UrlPreviews { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 53 | pub(crate) fn new(client: Client) -> Self { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 54 | Self(Arc::new(Inner { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 55 | client, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 56 | unsupported: AtomicBool::new(false), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 57 | cache: Mutex::new(Cache::default()), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 58 | lookups: Mutex::new(HashMap::new()), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 59 | })) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 60 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 61 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 62 | pub(crate) async fn preview(&self, url: &str) -> Result<Option<LinkPreview>, Error> { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 63 | if self.0.unsupported.load(Ordering::Relaxed) { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 64 | return Ok(None); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 65 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 66 | if let Some(preview) = self.0.cache.lock().await.get(url) { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 67 | return Ok(preview); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 68 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 69 | let lookup = { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 70 | let mut lookups = self.0.lookups.lock().await; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 71 | lookups.retain(|_, lookup| lookup.upgrade().is_some()); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 72 | match lookups.get(url).and_then(WeakShared::upgrade) { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 73 | Some(lookup) => lookup, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 74 | None => { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 75 | let inner = self.0.clone(); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 76 | let owned = url.to_owned(); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 77 | let lookup: Lookup = Box::pin(async move { inner.look_up(owned).await }); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 78 | let lookup = lookup.shared(); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 79 | if let Some(weak) = lookup.downgrade() { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 80 | lookups.insert(url.to_owned(), weak); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 81 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 82 | lookup |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 83 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 84 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 85 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 86 | lookup.await |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 87 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 88 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 89 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 90 | impl Inner { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 91 | async fn look_up(&self, url: String) -> Result<Option<LinkPreview>, Error> { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 92 | let data = match self.fetch(&url).await { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 93 | Ok(data) => data, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 94 | Err(error) => { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 95 | let Some(api) = error.as_client_api_error() else { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 96 | return Err(Error::from_sdk( |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 97 | "asking for a url preview failed", |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 98 | error.into(), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 99 | )); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 100 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 101 | let unrecognised = matches!(api.error_kind(), Some(ApiErrorKind::Unrecognized)) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 102 | || api.status_code.as_u16() == 405; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 103 | if unrecognised { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 104 | self.unsupported.store(true, Ordering::Relaxed); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 105 | return Ok(None); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 106 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 107 | // synapse answers 500 for a page it can't download, 502 when dns fails and 403 for |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 108 | // its blocklist (synapse/media/url_previewer.py). a real fault hides in the 500s too, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 109 | // but a missing card is cheap and only cached ten minutes |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 110 | if !matches!(api.status_code.as_u16(), 400 | 403 | 404 | 500 | 502) { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 111 | return Err(Error::from_sdk( |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 112 | "asking for a url preview failed", |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 113 | error.into(), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 114 | )); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 115 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 116 | None |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 117 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 118 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 119 | let preview = data.and_then(|data| server_preview(&url, &data)); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 120 | self.cache.lock().await.insert(url, preview.clone()); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 121 | Ok(preview) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 122 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 123 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 124 | async fn fetch(&self, url: &str) -> Result<Option<Raw<JsonObject>>, HttpError> { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 125 | let versions = self.client.supported_versions().await?; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 126 | let data = if authenticated_media::get_media_preview::v1::Request::PATH_BUILDER |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 127 | .is_supported(&versions) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 128 | { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 129 | self.client |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 130 | .send(authenticated_media::get_media_preview::v1::Request::new( |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 131 | url.to_owned(), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 132 | )) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 133 | .await? |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 134 | .data |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 135 | } else { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 136 | #[allow(deprecated)] |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 137 | let request = media::get_media_preview::v3::Request::new(url.to_owned()); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 138 | self.client.send(request).await?.data |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 139 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 140 | Ok(data.map(Raw::from_json)) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 141 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 142 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 143 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 144 | impl Cache { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 145 | fn get(&mut self, url: &str) -> Option<Option<LinkPreview>> { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 146 | let (at, preview) = self.entries.get(url)?; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 147 | let lasts = if preview.is_some() { HIT_FOR } else { MISS_FOR }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 148 | (at.elapsed() < lasts).then(|| preview.clone()) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 149 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 150 | |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 151 | fn insert(&mut self, url: String, preview: Option<LinkPreview>) { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 152 | if self |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 153 | .entries |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 154 | .insert(url.clone(), (Instant::now(), preview)) |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 155 | .is_none() |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 156 | { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 157 | self.order.push_back(url); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 158 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 159 | while self.entries.len() > REMEMBERED { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 160 | let Some(oldest) = self.order.pop_front() else { |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 161 | break; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 162 | }; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 163 | self.entries.remove(&oldest); |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 164 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 165 | } |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 166 | } |