summaryrefslogtreecommitdiff
path: root/src/login/repo.rs
diff options
context:
space:
mode:
authorKit La Touche <kit@transneptune.net>2024-10-30 16:50:06 -0400
committerKit La Touche <kit@transneptune.net>2024-10-30 16:50:06 -0400
commit113096a2cca42008c0a19110abe322180dbdf66b (patch)
treecb871dae060e60be7fd2114ee4741027ae38bd78 /src/login/repo.rs
parent610f6839d2e449d172aa6ac35e6c1de0677a0754 (diff)
parent06c839436900ce07ec5c53175b01f3c5011e507c (diff)
Merge branch 'main' into wip/mobile
Diffstat (limited to 'src/login/repo.rs')
-rw-r--r--src/login/repo.rs65
1 files changed, 28 insertions, 37 deletions
diff --git a/src/login/repo.rs b/src/login/repo.rs
index 611edd6..1c63a4b 100644
--- a/src/login/repo.rs
+++ b/src/login/repo.rs
@@ -3,7 +3,7 @@ use sqlx::{sqlite::Sqlite, SqliteConnection, Transaction};
use crate::{
clock::DateTime,
- event::{Instant, ResumePoint, Sequence},
+ event::{Instant, Sequence},
login::{password::StoredHash, History, Id, Login},
name::{self, Name},
};
@@ -58,7 +58,30 @@ impl<'c> Logins<'c> {
Ok(login)
}
- pub async fn all(&mut self, resume_at: ResumePoint) -> Result<Vec<History>, LoadError> {
+ pub async fn set_password(
+ &mut self,
+ login: &History,
+ to: &StoredHash,
+ ) -> Result<(), sqlx::Error> {
+ let login = login.id();
+
+ sqlx::query_scalar!(
+ r#"
+ update login
+ set password_hash = $1
+ where id = $2
+ returning id as "id: Id"
+ "#,
+ to,
+ login,
+ )
+ .fetch_one(&mut *self.0)
+ .await?;
+
+ Ok(())
+ }
+
+ pub async fn all(&mut self, resume_at: Sequence) -> Result<Vec<History>, LoadError> {
let logins = sqlx::query!(
r#"
select
@@ -68,7 +91,7 @@ impl<'c> Logins<'c> {
created_sequence as "created_sequence: Sequence",
created_at as "created_at: DateTime"
from login
- where coalesce(created_sequence <= $1, true)
+ where created_sequence <= $1
order by canonical_name
"#,
resume_at,
@@ -90,7 +113,7 @@ impl<'c> Logins<'c> {
Ok(logins)
}
- pub async fn replay(&mut self, resume_at: ResumePoint) -> Result<Vec<History>, LoadError> {
+ pub async fn replay(&mut self, resume_at: Sequence) -> Result<Vec<History>, LoadError> {
let logins = sqlx::query!(
r#"
select
@@ -100,7 +123,7 @@ impl<'c> Logins<'c> {
created_sequence as "created_sequence: Sequence",
created_at as "created_at: DateTime"
from login
- where coalesce(login.created_sequence > $1, true)
+ where login.created_sequence > $1
"#,
resume_at,
)
@@ -120,38 +143,6 @@ impl<'c> Logins<'c> {
Ok(logins)
}
-
- pub async fn recanonicalize(&mut self) -> Result<(), sqlx::Error> {
- let logins = sqlx::query!(
- r#"
- select
- id as "id: Id",
- display_name as "display_name: String"
- from login
- "#,
- )
- .fetch_all(&mut *self.0)
- .await?;
-
- for login in logins {
- let name = Name::from(login.display_name);
- let canonical_name = name.canonical();
-
- sqlx::query!(
- r#"
- update login
- set canonical_name = $1
- where id = $2
- "#,
- canonical_name,
- login.id,
- )
- .execute(&mut *self.0)
- .await?;
- }
-
- Ok(())
- }
}
#[derive(Debug, thiserror::Error)]