~ knotless Search/
↑↓ move↵ open⇧↵ all results

johnandersen777.bsky.social/vuln-notif

0
0
main
129 lines · 4.2 KB Raw History
//! Link extraction and local index.
//!
//! Local writes are made crash-durable by [`repair::index_or_queue`]: call it
//! with every record written to the PDS, commit the DB before advancing the
//! publish cursor, and drain queued repairs with
//! [`repair::drain_link_repairs`]. See [`repair`] for the full ordering
//! contract.

use serde_json::Value;

pub use crate::db::model::Link;
pub use repair::{LinkRepairCounters, drain_link_repairs, index_or_queue, repair_after_commit};

pub mod repair;

use crate::constants::{
    ATPROTO_COLLECTION, CHANNEL_COLLECTION, MANIFEST_COLLECTION, PROJECT_COLLECTION,
    PURL_COLLECTION, SOFTWARE_COLLECTION, STATUS_COLLECTION, VERSION_COLLECTION,
};
use crate::db::DbHandle;

/// Constellation source-path syntax; `[]` expands over array elements.
pub const LINK_SPECS: &[(&str, &[&str])] = &[
    (
        ATPROTO_COLLECTION,
        &[
            "nvd.uri",
            "software[].project.uri",
            "software[].channels[].uri",
            "software[].versions[].uri",
            "software[].manifests[].uri",
        ],
    ),
    (SOFTWARE_COLLECTION, &["project.uri"]),
    (PURL_COLLECTION, &["projects[].uri"]),
    (PROJECT_COLLECTION, &["parent", "renamedTo"]),
    (MANIFEST_COLLECTION, &["project"]),
    (VERSION_COLLECTION, &["project", "manifest.uri"]),
    (CHANNEL_COLLECTION, &["project", "version.uri"]),
    (STATUS_COLLECTION, &["subject"]),
    ("com.nahdig.release.projectMeta", &["project"]),
];

fn target(value: &Value) -> Vec<(String, Option<String>)> {
    if let Some(uri) = value.as_str() {
        return if uri.starts_with("at://") {
            vec![(uri.to_owned(), None)]
        } else {
            Vec::new()
        };
    }
    let Some(object) = value.as_object() else {
        return Vec::new();
    };
    let Some(uri) = object.get("uri").and_then(Value::as_str) else {
        return Vec::new();
    };
    if !uri.starts_with("at://") {
        return Vec::new();
    }
    let cid = object.get("cid").and_then(Value::as_str).map(str::to_owned);
    vec![(uri.to_owned(), cid)]
}

fn walk(value: &Value, segments: &[&str]) -> Vec<(String, Option<String>)> {
    let Some((head, rest)) = segments.split_first() else {
        return target(value);
    };
    let Some(object) = value.as_object() else {
        return Vec::new();
    };
    match head.strip_suffix("[]") {
        Some(field) => {
            let Some(items) = object.get(field).and_then(Value::as_array) else {
                return Vec::new();
            };
            items.iter().flat_map(|item| walk(item, rest)).collect()
        }
        None => match object.get(*head) {
            Some(item) => walk(item, rest),
            None => Vec::new(),
        },
    }
}

/// Return links for the collection's configured paths, in spec order.
pub fn extract_links(collection: &str, record: &Value) -> Vec<Link> {
    let paths = LINK_SPECS
        .iter()
        .find_map(|(name, paths)| (*name == collection).then_some(*paths))
        .unwrap_or(&[]);
    let mut links = Vec::new();
    let mut seen = std::collections::HashSet::new();
    for path in paths {
        let segments: Vec<&str> = path.split('.').collect();
        // A trailing `.uri` names the link target; walk to its containing
        // strongRef so the CID is captured too.
        let segments = match segments.split_last() {
            Some((last, head)) if *last == "uri" => head,
            _ => &segments[..],
        };
        for (subject_uri, subject_cid) in walk(record, segments) {
            if !seen.insert((path.to_string(), subject_uri.clone())) {
                continue;
            }
            links.push(Link {
                path: (*path).to_owned(),
                subject_uri,
                subject_cid,
            });
        }
    }
    links
}

/// Replace the local link rows for a successfully written record.
pub async fn index_record(
    db: &DbHandle,
    repo_did: &str,
    uri: &str,
    cid: Option<&str>,
    collection: &str,
    record: &Value,
) -> anyhow::Result<()> {
    let links = extract_links(collection, record);
    let rkey = uri.rsplit('/').next().unwrap_or(uri);
    db.replace_links(repo_did, uri, collection, rkey, cid, &links)
        .await
}