Implement User::get_local_instance_user()

This commit is contained in:
Kitaiti Makoto 2021-11-22 23:11:32 +09:00
parent 94cc260803
commit 0da9572627

View file

@ -3,11 +3,11 @@ use crate::{
medias::Media,
safe_string::SafeString,
schema::{instances, users},
users::{Role, User},
users::{NewUser, Role, User},
Connection, Error, Result,
};
use chrono::NaiveDateTime;
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
use diesel::{self, result::Error::NotFound, ExpressionMethods, QueryDsl, RunQueryDsl};
use plume_common::utils::md_to_html;
use std::sync::RwLock;
@ -45,6 +45,8 @@ lazy_static! {
static ref LOCAL_INSTANCE: RwLock<Option<Instance>> = RwLock::new(None);
}
const LOCAL_INSTANCE_USER: &str = "__instance__";
impl Instance {
pub fn set_local(self) {
LOCAL_INSTANCE.write().unwrap().replace(self);
@ -85,6 +87,28 @@ impl Instance {
.map_err(Error::from)
}
pub fn get_local_instance_user(conn: &Connection) -> Result<User> {
users::table
.filter(users::role.eq(3))
.first(conn)
.or_else(|err| match err {
NotFound => {
let instance = Instance::get_local().expect("Failed to get local instance");
let email = format!("{}@{}", LOCAL_INSTANCE_USER, &instance.public_domain);
NewUser::new_local(
conn,
LOCAL_INSTANCE_USER.into(),
instance.public_domain,
Role::Instance,
"Local instance",
email,
None,
)
}
_ => Err(Error::Db(err)),
})
}
insert!(instances, NewInstance);
get!(instances);
find_by!(instances, find_by_domain, public_domain as &str);