mirror of
https://github.com/scsibug/nostr-rs-relay.git
synced 2024-11-09 21:29:06 -05:00
refactor: clippy suggestions
This commit is contained in:
parent
ca0f01c94b
commit
e732f918f9
|
@ -22,9 +22,9 @@ impl From<CloseCmd> for Result<Close> {
|
||||||
fn from(cc: CloseCmd) -> Result<Close> {
|
fn from(cc: CloseCmd) -> Result<Close> {
|
||||||
// ensure command is correct
|
// ensure command is correct
|
||||||
if cc.cmd != "CLOSE" {
|
if cc.cmd != "CLOSE" {
|
||||||
return Err(Error::CommandUnknownError);
|
Err(Error::CommandUnknownError)
|
||||||
} else {
|
} else {
|
||||||
return Ok(Close { id: cc.id });
|
Ok(Close { id: cc.id })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,12 @@ pub struct ClientConn {
|
||||||
max_subs: usize,
|
max_subs: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for ClientConn {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ClientConn {
|
impl ClientConn {
|
||||||
/// Create a new, empty connection state.
|
/// Create a new, empty connection state.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
|
@ -77,7 +83,7 @@ impl ClientConn {
|
||||||
"Registered new subscription, currently have {} active subs",
|
"Registered new subscription, currently have {} active subs",
|
||||||
self.subscriptions.len()
|
self.subscriptions.len()
|
||||||
);
|
);
|
||||||
return Ok(());
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove the subscription for this connection.
|
/// Remove the subscription for this connection.
|
||||||
|
|
|
@ -130,7 +130,7 @@ pub fn write_event(conn: &mut Connection, e: &Event) -> Result<usize> {
|
||||||
let ev_id = tx.last_insert_rowid();
|
let ev_id = tx.last_insert_rowid();
|
||||||
// add all event tags into the event_ref table
|
// add all event tags into the event_ref table
|
||||||
let etags = e.get_event_tags();
|
let etags = e.get_event_tags();
|
||||||
if etags.len() > 0 {
|
if !etags.is_empty() {
|
||||||
for etag in etags.iter() {
|
for etag in etags.iter() {
|
||||||
tx.execute(
|
tx.execute(
|
||||||
"INSERT OR IGNORE INTO event_ref (event_id, referenced_event) VALUES (?1, ?2)",
|
"INSERT OR IGNORE INTO event_ref (event_id, referenced_event) VALUES (?1, ?2)",
|
||||||
|
@ -140,7 +140,7 @@ pub fn write_event(conn: &mut Connection, e: &Event) -> Result<usize> {
|
||||||
}
|
}
|
||||||
// add all event tags into the pubkey_ref table
|
// add all event tags into the pubkey_ref table
|
||||||
let ptags = e.get_pubkey_tags();
|
let ptags = e.get_pubkey_tags();
|
||||||
if ptags.len() > 0 {
|
if !ptags.is_empty() {
|
||||||
for ptag in ptags.iter() {
|
for ptag in ptags.iter() {
|
||||||
tx.execute(
|
tx.execute(
|
||||||
"INSERT OR IGNORE INTO event_ref (event_id, referenced_pubkey) VALUES (?1, ?2)",
|
"INSERT OR IGNORE INTO event_ref (event_id, referenced_pubkey) VALUES (?1, ?2)",
|
||||||
|
@ -238,7 +238,7 @@ fn query_from_sub(sub: &Subscription) -> String {
|
||||||
filter_components.push(created_clause);
|
filter_components.push(created_clause);
|
||||||
}
|
}
|
||||||
// combine all clauses, and add to filter_clauses
|
// combine all clauses, and add to filter_clauses
|
||||||
if filter_components.len() > 0 {
|
if !filter_components.is_empty() {
|
||||||
let mut fc = "( ".to_owned();
|
let mut fc = "( ".to_owned();
|
||||||
fc.push_str(&filter_components.join(" AND "));
|
fc.push_str(&filter_components.join(" AND "));
|
||||||
fc.push_str(" )");
|
fc.push_str(" )");
|
||||||
|
@ -247,7 +247,7 @@ fn query_from_sub(sub: &Subscription) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
// combine all filters with OR clauses, if any exist
|
// combine all filters with OR clauses, if any exist
|
||||||
if filter_clauses.len() > 0 {
|
if !filter_clauses.is_empty() {
|
||||||
query.push_str(" WHERE ");
|
query.push_str(" WHERE ");
|
||||||
query.push_str(&filter_clauses.join(" OR "));
|
query.push_str(&filter_clauses.join(" OR "));
|
||||||
}
|
}
|
||||||
|
|
29
src/event.rs
29
src/event.rs
|
@ -39,7 +39,7 @@ where
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let opt = Option::deserialize(deserializer)?;
|
let opt = Option::deserialize(deserializer)?;
|
||||||
Ok(opt.unwrap_or_else(|| vec![]))
|
Ok(opt.unwrap_or_else(Vec::new))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert network event to parsed/validated event.
|
/// Convert network event to parsed/validated event.
|
||||||
|
@ -47,11 +47,11 @@ impl From<EventCmd> for Result<Event> {
|
||||||
fn from(ec: EventCmd) -> Result<Event> {
|
fn from(ec: EventCmd) -> Result<Event> {
|
||||||
// ensure command is correct
|
// ensure command is correct
|
||||||
if ec.cmd != "EVENT" {
|
if ec.cmd != "EVENT" {
|
||||||
return Err(CommandUnknownError);
|
Err(CommandUnknownError)
|
||||||
} else if ec.event.is_valid() {
|
} else if ec.event.is_valid() {
|
||||||
return Ok(ec.event);
|
Ok(ec.event)
|
||||||
} else {
|
} else {
|
||||||
return Err(EventInvalid);
|
Err(EventInvalid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ impl Event {
|
||||||
}
|
}
|
||||||
let c = c_opt.unwrap();
|
let c = c_opt.unwrap();
|
||||||
// * compute the sha256sum.
|
// * compute the sha256sum.
|
||||||
let digest: sha256::Hash = sha256::Hash::hash(&c.as_bytes());
|
let digest: sha256::Hash = sha256::Hash::hash(c.as_bytes());
|
||||||
let hex_digest = format!("{:x}", digest);
|
let hex_digest = format!("{:x}", digest);
|
||||||
// * ensure the id matches the computed sha256sum.
|
// * ensure the id matches the computed sha256sum.
|
||||||
if self.id != hex_digest {
|
if self.id != hex_digest {
|
||||||
|
@ -88,10 +88,7 @@ impl Event {
|
||||||
let message = secp256k1::Message::from(digest);
|
let message = secp256k1::Message::from(digest);
|
||||||
let pubkey = schnorrsig::PublicKey::from_str(&self.pubkey).unwrap();
|
let pubkey = schnorrsig::PublicKey::from_str(&self.pubkey).unwrap();
|
||||||
let verify = secp.schnorrsig_verify(&sig, &message, &pubkey);
|
let verify = secp.schnorrsig_verify(&sig, &message, &pubkey);
|
||||||
match verify {
|
matches!(verify, Ok(()))
|
||||||
Ok(()) => true,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Convert event to canonical representation for signing.
|
/// Convert event to canonical representation for signing.
|
||||||
|
@ -99,7 +96,7 @@ impl Event {
|
||||||
// create a JsonValue for each event element
|
// create a JsonValue for each event element
|
||||||
let mut c: Vec<Value> = vec![];
|
let mut c: Vec<Value> = vec![];
|
||||||
// id must be set to 0
|
// id must be set to 0
|
||||||
let id = Number::from(0 as u64);
|
let id = Number::from(0_u64);
|
||||||
c.push(serde_json::Value::Number(id));
|
c.push(serde_json::Value::Number(id));
|
||||||
// public key
|
// public key
|
||||||
c.push(Value::String(self.pubkey.to_owned()));
|
c.push(Value::String(self.pubkey.to_owned()));
|
||||||
|
@ -135,10 +132,8 @@ impl Event {
|
||||||
pub fn get_event_tags(&self) -> Vec<&str> {
|
pub fn get_event_tags(&self) -> Vec<&str> {
|
||||||
let mut etags = vec![];
|
let mut etags = vec![];
|
||||||
for t in self.tags.iter() {
|
for t in self.tags.iter() {
|
||||||
if t.len() >= 2 {
|
if t.len() >= 2 && t.get(0).unwrap() == "e" {
|
||||||
if t.get(0).unwrap() == "e" {
|
etags.push(&t.get(1).unwrap()[..]);
|
||||||
etags.push(&t.get(1).unwrap()[..]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
etags
|
etags
|
||||||
|
@ -148,10 +143,8 @@ impl Event {
|
||||||
pub fn get_pubkey_tags(&self) -> Vec<&str> {
|
pub fn get_pubkey_tags(&self) -> Vec<&str> {
|
||||||
let mut ptags = vec![];
|
let mut ptags = vec![];
|
||||||
for t in self.tags.iter() {
|
for t in self.tags.iter() {
|
||||||
if t.len() >= 2 {
|
if t.len() >= 2 && t.get(0).unwrap() == "p" {
|
||||||
if t.get(0).unwrap() == "p" {
|
ptags.push(&t.get(1).unwrap()[..]);
|
||||||
ptags.push(&t.get(1).unwrap()[..]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ptags
|
ptags
|
||||||
|
|
|
@ -177,12 +177,9 @@ async fn nostr_server(
|
||||||
match parsed {
|
match parsed {
|
||||||
Ok(c) => {
|
Ok(c) => {
|
||||||
let stop_tx = running_queries.remove(&c.id);
|
let stop_tx = running_queries.remove(&c.id);
|
||||||
match stop_tx {
|
if let Some(tx) = stop_tx {
|
||||||
Some(tx) => {
|
info!("Removing query, telling DB to abandon query");
|
||||||
info!("Removing query, telling DB to abandon query");
|
tx.send(()).ok();
|
||||||
tx.send(()).ok();
|
|
||||||
},
|
|
||||||
None => {}
|
|
||||||
}
|
}
|
||||||
conn.unsubscribe(c);
|
conn.unsubscribe(c);
|
||||||
},
|
},
|
||||||
|
|
|
@ -44,7 +44,7 @@ pub struct NostrStream {
|
||||||
|
|
||||||
/// Given a websocket, return a protocol stream wrapper.
|
/// Given a websocket, return a protocol stream wrapper.
|
||||||
pub fn wrap_ws_in_nostr(ws: WebSocketStream<TcpStream>) -> NostrStream {
|
pub fn wrap_ws_in_nostr(ws: WebSocketStream<TcpStream>) -> NostrStream {
|
||||||
return NostrStream { ws_stream: ws };
|
NostrStream { ws_stream: ws }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Implement the [`Stream`] interface to produce Nostr messages.
|
/// Implement the [`Stream`] interface to produce Nostr messages.
|
||||||
|
|
|
@ -52,18 +52,18 @@ impl<'de> Deserialize<'de> for Subscription {
|
||||||
// check for array
|
// check for array
|
||||||
let va = v
|
let va = v
|
||||||
.as_array_mut()
|
.as_array_mut()
|
||||||
.ok_or(serde::de::Error::custom("not array"))?;
|
.ok_or_else(|| serde::de::Error::custom("not array"))?;
|
||||||
|
|
||||||
// check length
|
// check length
|
||||||
if va.len() < 3 {
|
if va.len() < 3 {
|
||||||
return Err(serde::de::Error::custom("not enough fields"));
|
return Err(serde::de::Error::custom("not enough fields"));
|
||||||
}
|
}
|
||||||
let mut i = va.into_iter();
|
let mut i = va.iter_mut();
|
||||||
// get command ("REQ") and ensure it is a string
|
// get command ("REQ") and ensure it is a string
|
||||||
let req_cmd_str: serde_json::Value = i.next().unwrap().take();
|
let req_cmd_str: serde_json::Value = i.next().unwrap().take();
|
||||||
let req = req_cmd_str.as_str().ok_or(serde::de::Error::custom(
|
let req = req_cmd_str
|
||||||
"first element of request was not a string",
|
.as_str()
|
||||||
))?;
|
.ok_or_else(|| serde::de::Error::custom("first element of request was not a string"))?;
|
||||||
if req != "REQ" {
|
if req != "REQ" {
|
||||||
return Err(serde::de::Error::custom("missing REQ command"));
|
return Err(serde::de::Error::custom("missing REQ command"));
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ impl<'de> Deserialize<'de> for Subscription {
|
||||||
let sub_id_str: serde_json::Value = i.next().unwrap().take();
|
let sub_id_str: serde_json::Value = i.next().unwrap().take();
|
||||||
let sub_id = sub_id_str
|
let sub_id = sub_id_str
|
||||||
.as_str()
|
.as_str()
|
||||||
.ok_or(serde::de::Error::custom("missing subscription id"))?;
|
.ok_or_else(|| serde::de::Error::custom("missing subscription id"))?;
|
||||||
|
|
||||||
let mut filters = vec![];
|
let mut filters = vec![];
|
||||||
for fv in i {
|
for fv in i {
|
||||||
|
@ -100,7 +100,7 @@ impl Subscription {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,9 +135,8 @@ impl ReqFilter {
|
||||||
self.id.as_ref().map(|v| v == &event.id).unwrap_or(true)
|
self.id.as_ref().map(|v| v == &event.id).unwrap_or(true)
|
||||||
&& self.since.map(|t| event.created_at > t).unwrap_or(true)
|
&& self.since.map(|t| event.created_at > t).unwrap_or(true)
|
||||||
&& self.kind_match(event.kind)
|
&& self.kind_match(event.kind)
|
||||||
&& self.author_match(&event)
|
&& self.author_match(event)
|
||||||
&& self.event_match(&event)
|
&& self.event_match(event)
|
||||||
&& true // match if all other fields are absent
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user