Repository navigation
Expand file tree
/
Copy pathmisc.rs
More file actions
108 lines (91 loc) · 3.69 KB
/
Copy pathmisc.rs
File metadata and controls
108 lines (91 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use crate::error::Error;
use crate::globals::GLOBALS;
use nostr_types::{Event, EventReference, Id, PayRequestData, PublicKey, UncheckedUrl};
use std::ops::Deref;
/// The state that a Zap is in (it moves through 5 states before it is complete)
#[derive(Debug, Clone)]
pub enum ZapState {
None,
CheckingLnurl(Id, PublicKey, UncheckedUrl),
SeekingAmount(Id, PublicKey, PayRequestData, UncheckedUrl),
LoadingInvoice(Id, PublicKey),
ReadyToPay(Id, String), // String is the Zap Invoice as a string, to be shown as a QR code
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Freshness {
NeverSought,
Stale,
Fresh,
}
/// The ancestors of a note
pub struct EventAncestors {
/// The root of the thread, if we know it (even if we don't have it)
pub root: Option<EventReference>,
/// Whether we have the root of the thread in local storage
pub root_is_local: bool,
/// The highest connected ancestor that is in local storage (we can render from it
/// straight to the event in question without anything missing)
pub highest_connected_local: Option<Event>,
/// If set, the next event up that we don't have yet (or the initial event before
/// we have even checked)
pub highest_connected_remote: Option<EventReference>,
}
/// Get the ancestors of an event
pub(crate) fn get_event_ancestors(main: EventReference) -> Result<EventAncestors, Error> {
let mut ancestors = EventAncestors {
root: None,
root_is_local: false,
highest_connected_local: None,
highest_connected_remote: Some(main),
};
loop {
if let Some(ref remote) = ancestors.highest_connected_remote {
// See if the remote is local
if let Some(event) = GLOBALS.db().read_event_reference(remote)? {
// It is!
ancestors.highest_connected_local = Some(event.clone());
// Maybe there is one higher, if so we will try to climb when we loop
ancestors.highest_connected_remote = None;
if let Some(parent) = event.replies_to() {
ancestors.highest_connected_remote = Some(parent);
}
// Set root data if we now have it
if let Some(root) = event.replies_to_root() {
ancestors.root_is_local = GLOBALS.db().read_event_reference(&root)?.is_some();
ancestors.root = Some(root);
}
} else {
// We have nothing more locally.
return Ok(ancestors);
}
} else {
// We have everything, nothing more is needed from remote
return Ok(ancestors);
}
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Private(pub bool);
impl Deref for Private {
type Target = bool;
fn deref(&self) -> &Self::Target {
&self.0
}
}
// We define the readable/writable impls to be exactly as if this was just a bool.
// This way we don't have to upgrade the database from when it actually was just a bool.
impl<'a, C: speedy::Context> speedy::Readable<'a, C> for Private {
fn read_from<R: speedy::Reader<'a, C>>(reader: &mut R) -> Result<Self, C::Error> {
Ok(Private(bool::read_from(reader)?))
}
}
impl<C: speedy::Context> speedy::Writable<C> for Private {
fn write_to<T: ?Sized + speedy::Writer<C>>(&self, writer: &mut T) -> Result<(), C::Error> {
self.0.write_to(writer)
}
}
pub fn exponential_decay(base: f32, halflife_seconds: u64, elapsed_seconds: u64) -> f32 {
use std::f32::consts::E;
let decay_constant = 2.0_f32.ln() / halflife_seconds as f32;
base * E.powf(-decay_constant * elapsed_seconds as f32)
}