upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/groups/groups.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/groups/groups.rs')
-rw-r--r--src/groups/groups.rs47
1 files changed, 47 insertions, 0 deletions
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 @@
1use std::fs;
2
3use nostr::EventId;
4
5use crate::{utils::load_file};
6
7use super::group::Group;
8
9pub struct Groups {
10 groups:Vec<Group>
11}
12impl Groups {
13 pub fn new() -> Self {
14
15 let cur_dir = std::env::current_dir().unwrap();
16
17 // check for potential problems
18 let ngit_path = cur_dir.clone().join(".ngit");
19 if !ngit_path.is_dir() {
20 panic!("ngit not initialised. Run 'ngit init' first...");
21 }
22
23 let mut groups = vec![];
24
25 for dir_entry in fs::read_dir(ngit_path.join("groups"))
26 .expect("groups folder to exist and read_dir to read it")
27 {
28 groups.push(
29 Group::new_from_json_event(
30 load_file(
31 dir_entry
32 .expect("DirEntry in read_dir should exist").path()
33 )
34 .expect("group json to load from file")
35 ).expect("group json to produce Group")
36 );
37 }
38
39 Self {
40 groups,
41 }
42 }
43
44 pub fn by_event_id(&self, id:&EventId) -> Option<&Group> {
45 self.groups.iter().find(|g| g.id == *id)
46 }
47} \ No newline at end of file