summaryrefslogtreecommitdiffstats
path: root/adenosine-pds/src/web.rs
blob: 5b9d6def28cec994991a3d682225e1d97d096b69 (plain)
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
use crate::models::*;
use crate::repo::RepoCommit;
use adenosine_cli::identifiers::{Did, Nsid, Tid};
use askama::Template;
use serde_json;

#[derive(Template)]
#[template(path = "home.html")]
pub struct GenericHomeView {
    pub domain: String,
}

#[derive(Template)]
#[template(path = "about.html")]
pub struct AboutView {
    pub domain: String,
}

#[derive(Template)]
#[template(path = "account.html")]
pub struct AccountView {
    pub domain: String,
    pub did: Did,
    pub profile: Profile,
    pub feed: Vec<FeedItem>,
}

#[derive(Template)]
#[template(path = "thread.html")]
pub struct ThreadView {
    pub domain: String,
    pub did: Did,
    pub collection: Nsid,
    pub tid: Tid,
    pub thread: Vec<ThreadItem>,
}

#[derive(Template)]
#[template(path = "at_repo.html")]
pub struct RepoView {
    pub domain: String,
    pub did: Did,
    pub commit: RepoCommit,
    pub describe: RepoDescribe,
}

#[derive(Template)]
#[template(path = "at_collection.html")]
pub struct CollectionView {
    pub domain: String,
    pub did: Did,
    pub collection: Nsid,
    pub records: Vec<serde_json::Value>,
}

#[derive(Template)]
#[template(path = "at_record.html")]
pub struct RecordView {
    pub domain: String,
    pub did: Did,
    pub collection: Nsid,
    pub tid: Tid,
    pub record: serde_json::Value,
}

mod filters {
    use crate::AtUri;
    use std::str::FromStr;

    pub fn aturi_to_path(aturi: &str) -> ::askama::Result<String> {
        let aturi = AtUri::from_str(aturi).expect("aturi parse");
        if aturi.record.is_some() {
            Ok(format!(
                "/at/{}/{}/{}",
                aturi.repository,
                aturi.collection.unwrap(),
                aturi.record.unwrap()
            ))
        } else if aturi.collection.is_some() {
            Ok(format!(
                "/at/{}/{}",
                aturi.repository,
                aturi.collection.unwrap()
            ))
        } else {
            Ok(format!("/at/{}", aturi.repository))
        }
    }

    pub fn aturi_to_thread_path(aturi: &str) -> ::askama::Result<String> {
        let aturi = AtUri::from_str(aturi).expect("aturi parse");
        Ok(format!(
            "/u/{}/post/{}",
            aturi.repository,
            aturi.record.unwrap()
        ))
    }

    pub fn aturi_to_tid(aturi: &str) -> ::askama::Result<String> {
        let aturi = AtUri::from_str(aturi).expect("aturi parse");
        if aturi.record.is_some() {
            Ok(aturi.record.unwrap())
        } else {
            // TODO: raise an askama error here?
            Ok("<MISSING>".to_string())
        }
    }
}