Skip to main content

cosmic_applet_avestruz/handlers/
via.rs

1// SPDX-License-Identifier: MIT
2
3//! Handlers for what a client says of itself: the "via" of a note.
4//!
5//! A note may name the client it was written with, and where that client
6//! says what it is: the address of its handler event (NIP-89, kind 31990),
7//! which has a name, a picture, a line about it and a website. A press on
8//! the "via" of such a note shows that under it.
9//!
10//! The store is read and the relays are asked at once. What the store has
11//! shows as soon as it is read, which is at once for a client seen before;
12//! what the relays send replaces it if it is newer, since a client may say
13//! something else of itself tomorrow. Which relays: see
14//! [`NostrService::fetch_handler`](crate::nostr::client::NostrService::fetch_handler).
15
16use cosmic::prelude::*;
17use nostr_sdk::prelude::EventId;
18
19use crate::messages::Message;
20use crate::nostr::models::{HandlerAddress, Profile};
21use crate::state::{AppModel, HandlerSaid, ViaMenu};
22
23/// What is known of what a client says of itself, which was `known`, now
24/// that the store was read (`asked` is false) or the relays were asked,
25/// and gave `said`.
26///
27/// What the store has shows only until the relays answer: theirs is the
28/// newest there is, the store's own included. And their having nothing is
29/// nothing only if nothing was found before.
30fn settled(known: HandlerSaid, asked: bool, said: Option<Box<Profile>>) -> HandlerSaid {
31    match (said, asked, known) {
32        (Some(said), true, _) | (Some(said), false, HandlerSaid::Asking) => HandlerSaid::Found(said),
33        (None, true, HandlerSaid::Asking) => HandlerSaid::Nowhere,
34        (_, _, known) => known,
35    }
36}
37
38impl AppModel {
39    /// The "via" of the note `note` was pressed: show what the client at
40    /// `address` says of itself under it, or put that away if it is
41    /// showing. Any other menu over the list is put away.
42    pub fn handle_show_via(&mut self, note: EventId, address: HandlerAddress) -> Task<cosmic::Action<Message>> {
43        self.repost_menu = None;
44        self.reaction_menu = None;
45        self.who_menu = None;
46        self.seen_menu = None;
47        if self.via_menu.take().is_some_and(|menu| menu.note == note) {
48            return Task::none();
49        }
50        let Some(service) = self.nostr.clone() else {
51            return Task::none();
52        };
53        self.via_menu = Some(ViaMenu { note, address: address.clone(), said: HandlerSaid::Asking });
54        let loaded = |address: HandlerAddress, asked: bool| {
55            move |said: Option<Profile>| {
56                cosmic::Action::App(Message::ViaLoaded { address: address.clone(), asked, said: said.map(Box::new) })
57            }
58        };
59        let stored = {
60            let (service, at) = (service.clone(), address.clone());
61            Task::perform(async move { service.stored_handler(&at).await }, loaded(address.clone(), false))
62        };
63        let asked = {
64            let at = address.clone();
65            Task::perform(async move { service.fetch_handler(&at).await }, loaded(address, true))
66        };
67        Task::batch([stored, asked])
68    }
69
70    /// What the client at `address` says of itself was read from the
71    /// store, or asked of the relays. Nothing comes of it if that is no
72    /// longer what is on show.
73    pub fn handle_via_loaded(&mut self, address: &HandlerAddress, asked: bool, said: Option<Box<Profile>>) {
74        if let Some(menu) = self.via_menu.as_mut().filter(|menu| &menu.address == address) {
75            menu.said = settled(std::mem::replace(&mut menu.said, HandlerSaid::Asking), asked, said);
76        }
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::settled;
83    use crate::nostr::models::Profile;
84    use crate::state::HandlerSaid;
85
86    fn saying(name: &str) -> Box<Profile> {
87        Box::new(Profile { name: Some(name.to_string()), ..Profile::default() })
88    }
89
90    #[test]
91    fn what_the_store_has_shows_until_the_relays_answer() {
92        let (old, new) = (saying("old"), saying("new"));
93        // The store has it: that shows while the relays are asked.
94        let stored = settled(HandlerSaid::Asking, false, Some(old.clone()));
95        assert_eq!(stored, HandlerSaid::Found(old.clone()));
96        // The relays have a newer one: that shows from then on.
97        assert_eq!(settled(stored.clone(), true, Some(new.clone())), HandlerSaid::Found(new.clone()));
98        // The relays did not answer in time: what the store had stays.
99        assert_eq!(settled(stored, true, None), HandlerSaid::Found(old.clone()));
100        // The store is read after the relays answered: theirs stays.
101        assert_eq!(settled(HandlerSaid::Found(new.clone()), false, Some(old)), HandlerSaid::Found(new.clone()));
102        assert_eq!(settled(HandlerSaid::Found(new.clone()), false, None), HandlerSaid::Found(new));
103    }
104
105    #[test]
106    fn nothing_is_said_to_be_found_only_once_the_relays_were_asked() {
107        // The store has nothing: the relays are still being asked.
108        assert_eq!(settled(HandlerSaid::Asking, false, None), HandlerSaid::Asking);
109        // They have nothing either.
110        assert_eq!(settled(HandlerSaid::Asking, true, None), HandlerSaid::Nowhere);
111        // And the store, read after that, changes nothing unless it has it.
112        assert_eq!(settled(HandlerSaid::Nowhere, false, None), HandlerSaid::Nowhere);
113    }
114}