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

ambee/giterated

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

Re-expose Operation State in generics.

This is the worst code I have ever written. I hate the way this changes everything. ugh.

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨90db3e2

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