cosmic_applet_avestruz/handlers/previews.rs
1// SPDX-License-Identifier: MIT
2
3//! Link preview message handlers for Avestruz.
4//!
5//! A row whose note links to a web page asks the
6//! [`PreviewStore`](crate::previews::PreviewStore) what the page says of
7//! itself, which sends the address here as a `LoadPreview`. This is where it
8//! is decided whether to fetch the page, and where what it said comes back
9//! to.
10
11use cosmic::prelude::*;
12
13use crate::messages::Message;
14use crate::previews::{self, Linked};
15use crate::state::AppModel;
16
17impl AppModel {
18 /// A view asked what a page says of itself. Fetch its head, unless that
19 /// is known or on its way already, or too many others are.
20 pub fn handle_load_preview(&mut self, url: String) -> Task<cosmic::Action<Message>> {
21 if !self.previews.begin(&url) {
22 return Task::none();
23 }
24 let http = self.http.clone();
25 Task::perform(
26 async move {
27 let result = previews::fetch(http, url.clone()).await;
28 (url, result)
29 },
30 |(url, result)| cosmic::Action::App(Message::PreviewLoaded(url, result)),
31 )
32 }
33
34 /// A page answered, or didn't. Either way the rows that link to it are
35 /// built again: with its preview, with the offer to play it if it
36 /// turned out to be a video, or without the room kept for either.
37 pub fn handle_preview_loaded(&mut self, url: String, result: Result<Linked, String>) {
38 if let Err(e) = &result {
39 // Pages that are gone, or say nothing, are ordinary: not worth
40 // a banner.
41 tracing::debug!("link not previewed ({e}): {url}");
42 }
43 self.previews.finish(url, result.ok());
44 }
45}