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 {
|
21 |
Err(Error::from(HandlerUnhandled))
|
22 |
}
|
23 |
}
|
24 |
|
25 |
async fn display_name(
|
26 |
Message(request): Message<UserDisplayNameRequest>,
|
27 |
State(connection_state): State<ConnectionState>,
|
28 |
) -> Result<(), UserError> {
|
29 |
let mut user_backend = connection_state.user_backend.lock().await;
|
30 |
let response = user_backend.display_name(request.clone()).await?;
|
31 |
|
32 |
drop(user_backend);
|
33 |
|
34 |
connection_state.send(response).await?;
|
35 |
|
36 |
Ok(())
|
37 |
}
|
38 |
|
39 |
async fn display_image(
|
40 |
Message(request): Message<UserDisplayImageRequest>,
|
41 |
State(connection_state): State<ConnectionState>,
|
42 |
) -> Result<(), UserError> {
|
43 |
let mut user_backend = connection_state.user_backend.lock().await;
|
44 |
let response = user_backend.display_image(request.clone()).await?;
|
45 |
|
46 |
drop(user_backend);
|
47 |
|
48 |
connection_state.send(response).await?;
|
49 |
|
50 |
Ok(())
|
51 |
}
|
52 |
|
53 |
async fn bio(
|
54 |
Message(request): Message<UserBioRequest>,
|
55 |
State(connection_state): State<ConnectionState>,
|
56 |
) -> Result<(), UserError> {
|
57 |
let mut user_backend = connection_state.user_backend.lock().await;
|
58 |
let response = user_backend.bio(request.clone()).await?;
|
59 |
|
60 |
drop(user_backend);
|
61 |
|
62 |
connection_state.send(response).await?;
|
63 |
|
64 |
Ok(())
|
65 |
}
|
66 |
|
67 |
async fn repositories(
|
68 |
Message(request): Message<UserRepositoriesRequest>,
|
69 |
State(connection_state): State<ConnectionState>,
|
70 |
) -> Result<(), UserError> {
|
71 |
let mut repository_backend = connection_state.repository_backend.lock().await;
|
72 |
|
73 |
let repositories = repository_backend
|
74 |
.repositories_for_user(&request.user)
|
75 |
.await;
|
76 |
|
77 |
let repositories = match repositories {
|
78 |
Ok(repositories) => repositories,
|
79 |
Err(err) => {
|
80 |
error!("Error handling request: {:?}", err);
|
81 |
return Ok(());
|
82 |
}
|
83 |
};
|
84 |
drop(repository_backend);
|
85 |
|
86 |
let mut user_backend = connection_state.user_backend.lock().await;
|
87 |
let user_exists = user_backend.exists(&request.user).await;
|
88 |
|
89 |
if repositories.is_empty() && !matches!(user_exists, Ok(true)) {
|
90 |
panic!()
|
91 |
}
|
92 |
|
93 |
let response: UserRepositoriesResponse = UserRepositoriesResponse { repositories };
|
94 |
|
95 |
connection_state.send(response).await?;
|
96 |
|
97 |
Ok(())
|
98 |
}
|
99 |
|
100 |
#[derive(Debug, thiserror::Error)]
|
101 |
pub enum UserError {
|
102 |
#[error("{0}")]
|
103 |
Other(#[from] Error),
|
104 |
}
|
105 |
|