upleb.uk

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

summaryrefslogtreecommitdiff
path: root/src/groups/groups.rs
blob: 3b837a700511f808fafaec1832a155771f136063 (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
use std::fs;

use nostr::EventId;

use crate::{utils::load_file};

use super::group::Group;

pub struct Groups {
    groups:Vec<Group>
}
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)
    }
}