feat(NIP-16): Implement NIP16

NIP16 introduces a replaceable and ephemeral event range:
[10000..20000) for replaceable and [20000..30000) for
ephemeral.
This commit is contained in:
Semisol 2022-05-31 01:03:00 +03:00 committed by Greg Heartsfield
parent a36ad378f6
commit 168cfc3b26
2 changed files with 43 additions and 45 deletions

View File

@ -207,6 +207,16 @@ pub async fn db_writer(
} }
// TODO: cache recent list of authors to remove a DB call. // TODO: cache recent list of authors to remove a DB call.
let start = Instant::now(); let start = Instant::now();
if event.kind >= 20000 && event.kind < 30000 {
info!(
"published ephemeral event {:?} from {:?} in {:?}",
event.get_event_id_prefix(),
event.get_author_prefix(),
start.elapsed()
);
bcast_tx.send(event.clone()).ok();
event_write = true
} else {
match write_event(&mut pool.get()?, &event) { match write_event(&mut pool.get()?, &event) {
Ok(updated) => { Ok(updated) => {
if updated == 0 { if updated == 0 {
@ -233,6 +243,7 @@ pub async fn db_writer(
.ok(); .ok();
} }
} }
}
// use rate limit, if defined, and if an event was actually written. // use rate limit, if defined, and if an event was actually written.
if event_write { if event_write {
@ -302,32 +313,19 @@ pub fn write_event(conn: &mut PooledConnection, e: &Event) -> Result<usize> {
} }
} }
} }
// if this event is for a metadata update, hide every other kind=0 // if this event is replaceable update, hide every other replaceable
// event from the same author that was issued earlier than this. // event with the same kind from the same author that was issued
if e.kind == 0 { // earlier than this.
if e.kind == 0 || e.kind == 3 || (e.kind >= 10000 && e.kind < 20000) {
let update_count = tx.execute( let update_count = tx.execute(
"UPDATE event SET hidden=TRUE WHERE id!=? AND kind=0 AND author=? AND created_at <= ? and hidden!=TRUE", "UPDATE event SET hidden=TRUE WHERE id!=? AND kind=? AND author=? AND created_at <= ? and hidden!=TRUE",
params![ev_id, hex::decode(&e.pubkey).ok(), e.created_at], params![ev_id, e.kind, hex::decode(&e.pubkey).ok(), e.created_at],
)?; )?;
if update_count > 0 { if update_count > 0 {
info!( info!(
"hid {} older metadata events for author {:?}", "hid {} older replaceable kind {} events for author {:?}",
update_count,
e.get_author_prefix()
);
}
}
// if this event is for a contact update, hide every other kind=3
// event from the same author that was issued earlier than this.
if e.kind == 3 {
let update_count = tx.execute(
"UPDATE event SET hidden=TRUE WHERE id!=? AND kind=3 AND author=? AND created_at <= ? and hidden!=TRUE",
params![ev_id, hex::decode(&e.pubkey).ok(), e.created_at],
)?;
if update_count > 0 {
info!(
"hid {} older contact events for author {:?}",
update_count, update_count,
e.kind,
e.get_author_prefix() e.get_author_prefix()
); );
} }

View File

@ -35,7 +35,7 @@ impl From<config::Info> for RelayInfo {
description: i.description, description: i.description,
pubkey: i.pubkey, pubkey: i.pubkey,
contact: i.contact, contact: i.contact,
supported_nips: Some(vec![1, 2, 11, 15]), supported_nips: Some(vec![1, 2, 11, 15, 16]),
software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()), software: Some("https://git.sr.ht/~gheartsfield/nostr-rs-relay".to_owned()),
version: CARGO_PKG_VERSION.map(|x| x.to_owned()), version: CARGO_PKG_VERSION.map(|x| x.to_owned()),
} }