Some work on interfaces
I'm not happy. It kinda sucks right now. I hope I can fix it~~~~~ But for now I must move to my laptop
This commit is contained in:
parent
62f32da927
commit
b2e0a7b499
8 changed files with 169 additions and 60 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -564,7 +564,9 @@ name = "db"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-trait",
|
"async-trait",
|
||||||
|
"atproto",
|
||||||
"sqlx",
|
"sqlx",
|
||||||
|
"thiserror 2.0.12",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,24 @@ use core::str::FromStr;
|
||||||
pub use atrium_api::types::{
|
pub use atrium_api::types::{
|
||||||
Collection,
|
Collection,
|
||||||
string::{
|
string::{
|
||||||
|
AtIdentifier as Authority,
|
||||||
|
Cid,
|
||||||
|
Datetime,
|
||||||
|
Did,
|
||||||
Nsid,
|
Nsid,
|
||||||
RecordKey,
|
RecordKey,
|
||||||
AtIdentifier as Authority,
|
Tid,
|
||||||
|
Handle,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod lexicons;
|
pub mod lexicons;
|
||||||
|
|
||||||
|
pub struct StrongRef<T> {
|
||||||
|
pub content: T,
|
||||||
|
pub cid: Cid,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Uri {
|
pub struct Uri {
|
||||||
whole: String,
|
whole: String,
|
||||||
// These fields could be useful in the future,
|
// These fields could be useful in the future,
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,8 @@ version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
thiserror = "2.0.12"
|
||||||
|
atproto.workspace = true
|
||||||
async-trait.workspace = true
|
async-trait.workspace = true
|
||||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio"] }
|
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio"] }
|
||||||
tokio.workspace = true
|
tokio.workspace = true
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,8 @@ use sqlx::{
|
||||||
};
|
};
|
||||||
use std::string::ToString;
|
use std::string::ToString;
|
||||||
|
|
||||||
pub struct Db<Dbimp: Database> {
|
pub struct Db<Db: Database> {
|
||||||
pool: Pool<Dbimp>
|
pool: Pool<Db>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
|
@ -55,45 +55,4 @@ impl Db<Postgres> {
|
||||||
Ok(Db { pool })
|
Ok(Db { pool })
|
||||||
}
|
}
|
||||||
//
|
//
|
||||||
// pub async fn add_user(&self, user: &User) -> Result<()> {
|
|
||||||
// query!(r#"
|
|
||||||
// INSERT INTO users(userdid, handle) VALUES ($1, $2)
|
|
||||||
// "#,
|
|
||||||
// user.userdid, user.handle
|
|
||||||
// ).execute(self.pool).await?;
|
|
||||||
// Ok(())
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub async fn add_session(&self, session: &Session) -> Result<()> {
|
|
||||||
// let mut transaction = self.pool.begin().await?;
|
|
||||||
//
|
|
||||||
// query!(r#"
|
|
||||||
// INSERT INTO sessions(sessionuri, label) VALUES ($1, $2)
|
|
||||||
// "#,
|
|
||||||
// session.sessionuri, session.label
|
|
||||||
// ).execute(&mut *transaction).await?;
|
|
||||||
//
|
|
||||||
// for participant in session.participants {
|
|
||||||
// query!(r#"
|
|
||||||
// INSERT INTO participants(sessionuri, userdid, role) VALUES ($1, $2, $3)
|
|
||||||
// "#,
|
|
||||||
// session.sessionuri, participant.userdid, participant.role.to_string()
|
|
||||||
// ).execute(&mut *transaction).await?;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// transaction.commit().await
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// pub async fn add_participant(&self, session: Session,
|
|
||||||
// participant: Participant) -> Result<Session> {
|
|
||||||
// query!(r#"
|
|
||||||
// INSERT INTO participants(sessionuri, userdid, role) VALUES ($1, $2, $3)
|
|
||||||
// "#,
|
|
||||||
// session.sessionuri, participant.userdid, participant.role.to_string()
|
|
||||||
// ).execute(self.pool).await?;
|
|
||||||
//
|
|
||||||
// session.participants.push(participant);
|
|
||||||
//
|
|
||||||
// Ok(session)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
use atproto::{
|
|
||||||
Did,
|
|
||||||
Uri,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct User {
|
|
||||||
userdid: Did,
|
|
||||||
handle: Handle,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Participant {
|
|
||||||
participantdid: Did,
|
|
||||||
role: Role,
|
|
||||||
}
|
|
||||||
|
|
||||||
1
db/src/interfaces/mod.rs
Normal file
1
db/src/interfaces/mod.rs
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
pub mod spoor;
|
||||||
137
db/src/interfaces/spoor.rs
Normal file
137
db/src/interfaces/spoor.rs
Normal file
|
|
@ -0,0 +1,137 @@
|
||||||
|
use crate::DbError;
|
||||||
|
use atproto::{
|
||||||
|
Cid,
|
||||||
|
Datetime,
|
||||||
|
Did,
|
||||||
|
StrongRef,
|
||||||
|
Tid,
|
||||||
|
Handle,
|
||||||
|
};
|
||||||
|
use sqlx::{
|
||||||
|
PgTransaction,
|
||||||
|
PgPool,
|
||||||
|
query,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Activity {
|
||||||
|
pub authority: User,
|
||||||
|
pub key: Tid,
|
||||||
|
|
||||||
|
pub cid: Option<Cid>,
|
||||||
|
pub session: Option<StrongRef<Session>>,
|
||||||
|
pub progress: Option<Progress>,
|
||||||
|
pub performed_at: Option<Datetime>,
|
||||||
|
pub created_at: Option<Datetime>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Session {
|
||||||
|
pub authority: User,
|
||||||
|
pub key: Tid,
|
||||||
|
|
||||||
|
pub cid: Option<Cid>,
|
||||||
|
pub content: Option<StrongRef<Content>>,
|
||||||
|
pub label: Option<String>,
|
||||||
|
pub created_at: Option<Datetime>,
|
||||||
|
pub other_participants: Option<Vec<Participant>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct User {
|
||||||
|
pub did: Did,
|
||||||
|
pub handle: Option<Handle>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum Participant {
|
||||||
|
Owner(User),
|
||||||
|
Added(User),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum Content {
|
||||||
|
UnknownContent
|
||||||
|
}
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
pub enum Progress {
|
||||||
|
UnknownProgress
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn ingest_session(db: PgPool, session: Session) -> Result<(), DbError> {
|
||||||
|
let mut transaction = db.begin().await?;
|
||||||
|
write_session(&mut transaction, session).await?;
|
||||||
|
transaction.commit().await.map_err(DbError::Backend)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn write_session(tr: &mut PgTransaction<'_>, session: Session) -> Result<(), DbError> {
|
||||||
|
query!(r#"
|
||||||
|
INSERT INTO sessions(sessionuri, label, created_at, contenturi, contentcid)
|
||||||
|
"#).execute(&mut tr).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub fn injest_activity(db: PgPool, activity: Activity) -> Result<(), Error> {
|
||||||
|
// todo!();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// let mut transaction = db.begin().await?;
|
||||||
|
//
|
||||||
|
// query!(r#"
|
||||||
|
// INSERT INTO sessions(sessionuri, label) VALUES ($1, $2)
|
||||||
|
// "#,
|
||||||
|
// session.sessionuri, session.label
|
||||||
|
// ).execute(&mut *transaction).await?;
|
||||||
|
//
|
||||||
|
// for participant in session.participants {
|
||||||
|
// query!(r#"
|
||||||
|
// INSERT INTO participants(sessionuri, userdid, role) VALUES ($1, $2, $3)
|
||||||
|
// "#,
|
||||||
|
// session.sessionuri, participant.userdid, participant.role.to_string()
|
||||||
|
// ).execute(&mut *transaction).await?;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// transaction.commit().await
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// pub async fn ingest_participant(db: PgPool, participant: Participant) -> Result<(), Error> {
|
||||||
|
// query!(r#"
|
||||||
|
// INSERT INTO sessions(sessionuri, label) VALUES ($1, $2)
|
||||||
|
// "#,
|
||||||
|
// session.sessionuri, session.label
|
||||||
|
// ).execute(&mut *transaction).await?;
|
||||||
|
//
|
||||||
|
// for participant in session.participants {
|
||||||
|
// query!(r#"
|
||||||
|
// INSERT INTO participants(sessionuri, userdid, role) VALUES ($1, $2, $3)
|
||||||
|
// "#,
|
||||||
|
// session.sessionuri, participant.userdid, participant.role.to_string()
|
||||||
|
// ).execute(&mut *transaction).await?;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// transaction.commit().await
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
|
||||||
|
// pub async fn add_user(&self, user: &User) -> Result<()> {
|
||||||
|
// query!(r#"
|
||||||
|
// INSERT INTO users(userdid, handle) VALUES ($1, $2)
|
||||||
|
// "#,
|
||||||
|
// user.userdid, user.handle
|
||||||
|
// ).execute(self.pool).await?;
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// pub async fn add_session(&self, session: &Session) -> Result<()> {
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// pub async fn add_participant(&self, session: Session,
|
||||||
|
// participant: Participant) -> Result<Session> {
|
||||||
|
// query!(r#"
|
||||||
|
// INSERT INTO participants(sessionuri, userdid, role) VALUES ($1, $2, $3)
|
||||||
|
// "#,
|
||||||
|
// session.sessionuri, participant.userdid, participant.role.to_string()
|
||||||
|
// ).execute(self.pool).await?;
|
||||||
|
//
|
||||||
|
// session.participants.push(participant);
|
||||||
|
//
|
||||||
|
// Ok(session)
|
||||||
|
// }
|
||||||
|
|
@ -1 +1,14 @@
|
||||||
pub struct db;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
pub mod interfaces;
|
||||||
|
|
||||||
|
#[non_exhaustive]
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum DbError {
|
||||||
|
#[error("Database Implementation Error: {0}")]
|
||||||
|
Backend(#[from] sqlx::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// pub struct db;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue