Extract Instance::create_local_instance_user() from get_local_instance_user_uncached()

This commit is contained in:
Kitaiti Makoto 2021-11-24 22:11:56 +09:00
parent 2f7a5cbf56
commit af5b0b961b

View File

@ -80,6 +80,20 @@ impl Instance {
.map_err(Error::from) .map_err(Error::from)
} }
pub fn create_local_instance_user(conn: &Connection) -> Result<User> {
let instance = Instance::get_local().expect("Failed to get local instance");
let email = format!("{}@{}", LOCAL_INSTANCE_USERNAME, &instance.public_domain);
NewUser::new_local(
conn,
LOCAL_INSTANCE_USERNAME.into(),
instance.public_domain,
Role::Instance,
"Local instance",
email,
None,
)
}
pub fn get_local_instance_user() -> Option<&'static User> { pub fn get_local_instance_user() -> Option<&'static User> {
LOCAL_INSTANCE_USER.get() LOCAL_INSTANCE_USER.get()
} }
@ -89,19 +103,7 @@ impl Instance {
.filter(users::role.eq(3)) .filter(users::role.eq(3))
.first(conn) .first(conn)
.or_else(|err| match err { .or_else(|err| match err {
NotFound => { NotFound => Self::create_local_instance_user(conn),
let instance = Instance::get_local().expect("Failed to get local instance");
let email = format!("{}@{}", LOCAL_INSTANCE_USERNAME, &instance.public_domain);
NewUser::new_local(
conn,
LOCAL_INSTANCE_USERNAME.into(),
instance.public_domain,
Role::Instance,
"Local instance",
email,
None,
)
}
_ => Err(Error::Db(err)), _ => Err(Error::Db(err)),
}) })
} }