fix(app): image viewer opens pictures at full size (#85)
Co-authored-by: Fülöp Márk <mark.fulop@blackcell.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@@ -9,6 +9,7 @@ use gpui_kit::prelude::FluentBuilder as _;
9
9
use gpui_kit::*;
10
10
11
11
use image::{Frame, ImageReader};
12
use teams_core::ImageRef;
12
13
13
14
use super::avatar::person_avatar;
14
15
use super::shortcuts::{ZoomIn, ZoomOut, ZoomReset};
@@ -58,6 +59,13 @@ struct RotatedImage {
58
59
render: Arc<RenderImage>,
59
60
}
60
61
62
/// The file last drawn for the picture at `index`, to notice when its full-size view replaces the inline one.
63
struct Shown {
64
index: usize,
65
key: String,
66
size: Option<(u32, u32)>,
67
}
68
61
69
#[derive(Clone, Copy)]
62
70
struct Layout {
63
71
stage: Pair,
@@ -72,11 +80,14 @@ pub struct ImageViewer {
72
80
conversation_id: String,
73
81
conversation_title: String,
74
82
items: Vec<GalleryItem>,
83
/// Per item, the full-resolution view fetched when it is opened; messages carry a compressed one.
84
full_sizes: Vec<Option<ImageRef>>,
75
85
index: usize,
76
86
scale: Option<f32>,
77
87
pan: Pair,
78
88
quarter_turns: u8,
79
89
rotated: Option<RotatedImage>,
90
shown: Option<Shown>,
80
91
entered_fullscreen: bool,
81
92
drag_from: Option<Point<Pixels>>,
82
93
notice: Option<String>,
@@ -209,11 +220,17 @@ impl ImageViewer {
209
220
conversation_id: request.conversation_id,
210
221
conversation_title: request.conversation_title,
211
222
index: request.index.min(request.items.len().saturating_sub(1)),
223
full_sizes: request
224
.items
225
.iter()
226
.map(|item| item.image.full_size())
227
.collect(),
212
228
items: request.items,
213
229
scale: None,
214
230
pan: (0., 0.),
215
231
quarter_turns: 0,
216
232
rotated: None,
233
shown: None,
217
234
entered_fullscreen: false,
218
235
drag_from: None,
219
236
notice: None,
@@ -249,12 +266,14 @@ impl ImageViewer {
249
266
.saturating_sub(1)
250
267
.max((self.index + 1).min(last_index));
251
268
let directory = &self.app.read(cx).directory;
269
let full_size = self.full_sizes.get(self.index).cloned().flatten();
252
270
let missing: Vec<_> = self
253
271
.items
254
272
.get(first..=last)
255
273
.unwrap_or_default()
256
274
.iter()
257
275
.map(|item| item.image.clone())
276
.chain(full_size)
258
277
.filter(|image| directory.image(&image.url).is_none())
259
278
.collect();
260
279
if !missing.is_empty() {
@@ -290,12 +309,61 @@ impl ImageViewer {
290
309
.is_some_and(|item| self.app.read(cx).directory.image_failed(&item.image.url))
291
310
}
292
311
293
fn current_path(&self, cx: &App) -> Option<PathBuf> {
312
/// Cache key of what is drawn for the current picture: its full-size view once downloaded, until then the inline one.
313
fn shown_key(&self, cx: &App) -> Option<&str> {
294
314
let item = self.current()?;
295
let entry = self.app.read(cx).directory.image(&item.image.url)?;
315
let directory = &self.app.read(cx).directory;
316
let full_size = self
317
.full_sizes
318
.get(self.index)
319
.and_then(Option::as_ref)
320
.map(|full| full.url.as_str())
321
.filter(|url| directory.image(url).is_some());
322
Some(full_size.unwrap_or(item.image.url.as_str()))
323
}
324
325
fn current_path(&self, cx: &App) -> Option<PathBuf> {
326
let entry = self.app.read(cx).directory.image(self.shown_key(cx)?)?;
296
327
Some(entry.path.clone())
297
328
}
298
329
330
/// Keeps the zoom and rotation when the full-size view replaces the inline one under them.
331
fn follow_full_size(&mut self, window: &mut Window, cx: &mut Context<Self>) {
332
let Some(key) = self.shown_key(cx).map(str::to_owned) else {
333
self.shown = None;
334
return;
335
};
336
let size = self
337
.app
338
.read(cx)
339
.directory
340
.image(&key)
341
.and_then(|entry| entry.size);
342
let previous = self.shown.replace(Shown {
343
index: self.index,
344
key: key.clone(),
345
size,
346
});
347
let Some(previous) = previous.filter(|shown| shown.index == self.index && shown.key != key)
348
else {
349
return;
350
};
351
let widths = previous
352
.size
353
.zip(size)
354
.map(|(before, after)| (before.0, after.0));
355
if let (Some(scale), Some((before, after))) = (self.scale, widths)
356
&& after > 0
357
{
358
self.scale = Some(scale * before as f32 / after as f32);
359
}
360
if self.quarter_turns != 0
361
&& let Some(path) = self.current_path(cx)
362
{
363
self.start_rotation(path, window, cx);
364
}
365
}
366
299
367
fn stage_size(window: &Window) -> Pair {
300
368
let viewport = window.viewport_size();
301
369
(
@@ -306,7 +374,7 @@ impl ImageViewer {
306
374
307
375
fn layout(&self, window: &Window, cx: &App) -> Option<Layout> {
308
376
let item = self.current()?;
309
let entry = self.app.read(cx).directory.image(&item.image.url)?;
377
let entry = self.app.read(cx).directory.image(self.shown_key(cx)?)?;
310
378
let stage = Self::stage_size(window);
311
379
let declared = item.image.width.zip(item.image.height);
312
380
let source = entry
@@ -442,47 +510,57 @@ impl ImageViewer {
442
510
}
443
511
444
512
fn rotate(&mut self, window: &mut Window, cx: &mut Context<Self>) {
445
let (Some(path), Some(item)) = (self.current_path(cx), self.current()) else {
513
let Some(path) = self.current_path(cx) else {
446
514
return;
447
515
};
448
let url = item.image.url.clone();
449
516
self.quarter_turns = next_rotation(self.quarter_turns);
450
517
self.scale = None;
451
518
self.pan = (0., 0.);
452
519
self.release_rotated(window);
520
if self.quarter_turns != 0 {
521
self.start_rotation(path, window, cx);
522
}
523
cx.notify();
524
}
525
526
/// Turns the file at `path` by the current rotation; the previous rotated picture stays until this one is ready.
527
fn start_rotation(&mut self, path: PathBuf, window: &mut Window, cx: &mut Context<Self>) {
528
let Some(item) = self.current() else {
529
return;
530
};
531
let url = item.image.url.clone();
453
532
let quarter_turns = self.quarter_turns;
454
if quarter_turns != 0 {
455
cx.spawn_in(window, async move |this, cx| {
456
let rotated = cx
457
.background_spawn(async move { rotated_render(&path, quarter_turns) })
458
.await;
459
this.update_in(cx, |this, window, cx| {
460
let still_current = this.quarter_turns == quarter_turns
461
&& this.current().is_some_and(|item| item.image.url == url);
462
if !still_current {
463
return;
533
cx.spawn_in(window, async move |this, cx| {
534
let source = path.clone();
535
let rotated = cx
536
.background_spawn(async move { rotated_render(&source, quarter_turns) })
537
.await;
538
this.update_in(cx, |this, window, cx| {
539
let still_current = this.quarter_turns == quarter_turns
540
&& this.current().is_some_and(|item| item.image.url == url)
541
&& this.current_path(cx).as_ref() == Some(&path);
542
if !still_current {
543
return;
544
}
545
match rotated {
546
Some(render) => {
547
this.release_rotated(window);
548
this.rotated = Some(RotatedImage {
549
url,
550
quarter_turns,
551
render: Arc::new(render),
552
});
464
553
}
465
match rotated {
466
Some(render) => {
467
this.release_rotated(window);
468
this.rotated = Some(RotatedImage {
469
url,
470
quarter_turns,
471
render: Arc::new(render),
472
});
473
}
474
None => {
475
this.quarter_turns = 0;
476
this.show_notice("Could not rotate", cx);
477
}
554
None => {
555
this.quarter_turns = 0;
556
this.show_notice("Could not rotate", cx);
478
557
}
479
cx.notify();
480
})
481
.ok();
558
}
559
cx.notify();
482
560
})
483
.detach();
484
}
485
cx.notify();
561
.ok();
562
})
563
.detach();
486
564
}
487
565
488
566
fn show_notice(&mut self, text: impl Into<String>, cx: &mut Context<Self>) {
@@ -798,15 +876,11 @@ impl ImageViewer {
798
876
fn picture(&self, layout: Layout, cx: &mut Context<Self>) -> AnyElement {
799
877
let display = display_size(layout.source, layout.scale);
800
878
let origin = image_origin(display, layout.stage, layout.pan);
801
let Some(item) = self.current() else {
879
if self.current().is_none() {
802
880
return div().into_any_element();
803
};
881
}
804
882
let source: Option<ImageSource> = if self.quarter_turns == 0 {
805
self.app
806
.read(cx)
807
.directory
808
.image(&item.image.url)
809
.map(|entry| entry.path.clone().into())
883
self.current_path(cx).map(Into::into)
810
884
} else {
811
885
self.rotated_render()
812
886
.map(|render| ImageSource::Render(render.clone()))
@@ -965,6 +1039,7 @@ fn loading_placeholder() -> AnyElement {
965
1039
966
1040
impl Render for ImageViewer {
967
1041
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
1042
self.follow_full_size(window, cx);
968
1043
let layout = self.layout(window, cx);
969
1044
let stage = Self::stage_size(window);
970
1045
let has_previous = self.index > 0;
@@ -9,6 +9,7 @@ use gpui_kit::prelude::FluentBuilder as _;9
use gpui_kit::*;9
use gpui_kit::*;10
10
11
use image::{Frame, ImageReader};11
use image::{Frame, ImageReader};12
use teams_core::ImageRef;12
13
13
use super::avatar::person_avatar;14
use super::avatar::person_avatar;14
use super::shortcuts::{ZoomIn, ZoomOut, ZoomReset};15
use super::shortcuts::{ZoomIn, ZoomOut, ZoomReset};@@ -58,6 +59,13 @@ struct RotatedImage {58
render: Arc<RenderImage>,59
render: Arc<RenderImage>,59
}60
}60
61
62
/// The file last drawn for the picture at `index`, to notice when its full-size view replaces the inline one.63
struct Shown {64
index: usize,65
key: String,66
size: Option<(u32, u32)>,67
}68
61
#[derive(Clone, Copy)]69
#[derive(Clone, Copy)]62
struct Layout {70
struct Layout {63
stage: Pair,71
stage: Pair,@@ -72,11 +80,14 @@ pub struct ImageViewer {72
conversation_id: String,80
conversation_id: String,73
conversation_title: String,81
conversation_title: String,74
items: Vec<GalleryItem>,82
items: Vec<GalleryItem>,83
/// Per item, the full-resolution view fetched when it is opened; messages carry a compressed one.84
full_sizes: Vec<Option<ImageRef>>,75
index: usize,85
index: usize,76
scale: Option<f32>,86
scale: Option<f32>,77
pan: Pair,87
pan: Pair,78
quarter_turns: u8,88
quarter_turns: u8,79
rotated: Option<RotatedImage>,89
rotated: Option<RotatedImage>,90
shown: Option<Shown>,80
entered_fullscreen: bool,91
entered_fullscreen: bool,81
drag_from: Option<Point<Pixels>>,92
drag_from: Option<Point<Pixels>>,82
notice: Option<String>,93
notice: Option<String>,@@ -209,11 +220,17 @@ impl ImageViewer {209
conversation_id: request.conversation_id,220
conversation_id: request.conversation_id,210
conversation_title: request.conversation_title,221
conversation_title: request.conversation_title,211
index: request.index.min(request.items.len().saturating_sub(1)),222
index: request.index.min(request.items.len().saturating_sub(1)),223
full_sizes: request224
.items225
.iter()226
.map(|item| item.image.full_size())227
.collect(),212
items: request.items,228
items: request.items,213
scale: None,229
scale: None,214
pan: (0., 0.),230
pan: (0., 0.),215
quarter_turns: 0,231
quarter_turns: 0,216
rotated: None,232
rotated: None,233
shown: None,217
entered_fullscreen: false,234
entered_fullscreen: false,218
drag_from: None,235
drag_from: None,219
notice: None,236
notice: None,@@ -249,12 +266,14 @@ impl ImageViewer {249
.saturating_sub(1)266
.saturating_sub(1)250
.max((self.index + 1).min(last_index));267
.max((self.index + 1).min(last_index));251
let directory = &self.app.read(cx).directory;268
let directory = &self.app.read(cx).directory;269
let full_size = self.full_sizes.get(self.index).cloned().flatten();252
let missing: Vec<_> = self270
let missing: Vec<_> = self253
.items271
.items254
.get(first..=last)272
.get(first..=last)255
.unwrap_or_default()273
.unwrap_or_default()256
.iter()274
.iter()257
.map(|item| item.image.clone())275
.map(|item| item.image.clone())276
.chain(full_size)258
.filter(|image| directory.image(&image.url).is_none())277
.filter(|image| directory.image(&image.url).is_none())259
.collect();278
.collect();260
if !missing.is_empty() {279
if !missing.is_empty() {@@ -290,12 +309,61 @@ impl ImageViewer {290
.is_some_and(|item| self.app.read(cx).directory.image_failed(&item.image.url))309
.is_some_and(|item| self.app.read(cx).directory.image_failed(&item.image.url))291
}310
}292
311
293
fn current_path(&self, cx: &App) -> Option<PathBuf> {312
/// Cache key of what is drawn for the current picture: its full-size view once downloaded, until then the inline one.313
fn shown_key(&self, cx: &App) -> Option<&str> {294
let item = self.current()?;314
let item = self.current()?;295
let entry = self.app.read(cx).directory.image(&item.image.url)?;315
let directory = &self.app.read(cx).directory;316
let full_size = self317
.full_sizes318
.get(self.index)319
.and_then(Option::as_ref)320
.map(|full| full.url.as_str())321
.filter(|url| directory.image(url).is_some());322
Some(full_size.unwrap_or(item.image.url.as_str()))323
}324
325
fn current_path(&self, cx: &App) -> Option<PathBuf> {326
let entry = self.app.read(cx).directory.image(self.shown_key(cx)?)?;296
Some(entry.path.clone())327
Some(entry.path.clone())297
}328
}298
329
330
/// Keeps the zoom and rotation when the full-size view replaces the inline one under them.331
fn follow_full_size(&mut self, window: &mut Window, cx: &mut Context<Self>) {332
let Some(key) = self.shown_key(cx).map(str::to_owned) else {333
self.shown = None;334
return;335
};336
let size = self337
.app338
.read(cx)339
.directory340
.image(&key)341
.and_then(|entry| entry.size);342
let previous = self.shown.replace(Shown {343
index: self.index,344
key: key.clone(),345
size,346
});347
let Some(previous) = previous.filter(|shown| shown.index == self.index && shown.key != key)348
else {349
return;350
};351
let widths = previous352
.size353
.zip(size)354
.map(|(before, after)| (before.0, after.0));355
if let (Some(scale), Some((before, after))) = (self.scale, widths)356
&& after > 0357
{358
self.scale = Some(scale * before as f32 / after as f32);359
}360
if self.quarter_turns != 0361
&& let Some(path) = self.current_path(cx)362
{363
self.start_rotation(path, window, cx);364
}365
}366
299
fn stage_size(window: &Window) -> Pair {367
fn stage_size(window: &Window) -> Pair {300
let viewport = window.viewport_size();368
let viewport = window.viewport_size();301
(369
(@@ -306,7 +374,7 @@ impl ImageViewer {306
374
307
fn layout(&self, window: &Window, cx: &App) -> Option<Layout> {375
fn layout(&self, window: &Window, cx: &App) -> Option<Layout> {308
let item = self.current()?;376
let item = self.current()?;309
let entry = self.app.read(cx).directory.image(&item.image.url)?;377
let entry = self.app.read(cx).directory.image(self.shown_key(cx)?)?;310
let stage = Self::stage_size(window);378
let stage = Self::stage_size(window);311
let declared = item.image.width.zip(item.image.height);379
let declared = item.image.width.zip(item.image.height);312
let source = entry380
let source = entry@@ -442,47 +510,57 @@ impl ImageViewer {442
}510
}443
511
444
fn rotate(&mut self, window: &mut Window, cx: &mut Context<Self>) {512
fn rotate(&mut self, window: &mut Window, cx: &mut Context<Self>) {445
let (Some(path), Some(item)) = (self.current_path(cx), self.current()) else {513
let Some(path) = self.current_path(cx) else {446
return;514
return;447
};515
};448
let url = item.image.url.clone();449
self.quarter_turns = next_rotation(self.quarter_turns);516
self.quarter_turns = next_rotation(self.quarter_turns);450
self.scale = None;517
self.scale = None;451
self.pan = (0., 0.);518
self.pan = (0., 0.);452
self.release_rotated(window);519
self.release_rotated(window);520
if self.quarter_turns != 0 {521
self.start_rotation(path, window, cx);522
}523
cx.notify();524
}525
526
/// Turns the file at `path` by the current rotation; the previous rotated picture stays until this one is ready.527
fn start_rotation(&mut self, path: PathBuf, window: &mut Window, cx: &mut Context<Self>) {528
let Some(item) = self.current() else {529
return;530
};531
let url = item.image.url.clone();453
let quarter_turns = self.quarter_turns;532
let quarter_turns = self.quarter_turns;454
if quarter_turns != 0 {533
cx.spawn_in(window, async move |this, cx| {455
cx.spawn_in(window, async move |this, cx| {534
let source = path.clone();456
let rotated = cx535
let rotated = cx457
.background_spawn(async move { rotated_render(&path, quarter_turns) })536
.background_spawn(async move { rotated_render(&source, quarter_turns) })458
.await;537
.await;459
this.update_in(cx, |this, window, cx| {538
this.update_in(cx, |this, window, cx| {460
let still_current = this.quarter_turns == quarter_turns539
let still_current = this.quarter_turns == quarter_turns461
&& this.current().is_some_and(|item| item.image.url == url);540
&& this.current().is_some_and(|item| item.image.url == url)462
if !still_current {541
&& this.current_path(cx).as_ref() == Some(&path);463
return;542
if !still_current {543
return;544
}545
match rotated {546
Some(render) => {547
this.release_rotated(window);548
this.rotated = Some(RotatedImage {549
url,550
quarter_turns,551
render: Arc::new(render),552
});464
}553
}465
match rotated {554
None => {466
Some(render) => {555
this.quarter_turns = 0;467
this.release_rotated(window);556
this.show_notice("Could not rotate", cx);468
this.rotated = Some(RotatedImage {469
url,470
quarter_turns,471
render: Arc::new(render),472
});473
}474
None => {475
this.quarter_turns = 0;476
this.show_notice("Could not rotate", cx);477
}478
}557
}479
cx.notify();558
}480
})559
cx.notify();481
.ok();482
})560
})483
.detach();561
.ok();484
}562
})485
cx.notify();563
.detach();486
}564
}487
565
488
fn show_notice(&mut self, text: impl Into<String>, cx: &mut Context<Self>) {566
fn show_notice(&mut self, text: impl Into<String>, cx: &mut Context<Self>) {@@ -798,15 +876,11 @@ impl ImageViewer {798
fn picture(&self, layout: Layout, cx: &mut Context<Self>) -> AnyElement {876
fn picture(&self, layout: Layout, cx: &mut Context<Self>) -> AnyElement {799
let display = display_size(layout.source, layout.scale);877
let display = display_size(layout.source, layout.scale);800
let origin = image_origin(display, layout.stage, layout.pan);878
let origin = image_origin(display, layout.stage, layout.pan);801
let Some(item) = self.current() else {879
if self.current().is_none() {802
return div().into_any_element();880
return div().into_any_element();803
};881
}804
let source: Option<ImageSource> = if self.quarter_turns == 0 {882
let source: Option<ImageSource> = if self.quarter_turns == 0 {805
self.app883
self.current_path(cx).map(Into::into)806
.read(cx)807
.directory808
.image(&item.image.url)809
.map(|entry| entry.path.clone().into())810
} else {884
} else {811
self.rotated_render()885
self.rotated_render()812
.map(|render| ImageSource::Render(render.clone()))886
.map(|render| ImageSource::Render(render.clone()))@@ -965,6 +1039,7 @@ fn loading_placeholder() -> AnyElement {965
1039
966
impl Render for ImageViewer {1040
impl Render for ImageViewer {967
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {1041
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {1042
self.follow_full_size(window, cx);968
let layout = self.layout(window, cx);1043
let layout = self.layout(window, cx);969
let stage = Self::stage_size(window);1044
let stage = Self::stage_size(window);970
let has_previous = self.index > 0;1045
let has_previous = self.index > 0;@@ -103,8 +103,8 @@ pub use language_settings::{
103
103
parse_language_settings, target_patch,
104
104
};
105
105
pub use links::{
106
LinkImage, LinkInfo, MessageLinks, hosted_content_image_url, is_ams_image_url,
107
is_link_image_url,
106
LinkImage, LinkInfo, MessageLinks, ams_full_size_url, hosted_content_image_url,
107
is_ams_image_url, is_link_image_url,
108
108
};
109
109
pub use mask::mask_conversation_id;
110
110
pub use members::{
@@ -103,8 +103,8 @@ pub use language_settings::{103
parse_language_settings, target_patch,103
parse_language_settings, target_patch,104
};104
};105
pub use links::{105
pub use links::{106
LinkImage, LinkInfo, MessageLinks, hosted_content_image_url, is_ams_image_url,106
LinkImage, LinkInfo, MessageLinks, ams_full_size_url, hosted_content_image_url,107
is_link_image_url,107
is_ams_image_url, is_link_image_url,108
};108
};109
pub use mask::mask_conversation_id;109
pub use mask::mask_conversation_id;110
pub use members::{110
pub use members::{@@ -12,6 +12,10 @@ const LIST_VIEW: &str = "msnp24Equivalent";
12
12
const HOSTED_CONTENTS_SEGMENT: &str = "/hostedContents/";
13
13
const HOSTED_URL_KEY: &str = ",url=";
14
14
const AMS_HOST_SUFFIX: &str = ".asm.skype.com";
15
const VIEWS_SEGMENT: &str = "/views/";
16
const FULL_SIZE_VIEW_PREFIX: &str = "imgpsh_fullsize";
17
/// The view Teams opens in its image viewer; `_anim` keeps animated GIFs moving.
18
const FULL_SIZE_VIEW: &str = "imgpsh_fullsize_anim";
15
19
16
20
#[derive(Debug, Clone, PartialEq, Eq)]
17
21
pub struct MessageLinks {
@@ -64,6 +68,26 @@ pub fn is_ams_image_url(url: &str) -> bool {
64
68
})
65
69
}
66
70
71
/// The full-resolution view of the AMS image at `url` (a plain AMS view or a Graph `hostedContents` link to one).
72
/// Messages carry the compressed `imgo` view; Teams swaps to this view when the image is opened.
73
/// `None` for other images, video clips and thumbnails, and views that already are full size.
74
pub fn ams_full_size_url(url: &str) -> Option<String> {
75
let decoded = hosted_content_image_url(url);
76
let url = decoded.as_deref().unwrap_or(url);
77
let is_object = |host: &str, path: &str| {
78
path.starts_with("v1/objects/")
79
&& (host.ends_with(AMS_HOST_SUFFIX) || host.ends_with(LINK_PROXY_HOST_SUFFIX))
80
};
81
let (host, path) = url.strip_prefix("https://")?.split_once('/')?;
82
if !is_object(host, path) {
83
return None;
84
}
85
let (object, view) = url.rsplit_once(VIEWS_SEGMENT)?;
86
let still_picture = view.starts_with("img") && !view.contains(['/', '?', '#']);
87
(still_picture && !view.starts_with(FULL_SIZE_VIEW_PREFIX))
88
.then(|| format!("{object}{VIEWS_SEGMENT}{FULL_SIZE_VIEW}"))
89
}
90
67
91
pub fn parse_message_links(body: &Value) -> Vec<MessageLinks> {
68
92
body.get("messages")
69
93
.and_then(Value::as_array)
@@ -225,6 +249,51 @@ mod tests {
225
249
));
226
250
}
227
251
252
#[test]
253
fn an_inline_ams_image_opens_in_its_full_size_view() {
254
assert_eq!(
255
ams_full_size_url("https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgo")
256
.as_deref(),
257
Some("https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgpsh_fullsize_anim")
258
);
259
assert_eq!(
260
ams_full_size_url(
261
"https://de-prod.asyncgw.teams.microsoft.com/v1/objects/0-weu-d4-abc/views/imgo"
262
)
263
.as_deref(),
264
Some(
265
"https://de-prod.asyncgw.teams.microsoft.com/v1/objects/0-weu-d4-abc/views/imgpsh_fullsize_anim"
266
)
267
);
268
let ams = "https://eu-api.asm.skype.com/v1/objects/0-frca-d14-abc/views/imgo";
269
let id = STANDARD.encode(format!("id=x_0-frca-d14-abc,type=1,url={ams}"));
270
assert_eq!(
271
ams_full_size_url(&format!(
272
"https://graph.microsoft.com/v1.0/chats/19:a/messages/1/hostedContents/{id}/$value"
273
))
274
.as_deref(),
275
Some(
276
"https://eu-api.asm.skype.com/v1/objects/0-frca-d14-abc/views/imgpsh_fullsize_anim"
277
)
278
);
279
}
280
281
#[test]
282
fn full_size_and_non_picture_views_have_no_separate_full_size() {
283
for url in [
284
"https://eu-api.asm.skype.com/v1/objects/a/views/imgpsh_fullsize",
285
"https://eu-api.asm.skype.com/v1/objects/a/views/imgpsh_fullsize_anim",
286
"https://eu-api.asm.skype.com/v1/objects/a/views/video",
287
"https://eu-api.asm.skype.com/v1/objects/a/views/thumbnail",
288
"https://eu-api.asm.skype.com/v1/objects/a",
289
"https://evil.example/v1/objects/a/views/imgo",
290
"https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/content?url=x/views/imgo",
291
"https://graph.microsoft.com/v1.0/chats/c/messages/1/hostedContents/aGk=/$value",
292
] {
293
assert_eq!(ams_full_size_url(url), None, "{url}");
294
}
295
}
296
228
297
#[test]
229
298
fn parses_link_info_with_target_url_and_thumbnail_size() {
230
299
let info = parse_link_info(&json!({
@@ -12,6 +12,10 @@ const LIST_VIEW: &str = "msnp24Equivalent";12
const HOSTED_CONTENTS_SEGMENT: &str = "/hostedContents/";12
const HOSTED_CONTENTS_SEGMENT: &str = "/hostedContents/";13
const HOSTED_URL_KEY: &str = ",url=";13
const HOSTED_URL_KEY: &str = ",url=";14
const AMS_HOST_SUFFIX: &str = ".asm.skype.com";14
const AMS_HOST_SUFFIX: &str = ".asm.skype.com";15
const VIEWS_SEGMENT: &str = "/views/";16
const FULL_SIZE_VIEW_PREFIX: &str = "imgpsh_fullsize";17
/// The view Teams opens in its image viewer; `_anim` keeps animated GIFs moving.18
const FULL_SIZE_VIEW: &str = "imgpsh_fullsize_anim";15
19
16
#[derive(Debug, Clone, PartialEq, Eq)]20
#[derive(Debug, Clone, PartialEq, Eq)]17
pub struct MessageLinks {21
pub struct MessageLinks {@@ -64,6 +68,26 @@ pub fn is_ams_image_url(url: &str) -> bool {64
})68
})65
}69
}66
70
71
/// The full-resolution view of the AMS image at `url` (a plain AMS view or a Graph `hostedContents` link to one).72
/// Messages carry the compressed `imgo` view; Teams swaps to this view when the image is opened.73
/// `None` for other images, video clips and thumbnails, and views that already are full size.74
pub fn ams_full_size_url(url: &str) -> Option<String> {75
let decoded = hosted_content_image_url(url);76
let url = decoded.as_deref().unwrap_or(url);77
let is_object = |host: &str, path: &str| {78
path.starts_with("v1/objects/")79
&& (host.ends_with(AMS_HOST_SUFFIX) || host.ends_with(LINK_PROXY_HOST_SUFFIX))80
};81
let (host, path) = url.strip_prefix("https://")?.split_once('/')?;82
if !is_object(host, path) {83
return None;84
}85
let (object, view) = url.rsplit_once(VIEWS_SEGMENT)?;86
let still_picture = view.starts_with("img") && !view.contains(['/', '?', '#']);87
(still_picture && !view.starts_with(FULL_SIZE_VIEW_PREFIX))88
.then(|| format!("{object}{VIEWS_SEGMENT}{FULL_SIZE_VIEW}"))89
}90
67
pub fn parse_message_links(body: &Value) -> Vec<MessageLinks> {91
pub fn parse_message_links(body: &Value) -> Vec<MessageLinks> {68
body.get("messages")92
body.get("messages")69
.and_then(Value::as_array)93
.and_then(Value::as_array)@@ -225,6 +249,51 @@ mod tests {225
));249
));226
}250
}227
251
252
#[test]253
fn an_inline_ams_image_opens_in_its_full_size_view() {254
assert_eq!(255
ams_full_size_url("https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgo")256
.as_deref(),257
Some("https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgpsh_fullsize_anim")258
);259
assert_eq!(260
ams_full_size_url(261
"https://de-prod.asyncgw.teams.microsoft.com/v1/objects/0-weu-d4-abc/views/imgo"262
)263
.as_deref(),264
Some(265
"https://de-prod.asyncgw.teams.microsoft.com/v1/objects/0-weu-d4-abc/views/imgpsh_fullsize_anim"266
)267
);268
let ams = "https://eu-api.asm.skype.com/v1/objects/0-frca-d14-abc/views/imgo";269
let id = STANDARD.encode(format!("id=x_0-frca-d14-abc,type=1,url={ams}"));270
assert_eq!(271
ams_full_size_url(&format!(272
"https://graph.microsoft.com/v1.0/chats/19:a/messages/1/hostedContents/{id}/$value"273
))274
.as_deref(),275
Some(276
"https://eu-api.asm.skype.com/v1/objects/0-frca-d14-abc/views/imgpsh_fullsize_anim"277
)278
);279
}280
281
#[test]282
fn full_size_and_non_picture_views_have_no_separate_full_size() {283
for url in [284
"https://eu-api.asm.skype.com/v1/objects/a/views/imgpsh_fullsize",285
"https://eu-api.asm.skype.com/v1/objects/a/views/imgpsh_fullsize_anim",286
"https://eu-api.asm.skype.com/v1/objects/a/views/video",287
"https://eu-api.asm.skype.com/v1/objects/a/views/thumbnail",288
"https://eu-api.asm.skype.com/v1/objects/a",289
"https://evil.example/v1/objects/a/views/imgo",290
"https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/content?url=x/views/imgo",291
"https://graph.microsoft.com/v1.0/chats/c/messages/1/hostedContents/aGk=/$value",292
] {293
assert_eq!(ams_full_size_url(url), None, "{url}");294
}295
}296
228
#[test]297
#[test]229
fn parses_link_info_with_target_url_and_thumbnail_size() {298
fn parses_link_info_with_target_url_and_thumbnail_size() {230
let info = parse_link_info(&json!({299
let info = parse_link_info(&json!({@@ -113,6 +113,18 @@ impl ImageRef {
113
113
&self.url
114
114
}
115
115
116
/// The same picture at full resolution, for the image viewer; `None` when the message already carries the only view there is.
117
pub fn full_size(&self) -> Option<ImageRef> {
118
let url = chatsvc::ams_full_size_url(&self.url)?;
119
Some(ImageRef {
120
id: format!("{}:full", self.id),
121
url,
122
width: None,
123
height: None,
124
alt: self.alt.clone(),
125
})
126
}
127
116
128
/// The Teams video clip this image is the thumbnail of.
117
129
pub fn video_url(&self) -> Option<String> {
118
130
thumbnail_video_url(&self.url)
@@ -949,6 +961,30 @@ mod tests {
949
961
assert_eq!(parsed[0].created_at, None);
950
962
}
951
963
964
#[test]
965
fn an_ams_picture_has_a_full_size_view_kept_apart_from_the_inline_one() {
966
let inline = ImageRef {
967
id: "0-weu-d1-abc".into(),
968
url: "https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgo".into(),
969
width: Some(640),
970
height: Some(360),
971
alt: Some("image".into()),
972
};
973
let full = inline.full_size().unwrap();
974
assert_eq!(
975
full.key(),
976
"https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgpsh_fullsize_anim"
977
);
978
assert_ne!(full.key(), inline.key());
979
assert_eq!((full.width, full.height), (None, None));
980
assert_eq!(full.full_size(), None);
981
let external = ImageRef {
982
url: "https://example.com/a.png".into(),
983
..inline
984
};
985
assert_eq!(external.full_size(), None);
986
}
987
952
988
#[test]
953
989
fn images_cover_hosted_contents_and_ams_and_skip_emoji() {
954
990
let html = concat!(
@@ -113,6 +113,18 @@ impl ImageRef {113
&self.url113
&self.url114
}114
}115
115
116
/// The same picture at full resolution, for the image viewer; `None` when the message already carries the only view there is.117
pub fn full_size(&self) -> Option<ImageRef> {118
let url = chatsvc::ams_full_size_url(&self.url)?;119
Some(ImageRef {120
id: format!("{}:full", self.id),121
url,122
width: None,123
height: None,124
alt: self.alt.clone(),125
})126
}127
116
/// The Teams video clip this image is the thumbnail of.128
/// The Teams video clip this image is the thumbnail of.117
pub fn video_url(&self) -> Option<String> {129
pub fn video_url(&self) -> Option<String> {118
thumbnail_video_url(&self.url)130
thumbnail_video_url(&self.url)@@ -949,6 +961,30 @@ mod tests {949
assert_eq!(parsed[0].created_at, None);961
assert_eq!(parsed[0].created_at, None);950
}962
}951
963
964
#[test]965
fn an_ams_picture_has_a_full_size_view_kept_apart_from_the_inline_one() {966
let inline = ImageRef {967
id: "0-weu-d1-abc".into(),968
url: "https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgo".into(),969
width: Some(640),970
height: Some(360),971
alt: Some("image".into()),972
};973
let full = inline.full_size().unwrap();974
assert_eq!(975
full.key(),976
"https://eu-api.asm.skype.com/v1/objects/0-weu-d1-abc/views/imgpsh_fullsize_anim"977
);978
assert_ne!(full.key(), inline.key());979
assert_eq!((full.width, full.height), (None, None));980
assert_eq!(full.full_size(), None);981
let external = ImageRef {982
url: "https://example.com/a.png".into(),983
..inline984
};985
assert_eq!(external.full_size(), None);986
}987
952
#[test]988
#[test]953
fn images_cover_hosted_contents_and_ams_and_skip_emoji() {989
fn images_cover_hosted_contents_and_ams_and_skip_emoji() {954
let html = concat!(990
let html = concat!(