Implement NIP-40 (event expiration)

This commit is contained in:
Tony Klink 2024-01-28 21:22:45 -06:00
parent 255905021c
commit bf08ac12e0
Signed by: klink
GPG key ID: 85175567C4D19231
2 changed files with 45 additions and 33 deletions

View file

@ -844,6 +844,12 @@ impl NostrSqlite {
let event = row.clone().to_event(); let event = row.clone().to_event();
if event.is_expired() {
return Err(Error::internal_with_message(
"Event has expired. Ignoring...",
));
}
Ok(event) Ok(event)
}) })
.await .await
@ -1059,8 +1065,10 @@ impl NostrSqlite {
let mut event_vec: Vec<Event> = vec![]; let mut event_vec: Vec<Event> = vec![];
while let Ok(Some(row)) = rows.next() { while let Ok(Some(row)) = rows.next() {
let event = EventRow::from(row).to_event(); let event = EventRow::from(row).to_event();
if !event.is_expired() {
event_vec.push(event); event_vec.push(event);
} }
}
Ok(event_vec) Ok(event_vec)
}) })
@ -1134,8 +1142,6 @@ impl NostrSqlite {
let Ok(query_result) = connection let Ok(query_result) = connection
.interact(move |conn| { .interact(move |conn| {
let (sql, values) = sql_statement let (sql, values) = sql_statement
.clear_selects()
.column(EventsTable::EventId)
.order_by(EventsTable::CreatedAt, sq_order.to_owned()) .order_by(EventsTable::CreatedAt, sq_order.to_owned())
.build_rusqlite(SqliteQueryBuilder); .build_rusqlite(SqliteQueryBuilder);
@ -1144,9 +1150,10 @@ impl NostrSqlite {
let mut event_vec: Vec<EventId> = vec![]; let mut event_vec: Vec<EventId> = vec![];
while let Ok(Some(row)) = rows.next() { while let Ok(Some(row)) = rows.next() {
let event_id_string: String = row.get(0).unwrap(); let event = EventRow::from(row).to_event();
let event_id = EventId::from_str(&event_id_string).unwrap(); if !event.is_expired() {
event_vec.push(event_id); event_vec.push(event.id);
}
} }
Ok(event_vec) Ok(event_vec)
@ -1178,7 +1185,8 @@ impl NostrSqlite {
}; };
let Ok(query_result) = connection let Ok(query_result) = connection
.interact(move |conn: &mut rusqlite::Connection| -> Result<bool, Error> { .interact(
move |conn: &mut rusqlite::Connection| -> Result<bool, Error> {
let (sql, value) = Query::select() let (sql, value) = Query::select()
.from(EventsTable::Table) .from(EventsTable::Table)
.columns([EventsTable::EventId, EventsTable::CreatedAt]) .columns([EventsTable::EventId, EventsTable::CreatedAt])
@ -1188,7 +1196,9 @@ impl NostrSqlite {
.equals((EventsTable::Table, EventsTable::EventId)), .equals((EventsTable::Table, EventsTable::EventId)),
) )
.and_where(sea_query::Expr::col((TagsTable::Table, TagsTable::Tag)).eq("a")) .and_where(sea_query::Expr::col((TagsTable::Table, TagsTable::Tag)).eq("a"))
.and_where(sea_query::Expr::col((TagsTable::Table, TagsTable::Value)).eq(ident)) .and_where(
sea_query::Expr::col((TagsTable::Table, TagsTable::Value)).eq(ident),
)
.and_where( .and_where(
sea_query::Expr::col((EventsTable::Table, EventsTable::CreatedAt)) sea_query::Expr::col((EventsTable::Table, EventsTable::CreatedAt))
.gte(timestamp.as_i64()), .gte(timestamp.as_i64()),
@ -1200,11 +1210,12 @@ impl NostrSqlite {
let mut rows = stmt.query(&*value.as_params()).unwrap(); let mut rows = stmt.query(&*value.as_params()).unwrap();
if let Ok(Some(record)) = rows.next() { if let Ok(Some(record)) = rows.next() {
return Ok(false) return Ok(false);
} }
Ok(true) Ok(true)
}) },
)
.await .await
else { else {
return Err(Error::internal_with_message( return Err(Error::internal_with_message(
@ -1272,7 +1283,8 @@ impl NostrDatabase for NostrSqlite {
coordinate: &Coordinate, coordinate: &Coordinate,
timestamp: Timestamp, timestamp: Timestamp,
) -> Result<bool, Self::Err> { ) -> Result<bool, Self::Err> {
self.has_coordinate_been_deleted(coordinate, timestamp).await self.has_coordinate_been_deleted(coordinate, timestamp)
.await
} }
/// Set [`EventId`] as seen by relay /// Set [`EventId`] as seen by relay

View file

@ -43,7 +43,7 @@ impl Config {
"contact": "klink@zhitno.st", "contact": "klink@zhitno.st",
"name": "zhitno.st", "name": "zhitno.st",
"description": "Very *special* nostr relay", "description": "Very *special* nostr relay",
"supported_nips": [ 1, 9, 11, 12, 15, 16, 20, 22, 28, 33, 45 ], "supported_nips": [ 1, 2, 9, 11, 12, 15, 16, 20, 22, 28, 33, 40, 45 ],
"software": "git+https://git.zhitno.st/Klink/sneedstr.git", "software": "git+https://git.zhitno.st/Klink/sneedstr.git",
"version": "0.1.0" "version": "0.1.0"
}) })