diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:19:43 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2023-05-21 11:19:43 +0000 |
| commit | a579e90666d49a0f9353319525b1790ad7b26c4a (patch) | |
| tree | 30f783750741eca1703e1edb3174d7e6cedbf0f8 /src/repos/repo.rs | |
| parent | e98276e79cea0c3e474ca0251c276c474c35ed70 (diff) | |
repos
Diffstat (limited to 'src/repos/repo.rs')
| -rw-r--r-- | src/repos/repo.rs | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/repos/repo.rs b/src/repos/repo.rs new file mode 100644 index 0000000..0d3001f --- /dev/null +++ b/src/repos/repo.rs | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | |||
| 2 | use std::{path::PathBuf}; | ||
| 3 | |||
| 4 | use nostr::{EventId, Event, prelude::{Nip19Event, ToBech32}, Tag}; | ||
| 5 | use nostr_sdk::{Keys}; | ||
| 6 | |||
| 7 | use crate::{groups::{group::{MembershipCollection, StartFinish}}, utils::load_file}; | ||
| 8 | |||
| 9 | use super::init::{InitializeRepo, self}; | ||
| 10 | |||
| 11 | /// [`Repo`] error | ||
| 12 | #[derive(Debug, thiserror::Error)] | ||
| 13 | pub enum Error { | ||
| 14 | /// Error processing initialisation Repo content json - incorrect format? | ||
| 15 | #[error("Repo cannot be initialised from content: {0}")] | ||
| 16 | InitializeJson(#[from] init::Error), | ||
| 17 | } | ||
| 18 | |||
| 19 | /// Repo, acts a branch if root_repo is set | ||
| 20 | pub struct Repo { | ||
| 21 | pub id: EventId, | ||
| 22 | pub name:Option<String>, | ||
| 23 | about:Option<String>, | ||
| 24 | picture:Option<String>, | ||
| 25 | pub relays:Vec<String>, | ||
| 26 | pub maintainers_group:MembershipCollection, | ||
| 27 | pub events:Vec<Event>, | ||
| 28 | pub root_repo: Option<EventId>, | ||
| 29 | hash: String, // hash of event IDs that make up this state | ||
| 30 | } | ||
| 31 | |||
| 32 | impl Repo { | ||
| 33 | |||
| 34 | pub fn new(init:&InitializeRepo, keys:&Keys) -> Result<Self,Error> { | ||
| 35 | let event = init.initialize(&keys); | ||
| 36 | Repo::new_from_event(event) | ||
| 37 | } | ||
| 38 | |||
| 39 | pub fn open(repo_dir_path: &PathBuf) -> Self { | ||
| 40 | Repo::new_from_json_event( | ||
| 41 | load_file( | ||
| 42 | repo_dir_path.join(".ngit/repo.json"), | ||
| 43 | ) | ||
| 44 | .expect("repo.json load from file") | ||
| 45 | ) | ||
| 46 | .expect("repo.json to produce Repo") | ||
| 47 | } | ||
| 48 | |||
| 49 | pub fn new_from_json_event(json_string:String) -> Result<Self,Error> { | ||
| 50 | let event = Event::from_json(json_string) | ||
| 51 | .expect("json_string to be formated as event"); | ||
| 52 | Repo::new_from_event(event) | ||
| 53 | } | ||
| 54 | |||
| 55 | pub fn new_from_event(event:Event) -> Result<Self,Error> { | ||
| 56 | match InitializeRepo::from_json(&event.content) { | ||
| 57 | Err(e) => return Err(Error::InitializeJson(e)), | ||
| 58 | Ok(g) => { | ||
| 59 | let start_finish = StartFinish { start: event.created_at, finish: None }; | ||
| 60 | // add maintainers_group | ||
| 61 | let mut maintainers_group = MembershipCollection::new(); | ||
| 62 | match g.maintainers_group { | ||
| 63 | None => (), | ||
| 64 | Some(t) => { | ||
| 65 | maintainers_group.add_group_dates( | ||
| 66 | Tag::parse(t.into()) | ||
| 67 | .expect("maintainers_group to parse into Tag"), | ||
| 68 | start_finish.clone(), | ||
| 69 | ) | ||
| 70 | } | ||
| 71 | } | ||
| 72 | Ok(Self { | ||
| 73 | id: event.id, | ||
| 74 | name: g.name, | ||
| 75 | about: g.about, | ||
| 76 | picture: g.picture, | ||
| 77 | relays: g.relays, | ||
| 78 | maintainers_group, | ||
| 79 | events:vec![event], | ||
| 80 | root_repo:None, | ||
| 81 | hash: "hash".to_string(), // hash of event IDs that make up this state | ||
| 82 | }) | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | pub fn nevent(&self) -> String { | ||
| 88 | let e = Nip19Event { | ||
| 89 | event_id: self.id.clone(), | ||
| 90 | relays: if self.relays.len() > 1 { | ||
| 91 | vec![self.relays[0].clone(),self.relays[1].clone()] | ||
| 92 | } | ||
| 93 | else if self.relays.len() == 1 { | ||
| 94 | vec![self.relays[0].clone()] | ||
| 95 | } | ||
| 96 | else { vec![] } | ||
| 97 | }; | ||
| 98 | e.to_bech32() | ||
| 99 | .expect("Nip19Event to produce nevent String") | ||
| 100 | } | ||
| 101 | } | ||