feat: per-entity federation privacy toggles for reviews and watchlist
- add federate_reviews + federate_watchlist to UserSettings (default true) - new UserFederationSettingsQuery port with FederationFlags struct - remove get_user_federate_goals from LocalApContentQuery - gate ReviewLogged, ReviewUpdated, WatchlistEntryAdded, on_poster_synced on flags - goals gating migrated to UserFederationSettingsQuery - ReviewDeleted and WatchlistEntryRemoved ungated (tombstones always fire) - sqlite + postgres migrations and adapter impls - settings API and SPA toggles
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE user_settings ADD COLUMN federate_reviews BOOLEAN NOT NULL DEFAULT TRUE;
|
||||
ALTER TABLE user_settings ADD COLUMN federate_watchlist BOOLEAN NOT NULL DEFAULT TRUE;
|
||||
@@ -229,23 +229,6 @@ impl LocalApContentQuery for PostgresApContentQuery {
|
||||
rows.into_iter().map(DiaryRow::into_domain).collect()
|
||||
}
|
||||
|
||||
async fn get_user_federate_goals(&self, user_id: &UserId) -> Result<bool, DomainError> {
|
||||
let uid = user_id.value().to_string();
|
||||
let row = sqlx::query("SELECT federate_goals FROM user_settings WHERE user_id = $1")
|
||||
.bind(&uid)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::map_err)?;
|
||||
|
||||
match row {
|
||||
Some(r) => {
|
||||
let val: i64 = r.try_get("federate_goals").unwrap_or(0);
|
||||
Ok(val != 0)
|
||||
}
|
||||
None => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_goal_with_progress(
|
||||
&self,
|
||||
user_id: &UserId,
|
||||
|
||||
@@ -93,6 +93,7 @@ pub struct PostgresWireOutput {
|
||||
pub wrapup_stats: std::sync::Arc<dyn domain::ports::WrapUpStatsQuery>,
|
||||
pub goal: std::sync::Arc<dyn domain::ports::GoalRepository>,
|
||||
pub user_settings: std::sync::Arc<dyn domain::ports::UserSettingsRepository>,
|
||||
pub federation_settings: std::sync::Arc<dyn domain::ports::UserFederationSettingsQuery>,
|
||||
pub remote_goal: std::sync::Arc<dyn domain::ports::RemoteGoalRepository>,
|
||||
}
|
||||
|
||||
@@ -108,6 +109,8 @@ pub async fn wire(database_url: &str) -> anyhow::Result<PostgresWireOutput> {
|
||||
.map_err(|e| anyhow::anyhow!("{e}"))
|
||||
.context("Database migration failed")?;
|
||||
|
||||
let user_settings_repo = std::sync::Arc::new(user_settings::PostgresUserSettingsRepository::new(pool.clone()));
|
||||
|
||||
Ok(PostgresWireOutput {
|
||||
pool: pool.clone(),
|
||||
movie: std::sync::Arc::new(PostgresMovieRepository::new(pool.clone())) as _,
|
||||
@@ -125,9 +128,8 @@ pub async fn wire(database_url: &str) -> anyhow::Result<PostgresWireOutput> {
|
||||
wrapup_repo: std::sync::Arc::new(PostgresWrapUpRepository::new(pool.clone())) as _,
|
||||
wrapup_stats: std::sync::Arc::new(PostgresWrapUpStatsQuery::new(pool.clone())) as _,
|
||||
goal: std::sync::Arc::new(goals::PostgresGoalRepository::new(pool.clone())) as _,
|
||||
user_settings: std::sync::Arc::new(user_settings::PostgresUserSettingsRepository::new(
|
||||
pool.clone(),
|
||||
)) as _,
|
||||
user_settings: std::sync::Arc::clone(&user_settings_repo) as _,
|
||||
federation_settings: user_settings_repo as _,
|
||||
remote_goal: std::sync::Arc::new(remote_goals::PostgresRemoteGoalRepository::new(pool))
|
||||
as _,
|
||||
})
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use async_trait::async_trait;
|
||||
use domain::{
|
||||
errors::DomainError, models::UserSettings, ports::UserSettingsRepository, value_objects::UserId,
|
||||
errors::DomainError,
|
||||
models::UserSettings,
|
||||
ports::{FederationFlags, UserFederationSettingsQuery, UserSettingsRepository},
|
||||
value_objects::UserId,
|
||||
};
|
||||
use sqlx::{PgPool, Row};
|
||||
|
||||
@@ -23,20 +26,25 @@ impl PostgresUserSettingsRepository {
|
||||
impl UserSettingsRepository for PostgresUserSettingsRepository {
|
||||
async fn get(&self, user_id: &UserId) -> Result<UserSettings, DomainError> {
|
||||
let uid = user_id.value().to_string();
|
||||
|
||||
let row =
|
||||
sqlx::query("SELECT user_id, federate_goals FROM user_settings WHERE user_id = $1")
|
||||
.bind(&uid)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::map_err)?;
|
||||
let row = sqlx::query(
|
||||
"SELECT federate_goals, federate_reviews, federate_watchlist \
|
||||
FROM user_settings WHERE user_id = $1",
|
||||
)
|
||||
.bind(&uid)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::map_err)?;
|
||||
|
||||
match row {
|
||||
Some(r) => {
|
||||
let federate: i64 = r.try_get("federate_goals").unwrap_or(0);
|
||||
let goals: bool = r.try_get("federate_goals").unwrap_or(true);
|
||||
let reviews: bool = r.try_get("federate_reviews").unwrap_or(true);
|
||||
let watchlist: bool = r.try_get("federate_watchlist").unwrap_or(true);
|
||||
Ok(UserSettings::from_persistence(
|
||||
user_id.clone(),
|
||||
federate != 0,
|
||||
goals,
|
||||
reviews,
|
||||
watchlist,
|
||||
))
|
||||
}
|
||||
None => Ok(UserSettings::new(user_id.clone())),
|
||||
@@ -45,18 +53,48 @@ impl UserSettingsRepository for PostgresUserSettingsRepository {
|
||||
|
||||
async fn save(&self, settings: &UserSettings) -> Result<(), DomainError> {
|
||||
let uid = settings.user_id().value().to_string();
|
||||
let federate = if settings.federate_goals() { 1i64 } else { 0 };
|
||||
|
||||
sqlx::query(
|
||||
"INSERT INTO user_settings (user_id, federate_goals) VALUES ($1, $2) \
|
||||
ON CONFLICT (user_id) DO UPDATE SET federate_goals = $2",
|
||||
"INSERT INTO user_settings (user_id, federate_goals, federate_reviews, federate_watchlist) \
|
||||
VALUES ($1, $2, $3, $4) \
|
||||
ON CONFLICT (user_id) DO UPDATE \
|
||||
SET federate_goals = $2, federate_reviews = $3, federate_watchlist = $4",
|
||||
)
|
||||
.bind(&uid)
|
||||
.bind(federate)
|
||||
.bind(settings.federate_goals())
|
||||
.bind(settings.federate_reviews())
|
||||
.bind(settings.federate_watchlist())
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(Self::map_err)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl UserFederationSettingsQuery for PostgresUserSettingsRepository {
|
||||
async fn get_federation_flags(&self, user_id: &UserId) -> Result<FederationFlags, DomainError> {
|
||||
let uid = user_id.value().to_string();
|
||||
let row = sqlx::query(
|
||||
"SELECT federate_goals, federate_reviews, federate_watchlist \
|
||||
FROM user_settings WHERE user_id = $1",
|
||||
)
|
||||
.bind(&uid)
|
||||
.fetch_optional(&self.pool)
|
||||
.await
|
||||
.map_err(Self::map_err)?;
|
||||
|
||||
match row {
|
||||
Some(r) => {
|
||||
let goals: bool = r.try_get("federate_goals").unwrap_or(true);
|
||||
let reviews: bool = r.try_get("federate_reviews").unwrap_or(true);
|
||||
let watchlist: bool = r.try_get("federate_watchlist").unwrap_or(true);
|
||||
Ok(FederationFlags { goals, reviews, watchlist })
|
||||
}
|
||||
None => Ok(FederationFlags {
|
||||
goals: true,
|
||||
reviews: true,
|
||||
watchlist: true,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user