From 73c08943b8c8f5faeaa215e383bae172d9046a49 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 15 May 2022 01:48:26 +0200 Subject: feat(tazjin/tgsa): use permanent media links in bbcode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit uses the tgsa-proxied media links in the message bbcode, leading to stable image serving even if telegram swaps cdn addresses around, as long as their embed page is nĂ¥gorlunda the same. Change-Id: I50af1b3512d4e429fae4a2b3d10395664169e7a1 Reviewed-on: https://cl.tvl.fyi/c/depot/+/5611 Reviewed-by: tazjin Tested-by: BuildkiteCI --- users/tazjin/tgsa/src/main.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) (limited to 'users/tazjin/tgsa') diff --git a/users/tazjin/tgsa/src/main.rs b/users/tazjin/tgsa/src/main.rs index 1069dd83b2..0f3ccd8023 100644 --- a/users/tazjin/tgsa/src/main.rs +++ b/users/tazjin/tgsa/src/main.rs @@ -147,20 +147,38 @@ fn parse_tgmessage(embed: &str) -> Result { }) } +// create a permanent media url that tgsa can redirect if telegram +// changes its upstream links. +// +// assumes that tgsa lives at tgsa.tazj.in (which it does) +fn media_url(link: &TgLink, idx: usize) -> String { + format!( + "https://tgsa.tazj.in/img/{}/{}/{}", + link.username, link.message_id, idx + ) +} + fn to_bbcode(link: &TgLink, msg: &TgMessage) -> String { let mut out = String::new(); out.push_str(&format!("[quote=\"{}\"]\n", msg.author)); - for video in &msg.videos { + for video in 0..msg.videos.len() { out.push_str(&format!("[url=\"{}\"]", link.to_url())); - out.push_str(&format!("[img]{}[/img]", video)); + + // video thumbnail links are appended to the photos, hence the + // addition here + out.push_str(&format!( + "[img]{}[/img]", + media_url(link, video + msg.photos.len()) + )); + out.push_str("[/url]\n"); out.push_str("[sub](Click thumbnail to open video)[/sub]\n") } - for photo in &msg.photos { - out.push_str(&format!("[timg]{}[/timg]\n", photo)); + for photo in 0..msg.photos.len() { + out.push_str(&format!("[timg]{}[/timg]\n", media_url(link, photo))); } if msg.has_audio { -- cgit 1.4.1