diff options
author | Vika <vika@fireburn.ru> | 2021-05-17 09:21:54 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2021-05-17 09:21:54 +0300 |
commit | 9d90a72131153d53d751505aab883047cd0645c7 (patch) | |
tree | 6121ff37ed61cbec0e0e5a7f0e3d96e8b4afb3db /src | |
parent | cc881705324a038b76c8e687f63f8a6972bbcad0 (diff) | |
download | kittybox-9d90a72131153d53d751505aab883047cd0645c7.tar.zst |
Added setting store per-user
Diffstat (limited to 'src')
-rw-r--r-- | src/database/mod.rs | 18 | ||||
-rw-r--r-- | src/database/redis/mod.rs | 10 |
2 files changed, 28 insertions, 0 deletions
diff --git a/src/database/mod.rs b/src/database/mod.rs index 943a1ad..98c2cae 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -160,6 +160,12 @@ pub trait Storage: Clone + Send + Sync { /// Deletes a post from the database irreversibly. 'nuff said. Must be idempotent. async fn delete_post<'a>(&self, url: &'a str) -> Result<()>; + + /// Gets a setting from the setting store and passes the result. + async fn get_setting<'a>(&self, setting: &'a str, user: &'a str) -> Result<String>; + + /// Commits a setting to the setting store. + async fn set_setting<'a>(&self, setting: &'a str, user: &'a str, value: &'a str) -> Result<()>; } #[cfg(test)] @@ -220,6 +226,11 @@ mod tests { assert_eq!(chans[0], MicropubChannel { uid: "https://fireburn.ru/feeds/main".to_string(), name: "Main Page".to_string() }); } + async fn test_backend_settings<Backend: Storage>(backend: Backend) { + backend.set_setting("site_name", "https://fireburn.ru/", "Vika's Hideout").await.unwrap(); + assert_eq!(backend.get_setting("site_name", "https://fireburn.ru/").await.unwrap(), "Vika's Hideout"); + } + #[async_std::test] async fn test_redis_storage_basic_operations() { let redis_instance = get_redis_instance().await; @@ -232,4 +243,11 @@ mod tests { let backend = super::RedisStorage::new(redis_instance.uri().to_string()).await.unwrap(); test_backend_get_channel_list(backend).await; } + + #[async_std::test] + async fn test_redis_settings() { + let redis_instance = get_redis_instance().await; + let backend = super::RedisStorage::new(redis_instance.uri().to_string()).await.unwrap(); + test_backend_settings(backend).await; + } } diff --git a/src/database/redis/mod.rs b/src/database/redis/mod.rs index 4ab4ce0..352cece 100644 --- a/src/database/redis/mod.rs +++ b/src/database/redis/mod.rs @@ -82,6 +82,16 @@ fn filter_post(mut post: serde_json::Value, user: &'_ Option<String>) -> Option< #[async_trait] impl Storage for RedisStorage { + async fn get_setting<'a>(&self, setting: &'a str, user: &'a str) -> Result<String> { + let mut conn = self.redis.get().await?; + Ok(conn.hget::<String, &str, String>(format!("settings_{}", user), setting).await?) + } + + async fn set_setting<'a>(&self, setting: &'a str, user: &'a str, value: &'a str) -> Result<()> { + let mut conn = self.redis.get().await?; + Ok(conn.hset::<String, &str, &str, ()>(format!("settings_{}", user), setting, value).await?) + } + async fn delete_post<'a>(&self, url: &'a str) -> Result<()> { let mut conn = self.redis.get().await?; Ok(conn.hdel::<&str, &str, ()>("posts", url).await?) |