Skip to content

Commit 9c191f4

Browse files
committed
refactor(desktop): split reaction builders into events/reactions.rs
Adding the NIP-25 `p` tag pushed `desktop/src-tauri/src/events.rs` to 1017 lines, over the repo's enforced 1000-line limit (the Desktop Core CI red). Convert `events.rs` to `events/mod.rs` and move the kind:7 add / kind:5 remove builders into a focused `events/reactions.rs`. The `tag` helper and `MAX_EMOJI_CHARS` become `pub(super)`; the builders are re-exported so `events::build_reaction` / `events::build_remove_reaction` callers are unchanged. mod.rs is now 984 lines. Also adds four focused tests for the moved builders (e+p tags present, the self-reaction p tag survives nostr's self-tag scrub, over-long emoji rejected, remove targets the reaction id) — there were none before. Signed-off-by: Shani Singh <[email protected]>
1 parent 9fafc5d commit 9c191f4

2 files changed

Lines changed: 107 additions & 38 deletions

File tree

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ use buzz_core_pkg::kind::{KIND_IA_ARCHIVE_REQUEST, KIND_IA_UNARCHIVE_REQUEST};
1313
use nostr::{EventBuilder, EventId, Kind, Tag};
1414
use uuid::Uuid;
1515

16+
mod reactions;
17+
18+
pub use reactions::{build_reaction, build_remove_reaction};
19+
1620
// ── Constants ────────────────────────────────────────────────────────────────
1721

1822
/// Maximum content size — matches buzz-sdk (64 KiB).
@@ -22,11 +26,11 @@ const MAX_CONTENT_BYTES: usize = 64 * 1024;
2226
const MAX_MENTIONS: usize = 50;
2327

2428
/// Maximum emoji length in characters — matches buzz-sdk.
25-
const MAX_EMOJI_CHARS: usize = 64;
29+
pub(super) const MAX_EMOJI_CHARS: usize = 64;
2630

2731
// ── Helpers ──────────────────────────────────────────────────────────────────
2832

29-
fn tag(parts: Vec<&str>) -> Result<Tag, String> {
33+
pub(super) fn tag(parts: Vec<&str>) -> Result<Tag, String> {
3034
Tag::parse(parts).map_err(|e| format!("invalid tag: {e}"))
3135
}
3236

@@ -440,42 +444,6 @@ pub fn build_delete_compat(
440444
Ok(EventBuilder::new(Kind::Custom(5), "").tags(tags))
441445
}
442446

443-
// ── Reactions ────────────────────────────────────────────────────────────────
444-
445-
/// Kind 7 — NIP-25 reaction.
446-
///
447-
/// `target_author` is the pubkey of the reacted-to event. NIP-25 requires the
448-
/// `p` tag: it is what makes the reaction visible to a
449-
/// `{"kinds":[7],"#p":[<self>]}` notification filter (the shape the relay's
450-
/// push-lease surface advertises for kind 7).
451-
///
452-
/// `.allow_self_tagging()` is required: reacting to your own message makes the
453-
/// `p` tag match the signer, and nostr strips matching `p` tags by default.
454-
pub fn build_reaction(
455-
target_event_id: EventId,
456-
target_author: nostr::PublicKey,
457-
emoji: &str,
458-
) -> Result<EventBuilder, String> {
459-
if emoji.chars().count() > MAX_EMOJI_CHARS {
460-
return Err(format!(
461-
"emoji exceeds maximum length of {MAX_EMOJI_CHARS} characters"
462-
));
463-
}
464-
let tags = vec![
465-
tag(vec!["e", &target_event_id.to_hex()])?,
466-
tag(vec!["p", &target_author.to_hex()])?,
467-
];
468-
Ok(EventBuilder::new(Kind::Custom(7), emoji)
469-
.tags(tags)
470-
.allow_self_tagging())
471-
}
472-
473-
/// Kind 5 — delete a reaction event.
474-
pub fn build_remove_reaction(reaction_event_id: EventId) -> Result<EventBuilder, String> {
475-
let tags = vec![tag(vec!["e", &reaction_event_id.to_hex()])?];
476-
Ok(EventBuilder::new(Kind::Custom(5), "").tags(tags))
477-
}
478-
479447
// ── Canvas ───────────────────────────────────────────────────────────────────
480448

481449
/// Kind 40100 — set canvas.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//! NIP-25 reaction builders (kind:7 add, kind:5 remove).
2+
//!
3+
//! Split out of the parent `events` module to keep each builder file focused
4+
//! and under the repo's per-file line limit. Shares the parent's `tag` helper
5+
//! and `MAX_EMOJI_CHARS` limit so validation matches the other builders.
6+
7+
use nostr::{EventBuilder, EventId, Kind};
8+
9+
use super::{tag, MAX_EMOJI_CHARS};
10+
11+
/// Kind 7 — NIP-25 reaction.
12+
///
13+
/// `target_author` is the pubkey of the reacted-to event. NIP-25 recommends the
14+
/// `p` tag (a SHOULD): it is what makes the reaction visible to a
15+
/// `{"kinds":[7],"#p":[<self>]}` notification filter — the shape the relay's
16+
/// push-lease surface advertises for kind 7. Pass the event's signer, so the
17+
/// tag matches what every other Buzz client emits for the same target.
18+
///
19+
/// `.allow_self_tagging()` is required: reacting to your own message makes the
20+
/// `p` tag match the signer, and nostr strips matching `p` tags by default.
21+
pub fn build_reaction(
22+
target_event_id: EventId,
23+
target_author: nostr::PublicKey,
24+
emoji: &str,
25+
) -> Result<EventBuilder, String> {
26+
if emoji.chars().count() > MAX_EMOJI_CHARS {
27+
return Err(format!(
28+
"emoji exceeds maximum length of {MAX_EMOJI_CHARS} characters"
29+
));
30+
}
31+
let tags = vec![
32+
tag(vec!["e", &target_event_id.to_hex()])?,
33+
tag(vec!["p", &target_author.to_hex()])?,
34+
];
35+
Ok(EventBuilder::new(Kind::Custom(7), emoji)
36+
.tags(tags)
37+
.allow_self_tagging())
38+
}
39+
40+
/// Kind 5 — delete a reaction event.
41+
pub fn build_remove_reaction(reaction_event_id: EventId) -> Result<EventBuilder, String> {
42+
let tags = vec![tag(vec!["e", &reaction_event_id.to_hex()])?];
43+
Ok(EventBuilder::new(Kind::Custom(5), "").tags(tags))
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
use nostr::Keys;
50+
51+
fn has_tag(ev: &nostr::Event, key: &str, val: &str) -> bool {
52+
ev.tags.iter().any(|t| {
53+
let s = t.as_slice();
54+
s.first().map(String::as_str) == Some(key) && s.get(1).map(String::as_str) == Some(val)
55+
})
56+
}
57+
58+
#[test]
59+
fn reaction_carries_target_e_and_p_tags() {
60+
let target = Keys::generate();
61+
let author = Keys::generate().public_key();
62+
let target_id = EventId::all_zeros();
63+
let ev = build_reaction(target_id, author, "👍")
64+
.unwrap()
65+
.sign_with_keys(&target)
66+
.unwrap();
67+
assert_eq!(ev.kind, Kind::Custom(7));
68+
assert!(has_tag(&ev, "e", &target_id.to_hex()));
69+
assert!(has_tag(&ev, "p", &author.to_hex()));
70+
}
71+
72+
#[test]
73+
fn self_reaction_keeps_its_p_tag() {
74+
// Reacting to your own message: the `p` tag equals the signer, which
75+
// nostr strips unless `.allow_self_tagging()` is set. Pins that it isn't.
76+
let me = Keys::generate();
77+
let ev = build_reaction(EventId::all_zeros(), me.public_key(), "👍")
78+
.unwrap()
79+
.sign_with_keys(&me)
80+
.unwrap();
81+
assert!(has_tag(&ev, "p", &me.public_key().to_hex()));
82+
}
83+
84+
#[test]
85+
fn reaction_rejects_overlong_emoji() {
86+
let author = Keys::generate().public_key();
87+
let long = "a".repeat(MAX_EMOJI_CHARS + 1);
88+
assert!(build_reaction(EventId::all_zeros(), author, &long).is_err());
89+
}
90+
91+
#[test]
92+
fn remove_reaction_targets_the_reaction_event() {
93+
let reaction_id = EventId::all_zeros();
94+
let ev = build_remove_reaction(reaction_id)
95+
.unwrap()
96+
.sign_with_keys(&Keys::generate())
97+
.unwrap();
98+
assert_eq!(ev.kind, Kind::Custom(5));
99+
assert!(has_tag(&ev, "e", &reaction_id.to_hex()));
100+
}
101+
}

0 commit comments

Comments
 (0)