Skip to content

Commit 0fcdee8

Browse files
committed
refactor: resultify token::exists
1 parent 26ae686 commit 0fcdee8

File tree

4 files changed

+15
-15
lines changed

4 files changed

+15
-15
lines changed

src/qr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ async fn decode_openpgp(context: &Context, qr: &str) -> Result<Qr> {
383383
.await
384384
.with_context(|| format!("can't check if address {addr:?} is our address"))?
385385
{
386-
if token::exists(context, token::Namespace::InviteNumber, &invitenumber).await {
386+
if token::exists(context, token::Namespace::InviteNumber, &invitenumber).await? {
387387
Ok(Qr::WithdrawVerifyGroup {
388388
grpname,
389389
grpid,
@@ -413,7 +413,7 @@ async fn decode_openpgp(context: &Context, qr: &str) -> Result<Qr> {
413413
})
414414
}
415415
} else if context.is_self_addr(&addr).await? {
416-
if token::exists(context, token::Namespace::InviteNumber, &invitenumber).await {
416+
if token::exists(context, token::Namespace::InviteNumber, &invitenumber).await? {
417417
Ok(Qr::WithdrawVerifyContact {
418418
contact_id,
419419
fingerprint,

src/securejoin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ pub(crate) async fn handle_securejoin_handshake(
314314
return Ok(HandshakeMessage::Ignore);
315315
}
316316
};
317-
if !token::exists(context, token::Namespace::InviteNumber, invitenumber).await {
317+
if !token::exists(context, token::Namespace::InviteNumber, invitenumber).await? {
318318
warn!(context, "Secure-join denied (bad invitenumber).");
319319
return Ok(HandshakeMessage::Ignore);
320320
}
@@ -398,7 +398,7 @@ pub(crate) async fn handle_securejoin_handshake(
398398
.await?;
399399
return Ok(HandshakeMessage::Ignore);
400400
};
401-
if !token::exists(context, token::Namespace::Auth, auth).await {
401+
if !token::exists(context, token::Namespace::Auth, auth).await? {
402402
could_not_establish_secure_connection(
403403
context,
404404
contact_id,

src/sync.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ mod tests {
514514
async fn test_execute_sync_items() -> Result<()> {
515515
let t = TestContext::new_alice().await;
516516

517-
assert!(!token::exists(&t, Namespace::Auth, "yip-auth").await);
517+
assert!(!token::exists(&t, Namespace::Auth, "yip-auth").await?);
518518

519519
let sync_items = t
520520
.parse_sync_items(
@@ -537,10 +537,10 @@ mod tests {
537537
.await?
538538
.is_none()
539539
);
540-
assert!(token::exists(&t, Namespace::InviteNumber, "yip-in").await);
541-
assert!(token::exists(&t, Namespace::Auth, "yip-auth").await);
542-
assert!(!token::exists(&t, Namespace::Auth, "non-existent").await);
543-
assert!(!token::exists(&t, Namespace::Auth, "directly deleted").await);
540+
assert!(token::exists(&t, Namespace::InviteNumber, "yip-in").await?);
541+
assert!(token::exists(&t, Namespace::Auth, "yip-auth").await?);
542+
assert!(!token::exists(&t, Namespace::Auth, "non-existent").await?);
543+
assert!(!token::exists(&t, Namespace::Auth, "directly deleted").await?);
544544

545545
Ok(())
546546
}
@@ -577,13 +577,13 @@ mod tests {
577577
let alice2 = TestContext::new_alice().await;
578578
alice2.set_config_bool(Config::SyncMsgs, true).await?;
579579
alice2.recv_msg(&sent_msg).await;
580-
assert!(token::exists(&alice2, token::Namespace::Auth, "testtoken").await);
580+
assert!(token::exists(&alice2, token::Namespace::Auth, "testtoken").await?);
581581
assert_eq!(Chatlist::try_load(&alice2, 0, None, None).await?.len(), 0);
582582

583583
// the same sync message sent to bob must not be executed
584584
let bob = TestContext::new_bob().await;
585585
bob.recv_msg(&sent_msg).await;
586-
assert!(!token::exists(&bob, token::Namespace::Auth, "testtoken").await);
586+
assert!(!token::exists(&bob, token::Namespace::Auth, "testtoken").await?);
587587

588588
Ok(())
589589
}

src/token.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,15 @@ pub async fn lookup_or_new(
103103
token
104104
}
105105

106-
pub async fn exists(context: &Context, namespace: Namespace, token: &str) -> bool {
107-
context
106+
pub async fn exists(context: &Context, namespace: Namespace, token: &str) -> Result<bool> {
107+
let exists = context
108108
.sql
109109
.exists(
110110
"SELECT COUNT(*) FROM tokens WHERE namespc=? AND token=?;",
111111
(namespace, token),
112112
)
113-
.await
114-
.unwrap_or_default()
113+
.await?;
114+
Ok(exists)
115115
}
116116

117117
pub async fn delete(context: &Context, namespace: Namespace, token: &str) -> Result<()> {

0 commit comments

Comments
 (0)