spa hardening, offline logging, rate limit fixes
server: - backup exporter, auth extractors, error shapes, CONTEXT (prior work) - spa assets served outside the rate limit via route_layer - requests_per_second went to per_second(), which takes an interval not a rate: 50 meant one request per 50s once burst was spent. now converted properly. 15/s, burst 60 spa fixes: - account delete cleared snake_case token keys that were never written - refresh interceptor could retry forever - date ranges used local day boundaries stamped +00:00 - "all" period trend plotted one page; calendar days fabricated mood 3 - chart grid invisible: hsl(var(--border)) against rgba tokens - blob url leak, orphaned media on failed save, devtools in prod bundle - pt-safe/safe-area-pb classes never existed spa features: - offline outbox: entries queue to IndexedDB, replay with backoff, only server refusals count against an entry - drafts persist, quick-log sheet, diary infinite scroll + filters - route error boundary, stale-chunk recovery, no service worker in dev a11y + perf: - mood picker is a radiogroup, activity picker keyboard-operable, text alternatives for colour/emoji, locale week start - dark glass over the bright photo: worst case 1.4:1 -> 4.9-9.6:1 - initial payload 1095->769kB raw, 306->230kB gzip; 38 unused components and 5 deps dropped; fonts 218->133kB 53 tests added (43 spa, 10 server)
This commit is contained in:
95
crates/adapters/sqlite/src/repositories/media/ownership.rs
Normal file
95
crates/adapters/sqlite/src/repositories/media/ownership.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use chrono::Utc;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
use domain::attachment::{MediaKind, MediaRef, PhotoId, VoiceMemoId};
|
||||
use domain::errors::DomainError;
|
||||
use domain::user::UserId;
|
||||
|
||||
use super::super::shared::db_err;
|
||||
|
||||
pub struct SqliteMediaOwnershipRepository {
|
||||
pool: SqlitePool,
|
||||
}
|
||||
|
||||
impl SqliteMediaOwnershipRepository {
|
||||
pub fn new(pool: SqlitePool) -> Self {
|
||||
Self { pool }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl domain::ports::MediaOwnershipPort for SqliteMediaOwnershipRepository {
|
||||
async fn remember(&self, owner: &UserId, media: MediaRef) -> Result<(), DomainError> {
|
||||
sqlx::query(
|
||||
"INSERT INTO media_owners (kind, media_id, user_id, created_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(kind, media_id) DO UPDATE SET user_id = excluded.user_id",
|
||||
)
|
||||
.bind(media.kind().name())
|
||||
.bind(media.id().to_string())
|
||||
.bind(owner.value().to_string())
|
||||
.bind(Utc::now().to_rfc3339())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn owner_of(&self, media: MediaRef) -> Result<Option<UserId>, DomainError> {
|
||||
let found: Option<(String,)> =
|
||||
sqlx::query_as("SELECT user_id FROM media_owners WHERE kind = ? AND media_id = ?")
|
||||
.bind(media.kind().name())
|
||||
.bind(media.id().to_string())
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
Ok(found
|
||||
.and_then(|(user_id,)| user_id.parse().ok())
|
||||
.map(UserId::from_uuid))
|
||||
}
|
||||
|
||||
async fn owned_by(&self, owner: &UserId) -> Result<Vec<MediaRef>, DomainError> {
|
||||
let rows: Vec<(String, String)> =
|
||||
sqlx::query_as("SELECT kind, media_id FROM media_owners WHERE user_id = ?")
|
||||
.bind(owner.value().to_string())
|
||||
.fetch_all(&self.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
Ok(rows.iter().filter_map(readable).collect())
|
||||
}
|
||||
|
||||
async fn forget(&self, media: MediaRef) -> Result<(), DomainError> {
|
||||
sqlx::query("DELETE FROM media_owners WHERE kind = ? AND media_id = ?")
|
||||
.bind(media.kind().name())
|
||||
.bind(media.id().to_string())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn forget_all_by_user(&self, owner: &UserId) -> Result<(), DomainError> {
|
||||
sqlx::query("DELETE FROM media_owners WHERE user_id = ?")
|
||||
.bind(owner.value().to_string())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(db_err)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn readable(row: &(String, String)) -> Option<MediaRef> {
|
||||
let (kind, media_id) = row;
|
||||
|
||||
match MediaKind::from_name(kind)? {
|
||||
MediaKind::Photo => Some(MediaRef::from(&PhotoId::from_uuid(media_id.parse().ok()?))),
|
||||
MediaKind::VoiceMemo => Some(MediaRef::from(&VoiceMemoId::from_uuid(
|
||||
media_id.parse().ok()?,
|
||||
))),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user