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

ambee/giterated

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

Fixes for authentication!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨969964d

⁨src/connection/user.rs⁩ - ⁨3687⁩ bytes
Raw
1 use anyhow::Error;
2
3 use crate::model::authenticated::AuthenticatedUser;
4 use crate::model::user::User;
5 use crate::{
6 messages::user::{
7 UserBioRequest, UserDisplayImageRequest, UserDisplayNameRequest, UserRepositoriesRequest,
8 UserRepositoriesResponse,
9 },
10 model::authenticated::{Message, MessageHandler, NetworkMessage, State},
11 };
12
13 use super::wrapper::ConnectionState;
14
15 pub async fn user_handle(
16 message_type: &str,
17 message: &NetworkMessage,
18 state: &ConnectionState,
19 ) -> Result<bool, Error> {
20 match message_type {
21 "&giterated_daemon::messages::user::UserDisplayNameRequest" => {
22 display_name.handle_message(&message, state).await?;
23
24 Ok(true)
25 }
26 "&giterated_daemon::messages::user::UserDisplayImageRequest" => {
27 display_image.handle_message(&message, state).await?;
28
29 Ok(true)
30 }
31 "&giterated_daemon::messages::user::UserBioRequest" => {
32 bio.handle_message(&message, state).await?;
33
34 Ok(true)
35 }
36 "&giterated_daemon::messages::user::UserRepositoriesRequest" => {
37 repositories.handle_message(&message, state).await?;
38
39 Ok(true)
40 }
41 _ => Ok(false),
42 }
43 }
44
45 async fn display_name(
46 Message(request): Message<UserDisplayNameRequest>,
47 State(connection_state): State<ConnectionState>,
48 ) -> Result<(), UserError> {
49 let mut user_backend = connection_state.user_backend.lock().await;
50 let response = user_backend.display_name(request.clone()).await?;
51
52 drop(user_backend);
53
54 connection_state.send(response).await?;
55
56 Ok(())
57 }
58
59 async fn display_image(
60 Message(request): Message<UserDisplayImageRequest>,
61 State(connection_state): State<ConnectionState>,
62 ) -> Result<(), UserError> {
63 let mut user_backend = connection_state.user_backend.lock().await;
64 let response = user_backend.display_image(request.clone()).await?;
65
66 drop(user_backend);
67
68 connection_state.send(response).await?;
69
70 Ok(())
71 }
72
73 async fn bio(
74 Message(request): Message<UserBioRequest>,
75 State(connection_state): State<ConnectionState>,
76 ) -> Result<(), UserError> {
77 let mut user_backend = connection_state.user_backend.lock().await;
78 let response = user_backend.bio(request.clone()).await?;
79
80 drop(user_backend);
81
82 connection_state.send(response).await?;
83
84 Ok(())
85 }
86
87 async fn repositories(
88 Message(request): Message<UserRepositoriesRequest>,
89 State(connection_state): State<ConnectionState>,
90 requesting_user: Option<AuthenticatedUser>,
91 ) -> Result<(), UserError> {
92 let requesting_user = requesting_user.map(|u| u.0);
93
94 let mut repository_backend = connection_state.repository_backend.lock().await;
95 let repositories = repository_backend
96 .repositories_for_user(requesting_user.as_ref(), &request.user)
97 .await;
98
99 let repositories = match repositories {
100 Ok(repositories) => repositories,
101 Err(err) => {
102 error!("Error handling request: {:?}", err);
103 return Ok(());
104 }
105 };
106 drop(repository_backend);
107
108 let mut user_backend = connection_state.user_backend.lock().await;
109 let user_exists = user_backend.exists(&request.user).await;
110
111 if repositories.is_empty() && !matches!(user_exists, Ok(true)) {
112 return Err(UserError::InvalidUser(request.user));
113 }
114
115 let response: UserRepositoriesResponse = UserRepositoriesResponse { repositories };
116
117 connection_state.send(response).await?;
118
119 Ok(())
120 }
121
122 #[derive(Debug, thiserror::Error)]
123 pub enum UserError {
124 #[error("invalid user {0}")]
125 InvalidUser(User),
126 #[error("{0}")]
127 Other(#[from] Error),
128 }
129