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

ambee/giterated

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

Major post-refactor cleanup

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨f90d7fb

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