about summary refs log tree commit diff
path: root/users
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-05-14T23·48+0200
committertazjin <tazjin@tvl.su>2022-05-15T00·01+0000
commit73c08943b8c8f5faeaa215e383bae172d9046a49 (patch)
treef9e9646563815ce71995f4ce5cb7dca70b913853 /users
parentb308361a73778c2921e7f657af434261a91c5a53 (diff)
feat(tazjin/tgsa): use permanent media links in bbcode r/4078
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 <tazjin@tvl.su>
Tested-by: BuildkiteCI
Diffstat (limited to 'users')
-rw-r--r--users/tazjin/tgsa/src/main.rs26
1 files changed, 22 insertions, 4 deletions
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<TgMessage> {
     })
 }
 
+// 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 {