save the admin bot localpart

This will be used later in the run.
This commit is contained in:
Charles Hall 2024-09-27 16:06:14 -07:00
parent 76ea95634e
commit 9589382cb8
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
6 changed files with 50 additions and 1 deletions

View file

@ -1071,6 +1071,13 @@ impl KeyValueDatabase {
Ok(())
})?;
// Ensure the admin bot localpart has been written once
if services().globals.saved_admin_bot_localpart()?.is_none() {
services().globals.save_admin_bot_localpart(
services().globals.admin_bot_user_id.localpart(),
)?;
}
assert_eq!(
services().globals.database_version().unwrap(),
latest_database_version,

View file

@ -385,4 +385,26 @@ lasttimelinecount_cache: {lasttimelinecount_cache}\n"
Ok(Some(server_name))
}
fn set_admin_bot_localpart(&self, localpart: &str) -> Result<()> {
self.global.insert(b"admin_bot_localpart", localpart.as_bytes())
}
fn admin_bot_localpart(&self) -> Result<Option<String>> {
let opt_bytes = self
.global
.get(b"admin_bot_localpart")
.map_err(|_| Error::bad_database("Failed to read from globals"))?;
// `admin_bot_localpart` has not been set yet
let Some(bytes) = opt_bytes else {
return Ok(None);
};
let localpart = String::from_utf8(bytes).map_err(|_| {
Error::bad_database("Invalid UTF-8 in admin_bot_localpart")
})?;
Ok(Some(localpart))
}
}

View file

@ -91,7 +91,9 @@ pub(crate) enum ServerNameChanged {
Renamed(OwnedServerName, OwnedServerName),
#[error(
"couldn't find {0} in the database, `server_name` must have changed"
"couldn't find {0} in the database, either the `server_name` changed \
or the admin bot localpart was reconfigured without running the \
server at least once with the original localpart"
)]
MissingAdminBot(OwnedUserId),

View file

@ -1283,6 +1283,8 @@ impl Service {
/// Create the admin room.
///
/// This function should only be run on a fresh database.
///
/// Users in this room are considered admins by grapevine, and the room can
/// be used to issue admin commands by talking to the server user inside
/// it.
@ -1300,6 +1302,9 @@ impl Service {
.await;
services().users.create(&services().globals.admin_bot_user_id, None)?;
services().globals.save_admin_bot_localpart(
services().globals.admin_bot_user_id.localpart(),
)?;
let room_version_id = services().globals.default_room_version();
let room_version = RoomVersion::try_from(&room_version_id)?;

View file

@ -656,6 +656,17 @@ impl Service {
pub(crate) fn saved_server_name(&self) -> Result<Option<OwnedServerName>> {
self.db.server_name()
}
pub(crate) fn save_admin_bot_localpart(
&self,
localpart: &str,
) -> Result<()> {
self.db.set_admin_bot_localpart(localpart)
}
pub(crate) fn saved_admin_bot_localpart(&self) -> Result<Option<String>> {
self.db.admin_bot_localpart()
}
}
fn reqwest_client_builder(config: &Config) -> Result<reqwest::ClientBuilder> {

View file

@ -121,4 +121,6 @@ pub(crate) trait Data: Send + Sync {
fn bump_database_version(&self, new_version: u64) -> Result<()>;
fn set_server_name(&self, server_name: &ServerName) -> Result<()>;
fn server_name(&self) -> Result<Option<OwnedServerName>>;
fn set_admin_bot_localpart(&self, localpart: &str) -> Result<()>;
fn admin_bot_localpart(&self) -> Result<Option<String>>;
}