JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Giterated Stack `ObjectValue` and `Setting` refactor.

This refactor adds value and setting update events, as well as value getters. Additionally, the stack is now the owner of the ability to write settings into storage. This is accomplished with the `MetadataProvider` trait. This sets up the ground work for push federation, cache, and basically everything else. commit 7befc583cb3e0c6719506c550ed66ac76293413c Author: Amber <[email protected]> Date: Fri Sep 29 15:46:48 2023 -0500 Finish value and settings refactor in the stack. commit 3ac09994a0caafd1a0b95d9a781c7f202f20e75b Author: Amber <[email protected]> Date: Fri Sep 29 09:46:32 2023 -0500 Add set_setting handling back in commit 84fd31e3eae85d98fa68a28b333dbb32cde3bdb8 Author: Amber <[email protected]> Date: Wed Sep 27 06:36:31 2023 -0500 Remove some allocations from meta types commit 16c310ce3680c4a14ed35083b6a230aaecd43152 Author: Amber <[email protected]> Date: Wed Sep 27 05:35:03 2023 -0500 Add cargo metadata commit eb2520a20001bac7b21c6c3d34f62db32f0ada80 Author: Amber <[email protected]> Date: Wed Sep 27 05:26:27 2023 -0500 Refactor setting and value management to use the unified stack. Allows for tight management, inspection, and eventing of setting and value management. commit 901fe103da0fce4b40f33b0a8b64404049ae03cf Author: Amber <[email protected]> Date: Wed Sep 27 02:38:33 2023 -0500 Set up ground work for value / settings refactor

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨c377e4d

⁨giterated-daemon/src/backend/settings.rs⁩ - ⁨2505⁩ bytes
Raw
1 use std::sync::Arc;
2
3 use anyhow::Error;
4
5 use giterated_models::repository::Repository;
6 use giterated_models::settings::AnySetting;
7 use giterated_models::user::User;
8
9 use giterated_stack::GiteratedStack;
10 use sqlx::PgPool;
11 use tokio::sync::OnceCell;
12
13 use super::MetadataBackend;
14
15 pub struct DatabaseSettings {
16 pub pg_pool: PgPool,
17 pub stack: Arc<OnceCell<Arc<GiteratedStack>>>,
18 }
19
20 #[async_trait::async_trait]
21 impl MetadataBackend for DatabaseSettings {
22 async fn user_get(&mut self, user: &User, name: &str) -> Result<AnySetting, Error> {
23 let row = sqlx::query_as!(
24 UserSettingRow,
25 "SELECT * FROM user_settings WHERE username = $1 AND name = $2",
26 user.username,
27 name
28 )
29 .fetch_one(&self.pg_pool)
30 .await?;
31
32 let setting = serde_json::from_str(&row.value)?;
33
34 Ok(setting)
35 }
36 async fn user_write(
37 &mut self,
38 user: &User,
39 name: &str,
40 value: AnySetting,
41 ) -> Result<(), Error> {
42 sqlx::query!("INSERT INTO user_settings VALUES ($1, $2, $3) ON CONFLICT (username, name) DO UPDATE SET value = $3",
43 user.username, name, serde_json::to_string(&value)?)
44 .execute(&self.pg_pool).await?;
45
46 Ok(())
47 }
48
49 async fn repository_get(
50 &mut self,
51 repository: &Repository,
52 name: &str,
53 ) -> Result<AnySetting, Error> {
54 let row = sqlx::query_as!(
55 RepositorySettingRow,
56 "SELECT * FROM repository_settings WHERE repository = $1 AND name = $2",
57 repository.to_string(),
58 name
59 )
60 .fetch_one(&self.pg_pool)
61 .await?;
62
63 let setting = serde_json::from_str(&row.value)?;
64
65 Ok(setting)
66 }
67 async fn repository_write(
68 &mut self,
69 repository: &Repository,
70 name: &str,
71 value: AnySetting,
72 ) -> Result<(), Error> {
73 sqlx::query!("INSERT INTO repository_settings VALUES ($1, $2, $3) ON CONFLICT (repository, name) DO UPDATE SET value = $3",
74 repository.to_string(), name, serde_json::to_string(&value)?)
75 .execute(&self.pg_pool).await?;
76
77 Ok(())
78 }
79 }
80
81 #[allow(unused)]
82 #[derive(Debug, sqlx::FromRow)]
83 pub struct UserSettingRow {
84 pub username: String,
85 pub name: String,
86 pub value: String,
87 }
88
89 #[allow(unused)]
90 #[derive(Debug, sqlx::FromRow)]
91 pub struct RepositorySettingRow {
92 pub repository: String,
93 pub name: String,
94 pub value: String,
95 }
96