From e98276e79cea0c3e474ca0251c276c474c35ed70 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Sun, 21 May 2023 11:19:28 +0000 Subject: groups --- src/groups/groups.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/groups/groups.rs (limited to 'src/groups/groups.rs') diff --git a/src/groups/groups.rs b/src/groups/groups.rs new file mode 100644 index 0000000..3b837a7 --- /dev/null +++ b/src/groups/groups.rs @@ -0,0 +1,47 @@ +use std::fs; + +use nostr::EventId; + +use crate::{utils::load_file}; + +use super::group::Group; + +pub struct Groups { + groups:Vec +} +impl Groups { + pub fn new() -> Self { + + let cur_dir = std::env::current_dir().unwrap(); + + // check for potential problems + let ngit_path = cur_dir.clone().join(".ngit"); + if !ngit_path.is_dir() { + panic!("ngit not initialised. Run 'ngit init' first..."); + } + + let mut groups = vec![]; + + for dir_entry in fs::read_dir(ngit_path.join("groups")) + .expect("groups folder to exist and read_dir to read it") + { + groups.push( + Group::new_from_json_event( + load_file( + dir_entry + .expect("DirEntry in read_dir should exist").path() + ) + .expect("group json to load from file") + ).expect("group json to produce Group") + ); + } + + Self { + groups, + } + } + + pub fn by_event_id(&self, id:&EventId) -> Option<&Group> { + self.groups.iter().find(|g| g.id == *id) + } +} \ No newline at end of file -- cgit v1.2.3