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

ambee/giterated

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

Add settings

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨0448edb

⁨src/connection/user.rs⁩ - ⁨5443⁩ bytes
Raw
1 use anyhow::Error;
2
3 use crate::messages::user::{
4 UserSettingsRequest, UserSettingsResponse, UserWriteSettingsRequest, UserWriteSettingsResponse,
5 };
6 use crate::model::authenticated::AuthenticatedUser;
7 use crate::model::user::User;
8 use crate::{
9 messages::user::{
10 UserBioRequest, UserDisplayImageRequest, UserDisplayNameRequest, UserRepositoriesRequest,
11 UserRepositoriesResponse,
12 },
13 model::authenticated::{Message, MessageHandler, NetworkMessage, State},
14 };
15
16 use super::wrapper::ConnectionState;
17
18 pub async fn user_handle(
19 message_type: &str,
20 message: &NetworkMessage,
21 state: &ConnectionState,
22 ) -> Result<bool, Error> {
23 match message_type {
24 "&giterated_daemon::messages::user::UserDisplayNameRequest" => {
25 display_name.handle_message(&message, state).await?;
26
27 Ok(true)
28 }
29 "&giterated_daemon::messages::user::UserDisplayImageRequest" => {
30 display_image.handle_message(&message, state).await?;
31
32 Ok(true)
33 }
34 "&giterated_daemon::messages::user::UserBioRequest" => {
35 bio.handle_message(&message, state).await?;
36
37 Ok(true)
38 }
39 "&giterated_daemon::messages::user::UserRepositoriesRequest" => {
40 repositories.handle_message(&message, state).await?;
41
42 Ok(true)
43 }
44 "&giterated_daemon::messages::user::UserSettingsRequest" => {
45 user_settings.handle_message(&message, state).await?;
46
47 Ok(true)
48 }
49 "&giterated_daemon::messages::user::UserWriteSettingsRequest" => {
50 write_user_settings.handle_message(&message, state).await?;
51
52 Ok(true)
53 }
54 _ => Ok(false),
55 }
56 }
57
58 async fn display_name(
59 Message(request): Message<UserDisplayNameRequest>,
60 State(connection_state): State<ConnectionState>,
61 ) -> Result<(), UserError> {
62 let mut user_backend = connection_state.user_backend.lock().await;
63 let response = user_backend.display_name(request.clone()).await?;
64
65 drop(user_backend);
66
67 connection_state.send(response).await?;
68
69 Ok(())
70 }
71
72 async fn display_image(
73 Message(request): Message<UserDisplayImageRequest>,
74 State(connection_state): State<ConnectionState>,
75 ) -> Result<(), UserError> {
76 let mut user_backend = connection_state.user_backend.lock().await;
77 let response = user_backend.display_image(request.clone()).await?;
78
79 drop(user_backend);
80
81 connection_state.send(response).await?;
82
83 Ok(())
84 }
85
86 async fn bio(
87 Message(request): Message<UserBioRequest>,
88 State(connection_state): State<ConnectionState>,
89 ) -> Result<(), UserError> {
90 let mut user_backend = connection_state.user_backend.lock().await;
91 let response = user_backend.bio(request.clone()).await?;
92
93 drop(user_backend);
94
95 connection_state.send(response).await?;
96
97 Ok(())
98 }
99
100 async fn repositories(
101 Message(request): Message<UserRepositoriesRequest>,
102 State(connection_state): State<ConnectionState>,
103 requesting_user: Option<AuthenticatedUser>,
104 ) -> Result<(), UserError> {
105 let requesting_user = requesting_user.map(|u| u.0);
106
107 let mut repository_backend = connection_state.repository_backend.lock().await;
108 let repositories = repository_backend
109 .repositories_for_user(requesting_user.as_ref(), &request.user)
110 .await;
111
112 let repositories = match repositories {
113 Ok(repositories) => repositories,
114 Err(err) => {
115 error!("Error handling request: {:?}", err);
116 return Ok(());
117 }
118 };
119 drop(repository_backend);
120
121 let mut user_backend = connection_state.user_backend.lock().await;
122 let user_exists = user_backend.exists(&request.user).await;
123
124 if repositories.is_empty() && !matches!(user_exists, Ok(true)) {
125 return Err(UserError::InvalidUser(request.user));
126 }
127
128 let response: UserRepositoriesResponse = UserRepositoriesResponse { repositories };
129
130 connection_state.send(response).await?;
131
132 Ok(())
133 }
134
135 async fn user_settings(
136 Message(request): Message<UserSettingsRequest>,
137 State(connection_state): State<ConnectionState>,
138 AuthenticatedUser(requesting_user): AuthenticatedUser,
139 ) -> Result<(), UserError> {
140 if request.user != requesting_user {
141 return Err(UserError::InvalidUser(request.user));
142 }
143
144 let mut user_backend = connection_state.user_backend.lock().await;
145 let mut settings = user_backend.settings(&request.user).await?;
146
147 drop(user_backend);
148
149 let response = UserSettingsResponse {
150 settings: settings.drain(..).collect(),
151 };
152
153 connection_state.send(response).await?;
154
155 Ok(())
156 }
157
158 async fn write_user_settings(
159 Message(request): Message<UserWriteSettingsRequest>,
160 State(connection_state): State<ConnectionState>,
161 AuthenticatedUser(requesting_user): AuthenticatedUser,
162 ) -> Result<(), UserError> {
163 if request.user != requesting_user {
164 return Err(UserError::InvalidUser(request.user));
165 }
166
167 let mut user_backend = connection_state.user_backend.lock().await;
168 user_backend
169 .write_settings(&request.user, &request.settings)
170 .await?;
171
172 drop(user_backend);
173
174 let response = UserWriteSettingsResponse {};
175
176 connection_state.send(response).await?;
177
178 Ok(())
179 }
180
181 #[derive(Debug, thiserror::Error)]
182 pub enum UserError {
183 #[error("invalid user {0}")]
184 InvalidUser(User),
185 #[error("{0}")]
186 Other(#[from] Error),
187 }
188