mirror of
https://git.joinplu.me/Plume/Plume.git
synced 2024-12-24 11:00:34 +00:00
Implement LOCAL_INSTANCE_USER and related methods
This commit is contained in:
parent
897ea8e11e
commit
1506802c20
1 changed files with 22 additions and 8 deletions
|
@ -8,6 +8,7 @@ use crate::{
|
||||||
};
|
};
|
||||||
use chrono::NaiveDateTime;
|
use chrono::NaiveDateTime;
|
||||||
use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQueryDsl};
|
use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||||
|
use once_cell::sync::OnceCell;
|
||||||
use plume_common::utils::md_to_html;
|
use plume_common::utils::md_to_html;
|
||||||
use std::sync::RwLock;
|
use std::sync::RwLock;
|
||||||
|
|
||||||
|
@ -46,6 +47,7 @@ lazy_static! {
|
||||||
}
|
}
|
||||||
|
|
||||||
const LOCAL_INSTANCE_USERNAME: &str = "__instance__";
|
const LOCAL_INSTANCE_USERNAME: &str = "__instance__";
|
||||||
|
static LOCAL_INSTANCE_USER: OnceCell<User> = OnceCell::new();
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
pub fn set_local(self) {
|
pub fn set_local(self) {
|
||||||
|
@ -78,16 +80,11 @@ impl Instance {
|
||||||
.map_err(Error::from)
|
.map_err(Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<Instance>> {
|
pub fn get_local_instance_user() -> Option<&'static User> {
|
||||||
instances::table
|
LOCAL_INSTANCE_USER.get()
|
||||||
.order(instances::public_domain.asc())
|
|
||||||
.offset(min.into())
|
|
||||||
.limit((max - min).into())
|
|
||||||
.load::<Instance>(conn)
|
|
||||||
.map_err(Error::from)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_local_instance_user(conn: &Connection) -> Result<User> {
|
pub fn get_local_instance_user_uncached(conn: &Connection) -> Result<User> {
|
||||||
users::table
|
users::table
|
||||||
.filter(users::role.eq(3))
|
.filter(users::role.eq(3))
|
||||||
.first(conn)
|
.first(conn)
|
||||||
|
@ -109,6 +106,23 @@ impl Instance {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn cache_local_instance_user(conn: &Connection) {
|
||||||
|
let user = Self::get_local_instance_user_uncached(conn)
|
||||||
|
.expect("Failed to get local instance user");
|
||||||
|
LOCAL_INSTANCE_USER
|
||||||
|
.set(user)
|
||||||
|
.expect("Failed to set local instance user");
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<Instance>> {
|
||||||
|
instances::table
|
||||||
|
.order(instances::public_domain.asc())
|
||||||
|
.offset(min.into())
|
||||||
|
.limit((max - min).into())
|
||||||
|
.load::<Instance>(conn)
|
||||||
|
.map_err(Error::from)
|
||||||
|
}
|
||||||
|
|
||||||
insert!(instances, NewInstance);
|
insert!(instances, NewInstance);
|
||||||
get!(instances);
|
get!(instances);
|
||||||
find_by!(instances, find_by_domain, public_domain as &str);
|
find_by!(instances, find_by_domain, public_domain as &str);
|
||||||
|
|
Loading…
Reference in a new issue