fix(NIP-33): only delete older events with matching 'd' tags

This commit is contained in:
Greg Heartsfield 2023-02-02 16:09:17 -06:00
parent 866c239cc9
commit 8004ea9b44
2 changed files with 4 additions and 4 deletions

View File

@ -180,14 +180,14 @@ ON CONFLICT (id) DO NOTHING"#,
if let Some(d_tag) = e.distinct_param() { if let Some(d_tag) = e.distinct_param() {
let update_count; let update_count;
if is_lower_hex(&d_tag) && (d_tag.len() % 2 == 0) { if is_lower_hex(&d_tag) && (d_tag.len() % 2 == 0) {
update_count = sqlx::query("DELETE FROM event WHERE kind=$1 AND pub_key=$2 AND id NOT IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=$1 AND e.pub_key=$2 AND t.name='d' AND t.value_hex=$3 ORDER BY created_at DESC LIMIT 1);") update_count = sqlx::query("DELETE FROM event WHERE kind=$1 AND pub_key=$2 AND id IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=$1 AND e.pub_key=$2 AND t.name='d' AND t.value_hex=$3 ORDER BY created_at DESC OFFSET 1);")
.bind(e.kind as i64) .bind(e.kind as i64)
.bind(hex::decode(&e.pubkey).ok()) .bind(hex::decode(&e.pubkey).ok())
.bind(hex::decode(d_tag).ok()) .bind(hex::decode(d_tag).ok())
.execute(&mut tx) .execute(&mut tx)
.await?.rows_affected(); .await?.rows_affected();
} else { } else {
update_count = sqlx::query("DELETE FROM event WHERE kind=$1 AND pub_key=$2 AND id NOT IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=$1 AND e.pub_key=$2 AND t.name='d' AND t.value=$3 ORDER BY created_at DESC LIMIT 1);") update_count = sqlx::query("DELETE FROM event WHERE kind=$1 AND pub_key=$2 AND id IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=$1 AND e.pub_key=$2 AND t.name='d' AND t.value=$3 ORDER BY created_at DESC OFFSET 1);")
.bind(e.kind as i64) .bind(e.kind as i64)
.bind(hex::decode(&e.pubkey).ok()) .bind(hex::decode(&e.pubkey).ok())
.bind(d_tag.as_bytes()) .bind(d_tag.as_bytes())

View File

@ -204,11 +204,11 @@ impl SqliteRepo {
let update_count; let update_count;
if is_lower_hex(&d_tag) && (d_tag.len() % 2 == 0) { if is_lower_hex(&d_tag) && (d_tag.len() % 2 == 0) {
update_count = tx.execute( update_count = tx.execute(
"DELETE FROM event WHERE kind=? AND author=? AND id NOT IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=? AND e.author=? AND t.name='d' AND t.value_hex=? ORDER BY created_at DESC LIMIT 1);", "DELETE FROM event WHERE kind=? AND author=? AND id IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=? AND e.author=? AND t.name='d' AND t.value_hex=? ORDER BY created_at DESC LIMIT -1 OFFSET 1);",
params![e.kind, pubkey_blob, e.kind, pubkey_blob, hex::decode(d_tag).ok()])?; params![e.kind, pubkey_blob, e.kind, pubkey_blob, hex::decode(d_tag).ok()])?;
} else { } else {
update_count = tx.execute( update_count = tx.execute(
"DELETE FROM event WHERE kind=? AND author=? AND id NOT IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=? AND e.author=? AND t.name='d' AND t.value=? ORDER BY created_at DESC LIMIT 1);", "DELETE FROM event WHERE kind=? AND author=? AND id IN (SELECT e.id FROM event e LEFT JOIN tag t ON e.id=t.event_id WHERE e.kind=? AND e.author=? AND t.name='d' AND t.value=? ORDER BY created_at DESC LIMIT -1 OFFSET 1);",
params![e.kind, pubkey_blob, e.kind, pubkey_blob, d_tag])?; params![e.kind, pubkey_blob, e.kind, pubkey_blob, d_tag])?;
} }
if update_count > 0 { if update_count > 0 {