hashtree-cli 0.2.67

Hashtree daemon and CLI - content-addressed storage with P2P sync
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;

use hashtree_cli::config::{ensure_keys_string, parse_npub};
use hashtree_cli::Config;

fn load_hex_list(path: &Path) -> Result<Vec<String>> {
    if path.exists() {
        let data = std::fs::read_to_string(path)?;
        Ok(serde_json::from_str(&data).unwrap_or_default())
    } else {
        Ok(Vec::new())
    }
}

fn save_hex_list(path: &Path, list: &[String]) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::write(path, serde_json::to_string_pretty(list)?)?;
    Ok(())
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct MuteEntry {
    pub(crate) pubkey: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub(crate) reason: Option<String>,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub(crate) enum MuteUpdate {
    Added,
    Updated,
    Removed,
    Unchanged,
}

fn normalize_reason(reason: Option<&str>) -> Option<String> {
    reason
        .map(|r| r.trim())
        .filter(|r| !r.is_empty())
        .map(|r| r.to_string())
}

pub(crate) fn load_mute_entries(path: &Path) -> Result<Vec<MuteEntry>> {
    if !path.exists() {
        return Ok(Vec::new());
    }

    let data = std::fs::read_to_string(path)?;
    let value: serde_json::Value = match serde_json::from_str(&data) {
        Ok(v) => v,
        Err(_) => return Ok(Vec::new()),
    };

    let Some(items) = value.as_array() else {
        return Ok(Vec::new());
    };

    let mut entries = Vec::new();
    for item in items {
        match item {
            serde_json::Value::String(pk) => entries.push(MuteEntry {
                pubkey: pk.to_string(),
                reason: None,
            }),
            serde_json::Value::Object(obj) => {
                let pubkey = obj.get("pubkey").and_then(|v| v.as_str());
                if let Some(pubkey) = pubkey {
                    let reason = obj.get("reason").and_then(|v| v.as_str());
                    entries.push(MuteEntry {
                        pubkey: pubkey.to_string(),
                        reason: normalize_reason(reason),
                    });
                }
            }
            _ => {}
        }
    }

    Ok(entries)
}

fn save_mute_entries(path: &Path, entries: &[MuteEntry]) -> Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let data = if entries.iter().all(|entry| entry.reason.is_none()) {
        let pubkeys: Vec<String> = entries.iter().map(|entry| entry.pubkey.clone()).collect();
        serde_json::to_string_pretty(&pubkeys)?
    } else {
        serde_json::to_string_pretty(entries)?
    };

    std::fs::write(path, data)?;
    Ok(())
}

pub(crate) fn update_mute_list_file_with_status(
    path: &Path,
    target_hex: &str,
    reason: Option<&str>,
    add: bool,
) -> Result<(Vec<MuteEntry>, MuteUpdate)> {
    let mut entries = load_mute_entries(path)?;
    let normalized_reason = normalize_reason(reason);

    let update = if add {
        if let Some(entry) = entries.iter_mut().find(|entry| entry.pubkey == target_hex) {
            if let Some(new_reason) = normalized_reason {
                if entry.reason.as_deref() != Some(new_reason.as_str()) {
                    entry.reason = Some(new_reason);
                    MuteUpdate::Updated
                } else {
                    MuteUpdate::Unchanged
                }
            } else {
                MuteUpdate::Unchanged
            }
        } else {
            entries.push(MuteEntry {
                pubkey: target_hex.to_string(),
                reason: normalized_reason,
            });
            MuteUpdate::Added
        }
    } else if let Some(pos) = entries.iter().position(|entry| entry.pubkey == target_hex) {
        entries.remove(pos);
        MuteUpdate::Removed
    } else {
        MuteUpdate::Unchanged
    };

    if matches!(
        update,
        MuteUpdate::Added | MuteUpdate::Updated | MuteUpdate::Removed
    ) {
        save_mute_entries(path, &entries)?;
    }

    Ok((entries, update))
}

pub(crate) fn update_hex_list_file_with_status(
    path: &Path,
    target_hex: &str,
    add: bool,
) -> Result<(Vec<String>, bool)> {
    let mut list = load_hex_list(path)?;
    let changed = if add {
        if list.contains(&target_hex.to_string()) {
            false
        } else {
            list.push(target_hex.to_string());
            true
        }
    } else if let Some(pos) = list.iter().position(|x| x == target_hex) {
        list.remove(pos);
        true
    } else {
        false
    };

    if changed {
        save_hex_list(path, &list)?;
    }

    Ok((list, changed))
}

#[cfg(test)]
pub(crate) fn update_hex_list_file(
    path: &Path,
    target_hex: &str,
    add: bool,
) -> Result<Vec<String>> {
    let (list, _changed) = update_hex_list_file_with_status(path, target_hex, add)?;
    Ok(list)
}

fn build_pubkey_list_event(
    kind: nostr::Kind,
    pubkeys: &[String],
    keys: &nostr::Keys,
) -> Result<nostr::Event> {
    use nostr::{EventBuilder, PublicKey, Tag};

    let tags: Vec<Tag> = pubkeys
        .iter()
        .filter_map(|pk_hex| PublicKey::from_hex(pk_hex).ok().map(Tag::public_key))
        .collect();

    EventBuilder::new(kind, "")
        .tags(tags)
        .sign_with_keys(keys)
        .context("Failed to sign list event")
}

pub(crate) fn build_mute_list_event(
    mutes: &[MuteEntry],
    keys: &nostr::Keys,
) -> Result<nostr::Event> {
    use nostr::{EventBuilder, PublicKey, Tag};

    let mut tags: Vec<Tag> = Vec::new();
    for entry in mutes {
        let Ok(pubkey) = PublicKey::from_hex(&entry.pubkey) else {
            continue;
        };

        if let Some(reason) = entry.reason.as_ref().filter(|r| !r.is_empty()) {
            tags.push(Tag::parse(vec![
                "p".to_string(),
                pubkey.to_hex(),
                reason.to_string(),
            ])?);
        } else {
            tags.push(Tag::public_key(pubkey));
        }
    }

    EventBuilder::new(nostr::Kind::Custom(10000), "")
        .tags(tags)
        .sign_with_keys(keys)
        .context("Failed to sign mute list event")
}

async fn publish_event_to_relays(relays: &[String], event_json: &str) -> usize {
    use futures::sink::SinkExt;
    use tokio_tungstenite::connect_async;

    let mut success_count = 0;
    for relay in relays {
        if let Ok((mut ws, _)) = connect_async(relay).await {
            if ws
                .send(tokio_tungstenite::tungstenite::Message::Text(
                    event_json.to_string(),
                ))
                .await
                .is_ok()
            {
                success_count += 1;
            }
            let _ = ws.close(None).await;
        }
    }
    success_count
}

/// Follow or unfollow a user by publishing an updated kind 3 contact list.
pub(crate) async fn follow_user(data_dir: &Path, npub_str: &str, follow: bool) -> Result<()> {
    use nostr::{ClientMessage, JsonUtil, Keys, Kind};

    // Load config for relay list
    let config = Config::load()?;

    // Ensure nsec exists
    let (nsec_str, _) = ensure_keys_string()?;
    let keys = Keys::parse(&nsec_str).context("Failed to parse nsec")?;

    // Parse target npub
    let target_pubkey = parse_npub(npub_str).context("Invalid npub")?;
    let target_pubkey_hex = hex::encode(target_pubkey);

    // Update contact list locally
    let contacts_file = data_dir.join("contacts.json");
    let (contacts, changed) =
        update_hex_list_file_with_status(&contacts_file, &target_pubkey_hex, follow)?;

    if follow {
        if changed {
            println!("Following: {}", npub_str);
        } else {
            println!("Already following: {}", npub_str);
            return Ok(());
        }
    } else if changed {
        println!("Unfollowed: {}", npub_str);
    } else {
        println!("Not following: {}", npub_str);
        return Ok(());
    }

    let event = build_pubkey_list_event(Kind::ContactList, &contacts, &keys)?;

    let event_json = ClientMessage::event(event).as_json();

    let success_count = publish_event_to_relays(&config.nostr.relays, &event_json).await;

    println!("Published contact list to {} relays", success_count);
    Ok(())
}

/// Mute or unmute a user by publishing an updated kind 10000 mute list.
pub(crate) async fn mute_user(
    data_dir: &Path,
    npub_str: &str,
    reason: Option<&str>,
    mute: bool,
) -> Result<()> {
    use nostr::{ClientMessage, JsonUtil, Keys};

    let config = Config::load()?;

    let (nsec_str, _) = ensure_keys_string()?;
    let keys = Keys::parse(&nsec_str).context("Failed to parse nsec")?;

    let target_pubkey = parse_npub(npub_str).context("Invalid npub")?;
    let target_pubkey_hex = hex::encode(target_pubkey);

    let mutes_file = data_dir.join("mutes.json");
    let (mutes, update) =
        update_mute_list_file_with_status(&mutes_file, &target_pubkey_hex, reason, mute)?;

    if mute {
        if update == MuteUpdate::Added {
            println!("Muted: {}", npub_str);
        } else if update == MuteUpdate::Updated {
            println!("Updated mute reason for: {}", npub_str);
        } else {
            println!("Already muted: {}", npub_str);
            return Ok(());
        }
    } else if update == MuteUpdate::Removed {
        println!("Unmuted: {}", npub_str);
    } else {
        println!("Not muted: {}", npub_str);
        return Ok(());
    }

    let event = build_mute_list_event(&mutes, &keys)?;
    let event_json = ClientMessage::event(event).as_json();

    let success_count = publish_event_to_relays(&config.nostr.relays, &event_json).await;

    println!("Published mute list to {} relays", success_count);
    Ok(())
}

/// Show or update Nostr profile (kind 0).
pub(crate) async fn update_profile(
    name: Option<String>,
    about: Option<String>,
    picture: Option<String>,
) -> Result<()> {
    use nostr::nips::nip19::ToBech32;
    use nostr::{EventBuilder, Filter, Keys, Kind};
    use nostr_sdk::ClientBuilder;
    use std::time::Duration;

    // Load config for relay list
    let config = Config::load()?;

    // Ensure nsec exists
    let (nsec_str, _) = ensure_keys_string()?;
    let keys = Keys::parse(&nsec_str).context("Failed to parse nsec")?;
    let npub = keys.public_key().to_bech32()?;

    // Check if we're just showing the profile (no args)
    let is_show_only = name.is_none() && about.is_none() && picture.is_none();

    // Fetch existing profile
    let client = ClientBuilder::default().build();
    for relay in &config.nostr.relays {
        let _ = client.add_relay(relay).await;
    }
    client.connect().await;

    // Wait for relay connections to establish
    tokio::time::sleep(Duration::from_millis(500)).await;

    let filter = Filter::new()
        .author(keys.public_key())
        .kind(Kind::Metadata)
        .limit(1);

    let timeout = Duration::from_secs(5);
    let events = tokio::time::timeout(timeout, client.fetch_events(filter, timeout))
        .await
        .ok()
        .and_then(|r| r.ok())
        .map(|events| events.to_vec())
        .unwrap_or_default();
    let _ = client.disconnect().await;

    // Parse existing profile or start fresh
    let mut profile: serde_json::Map<String, serde_json::Value> = events
        .into_iter()
        .next()
        .and_then(|e| serde_json::from_str(&e.content).ok())
        .unwrap_or_default();

    if is_show_only {
        // Just display current profile
        println!("Profile: {}\n", npub);
        if let Some(n) = profile.get("name").and_then(|v| v.as_str()) {
            println!("  name:    {}", n);
        }
        if let Some(a) = profile.get("about").and_then(|v| v.as_str()) {
            println!("  about:   {}", a);
        }
        if let Some(p) = profile.get("picture").and_then(|v| v.as_str()) {
            println!("  picture: {}", p);
        }
        if profile.is_empty() {
            println!("  (no profile set)");
        }
        return Ok(());
    }

    // Update fields
    if let Some(n) = name {
        profile.insert("name".to_string(), serde_json::Value::String(n));
    }
    if let Some(a) = about {
        profile.insert("about".to_string(), serde_json::Value::String(a));
    }
    if let Some(p) = picture {
        profile.insert("picture".to_string(), serde_json::Value::String(p));
    }

    // Build and sign kind 0 event
    let content = serde_json::to_string(&profile)?;
    let event = EventBuilder::new(Kind::Metadata, &content)
        .sign_with_keys(&keys)
        .context("Failed to sign profile event")?;

    // Reuse the client we already have connected for publishing
    let client = ClientBuilder::default().build();
    for relay in &config.nostr.relays {
        let _ = client.add_relay(relay).await;
    }
    client.connect().await;
    tokio::time::sleep(Duration::from_millis(500)).await;

    // Publish using nostr_sdk
    match client.send_event(&event).await {
        Ok(output) => {
            let success_count = output.success.len();
            let failed_count = output.failed.len();
            if success_count > 0 {
                println!("Profile updated, published to {} relays", success_count);
            }
            if failed_count > 0 {
                eprintln!("Failed to publish to {} relays", failed_count);
            }
        }
        Err(e) => {
            eprintln!("Failed to publish profile: {}", e);
        }
    }

    let _ = client.disconnect().await;
    Ok(())
}

/// List users we follow.
pub(crate) async fn list_following(data_dir: &Path) -> Result<()> {
    use nostr::nips::nip19::ToBech32;
    use nostr::PublicKey;

    // Load contacts from local storage
    let contacts_file = data_dir.join("contacts.json");
    let contacts = load_hex_list(&contacts_file)?;

    if contacts.is_empty() {
        println!("Not following anyone");
        return Ok(());
    }

    println!("Following {} users:", contacts.len());
    for pk_hex in &contacts {
        if let Ok(pk) = PublicKey::from_hex(pk_hex) {
            println!("  {}", pk.to_bech32().expect("npub encoding is infallible"));
        } else {
            println!("  {} (invalid)", pk_hex);
        }
    }

    Ok(())
}

/// List users we mute.
pub(crate) async fn list_muted(data_dir: &Path) -> Result<()> {
    use nostr::nips::nip19::ToBech32;
    use nostr::PublicKey;

    let mutes_file = data_dir.join("mutes.json");
    let mutes = load_mute_entries(&mutes_file)?;

    if mutes.is_empty() {
        println!("Not muting anyone");
        return Ok(());
    }

    println!("Muted {} users:", mutes.len());
    for entry in &mutes {
        let label = if let Ok(pk) = PublicKey::from_hex(&entry.pubkey) {
            pk.to_bech32().expect("npub encoding is infallible")
        } else {
            format!("{} (invalid)", entry.pubkey)
        };
        if let Some(reason) = entry.reason.as_ref() {
            println!("  {} - {}", label, reason);
        } else {
            println!("  {}", label);
        }
    }

    Ok(())
}