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

ambee/giterated

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

merge changes

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨5213bdc

⁨src/backend/user.rs⁩ - ⁨2389⁩ bytes
Raw
1 use std::error::Error;
2
3 use sqlx::PgPool;
4
5 use crate::messages::user::{
6 UserBioRequest, UserBioResponse, UserDisplayImageRequest, UserDisplayImageResponse,
7 UserDisplayNameRequest, UserDisplayNameResponse,
8 };
9
10 use super::{AuthBackend, UserBackend};
11
12 pub struct UserAuth {
13 pub pg_pool: PgPool,
14 }
15
16 impl UserAuth {
17 pub fn new(pool: PgPool) -> Self {
18 Self { pg_pool: pool }
19 }
20 }
21
22 #[async_trait::async_trait]
23 impl UserBackend for UserAuth {
24 async fn display_name(
25 &mut self,
26 request: UserDisplayNameRequest,
27 ) -> Result<UserDisplayNameResponse, Box<dyn std::error::Error + Send>> {
28 let db_row = sqlx::query_as!(
29 UserRow,
30 r#"SELECT * FROM users WHERE username = $1"#,
31 request.user.username
32 )
33 .fetch_one(&self.pg_pool.clone())
34 .await.unwrap();
35
36 Ok(UserDisplayNameResponse {
37 display_name: db_row.display_name,
38 })
39 }
40
41 async fn display_image(
42 &mut self,
43 request: UserDisplayImageRequest,
44 ) -> Result<UserDisplayImageResponse, Box<dyn std::error::Error + Send>> {
45 let db_row = sqlx::query_as!(
46 UserRow,
47 r#"SELECT * FROM users WHERE username = $1"#,
48 request.user.username
49 )
50 .fetch_one(&self.pg_pool.clone())
51 .await.unwrap();
52
53 Ok(UserDisplayImageResponse {
54 image_url: db_row.image_url,
55 })
56 }
57
58 async fn bio(
59 &mut self,
60 request: UserBioRequest,
61 ) -> Result<UserBioResponse, Box<dyn std::error::Error + Send>> {
62 let db_row = sqlx::query_as!(
63 UserRow,
64 r#"SELECT * FROM users WHERE username = $1"#,
65 request.user.username
66 )
67 .fetch_one(&self.pg_pool.clone())
68 .await.unwrap();
69
70 Ok(UserBioResponse { bio: db_row.bio })
71 }
72 }
73
74 #[async_trait::async_trait]
75 impl AuthBackend for UserAuth {
76 async fn register(&mut self, request: ()) -> Result<(), Box<dyn Error + Send>> {
77 todo!()
78 }
79
80 async fn login(&mut self, request: ()) -> Result<(), Box<dyn Error + Send>> {
81 todo!()
82 }
83 }
84
85 #[derive(Debug, sqlx::FromRow)]
86 struct UserRow {
87 pub username: String,
88 pub image_url: Option<String>,
89 pub display_name: Option<String>,
90 pub bio: Option<String>,
91 pub email: Option<String>,
92 pub password: String,
93 }
94