-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapply_writes.rs
More file actions
347 lines (320 loc) · 10.7 KB
/
Copy pathapply_writes.rs
File metadata and controls
347 lines (320 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! com.atproto.repo.applyWrites endpoint.
//!
//! Applies multiple write operations to the repository atomically.
use std::net::SocketAddr;
use std::sync::Arc;
use axum::{
Json,
extract::{ConnectInfo, State},
http::{HeaderMap, StatusCode},
response::{IntoResponse, Response},
};
use serde::{Deserialize, Serialize};
use crate::pds::db::StatisticKey;
use crate::pds::server::PdsState;
use crate::pds::user_repo::{ApplyWritesOperation, UserRepo, parse_json_to_dag_cbor, write_type};
use crate::pds::auth::{auth_failure_response, check_user_auth};
use super::{get_caller_info};
/// A single write operation in the request.
#[derive(Deserialize)]
pub struct WriteOperation {
/// Operation type.
#[serde(rename = "$type")]
op_type: String,
/// Collection NSID.
collection: String,
/// Record key (optional for creates).
rkey: Option<String>,
/// Record value (for create/update).
value: Option<serde_json::Value>,
}
/// Request body for applyWrites.
#[derive(Deserialize)]
pub struct ApplyWritesRequest {
/// Repository DID (must match authenticated user).
repo: String,
/// List of write operations.
writes: Vec<WriteOperation>,
/// Optional swap commit CID for optimistic concurrency.
#[serde(rename = "swapCommit")]
swap_commit: Option<String>,
}
/// Commit information in the response.
#[derive(Serialize)]
pub struct CommitInfo {
/// CID of the commit.
cid: String,
/// Revision string.
rev: String,
}
/// Result of a single write operation.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WriteResult {
/// Result type.
#[serde(rename = "$type")]
result_type: String,
/// AT URI of the record.
#[serde(skip_serializing_if = "Option::is_none")]
uri: Option<String>,
/// CID of the record.
#[serde(skip_serializing_if = "Option::is_none")]
cid: Option<String>,
/// Validation status.
#[serde(skip_serializing_if = "Option::is_none")]
validation_status: Option<String>,
}
/// Successful response for applyWrites.
#[derive(Serialize)]
pub struct ApplyWritesResponse {
/// Commit information.
commit: CommitInfo,
/// Results for each write operation.
results: Vec<WriteResult>,
}
/// Error response for applyWrites.
#[derive(Serialize)]
pub struct ApplyWritesError {
error: String,
message: String,
}
/// POST /xrpc/com.atproto.repo.applyWrites - Apply multiple write operations.
///
/// Applies a batch of create, update, and delete operations atomically.
///
/// # Request Body
///
/// * `repo` - Repository DID
/// * `writes` - List of write operations
/// * `swapCommit` - Optional CID for optimistic concurrency
///
/// # Returns
///
/// * `200 OK` with commit info and results
/// * `400 Bad Request` if parameters are invalid
/// * `401 Unauthorized` if not authenticated
pub async fn apply_writes(
State(state): State<Arc<PdsState>>,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
headers: HeaderMap,
Json(body): Json<ApplyWritesRequest>,
) -> Response {
// Get caller info for statistics
let (ip_address, user_agent) = get_caller_info(&headers, Some(addr));
// Increment statistics
let stat_key = StatisticKey {
name: "xrpc/com.atproto.repo.applyWrites".to_string(),
ip_address: ip_address.clone(),
user_agent: user_agent.clone(),
};
let _ = state.db.increment_statistic_for_endpoint(&stat_key);
// Check authentication (supports Legacy and OAuth)
let auth_result = check_user_auth(
&state,
&headers,
None,
"POST",
"/xrpc/com.atproto.repo.applyWrites",
);
if !auth_result.is_authenticated {
return auth_failure_response(&auth_result);
}
// Validate input
if body.repo.is_empty() {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidRequest".to_string(),
message: "Error: invalid params.".to_string(),
}),
)
.into_response();
}
if body.writes.is_empty() {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidRequest".to_string(),
message: "Error: writes array is required.".to_string(),
}),
)
.into_response();
}
// Check swapCommit if provided
if let Some(swap_cid) = &body.swap_commit {
match state.db.get_repo_commit() {
Ok(current_commit) => {
if ¤t_commit.cid != swap_cid {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidSwap".to_string(),
message: "Commit CID mismatch.".to_string(),
}),
)
.into_response();
}
}
Err(_) => {
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(ApplyWritesError {
error: "InternalError".to_string(),
message: "Failed to get current commit.".to_string(),
}),
)
.into_response();
}
}
}
// Parse write operations
let mut operations = Vec::new();
for write in &body.writes {
if write.collection.is_empty() {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidRequest".to_string(),
message: "Error: missing collection in write operation.".to_string(),
}),
)
.into_response();
}
// Determine operation type
let normalized_type = if write.op_type.contains("#create") {
write_type::CREATE
} else if write.op_type.contains("#update") {
write_type::UPDATE
} else if write.op_type.contains("#delete") {
write_type::DELETE
} else {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidRequest".to_string(),
message: format!("Error: unknown operation type: {}", write.op_type),
}),
)
.into_response();
};
// Generate rkey if not provided for create
let rkey = match &write.rkey {
Some(r) if !r.is_empty() => r.clone(),
_ => {
if normalized_type == write_type::CREATE {
UserRepo::generate_tid()
} else {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidRequest".to_string(),
message: "Error: rkey is required for update/delete operations.".to_string(),
}),
)
.into_response();
}
}
};
// Parse record for create/update
let record = if normalized_type == write_type::CREATE || normalized_type == write_type::UPDATE {
match &write.value {
Some(v) => match parse_json_to_dag_cbor(v) {
Ok(r) => Some(r),
Err(e) => {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidRequest".to_string(),
message: format!("Error: failed to parse record value: {}", e),
}),
)
.into_response();
}
},
None => {
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "InvalidRequest".to_string(),
message: "Error: value is required for create/update operations.".to_string(),
}),
)
.into_response();
}
}
} else {
None
};
operations.push(ApplyWritesOperation {
op_type: normalized_type.to_string(),
collection: write.collection.clone(),
rkey,
record,
});
}
// Create UserRepo and apply the writes
let user_repo = match UserRepo::new(&state.db) {
Ok(repo) => repo,
Err(e) => {
state.log.error(&format!("Failed to create UserRepo: {}", e));
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(ApplyWritesError {
error: "InternalError".to_string(),
message: "Failed to initialize repository".to_string(),
}),
)
.into_response();
}
};
let results = match user_repo.apply_writes(operations, &ip_address, &user_agent) {
Ok(results) => results,
Err(e) => {
state.log.error(&format!("Failed to apply writes: {}", e));
return (
StatusCode::BAD_REQUEST,
Json(ApplyWritesError {
error: "ApplyWritesFailed".to_string(),
message: format!("Error applying writes: {}", e),
}),
)
.into_response();
}
};
// Get updated commit info
let commit = match state.db.get_repo_commit() {
Ok(c) => c,
Err(e) => {
state.log.error(&format!("Failed to get repo commit: {}", e));
return (
StatusCode::INTERNAL_SERVER_ERROR,
Json(ApplyWritesError {
error: "InternalError".to_string(),
message: "Failed to get commit info".to_string(),
}),
)
.into_response();
}
};
// Build response results
let response_results: Vec<WriteResult> = results
.iter()
.map(|r| WriteResult {
result_type: r.result_type.clone(),
uri: r.uri.clone(),
cid: r.cid.as_ref().map(|c| c.base32.clone()),
validation_status: r.validation_status.clone(),
})
.collect();
(
StatusCode::OK,
Json(ApplyWritesResponse {
commit: CommitInfo {
cid: commit.cid,
rev: commit.rev,
},
results: response_results,
}),
)
.into_response()
}