From af5b0b961b6889f4ea749cdbf18f5ddc51b10cc5 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Wed, 24 Nov 2021 22:11:56 +0900 Subject: [PATCH] Extract Instance::create_local_instance_user() from get_local_instance_user_uncached() --- plume-models/src/instance.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/plume-models/src/instance.rs b/plume-models/src/instance.rs index 5f4905d7..e791cb67 100644 --- a/plume-models/src/instance.rs +++ b/plume-models/src/instance.rs @@ -80,6 +80,20 @@ impl Instance { .map_err(Error::from) } + pub fn create_local_instance_user(conn: &Connection) -> Result { + 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> { LOCAL_INSTANCE_USER.get() } @@ -89,19 +103,7 @@ impl Instance { .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_USERNAME, &instance.public_domain); - NewUser::new_local( - conn, - LOCAL_INSTANCE_USERNAME.into(), - instance.public_domain, - Role::Instance, - "Local instance", - email, - None, - ) - } + NotFound => Self::create_local_instance_user(conn), _ => Err(Error::Db(err)), }) }