@@ -240,14 +240,7 @@ proc updateSchema*(db: DbConn) =
240240 store INTEGER DEFAULT 0,
241241 PRIMARY KEY (period, ip, pubkey)
242242 ) """ )
243-
244- # ----------- in-memory stuff
245- db.exec (sql """ CREATE TEMPORARY TABLE note_sub (
246- topic TEXT PRIMARY KEY,
247- pubkey TEXT NOT NULL
248- ) """ )
249- db.exec (sql " CREATE INDEX note_sub_pubkey ON note_sub(pubkey)" )
250-
243+
251244
252245# -------------------------------------------------------------------
253246# Relay code
@@ -318,7 +311,6 @@ proc initAuth*[T](relay: Relay[T], client: T): RelayConnection[T] =
318311proc disconnect * [T](relay: Relay [T], conn: RelayConnection [T]) =
319312 if conn.pubkey.isSome:
320313 let pubkey = conn.pubkey.get ()
321- relay.db.exec (sql " DELETE FROM note_sub WHERE pubkey=?" , pubkey)
322314 relay.clients.del (pubkey)
323315 info & " [{ conn.pubkey.abbr} ] disconnected "
324316
@@ -416,21 +408,6 @@ proc delExpiredNotes(relay: Relay) =
416408 let offstring = & " { offset} seconds"
417409 relay.db.exec (sql " DELETE FROM note WHERE created <= datetime('now', ?)" , offstring)
418410
419- proc addNoteSub (relay: Relay , topic: string , pubkey: PublicKey ) =
420- # # Record that a pubkey is subscribed to a topic
421- try :
422- relay.db.exec (sql " INSERT INTO note_sub (topic, pubkey) VALUES (?,?)" , topic.DbBlob , pubkey)
423- info & " [{ pubkey.abbr} ] sub { topic} "
424- except CatchableError :
425- raise ValueError .newException (" Topic already subscribed" )
426-
427- proc getNoteSub (relay: Relay , topic: string ): Option [PublicKey ] =
428- # # Return a PublicKey who is listening for a note by topic.
429- relay.delExpiredNotes ()
430- let orow = relay.db.getRow (sql " SELECT pubkey FROM note_sub WHERE topic = ?" , topic.DbBlob )
431- if orow.isSome:
432- return some (PublicKey .fromDB (orow.get ()[0 ].b))
433-
434411proc popNote (relay: Relay , topic: string ): Option [string ] =
435412 let db = relay.db
436413 relay.delExpiredNotes ()
@@ -449,10 +426,6 @@ proc popNote(relay: Relay, topic: string): Option[string] =
449426 warn & " [note] error " & getCurrentExceptionMsg ()
450427 db.exec (sql " ROLLBACK" )
451428
452- proc delNoteSub (relay: Relay , topic: string ) =
453- relay.db.exec (sql " DELETE FROM note_sub WHERE topic = ?" , topic.DbBlob )
454- info & " [note] del { topic} "
455-
456429proc noteCount (relay: Relay , pubkey: PublicKey ): int =
457430 # # Return the number of notes currently published by this ip
458431 relay.db.getRow (sql " SELECT count(*) FROM note WHERE src = ?" , pubkey).get ()[0 ].i.int
@@ -572,34 +545,15 @@ proc handleCommand*[T](relay: Relay[T], conn: var RelayConnection[T], cmd: Relay
572545 pubkey = pubkey,
573546 publish = 1 ,
574547 )
575- let opubkey = relay.getNoteSub (cmd.pub_topic)
576- if opubkey.isSome:
577- # someone is waiting
578- var other_conn = relay.clients[opubkey.get ()]
579- conn.sendOkay (cmd)
580- other_conn.sendMessage (RelayMessage (
581- kind: Note ,
582- resp_id: 0 , # Not triggered by other_conn's command
583- note_data: cmd.pub_data,
584- note_topic: cmd.pub_topic,
585- ))
586- relay.delNoteSub (cmd.pub_topic)
587- relay.db.record_transfer_stat (
588- ip = other_conn.ip,
589- pubkey = other_conn.pubkey.get (),
590- data_out = cmd.pub_data.len,
548+ try :
549+ relay.db.exec (sql " INSERT INTO note (topic, data, src) VALUES (?, ?, ?)" ,
550+ cmd.pub_topic.DbBlob ,
551+ cmd.pub_data.DbBlob ,
552+ pubkey,
591553 )
592- else :
593- # no one is waiting
594- try :
595- relay.db.exec (sql " INSERT INTO note (topic, data, src) VALUES (?, ?, ?)" ,
596- cmd.pub_topic.DbBlob ,
597- cmd.pub_data.DbBlob ,
598- pubkey,
599- )
600- conn.sendOkay (cmd)
601- except :
602- conn.sendError (cmd, " Duplicate topic" , Generic )
554+ conn.sendOkay (cmd)
555+ except :
556+ conn.sendError (cmd, " Duplicate topic" , Generic )
603557 of FetchNote :
604558 if cmd.fetch_topic.len > RELAY_MAX_TOPIC_SIZE :
605559 conn.sendError (cmd, " Topic too long" , TooLarge )
@@ -620,8 +574,8 @@ proc handleCommand*[T](relay: Relay[T], conn: var RelayConnection[T], cmd: Relay
620574 data_out = data.len,
621575 )
622576 else :
623- # the note isn 't here yet
624- relay. addNoteSub (cmd.fetch_topic, conn.pubkey. get () )
577+ # the note doesn 't exist
578+ conn. sendError (cmd, " Topic not found " , NotFound )
625579 of SendData :
626580 if cmd.send_val.len > RELAY_MAX_MESSAGE_SIZE :
627581 conn.sendError (cmd, " Data too long" , TooLarge )
0 commit comments