cosmic_applet_avestruz/guide/decisions.rs
1// SPDX-License-Identifier: MIT
2
3//! What was decided and why, the rules everything else keeps to, and what
4//! is not there yet. Documentation only.
5//!
6//! Most of what is particular about this code follows from a few
7//! decisions that are not visible in any one file. A change that goes
8//! against one of them will compile, pass its tests, and be wrong. This
9//! page is the list, each with its reason and with where it is kept, so
10//! that a decision can be found before it is undone, or undone on purpose.
11//!
12//! 1. [The rules, at a glance](#the-rules-at-a-glance)
13//! 2. [What is on show is the reader's](#what-is-on-show-is-the-readers)
14//! 3. [The popup takes nothing with it](#the-popup-takes-nothing-with-it)
15//! 4. [The store first, the relays after](#the-store-first-the-relays-after)
16//! 5. [A secret is in the keyring, or nowhere](#a-secret-is-in-the-keyring-or-nowhere)
17//! 6. [What takes the key waits for someone to look](#what-takes-the-key-waits-for-someone-to-look)
18//! 7. [A list is other clients' too](#a-list-is-other-clients-too)
19//! 8. [What is the account's alone stays so](#what-is-the-accounts-alone-stays-so)
20//! 9. [No relay is built in for a purpose](#no-relay-is-built-in-for-a-purpose)
21//! 10. [Every address is somebody else's word](#every-address-is-somebody-elses-word)
22//! 11. [Pictures are shown as they are](#pictures-are-shown-as-they-are)
23//! 12. [It holds no money](#it-holds-no-money)
24//! 13. [How the code is written](#how-the-code-is-written)
25//! 14. [What is not there yet](#what-is-not-there-yet)
26//!
27//! # The rules, at a glance
28//!
29//! ```mermaid
30//! mindmap
31//! root((Avestruz))
32//! The reader
33//! What is on show stays put
34//! Nothing above what is being read
35//! A row is the same height before and after
36//! The popup
37//! Closes at any click elsewhere
38//! Everything outlives it in the model
39//! The account
40//! Secrets in the keyring only
41//! The key is asked for when someone looks
42//! Lists are never written blind
43//! Privacy
44//! Bookmarks are private
45//! Drafts stay on this machine
46//! A search leaves no trace
47//! No relay or media server built in for a purpose
48//! A picture is sent without what it says beside the picture
49//! Other people
50//! Read from where they write
51//! Few relays, chosen
52//! Their addresses are their word
53//! The machine
54//! The store is a cache
55//! Memory is budgeted
56//! Nothing blocks the window
57//! ```
58//!
59//! # What is on show is the reader's
60//!
61//! **Where a list is scrolled to never changes unless its reader changes
62//! it.** Not when notes arrive, not when a picture loads, not when a link's
63//! preview comes in, not when the popup is closed and opened again.
64//!
65//! A feed that moves under the eye can't be read, and a row that moves
66//! under the pointer gets the wrong one clicked. Holding to this takes
67//! something in almost every part of what is shown:
68//!
69//! ```mermaid
70//! flowchart TD
71//! still["The rows on screen stay where they are"]
72//! above["Nothing goes in above them unasked"] --> still
73//! height["No row changes height after it is shown"] --> still
74//! kept["The place is kept while the list is not on show"] --> still
75//! pending["New notes wait behind a button"] --> above
76//! older["Older pages go in below"] --> above
77//! room["A picture's room is held before it arrives"] --> height
78//! card["A link's card is as tall asked as answered"] --> height
79//! quotes["Quoted notes are looked for before the note shows"] --> height
80//! measured["Heights are remembered by note, not by index"] --> kept
81//! cell["The scroll offset is in the model"] --> kept
82//! estimate["A row never shown has an estimate that depends only on its note and the width"] --> kept
83//! ```
84//!
85//! | What would move the list | What keeps it still | Where |
86//! |---|---|---|
87//! | Notes arriving at the top | They wait, and are offered by a button | [`Pending`](crate::nostr::feed::Pending) |
88//! | A picture arriving in a row | The row holds its room from the start, when the note says how large it is | [`MediaPlaceholder`](crate::views::components::media_placeholder) |
89//! | A link's preview arriving | Its card is the same height before, after, and for every page | [`crate::views::components::link_preview`](mod@crate::views::components::link_preview) |
90//! | A video being played | Its room is settled from what the note says of it, before anything of it is fetched, and what plays is fitted inside | [`box_size`](crate::video::box_size) |
91//! | A quoted note arriving | It is looked for before the note quoting it is shown at all | [`crate::views::components::quoted_note`](mod@crate::views::components::quoted_note) |
92//! | The popup closing | The offset and the measured heights are in the model | [`ScrollPosition`](crate::views::components::ScrollPosition), [`RowHeights`](crate::views::components::RowHeights) |
93//! | Rows never yet shown | Each has an estimate that depends on nothing but itself, so it doesn't change as others are measured | [`crate::views::components::virtual_list`] |
94//! | A bookmark taken off, on the bookmarks page | The note stays in its place with an empty ribbon | [`crate::views::bookmarks`] |
95//! | A long note shown in full in another list | Which notes show in full is each list's own: no row grows in a list that is not on show | [`Expanded`](crate::state::Expanded) |
96//!
97//! The estimate is held to by a test: the estimated height of a row has
98//! to stay close to what the row measures.
99//!
100//! Two places still move. A picture whose note doesn't say how large it
101//! is grows its row when it loads. And a link that turned out to be a
102//! video only when it was asked for its preview has a preview's card,
103//! which gives way to the taller room of a video when it is played: at
104//! its reader's pressing, and never by itself.
105//!
106//! A long note is the one row that changes height at its reader's
107//! asking, and by a lot. Shown in full, it grows down from where it is:
108//! what is above it stays, and the list is not scrolled. Cut again, it
109//! would leave its reader looking at whatever was further down, if its
110//! top had been scrolled out of view: so the list is scrolled back to
111//! the top of the row, and only then. Either way the row is measured
112//! before the list is laid out again, in view or not
113//! ([`RowHeights::measure_anew`](crate::views::components::RowHeights::measure_anew)),
114//! and the scroller is left at the place it shows: one told to go past
115//! the end of a list that just got shorter would keep that place, and go
116//! there by itself when the list next grew
117//! ([`scroll_list_within`](crate::handlers::navigation::scroll_list_within)).
118//!
119//! # The popup takes nothing with it
120//!
121//! A panel applet's popup closes when anything else is clicked. Whatever
122//! was only in a widget is gone then: so **nothing that matters is only in
123//! a widget.** What was typed, where each list is, which pages are open,
124//! what was changed on the relays page and not saved: all of it is in
125//! [`AppModel`](crate::state::AppModel), and the widgets are given it back
126//! when they are built again. See [`crate::guide::architecture`].
127//!
128//! This is also why there is one process and one model rather than a
129//! window with a life of its own: the applet runs for as long as the
130//! session does, and the popup is a view of it that comes and goes.
131//!
132//! # The store first, the relays after
133//!
134//! **Whatever can be shown from the store is shown from the store, at
135//! once, and the relays are asked behind it.** A relay may take seconds
136//! or never answer; the store answers within the frame.
137//!
138//! Every event a relay sends is kept, so the store is also what makes the
139//! second look at anything immediate. It is a cache and is treated as
140//! one: once a day it lets go of what is old and can be asked for again
141//! ([`crate::nostr::sweep`]), and anything that needs an event the store
142//! may have let go of asks the relays for it.
143//!
144//! There is no live subscription. New notes are looked for on a timer,
145//! and wait to be asked for: a feed that fills itself as it is read is
146//! the thing the first rule is against.
147//!
148//! # A secret is in the keyring, or nowhere
149//!
150//! An account's secret key, or the address of its remote signer, is in
151//! the system keyring. **It is never in the config, the event store, a
152//! log, or the model.** The config names the account by its `npub`.
153//!
154//! - With an `nsec`, the key is fetched from the keyring for each
155//! signature and let go of after: the applet runs all day, and holds no
156//! secret between one signature and the next.
157//! - What is typed in the sign-in field is wrapped in a type that prints
158//! nothing ([`SecretInput`](crate::messages::SecretInput)), because
159//! messages are `Debug` and what is `Debug` ends up in a log.
160//! - A key typed in the *search* field is cleared and sent nowhere.
161//! - A remote signer knows this client by a key of the client's own, one
162//! for each signer and the same ever after, so that a signer told about
163//! it once keeps answering.
164//!
165//! # What takes the key waits for someone to look
166//!
167//! With a remote signer, every signature and every decryption is a
168//! request to another machine, and may be a prompt on somebody's phone.
169//! So **nothing that takes the key happens before the popup is open, and
170//! nothing is decrypted twice.**
171//!
172//! | What takes the key | When |
173//! |---|---|
174//! | Reading the bookmarks | The popup opens |
175//! | Reading the private half of the blocked and indexer relays | The popup opens |
176//! | Reading the drafts | The popup first opens in a run |
177//! | Reading the private half of the search relays | A search page opens |
178//! | Reading the private half of the other lists of relays | The relays page opens |
179//! | Keeping a draft | The composer is left with something new in it |
180//! | Anything published | It is published |
181//!
182//! A list that was decrypted is kept as read while it is the newest, and
183//! is not sent to the signer again.
184//!
185//! # A list is other clients' too
186//!
187//! The contact list, the bookmark list and the lists of relays are each
188//! one event that every client the account uses reads and replaces. So:
189//!
190//! - **A list is asked of the relays again just before it is changed**,
191//! and the change is made to the newest.
192//! - **Everything in it that is not being changed is carried over as it
193//! was**, known or not.
194//! - **A first list is never written without saying so**: if no relay has
195//! one, the account is told that writing one replaces any kept
196//! elsewhere, and has to ask again.
197//! - **And not at all if a relay didn't answer**: that relay may be the
198//! one that has the list.
199//! - **A list whose private half can't be read is never written over**:
200//! written without it, the list would lose it.
201//!
202//! The cost is that a change sometimes doesn't happen, and says why. The
203//! other way round, a follow list of years is one bad moment from being
204//! replaced by a list of one.
205//!
206//! # What is the account's alone stays so
207//!
208//! - **Bookmarks are private.** They are written to the encrypted half of
209//! the bookmark list and read from there, and the public half is carried
210//! over unread. What someone bookmarks is nobody else's to know.
211//! - **Drafts stay on this machine.** They are events, sent to a relay
212//! that runs inside the process and listens nowhere, and encrypted all
213//! the same in the store.
214//! - **A search leaves no trace.** Nothing typed there is kept or logged,
215//! and what names a person or a note outright is opened without any
216//! relay being told.
217//! - **A name at a domain is checked only where it is looked at.** A feed
218//! would have a hundred domains told who reads what.
219//! - **Once the account has said where it is, nobody else is asked about
220//! everything.** The config's relays stand down.
221//! - **Nothing of a video is fetched until it is played.** A picture is
222//! small and is what the note is; a video is large, has sound, and is
223//! decoded by far more code.
224//! - **When the notifications were last looked at stays on this
225//! machine.** It is one of the settings, for each account, and is in
226//! no event: when someone reads what was written to them is nobody
227//! else's to know.
228//! - **Which emoji are reacted with most stays on this machine.** It is
229//! one of the settings, and is in no event: the reactions themselves
230//! are public, a tally of them need not be.
231//!
232//! What can't be helped is said rather than hidden: every server a
233//! picture, a preview or a video is fetched from sees the request, and
234//! whatever can read the user's files can read the event store.
235//!
236//! # No relay is built in for a purpose
237//!
238//! The config names three large relays to start from, since an account
239//! has to be looked for somewhere. Past that, **which relays search, and
240//! which are asked for profiles nobody else has, is the account's to
241//! say**, in its own lists. With none set, nothing is searched, and the
242//! page says where to set them.
243//!
244//! A relay built in for searching would be a party every user's searches
245//! are sent to because a developer chose it.
246//!
247//! The same goes for media servers: **where an account's files are kept
248//! is the account's to say**, in its server list. With none set, nothing
249//! can be attached, and the page they are set on opens instead and says
250//! so. No list is written that the account did not ask for.
251//!
252//! And **a picture is sent as the picture and no more**: where, when,
253//! with what and by whom it was made is left out of the file before it
254//! goes anywhere, and a picture that can't be made bare is not sent
255//! ([`crate::media`]).
256//!
257//! One site is built in all the same: the one a note's share link leads
258//! to ([`SHARE_GATEWAY`](crate::nostr::share::SHARE_GATEWAY)). A link
259//! that opens in any browser has to be to somebody's site. Avestruz
260//! itself asks that site nothing: it is named in a link the user copies,
261//! and hears of the note when whoever is given the link opens it.
262//!
263//! # Every address is somebody else's word
264//!
265//! Every URL this client fetches, and every relay it hears of from a list
266//! or a note, was written by someone else, who may have meant harm.
267//!
268//! | What | The rule | Where |
269//! |---|---|---|
270//! | Anything fetched from the web | Only `https`, redirects included. A time limit. No more read than is kept | [`crate::http`] |
271//! | A picture | A cap on the download and another on how large it may decode | [`crate::images`] |
272//! | A linked page | Only its head, read with a tokenizer. Nothing is run | [`crate::previews`] |
273//! | A YouTube link | Nothing of it reaches yt-dlp's command line but the video's id, once that is known to be eleven letters, figures, `-` or `_`. What yt-dlp finds is played only if it is `https` | [`VideoId`](crate::youtube::VideoId) |
274//! | A video | Only `https`, and only when its reader plays it. GStreamer fetches and decodes it: the one thing not fetched by [`crate::http`], and not held to its limits | [`crate::video`] |
275//! | A link that is opened | Only web links are handed to the desktop | [`crate::links`] |
276//! | A relay somebody names | Only `wss`, on the open internet, not blocked | [`public`](crate::nostr::outbox::public) |
277//! | An invoice | Only shown if it asks for the amount that was chosen | [`crate::nostr::zap`] |
278//! | A link a note gives a label of its own | Shown as the label and, beside it, the site it goes to: a label can say anything | [`crate::nostr::content`] |
279//! | Markdown in a note | Each mark is taken for one only where it could be nothing else, so a note that means none is shown as written. Marks inside marks are followed six deep, and a mark that nothing closes is looked for once | [`crate::nostr::markdown`] |
280//! | A name at a domain | Not shown until the domain itself has said it is theirs | [`crate::nostr::nip05`] |
281//!
282//! # The button and the window are two processes
283//!
284//! What the panel starts is a button and nothing else; the client is a
285//! second process of the same program, which the button starts and talks
286//! to over a pipe ([`crate::button`]).
287//!
288//! It was one process, an applet whose popup the panel placed under its
289//! button. An applet's surfaces are the panel's, which is a compositor of
290//! its own and takes no GPU buffers from its applets: an applet is drawn
291//! by the CPU, and a list of pictures scrolled on a screen of many pixels
292//! is more than a CPU draws in the time a frame has. A surface the window
293//! puts on the session's compositor itself is drawn by the GPU.
294//!
295//! What it costs is that the panel no longer places the popup: the window
296//! is told which panel its button is in and which part of it, not where,
297//! so the popup is put against the panel's edge at the end the button is
298//! nearer, and not exactly under it. And closing on a click elsewhere,
299//! which the panel did, is the window's to do: the popup has the keyboard
300//! while it is open, and closes when it loses it.
301//!
302//! What is copied in the popup has to outlive it: a link is copied to
303//! be pasted somewhere else, and the click that goes there closes the
304//! popup. The toolkit keeps what was copied with the first surface a
305//! process opened, so the window opens one before any popup, which
306//! shows nothing and stays
307//! ([`keep_what_is_copied`](crate::state::AppModel::keep_what_is_copied)).
308//! The same surface reaches from the top of the screen to the bottom,
309//! and is what says how much room the screen has for the popup, and at
310//! what scale it is drawn ([`crate::screen`]): nothing else is asked.
311//!
312//! # Pictures are kept at the size they are shown at
313//!
314//! A picture fills the width of its row, is as tall as that makes it, and
315//! is never shown wider than it is, a point to its pixel. It is kept at
316//! as many of the screen's pixels as that takes, and not as it came: made
317//! that size once, when it is decoded, and from then on copied into each
318//! frame pixel for pixel (see [`crate::images`] and
319//! [`Picture`](crate::views::components::Picture)).
320//!
321//! It was kept as it came at first, and scaled by the renderer in every
322//! frame. Measured on a feed of real notes, in a popup of 1088 by 1530
323//! pixels, that was two thirds of what a frame of a scrolling list cost,
324//! and six frames in ten took longer than a screen of 60 Hz waits for:
325//! the renderer was a CPU one then, a photograph has many times the
326//! pixels its row has, and rounding its corners copied all of them, every
327//! frame. The GPU that draws the popup now scales a picture for next to
328//! nothing, and the CPU renderer still draws it where there is no GPU. Nothing shows a picture
329//! larger than its row, so the original is not kept at all.
330//!
331//! What bounds memory is that size, a limit on how large one picture may
332//! decode, and a budget on the bytes kept, with the least recently shown
333//! dropped first.
334//!
335//! Three kinds of picture are cut to a shape, in their own pixels, when
336//! they are decoded: an avatar to its middle square and round, the
337//! picture across a profile to a strip, and the one beside a link's
338//! preview to a thumbnail. The CPU renderer does neither reliably at draw
339//! time for a picture that isn't the shape it is shown in.
340//!
341//! # It holds no money
342//!
343//! Zapping gets as far as an invoice and shows it as a QR code. There is
344//! no wallet in this client and no connection to one: a key that can
345//! spend is a different kind of secret from a key that can post, and a
346//! panel applet is not where it should live.
347//!
348//! # How the code is written
349//!
350//! | Rule | Kept by |
351//! |---|---|
352//! | Nothing in `src/` may panic: no `unwrap`, `expect`, `panic!`, or indexing | Clippy lints, denied in `Cargo.toml`; tests may |
353//! | No wildcard imports | The same |
354//! | No dependency under a licence that would bind the whole (the project is MIT) | `cargo deny`, with the allow list in `deny.toml` |
355//! | No dependency that isn't used | `cargo machete` |
356//! | One TLS crypto provider | A test: with two compiled in, the first connection panics |
357//! | A view takes `&AppModel` | The types |
358//! | An error is a value with a reason, and the reason is said to the user in their language | `fl!`, and one function for each kind of error that turns it into words |
359//! | A message that carries a secret doesn't print it | [`SecretInput`](crate::messages::SecretInput) |
360//! | Every module says what it is for, in its first lines | Read by whoever opens the file |
361//! | A test is named for what it holds to be so | See [`crate::guide::testing`] |
362//!
363//! All of the first four, the formatting, and that these pages build, are
364//! one command: `just check`.
365//!
366//! # What is not there yet
367//!
368//! Decided against, for now or for good:
369//!
370//! - **A global feed.** The feed is who the account follows.
371//! - **A wallet**, as above.
372//! - **Public bookmarks.**
373//! - **A standalone window.** It is a panel applet; a window of its own
374//! may come.
375//!
376//! Not built yet:
377//!
378//! - Direct messages, long-form articles.
379//! - Reading the feed from the account's inbox relays; they are asked
380//! what names the account, and no more.
381//! - Notifications of new followers, and from the desktop while the
382//! popup is closed, beyond the dot on the panel button.
383//! - Reposts by the people followed, in the feed.
384//! - A live subscription, in place of looking on a timer.
385//! - Older pages of a feed read from the store when no relay answers.
386//! - Pictures kept on disk; audio and live streams; animated pictures.
387//! - A video larger than its row: full screen, or a window of its own.
388//! - YouTube in more than its smallest size, which takes playing
389//! pictures and sound from two streams; its playlists.
390//! - Hashtags that open anything.
391//! - Taking what a video says of where it was made out of it before it
392//! is attached; a thumbnail for a picture attached (`thumbhash`,
393//! `blurhash`); dropping a file on the composer; deleting a file from
394//! a media server.
395//! - Showing a picture the way up its EXIF says it is.
396//! - Custom emoji (NIP-30), to react with or shown in a reaction; skin
397//! tones to choose from where all emoji are; taking a reaction back.
398//! - Drawing the colour glyphs that are gradients ([`crate::fonts`]):
399//! where the system's emoji font has only those, emoji are in colour
400//! once a font with bitmaps is put among the applet's own.
401//! - Verifying the zap receipts counted under other people's notes. The
402//! ones the account is told of are: see
403//! [`crate::nostr::notifications`].
404//! - Any language but English.