feat: http wrapper, start type rewrite, add nextest

This commit is contained in:
nullishamy 2025-04-28 00:26:59 +01:00
parent 9bc6c12392
commit d59660da37
Signed by: amy
SSH key fingerprint: SHA256:WmV0uk6WgAQvDJlM8Ld4mFPHZo02CLXXP5VkwQ5xtyk
18 changed files with 369 additions and 144 deletions

View file

@ -3,7 +3,7 @@ use chrono::{DateTime, Utc};
use serde::Serialize;
use sqlx::Sqlite;
const POST_TYPE: &str = "Post";
const POST_TYPE: &str = "Note";
#[derive(Clone)]
pub struct Post {

View file

@ -42,6 +42,9 @@ impl RequestQueue {
let recv = self.recv;
while let Ok(req) = recv.recv() {
// FIXME: When we make this do async things we will need to add tokio and
// use proper async handled spans as the enter/drop won't work.
// See inbox.rs for how we handle that.
let s = span!(Level::INFO, "queue", queue_name = self.name);
let _enter = s.enter();

View file

@ -106,9 +106,10 @@ impl User {
"#,
username
)
.fetch_one(conn)
.await
.unwrap();
.fetch_one(conn)
.await
.unwrap();
User {
id: user.id,
username: user.username,

View file

@ -1,5 +1,7 @@
pub mod ap;
pub mod config;
mod types_rewrite;
use rand::{Rng, distributions::Alphanumeric};
pub fn gen_token(len: usize) -> String {

View file

@ -0,0 +1,28 @@
use crate::types_rewrite::api;
use crate::types_rewrite::ap;
use crate::types_rewrite::db;
use crate::types_rewrite::{Object, as_context};
impl From<db::Actor> for ap::Actor {
fn from(val: db::Actor) -> ap::Actor {
ap::Actor {
obj: Object {
context: as_context(),
id: val.id
},
inbox: val.inbox,
outbox: val.outbox
}
}
}
impl From<ap::Actor> for db::Actor {
fn from(val: ap::Actor) -> db::Actor {
db::Actor {
id: val.obj.id,
inbox: val.inbox,
outbox: val.outbox
}
}
}

View file

@ -0,0 +1,87 @@
use serde::{Serialize, Deserialize};
mod convert;
pub use convert::*;
pub const AS_CONTEXT_RAW: &'static str = "https://www.w3.org/ns/activitystreams";
pub fn as_context() -> ObjectContext {
ObjectContext::Str(AS_CONTEXT_RAW.to_string())
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(untagged)]
pub enum ObjectContext {
Str(String),
Vec(Vec<serde_json::Value>),
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct ObjectUri(String);
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Object {
#[serde(rename = "@context")]
context: ObjectContext,
id: ObjectUri,
}
pub mod db {
use serde::{Serialize, Deserialize};
use super::*;
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Actor {
pub id: ObjectUri,
pub inbox: String,
pub outbox: String,
}
}
pub mod ap {
use serde::{Serialize, Deserialize};
use super::*;
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct Actor {
#[serde(flatten)]
pub obj: Object,
pub inbox: String,
pub outbox: String,
}
}
pub mod api {
use serde::{Serialize, Deserialize};
use super::*;
// API will not really use actors so treat them as DB actors
// until we require specificity
pub type Actor = db::Actor;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ap_actor_to_db() {
let domain = "https://example.com";
let ap = ap::Actor {
obj: Object {
context: as_context(),
id: ObjectUri(format!("{}/users/sample", domain)),
},
inbox: format!("{}/users/sample/inbox", domain),
outbox: format!("{}/users/sample/outbox", domain),
};
let db: db::Actor = ap.into();
assert_eq!(db, db::Actor {
id: ObjectUri("https://example.com/users/sample".to_string()),
inbox: "https://example.com/users/sample/inbox".to_string(),
outbox: "https://example.com/users/sample/outbox".to_string(),
});
}
}