aboutsummaryrefslogtreecommitdiff
path: root/logic/src/admin/mod.rs
blob: 804cb2b233d7de6b00980dab452e44c0d34a9894 (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
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/

pub mod log;
pub mod user;

use crate::{DATABASE, session::AdminSession};
use anyhow::{Result, anyhow};
use jellyimport::{IMPORT_ERRORS, import_wrap};
use rand::Rng;
use std::time::{Duration, Instant};
use tokio::task::spawn_blocking;

pub async fn get_import_errors(_session: &AdminSession) -> Vec<String> {
    IMPORT_ERRORS.read().await.to_owned()
}
pub fn list_invites(_session: &AdminSession) -> Result<Vec<String>> {
    DATABASE.list_invites()
}

pub fn create_invite(_session: &AdminSession) -> Result<String> {
    let i = format!("{}", rand::rng().random::<u128>());
    DATABASE.create_invite(&i)?;
    Ok(i)
}
pub fn delete_invite(_session: &AdminSession, invite: &str) -> Result<()> {
    if !DATABASE.delete_invite(invite)? {
        Err(anyhow!("invite does not exist"))?;
    };
    Ok(())
}
pub async fn update_search_index(_session: &AdminSession) -> Result<()> {
    spawn_blocking(move || DATABASE.search_create_index()).await?
}
pub async fn do_import(
    _session: &AdminSession,
    incremental: bool,
) -> Result<(Duration, Result<()>)> {
    let t = Instant::now();
    if !incremental {
        DATABASE.clear_nodes()?;
    }
    let r = import_wrap((*DATABASE).clone(), incremental).await;
    Ok((t.elapsed(), r))
}