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

ambee/giterated

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

Fix repositories for user request not taking in optional authenticated user

Type: Fix

emilia - ⁨2⁩ years ago

parent: tbd commit: ⁨aad12bc

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