upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/repos/init.rs
blob: 3254d3e7f98ac63d3cca59ce80fd681ad45b96b0 (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
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

use std::fmt::Debug;
use nostr_sdk::{EventBuilder, Tag, Keys, Event};
use serde::{Deserialize, Serialize};

use crate::{kind::Kind, ngit_tag::{tag_repo, tag_relays, tag_hashtag, tag_group_with_relays, tag_into_event}};

/// [`InitializeRepo`] error
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Error serializing or deserializing JSON data
    #[error("json error: {0}")]
    Json(#[from] serde_json::Error),
}

impl InitializeRepo {

    pub fn initialize(&self,keys:&Keys) -> Event {
        // let keys = Keys::generate();
        EventBuilder::new(
            nostr_sdk::Kind::Custom(
                match self.root_repo {
                    None => u64::from(Kind::InitializeRepo),
                    _ => u64::from(Kind::InitializeBranch),
                }
            ),
            self.as_json(),
            &self.generate_tags(),
        )
        .to_unsigned_event(keys.public_key())
        .sign(&keys)
        .unwrap()
    }

    fn generate_tags(&self) -> Vec<Tag> {
        let mut tags = 
        vec![
            self.maintainers_group.as_ref()
                .expect("there always to be a maintainers group when initialising")
                .clone(),
            tag_hashtag("ngit-event"),
            tag_hashtag("ngit-format-0.0.1"),
        ];
        if !self.relays.is_empty() {
            tags.push(
                tag_relays(&self.relays)
            );
        }

        match &self.root_repo {
            None =>(),
            Some(id) => {
                tags.push(
                    tag_repo(id)
                );
                tags.push(
                    tag_into_event(
                        // its a bit silly / lazy reusing this function just to get the tags formatted with relays when it is not a group
                        tag_group_with_relays(id, &self.relays)
                    )
                );

            }
        }
        tags
    }
}

/// InitializeRepo
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
pub struct InitializeRepo {
    /// Name
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Description
    #[serde(skip_serializing_if = "Option::is_none")]
    pub about: Option<String>,
    /// Picture
    #[serde(skip_serializing_if = "Option::is_none")]
    pub picture: Option<String>,
    /// relays
    pub relays: Vec<String>,
    /// Maintainers Group
    #[serde(skip_serializing_if = "Option::is_none")]
    pub maintainers_group: Option<Tag>,
    /// Maintainers Group
    #[serde(skip_serializing_if = "Option::is_none")]
    pub root_repo: Option<String>,
}

impl Default for InitializeRepo {
    fn default() -> Self {
        Self::new()
    }
}

impl InitializeRepo {
    /// New empty [`InitializeRepo`]
    pub fn new() -> Self {
        Self {
            name: None,
            about: None,
            picture: None,
            relays: vec![],
            maintainers_group: None,
            root_repo:None,
        }
    }

    /// Deserialize [`InitializeRepo`] from `JSON` string
    pub fn from_json<S>(json: S) -> Result<Self, Error>
    where
        S: Into<String>,
    {
        Ok(serde_json::from_str(&json.into())?)
    }

    /// Serialize [`InitializeRepo`] to `JSON` string
    pub fn as_json(&self) -> String {
        serde_json::json!(self).to_string()
    }

    /// Set name
    pub fn name<S>(self, name: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            name: Some(name.into()),
            ..self
        }
    }

    /// Set about
    pub fn about<S>(self, about: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            about: Some(about.into()),
            ..self
        }
    }

    /// Set picture
    pub fn picture<S>(self, picture: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            picture: Some(picture.into()),
            ..self
        }
    }

    /// Set relays
    pub fn relays(mut self, relays: &Vec<String>) -> Self {
        for m in relays {
            self.relays.push(m.clone());
        }
        self
    }

    /// Set maintainers_group
    pub fn maintainers_group(self, group_ref: Tag) -> Self {
        Self {
            maintainers_group: Some(group_ref),
            ..self
        }
    }

    /// Set root_repo
    pub fn root_repo<S>(self, root_repo: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            root_repo: Some(root_repo.into()),
            ..self
        }
    }
    
}