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

ambee/giterated

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

Major refactor to handler traits

Added `IntoGiteratedHandler`, which is the new trait for handler functions. This allows us to finally get rid of the Object and ObjectOperation bounds that resulted in hacks around the newer features of the unified stack. Squashed commit of the following: commit 62e1ecf76ee31cda0bab4602d9d00fa0dc2f9158 Author: Amber <[email protected]> Date: Wed Oct 11 09:31:11 2023 -0500 Update commit dfd2d1b0b5d81ee3bc48f0321c6aceaa677e3b8b Author: Amber <[email protected]> Date: Wed Oct 11 09:31:07 2023 -0500 Major refactor to handler traits Added `IntoGiteratedHandler`, which is the new trait for handler functions. This allows us to finally get rid of the Object and ObjectOperation bounds that resulted in hacks around the newer features of the unified stack. Removed dead and legacy code. I think... commit 57b4b398eff32e69f2f4b9700e42a1277a4d1055 Author: Amber <[email protected]> Date: Sun Oct 1 23:05:10 2023 -0500 New handler trait for giterated stack Refactor the old handler trait so it is more generic and can be used for specific kinds of handlers

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨90c4780

⁨giterated-daemon/src/backend/settings.rs⁩ - ⁨2426⁩ 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<Arc<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