logs database schema & method shifts

This commit is contained in:
2026-04-03 12:33:06 +02:00
parent f935e626fc
commit 42c54ba6a0
3 changed files with 50 additions and 20 deletions

View File

@@ -76,14 +76,17 @@ CREATE TABLE quote_tags (
) WITHOUT ROWID; ) WITHOUT ROWID;
CREATE INDEX quote_tags_reverse_index ON quote_tags(tag_id, quote_id); CREATE INDEX quote_tags_reverse_index ON quote_tags(tag_id, quote_id);
-- CREATE TABLE logs ( CREATE TABLE logs (
-- id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes id BLOB NOT NULL UNIQUE PRIMARY KEY, -- UUIDv7 as bytes
-- actor BLOB NOT NULL REFERENCES users(id), -- UUIDv7 as bytes actor BLOB NOT NULL REFERENCES users(id), -- UUIDv7 as bytes
-- -- (userID with special cases: UUID::nil if system, UUID::max if infradmin) -- (userID with special cases: UUID::nil if system, UUID::max if infradmin)
-- -- ((infradmin & system shall both be users)) -- ((infradmin & system shall both be users))
-- target BLOB, -- Option<UUIDv7 as bytes (userID)> target BLOB, -- Option<UUIDv7 as bytes (userID)>
-- change TEXT NOT NULL actiontype TEXT NOT NULL,
-- ); payload TEXT
);
CREATE INDEX logs_by_actor ON logs(actor);
CREATE INDEX logs_by_target ON logs(target);
-- all this to be followed by: -- all this to be followed by:
-- - a better access scoping mechanism (role-based like discord) -- - a better access scoping mechanism (role-based like discord)

View File

@@ -1,16 +1,35 @@
use std::fmt::Display; use std::fmt::Display;
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, IntoStaticStr};
use uuid::Uuid; use uuid::Uuid;
use crate::users::User; use crate::{database, users::User};
#[derive(Debug)]
pub struct LogEntry { pub struct LogEntry {
pub id: Uuid, pub id: Uuid,
pub actor: User, pub actor: User,
pub data: LogAction, pub data: LogAction,
} }
#[derive(Debug)] impl LogEntry {
pub fn new(actor: User, data: LogAction) -> Result<LogEntry, rusqlite::Error> {
let log = LogEntry {
id: Uuid::now_v7(),
actor,
data,
};
let conn = database::conn()?;
conn.prepare(
"INSERT INTO logs(id, actor, target, actiontype, payload) VALUES (?1,?2,?3,?4,?5)",
)?
.execute((&log.id, &log.actor.id, log.data.get_target_id(), "a", "b"))?;
Ok(log)
}
}
#[derive(Debug, IntoStaticStr, Serialize, Deserialize)]
pub enum LogAction { pub enum LogAction {
Initialize, Initialize,
RegenInfradmin, RegenInfradmin,
@@ -19,23 +38,31 @@ pub enum LogAction {
CreatePerson { id: Uuid, pname: String }, CreatePerson { id: Uuid, pname: String },
ChangeUserHandle { id: Uuid, old: String, new: String }, ChangeUserHandle { id: Uuid, old: String, new: String },
} }
impl LogAction {
impl Display for LogAction { pub fn get_target_id(&self) -> Option<Uuid> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self { match self {
LogAction::Initialize => write!(f, "Initialized Mnemosyne."), Self::Initialize | Self::RegenInfradmin => None,
LogAction::RegenInfradmin => write!(f, "Regenerated the Infradmin account."), Self::CreateUser { id, .. } => Some(*id),
Self::CreateTag { id, .. } => Some(*id),
Self::CreatePerson { id, .. } => Some(*id),
Self::ChangeUserHandle { id, .. } => Some(*id),
}
}
pub fn get_humanreadable_payload(&self) -> String {
match self {
LogAction::Initialize => format!("Initialized Mnemosyne."),
LogAction::RegenInfradmin => format!("Regenerated the Infradmin account."),
LogAction::CreateUser { id, handle } => { LogAction::CreateUser { id, handle } => {
write!(f, "Created user @{handle} (uid: {id})") format!("Created user @{handle} (uid: {id})")
} }
LogAction::CreateTag { id, name } => { LogAction::CreateTag { id, name } => {
write!(f, "Created tag #{name} (id: {id})") format!("Created tag #{name} (id: {id})")
} }
LogAction::CreatePerson { id, pname } => { LogAction::CreatePerson { id, pname } => {
write!(f, "Created person ~{pname} (id: {id})") format!("Created person ~{pname} (id: {id})")
} }
LogAction::ChangeUserHandle { id, old, new } => { LogAction::ChangeUserHandle { id, old, new } => {
write!(f, "Changed user handle @{old} -> @{new} (uid: {id})") format!("Changed user handle @{old} -> @{new} (uid: {id})")
} }
} }
} }

View File

@@ -67,7 +67,7 @@ pub async fn page(req: Request) -> Result<Response, CompositeError> {
.unwrap_or_else(|| "no timestamp".to_string())) .unwrap_or_else(|| "no timestamp".to_string()))
} }
div class="p-2 font-light" style=(s) {(log.actor.handle)} div class="p-2 font-light" style=(s) {(log.actor.handle)}
div class="p-2 font-light" style=(s) {(log.data)} div class="p-2 font-light" style=(s) {(log.data.get_humanreadable_payload())}
} }
@if true { @if true {
div class="p-2 col-span-3 text-center font-light text-neutral-400" {"You've reached the end of all logs."} div class="p-2 col-span-3 text-center font-light text-neutral-400" {"You've reached the end of all logs."}