diff --git a/plume-models/src/instance.rs b/plume-models/src/instance.rs index 80babed5..cabc9b9b 100644 --- a/plume-models/src/instance.rs +++ b/plume-models/src/instance.rs @@ -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> = 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 { + 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);