-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
you can now reply to notes Signed-off-by: William Casarin <[email protected]>
- Loading branch information
Showing
6 changed files
with
139 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,87 @@ | ||
use nostrdb::NoteBuilder; | ||
use nostrdb::{Note, NoteBuilder, NoteReply}; | ||
use std::collections::HashSet; | ||
|
||
pub struct NewPost { | ||
pub content: String, | ||
pub account: usize, | ||
} | ||
|
||
impl NewPost { | ||
pub fn to_note(&self, seckey: &[u8; 32]) -> nostrdb::Note { | ||
pub fn to_note(&self, seckey: &[u8; 32]) -> Note { | ||
NoteBuilder::new() | ||
.kind(1) | ||
.content(&self.content) | ||
.sign(seckey) | ||
.build() | ||
.expect("note should be ok") | ||
} | ||
|
||
pub fn to_reply(&self, seckey: &[u8; 32], replying_to: &Note) -> Note { | ||
let builder = NoteBuilder::new().kind(1).content(&self.content); | ||
|
||
let nip10 = NoteReply::new(replying_to.tags()); | ||
|
||
let mut builder = if let Some(root) = nip10.root() { | ||
builder | ||
.start_tag() | ||
.tag_str("e") | ||
.tag_str(&hex::encode(root.id)) | ||
.tag_str("") | ||
.tag_str("root") | ||
.start_tag() | ||
.tag_str("e") | ||
.tag_str(&hex::encode(replying_to.id())) | ||
.tag_str("") | ||
.tag_str("reply") | ||
.sign(seckey) | ||
} else { | ||
// we're replying to a post that isn't in a thread, | ||
// just add a single reply-to-root tag | ||
builder | ||
.start_tag() | ||
.tag_str("e") | ||
.tag_str(&hex::encode(replying_to.id())) | ||
.tag_str("") | ||
.tag_str("root") | ||
.sign(seckey) | ||
}; | ||
|
||
let mut seen_p: HashSet<&[u8; 32]> = HashSet::new(); | ||
|
||
builder = builder | ||
.start_tag() | ||
.tag_str("p") | ||
.tag_str(&hex::encode(replying_to.pubkey())); | ||
|
||
seen_p.insert(replying_to.pubkey()); | ||
|
||
for tag in replying_to.tags() { | ||
if tag.count() < 2 { | ||
continue; | ||
} | ||
|
||
if tag.get_unchecked(0).variant().str() != Some("p") { | ||
continue; | ||
} | ||
|
||
let id = if let Some(id) = tag.get_unchecked(0).variant().id() { | ||
id | ||
} else { | ||
continue; | ||
}; | ||
|
||
if seen_p.contains(id) { | ||
continue; | ||
} | ||
|
||
seen_p.insert(id); | ||
|
||
builder = builder.start_tag().tag_str("p").tag_str(&hex::encode(id)); | ||
} | ||
|
||
builder | ||
.sign(seckey) | ||
.build() | ||
.expect("expected build to work") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters