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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use crate::user::CaracalUser;
use duration_str::parse as dur_parse;
use nostr_sdk::prelude::*;
use std::sync::Arc;
pub async fn user_subscribe(user: &CaracalUser) {
let Ok(contacts) =
user.client.get_contact_list(dur_parse("8s").unwrap()).await
else {
eprintln!("Failed to retrieve contact list");
return;
};
let now = Timestamp::now();
let sub_id = SubscriptionId::new("user");
let Ok(signer_pubk) = user.signer.get_public_key().await else {
eprintln!("Cannot get public key from signer");
return;
};
user.client.connect().await;
user.client.unsubscribe(&sub_id).await;
let mut follow_pubkeys: Vec<PublicKey> =
contacts.into_iter().map(|c| c.public_key).collect();
follow_pubkeys.push(signer_pubk);
let subs = vec![
// Metadata and relay+inbox lists
Filter::new()
.kind(Kind::Metadata)
.kind(Kind::RelayList)
.kind(Kind::InboxRelays)
.since(now - dur_parse("6mon").unwrap())
.limit(30),
// Contact and bookmarks
Filter::new()
.kind(Kind::ContactList)
.kind(Kind::Bookmarks)
// Payment targets
.kind(Kind::Custom(10133))
.since(now - dur_parse("90d").unwrap())
.limit(10),
// Notes, reactions and reposts
Filter::new()
.kind(Kind::TextNote)
.kind(Kind::LongFormTextNote)
.kind(Kind::Reaction)
.kind(Kind::GenericRepost)
.kind(Kind::Repost)
.since(now - dur_parse("90d").unwrap())
.limit(100),
];
for pubkey in &follow_pubkeys {
for sub in &subs {
if let Err(err) = user
.client
.subscribe_with_id(
SubscriptionId::new(format!("{pubkey}")),
sub.clone().author(*pubkey),
None,
)
.await
{
eprintln!("Error subscribing: {err}")
}
}
}
}
pub async fn prepare_nostr_client(client: Arc<Client>) {
client.connect().await;
let _ = client
.subscribe(
Filter::new()
.kind(Kind::Metadata)
.limit(1000)
.since(Timestamp::now() - dur_parse("90d").unwrap()),
None,
)
.await;
}
pub async fn handle_nostr_notifications(
client: Arc<Client>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
client
.handle_notifications(|notification| async {
if let RelayPoolNotification::Event {
relay_url: _,
subscription_id: _,
event,
} = notification
{
match event.kind {
Kind::TextNote => {
if let Err(err) = client
.subscribe(
Filter::new()
.kind(Kind::Metadata)
.author(event.pubkey)
.limit(1),
None,
)
.await
{
eprintln!("Metadata sub failed: {err}");
}
}
Kind::RelayList => {
for (url, _metadata) in
nip65::extract_relay_list(&event)
{
let Ok(_) = client.add_discovery_relay(url).await
else {
continue;
};
let Ok(_) = client.add_read_relay(url).await else {
continue;
};
}
}
_ => {}
}
} else if let RelayPoolNotification::Message {
relay_url: _,
message,
} = notification
&& let RelayMessage::Event {
subscription_id: _,
ref event,
} = message
&& let Kind::RelayList = event.kind
{
for (url, _metadata) in nip65::extract_relay_list(event) {
let Ok(_) = client.add_discovery_relay(url).await else {
continue;
};
let Ok(_) = client.add_read_relay(url).await else {
continue;
};
/*
if let Some(RelayMetadata::Write) = metadata {
println!("{} {}", "=>".cyan(), url.to_string().bold().purple());
}
*/
}
}
Ok(false)
})
.await?;
Ok(())
}