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

ambee/giterated

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

Expose errors

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨6b2125c

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