sbol-db-server 0.1.7

HTTP server for sbol-db.
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//! SynBioHub v1 field-edit and membership routes.
//!
//! These port classic SynBioHub's in-place mutations: the mutable text fields
//! (`/updateMutableDescription`, `/updateMutableNotes`, `/updateMutableSource`)
//! and citations (`/updateCitations`), which take the target `uri` in the body;
//! and the generic `<uri>/edit|add|remove/:field` plus `<uri>/addToCollection`
//! and `<uri>/removeMembership`, which take the target in the path. Every route
//! is identity-gated: an anonymous caller is `403`, and the facade rejects a
//! non-owner (or a non-admin editing a public object).

use axum::body::Bytes;
use axum::extract::{Path, State};
use axum::http::HeaderMap;
use axum::response::{IntoResponse, Response};
use axum::Extension;
use sbol_db_app::{EditService, FieldValue};
use serde::Deserialize;

use super::CurrentUser;
use crate::{ApiError, AppState};

const DCTERMS_TITLE: &str = "http://purl.org/dc/terms/title";
const DCTERMS_DESCRIPTION: &str = "http://purl.org/dc/terms/description";
const SBOL2_ROLE: &str = "http://sbols.org/v2#role";
const SBOL2_TYPE: &str = "http://sbols.org/v2#type";
const PROV_WAS_DERIVED_FROM: &str = "http://www.w3.org/ns/prov#wasDerivedFrom";

/// Build the [`EditService`] from the shared facade handles.
fn service(state: &AppState) -> EditService {
    state.app.edit_service()
}

/// The authenticated caller, or a `403` for an anonymous mutation attempt.
fn require_user(user: Option<sbol_db_core::User>) -> Result<sbol_db_core::User, ApiError> {
    user.ok_or_else(|| ApiError::Forbidden("authentication is required to edit".to_owned()))
}

/// The classic plain-text success body.
fn success() -> Response {
    (axum::http::StatusCode::OK, "Success").into_response()
}

// --- mutable fields + citations (uri in body) --------------------------------

/// A `{uri, value}` body, posted by the mutable-field and citation editors.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct MutableForm {
    pub uri: Option<String>,
    pub value: Option<String>,
}

pub async fn update_mutable_description(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let user = require_user(user)?;
    let form = parse_body::<MutableForm>(&headers, &body)?;
    let uri = required(form.uri, "uri")?;
    service(&state)
        .update_mutable_description(&user.graph_uri, user.is_admin, &uri, &value_of(&form.value))
        .await?;
    Ok(success())
}

pub async fn update_mutable_notes(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let user = require_user(user)?;
    let form = parse_body::<MutableForm>(&headers, &body)?;
    let uri = required(form.uri, "uri")?;
    service(&state)
        .update_mutable_notes(&user.graph_uri, user.is_admin, &uri, &value_of(&form.value))
        .await?;
    Ok(success())
}

pub async fn update_mutable_source(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let user = require_user(user)?;
    let form = parse_body::<MutableForm>(&headers, &body)?;
    let uri = required(form.uri, "uri")?;
    service(&state)
        .update_mutable_source(&user.graph_uri, user.is_admin, &uri, &value_of(&form.value))
        .await?;
    Ok(success())
}

pub async fn update_citations(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let user = require_user(user)?;
    let form = parse_body::<MutableForm>(&headers, &body)?;
    let uri = required(form.uri, "uri")?;
    let citations = parse_citations(form.value.as_deref())?;
    service(&state)
        .update_citations(&user.graph_uri, user.is_admin, &uri, &citations)
        .await?;
    Ok(success())
}

// --- generic edit/add/remove of a field (uri in path) ------------------------

/// A `<uri>/edit|add|remove/:field` body: the value, an optional previous value
/// (edit only), and the arbitrary predicate for the `annotation` field.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct FieldForm {
    pub object: Option<String>,
    pub previous: Option<String>,
    pub pred: Option<String>,
}

/// A user object path plus the trailing `:field` segment.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserFieldPath {
    pub user_id: String,
    pub collection_id: String,
    pub display_id: String,
    pub version: String,
    pub field: String,
}

/// A public object path plus the trailing `:field` segment.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicFieldPath {
    pub collection_id: String,
    pub display_id: String,
    pub version: String,
    pub field: String,
}

fn user_field_uri(state: &AppState, p: &UserFieldPath) -> String {
    format!(
        "{}user/{}/{}/{}/{}",
        state.app.registry_namespace.database_prefix(),
        p.user_id,
        p.collection_id,
        p.display_id,
        p.version
    )
}

fn public_field_uri(state: &AppState, p: &PublicFieldPath) -> String {
    format!(
        "{}public/{}/{}/{}",
        state.app.registry_namespace.database_prefix(),
        p.collection_id,
        p.display_id,
        p.version
    )
}

/// The three field-edit verbs, sharing predicate resolution and value
/// formatting.
enum Verb {
    Edit,
    Add,
    Remove,
}

pub async fn user_edit_field(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<UserFieldPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    run_field(
        state.clone(),
        user,
        user_field_uri(&state, &p),
        &p.field,
        Verb::Edit,
        &headers,
        &body,
    )
    .await
}

pub async fn user_add_field(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<UserFieldPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    run_field(
        state.clone(),
        user,
        user_field_uri(&state, &p),
        &p.field,
        Verb::Add,
        &headers,
        &body,
    )
    .await
}

pub async fn user_remove_field(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<UserFieldPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    run_field(
        state.clone(),
        user,
        user_field_uri(&state, &p),
        &p.field,
        Verb::Remove,
        &headers,
        &body,
    )
    .await
}

pub async fn public_edit_field(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<PublicFieldPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    run_field(
        state.clone(),
        user,
        public_field_uri(&state, &p),
        &p.field,
        Verb::Edit,
        &headers,
        &body,
    )
    .await
}

pub async fn public_add_field(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<PublicFieldPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    run_field(
        state.clone(),
        user,
        public_field_uri(&state, &p),
        &p.field,
        Verb::Add,
        &headers,
        &body,
    )
    .await
}

pub async fn public_remove_field(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<PublicFieldPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    run_field(
        state.clone(),
        user,
        public_field_uri(&state, &p),
        &p.field,
        Verb::Remove,
        &headers,
        &body,
    )
    .await
}

async fn run_field(
    state: AppState,
    user: Option<sbol_db_core::User>,
    uri: String,
    field: &str,
    verb: Verb,
    headers: &HeaderMap,
    body: &[u8],
) -> Result<Response, ApiError> {
    let user = require_user(user)?;
    let form = parse_body::<FieldForm>(headers, body)?;

    let predicate = resolve_predicate(field, form.pred.as_deref())?;
    let object = form
        .object
        .filter(|s| !s.is_empty())
        .ok_or_else(|| ApiError::BadRequest("object is required".to_owned()))?;
    let value = format_field_value(field, &object);

    let svc = service(&state);
    match verb {
        Verb::Edit => {
            // Title/description replace whatever value is present (previous is a
            // wildcard); other fields replace only a matching previous value.
            let previous = if field == "title" || field == "description" {
                None
            } else {
                form.previous
                    .filter(|s| !s.is_empty())
                    .map(|p| format_field_value(field, &p))
            };
            svc.edit_field(
                &user.graph_uri,
                user.is_admin,
                &uri,
                &predicate,
                &value,
                previous.as_ref(),
            )
            .await?;
        }
        Verb::Add => {
            svc.add_field(&user.graph_uri, user.is_admin, &uri, &predicate, &value)
                .await?;
        }
        Verb::Remove => {
            svc.remove_field(&user.graph_uri, user.is_admin, &uri, &predicate, &value)
                .await?;
        }
    }
    Ok(success())
}

/// Map a field name to its predicate IRI. `annotation` takes the arbitrary
/// predicate from the request body; an unknown field is `404`.
fn resolve_predicate(field: &str, pred: Option<&str>) -> Result<String, ApiError> {
    match field {
        "title" => Ok(DCTERMS_TITLE.to_owned()),
        "description" => Ok(DCTERMS_DESCRIPTION.to_owned()),
        "role" => Ok(SBOL2_ROLE.to_owned()),
        "type" => Ok(SBOL2_TYPE.to_owned()),
        "wasDerivedFrom" => Ok(PROV_WAS_DERIVED_FROM.to_owned()),
        "annotation" => pred
            .filter(|s| !s.is_empty())
            .map(str::to_owned)
            .ok_or_else(|| ApiError::BadRequest("annotation predicate is required".to_owned())),
        _ => Err(ApiError::NotFound(format!("unknown field: {field}"))),
    }
}

/// Format a field's object as an IRI or a literal, mirroring classic
/// `formatObject`: `role`/`type`/`wasDerivedFrom` and any `http(s)://` value are
/// IRIs, except `title`/`description`, which are always literals.
fn format_field_value(field: &str, object: &str) -> FieldValue {
    let is_iri = (field == "wasDerivedFrom"
        || field == "type"
        || field == "role"
        || object.starts_with("http://")
        || object.starts_with("https://"))
        && field != "title"
        && field != "description";
    if is_iri {
        FieldValue::Iri(object.to_owned())
    } else {
        FieldValue::Literal(object.to_owned())
    }
}

// --- membership (uri in path) ------------------------------------------------

/// A user or public object path.
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserObjectPath {
    pub user_id: String,
    pub collection_id: String,
    pub display_id: String,
    pub version: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicObjectPath {
    pub collection_id: String,
    pub display_id: String,
    pub version: String,
}

/// A `{member}` body naming the object to remove from a Collection.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct MemberForm {
    pub member: Option<String>,
}

/// The `{collections}` body of an `addToCollection`: the Collections to add the
/// path object to. Classic keys this on `collections` (the object is the URL
/// path), so a request that omits it adds no membership.
#[derive(Debug, Default, Deserialize)]
#[serde(default)]
pub struct AddToCollectionForm {
    pub collections: Option<String>,
}

pub async fn user_add_to_collection(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<UserObjectPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let object = format!(
        "{}user/{}/{}/{}/{}",
        state.app.registry_namespace.database_prefix(),
        p.user_id,
        p.collection_id,
        p.display_id,
        p.version
    );
    add_to_collections(state, user, object, &headers, &body).await
}

pub async fn user_remove_membership(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<UserObjectPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let collection = format!(
        "{}user/{}/{}/{}/{}",
        state.app.registry_namespace.database_prefix(),
        p.user_id,
        p.collection_id,
        p.display_id,
        p.version
    );
    membership(state, user, collection, false, &headers, &body).await
}

pub async fn public_add_to_collection(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<PublicObjectPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let object = format!(
        "{}public/{}/{}/{}",
        state.app.registry_namespace.database_prefix(),
        p.collection_id,
        p.display_id,
        p.version
    );
    add_to_collections(state, user, object, &headers, &body).await
}

pub async fn public_remove_membership(
    State(state): State<AppState>,
    Extension(CurrentUser(user)): Extension<CurrentUser>,
    Path(p): Path<PublicObjectPath>,
    headers: HeaderMap,
    body: Bytes,
) -> Result<Response, ApiError> {
    let collection = format!(
        "{}public/{}/{}/{}",
        state.app.registry_namespace.database_prefix(),
        p.collection_id,
        p.display_id,
        p.version
    );
    membership(state, user, collection, false, &headers, &body).await
}

/// `removeMembership`: drop `member` (a body field) from `collection` (the URL
/// path object). Mirrors classic `removeMembership`, which keys the member on
/// the `member` body field.
async fn membership(
    state: AppState,
    user: Option<sbol_db_core::User>,
    collection: String,
    add: bool,
    headers: &HeaderMap,
    body: &[u8],
) -> Result<Response, ApiError> {
    debug_assert!(!add, "addToCollection is served by add_to_collections");
    let user = require_user(user)?;
    let form = parse_body::<MemberForm>(headers, body)?;
    let member = normalize_member(&state, required(form.member, "member")?);
    service(&state)
        .remove_membership(&user.graph_uri, user.is_admin, &collection, &member)
        .await?;
    Ok(success())
}

/// `addToCollection`: add `object` (the URL path) as a `sbol:member` of every
/// Collection named in the `collections` body field, mirroring classic
/// `addToCollection` (the object is the path, the target Collections are the
/// body). A request that omits `collections` adds no membership.
async fn add_to_collections(
    state: AppState,
    user: Option<sbol_db_core::User>,
    object: String,
    headers: &HeaderMap,
    body: &[u8],
) -> Result<Response, ApiError> {
    let user = require_user(user)?;
    let form = parse_body::<AddToCollectionForm>(headers, body)?;
    let svc = service(&state);
    if let Some(collections) = form.collections.filter(|s| !s.is_empty()) {
        for target in collections
            .split(',')
            .map(str::trim)
            .filter(|s| !s.is_empty())
        {
            let target = normalize_member(&state, target.to_owned());
            svc.add_to_collection(&user.graph_uri, user.is_admin, &target, &object)
                .await?;
        }
    }
    Ok(success())
}

/// Expand a member reference to an absolute URI, mirroring classic's rewrite of
/// a leading `/public/` or `/user/` path onto the database prefix.
fn normalize_member(state: &AppState, member: String) -> String {
    let base = state.app.registry_namespace.database_prefix();
    if let Some(rest) = member.strip_prefix("/public/") {
        format!("{base}public/{rest}")
    } else if let Some(rest) = member.strip_prefix("/user/") {
        format!("{base}user/{rest}")
    } else {
        member
    }
}

// --- shared helpers ----------------------------------------------------------

/// The mutable-field value, defaulting an absent field to the empty string
/// (which clears the field, matching classic).
fn value_of(value: &Option<String>) -> String {
    value.clone().unwrap_or_default()
}

/// A required form field, or a `400` naming the missing field.
fn required(value: Option<String>, field: &str) -> Result<String, ApiError> {
    value
        .filter(|s| !s.is_empty())
        .ok_or_else(|| ApiError::BadRequest(format!("{field} is required")))
}

/// Split and validate a comma-separated citations field as PubMed ids
/// (digits only), mirroring classic's `^[0-9]+(,[0-9]*)*$` guard.
fn parse_citations(raw: Option<&str>) -> Result<Vec<String>, ApiError> {
    let raw = raw.unwrap_or("").trim();
    if raw.is_empty() {
        return Ok(Vec::new());
    }
    let mut ids = Vec::new();
    for part in raw.split(',') {
        let part = part.trim();
        if part.is_empty() {
            continue;
        }
        if !part.bytes().all(|b| b.is_ascii_digit()) {
            return Err(ApiError::BadRequest(
                "citations must be comma-separated PubMed ids".to_owned(),
            ));
        }
        ids.push(part.to_owned());
    }
    Ok(ids)
}

/// Parse a request body as JSON when the `Content-Type` says so, else as
/// form-encoded, matching the auth and makePublic routes. An empty body yields
/// the type's default.
fn parse_body<T: for<'de> Deserialize<'de> + Default>(
    headers: &HeaderMap,
    body: &[u8],
) -> Result<T, ApiError> {
    if body.is_empty() {
        return Ok(T::default());
    }
    let is_json = headers
        .get(axum::http::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .map(|v| v.starts_with("application/json"))
        .unwrap_or(false);
    if is_json {
        serde_json::from_slice(body)
            .map_err(|e| ApiError::BadRequest(format!("invalid JSON body: {e}")))
    } else {
        serde_urlencoded::from_bytes(body)
            .map_err(|e| ApiError::BadRequest(format!("invalid form body: {e}")))
    }
}