This commit is contained in:
Sebastian Moser
2026-03-25 12:13:00 +01:00
parent 299f00c94f
commit 94f0a960f6
6 changed files with 258 additions and 139 deletions

View File

@@ -9,5 +9,5 @@ edition = "2021"
crate-type = ["cdylib"]
[dependencies]
spacetimedb = "1.11.*"
spacetimedb = "2.1.0"
log = "0.4"

View File

@@ -1,36 +1,89 @@
use log::*;
use spacetimedb::{ReducerContext, Table};
use spacetimedb::{reducer, table, Identity, ReducerContext, Table, Timestamp};
#[spacetimedb::table(name = person, public)]
pub struct Person {
#[table(accessor = profiles, public)]
pub struct Profile {
#[primary_key]
identity: Identity,
#[unique]
username: String,
}
#[spacetimedb::reducer(init)]
#[table(accessor = activities, public)]
pub struct Activity {
#[primary_key]
#[auto_inc]
id: u64,
name: String,
task: Option<u64>,
}
#[table(accessor = tasks, public)]
pub struct Task {
#[primary_key]
#[auto_inc]
id: u64,
done: bool,
name: String,
parent: Option<u64>,
}
#[table(accessor = events, public)]
pub struct Event {
start: Timestamp,
end: Timestamp,
activity: Activity,
}
// TODO....
#[reducer(init)]
pub fn init(_ctx: &ReducerContext) {
// Called when the module is initially published
info!("hiiiiiii");
info!("hiiiiiii from ppc module");
}
#[spacetimedb::reducer(client_connected)]
pub fn identity_connected(_ctx: &ReducerContext) {
// Called everytime a new client connects
// the one from ppc, hardcoded rn, as we for now only support config at run time.
// this needs to be loaded at build time. (this code runs as a wasm module on the spacetimedb server)
const PPC_AUTH_CLIENT_ID: &str = "363331597203734531";
#[reducer(client_connected)]
pub fn connect(ctx: &ReducerContext) -> Result<(), String> {
check_auth(ctx)?;
// add user
ctx.db.profiles().insert(Profile {
identity: ctx.sender(),
username: ctx.identity().to_string(),
});
Ok(())
}
#[spacetimedb::reducer(client_disconnected)]
fn check_auth(ctx: &ReducerContext) -> Result<(), String> {
let jwt = ctx
.sender_auth()
.jwt()
.ok_or("Authentication required".to_string())?;
if jwt.issuer() != "https://auth.ppc.social" {
return Err("Invalid issuer".to_string());
}
if !jwt.audience().iter().any(|a| a == PPC_AUTH_CLIENT_ID) {
return Err("Invalid audience".to_string());
}
Ok(())
}
#[reducer(client_disconnected)]
pub fn identity_disconnected(_ctx: &ReducerContext) {
// Called everytime a client disconnects
}
#[spacetimedb::reducer]
pub fn add(ctx: &ReducerContext, username: String) {
ctx.db.person().insert(Person { username });
}
#[spacetimedb::reducer]
pub fn say_hello(ctx: &ReducerContext) {
for person in ctx.db.person().iter() {
log::info!("Hello, {}!", person.username);
}
log::info!("Hello, World!");
#[reducer]
pub fn add_activity(ctx: &ReducerContext, name: String) {
ctx.db.activities().insert(Activity {
name,
task: None,
id: 0, // auto-increments
});
}