Db, rename spoor to direct, implement sessions
This commit is contained in:
parent
df1da0905d
commit
8160b161f5
4 changed files with 103 additions and 79 deletions
2
db/src/interfaces/direct.rs
Normal file
2
db/src/interfaces/direct.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod types;
|
||||
pub mod functions;
|
||||
|
|
@ -1,59 +1,22 @@
|
|||
use crate::Error;
|
||||
use atproto::{
|
||||
Cid,
|
||||
Uri,
|
||||
use atproto::types::{
|
||||
Datetime,
|
||||
Did,
|
||||
StrongRef,
|
||||
Handle,
|
||||
Uri,
|
||||
};
|
||||
use crate::{
|
||||
Error,
|
||||
interfaces::direct::types::{
|
||||
Participant,
|
||||
Session,
|
||||
User
|
||||
},
|
||||
};
|
||||
use sqlx::{
|
||||
PgTransaction,
|
||||
Transaction,
|
||||
Postgres,
|
||||
PgPool,
|
||||
query,
|
||||
};
|
||||
|
||||
pub struct Activity {
|
||||
pub authority: Uri,
|
||||
|
||||
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 uri: Uri,
|
||||
|
||||
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<(), Error> {
|
||||
|
|
@ -63,49 +26,49 @@ pub async fn ingest_session(
|
|||
}
|
||||
|
||||
async fn write_session(
|
||||
tr: &mut PgTransaction<'_>, session: Session
|
||||
tr: &mut Transaction<'_, Postgres>, session: Session
|
||||
) -> Result<(), Error> {
|
||||
|
||||
let (contenturi, contentcid): (Option<Content>, String) =
|
||||
match session.content {
|
||||
Some(sr) => (Some(sr.content), sr.cid.to_string()),
|
||||
None => (None, "".to_string()),
|
||||
};
|
||||
|
||||
query!(r#"
|
||||
INSERT INTO sessions(
|
||||
sessionuri, sessioncid, label, created_at, contenturi, contentcid
|
||||
) VALUES ($1, $2, $3, $4, $5, $6)
|
||||
INSERT INTO
|
||||
session(uri, cid, owner, content, contentcid, label, created_at, indexed_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
"#,
|
||||
session.uri,
|
||||
session.cid.map_or("", |cid| cid.to_string()),
|
||||
session.label.unwrap_or(""),
|
||||
session.created_at.map_or("", |dt| (*dt.as_str()).to_string()),
|
||||
contenturi,
|
||||
contentcid,
|
||||
).execute(&mut tr).await?;
|
||||
&session.uri.to_string(),
|
||||
&session.cid.to_string(),
|
||||
&session.uri.authority_as_did().to_string(),
|
||||
&session.content.get_content().to_string(),
|
||||
&session.content.get_cid().to_string(),
|
||||
session.label,
|
||||
session.created_at.map(|dt| dt.to_string()),
|
||||
&Datetime::now()?.to_string()
|
||||
).execute(&mut **tr).await?;
|
||||
|
||||
session.other_participants.and_then(|participants| {
|
||||
if let Some(participants) = session.other_participants {
|
||||
for participant in participants {
|
||||
write_participant(tr, participant, session.uri).await?
|
||||
write_participant(tr, &participant, &session.uri).await?
|
||||
}
|
||||
}).unwrap_or(Ok(()))
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn write_participant(
|
||||
tr: &mut PgTransaction<'_>, participant: Participant, sessionuri: Uri
|
||||
tr: &mut Transaction<'_, Postgres>, participant: &Participant, sessionuri: &Uri
|
||||
) -> Result<(), Error> {
|
||||
let (participantType, user): (String, User) = match participant {
|
||||
let (participant_type, user): (String, &User) = match participant {
|
||||
Participant::Owner(user) => ("Owner".to_string(), user),
|
||||
Participant::Added(user) => ("Participant".to_string(), user),
|
||||
};
|
||||
|
||||
query!(r#"
|
||||
INSERT INTO participants(
|
||||
sessionuri, userdid, role
|
||||
) VALUES ($1, $2, $3)
|
||||
"#, sessionuri.to_string(), user.did.to_string, participantType
|
||||
).execute(&mut tr).await?;
|
||||
INSERT INTO
|
||||
participant(participantdid, sessionuri, role)
|
||||
VALUES ($1, $2, $3)
|
||||
"#,
|
||||
user.did.to_string(),
|
||||
sessionuri.to_string(),
|
||||
participant_type,
|
||||
).execute(&mut **tr).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
59
db/src/interfaces/direct/types.rs
Normal file
59
db/src/interfaces/direct/types.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use crate::Error;
|
||||
use atproto::types::{
|
||||
Cid,
|
||||
Uri,
|
||||
Datetime,
|
||||
Did,
|
||||
StrongRef,
|
||||
Handle,
|
||||
};
|
||||
use std::fmt::{Display, Formatter, Result as FmtResult};
|
||||
|
||||
pub struct Activity {
|
||||
pub authority: Uri,
|
||||
pub cid: Cid,
|
||||
|
||||
pub session: Option<StrongRef<Session>>,
|
||||
pub progress: Option<Progress>,
|
||||
pub performed_at: Option<Datetime>,
|
||||
pub created_at: Option<Datetime>,
|
||||
}
|
||||
|
||||
pub struct Session {
|
||||
pub uri: Uri,
|
||||
pub cid: Cid,
|
||||
|
||||
pub content: 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
|
||||
}
|
||||
|
||||
impl Display for Content {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||
write!(f, "{}", match self {
|
||||
Content::UnknownContent => "UnknownContentType",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[non_exhaustive]
|
||||
pub enum Progress {
|
||||
UnknownProgress
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
pub mod spoor;
|
||||
pub mod direct;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue