~james/core

core/sites/src/kvmap.rs -rw-r--r-- 3.1 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
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
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use worker::*;

use crate::claim::SitesStore;

#[derive(Deserialize)]
#[serde(untagged)]
pub enum RepoEntry {
    New {
        rkey: String,
        #[serde(default)]
        is_index: bool,
    },
    Legacy(bool),
}

impl RepoEntry {
    pub fn is_index(&self) -> bool {
        match self {
            RepoEntry::New { is_index, .. } => *is_index,
            RepoEntry::Legacy(b) => *b,
        }
    }

    pub fn rkey<'a>(&'a self, name: &'a str) -> &'a str {
        match self {
            RepoEntry::New { rkey, .. } => rkey.as_str(),
            RepoEntry::Legacy(_) => name,
        }
    }
}

#[derive(Deserialize)]
pub struct DomainMapping {
    #[serde(default)]
    pub did: String,
    #[serde(default)]
    pub repos: HashMap<String, RepoEntry>,
}

impl DomainMapping {
    pub fn index_repo(&self) -> Option<(&str, &RepoEntry)> {
        self.repos.iter().find_map(|(name, entry)| {
            if entry.is_index() {
                Some((name.as_str(), entry))
            } else {
                None
            }
        })
    }
}

#[derive(Serialize)]
struct MappingEntry {
    rkey: String,
    #[serde(default)]
    is_index: bool,
}

#[derive(Serialize)]
struct WriteMapping {
    did: String,
    repos: HashMap<String, MappingEntry>,
}

pub async fn rebuild_for_domain(
    kv: &KvStore,
    store: &SitesStore,
    domain: &str,
) -> Result<(), String> {
    match store.mapping_rows_for_domain(domain).await? {
        None => {
            kv.put(domain, "{}")
                .map_err(|e| e.to_string())?
                .execute()
                .await
                .map_err(|e| e.to_string())?;
        }
        Some((owner_did, sites_list)) => {
            let mut repos = HashMap::with_capacity(sites_list.len());
            for (name, rkey, is_index) in sites_list {
                repos.insert(name, MappingEntry { rkey, is_index });
            }
            let mapping = WriteMapping {
                did: owner_did,
                repos,
            };
            let raw = serde_json::to_string(&mapping).map_err(|e| e.to_string())?;
            kv.put(domain, raw)
                .map_err(|e| e.to_string())?
                .execute()
                .await
                .map_err(|e| e.to_string())?;
        }
    }
    Ok(())
}

pub async fn delete_r2_prefix(bucket: &Bucket, prefix: &str) -> Result<(), String> {
    let mut cursor: Option<String> = None;
    loop {
        let mut list = bucket.list().prefix(prefix.to_string()).limit(1000);
        if let Some(c) = cursor {
            list = list.cursor(c);
        }
        let objects = list.execute().await.map_err(|e| e.to_string())?;
        let keys: Vec<String> = objects.objects().iter().map(|o| o.key()).collect();
        if !keys.is_empty() {
            bucket
                .delete_multiple(keys)
                .await
                .map_err(|e| e.to_string())?;
        }
        if !objects.truncated() {
            break;
        }
        match objects.cursor() {
            Some(c) => cursor = Some(c),
            None => break,
        }
    }
    Ok(())
}