mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-09 21:29:06 -05:00
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:
parent
a36ad378f6
commit
168cfc3b26
86
src/db.rs
86
src/db.rs
|
@ -207,30 +207,41 @@ 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();
|
||||||
match write_event(&mut pool.get()?, &event) {
|
if event.kind >= 20000 && event.kind < 30000 {
|
||||||
Ok(updated) => {
|
info!(
|
||||||
if updated == 0 {
|
"published ephemeral event {:?} from {:?} in {:?}",
|
||||||
trace!("ignoring duplicate event");
|
event.get_event_id_prefix(),
|
||||||
} else {
|
event.get_author_prefix(),
|
||||||
info!(
|
start.elapsed()
|
||||||
"persisted event {:?} from {:?} in {:?}",
|
);
|
||||||
event.get_event_id_prefix(),
|
bcast_tx.send(event.clone()).ok();
|
||||||
event.get_author_prefix(),
|
event_write = true
|
||||||
start.elapsed()
|
} else {
|
||||||
);
|
match write_event(&mut pool.get()?, &event) {
|
||||||
event_write = true;
|
Ok(updated) => {
|
||||||
// send this out to all clients
|
if updated == 0 {
|
||||||
bcast_tx.send(event.clone()).ok();
|
trace!("ignoring duplicate event");
|
||||||
|
} else {
|
||||||
|
info!(
|
||||||
|
"persisted event {:?} from {:?} in {:?}",
|
||||||
|
event.get_event_id_prefix(),
|
||||||
|
event.get_author_prefix(),
|
||||||
|
start.elapsed()
|
||||||
|
);
|
||||||
|
event_write = true;
|
||||||
|
// send this out to all clients
|
||||||
|
bcast_tx.send(event.clone()).ok();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
warn!("event insert failed: {:?}", err);
|
||||||
|
notice_tx
|
||||||
|
.try_send(
|
||||||
|
"relay experienced an error trying to publish the latest event"
|
||||||
|
.to_owned(),
|
||||||
|
)
|
||||||
|
.ok();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
warn!("event insert failed: {:?}", err);
|
|
||||||
notice_tx
|
|
||||||
.try_send(
|
|
||||||
"relay experienced an error trying to publish the latest event"
|
|
||||||
.to_owned(),
|
|
||||||
)
|
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user