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

ambee/giterated

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

Fixes!

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨398622e

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