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

ambee/giterated

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

Add repository settings

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨f8eaf38

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