From 01b2928d5581a53cc0e38895b90a3226327907fa Mon Sep 17 00:00:00 2001
From: Jason Volk <jason@zemos.net>
Date: Tue, 9 Jul 2024 06:41:29 +0000
Subject: [PATCH] add make user admin command (#136)

Signed-off-by: Jason Volk <jason@zemos.net>
---
 src/admin/user/commands.rs | 15 +++++++++++++++
 src/admin/user/mod.rs      |  8 ++++++++
 2 files changed, 23 insertions(+)

diff --git a/src/admin/user/commands.rs b/src/admin/user/commands.rs
index 218dadb9..9e4b348b 100644
--- a/src/admin/user/commands.rs
+++ b/src/admin/user/commands.rs
@@ -348,6 +348,21 @@ pub(super) async fn force_join_room(
 	)))
 }
 
+pub(super) async fn make_user_admin(_body: Vec<&str>, user_id: String) -> Result<RoomMessageEventContent> {
+	let user_id = parse_local_user_id(&user_id)?;
+	let displayname = services()
+		.users
+		.displayname(&user_id)?
+		.unwrap_or_else(|| user_id.to_string());
+
+	assert!(service::user_is_local(&user_id), "Parsed user_id must be a local user");
+	service::admin::make_user_admin(&user_id, displayname).await?;
+
+	Ok(RoomMessageEventContent::notice_markdown(format!(
+		"{user_id} has been granted admin privileges.",
+	)))
+}
+
 pub(super) async fn put_room_tag(
 	_body: Vec<&str>, user_id: String, room_id: Box<RoomId>, tag: String,
 ) -> Result<RoomMessageEventContent> {
diff --git a/src/admin/user/mod.rs b/src/admin/user/mod.rs
index e7d3d251..cdb5fa5e 100644
--- a/src/admin/user/mod.rs
+++ b/src/admin/user/mod.rs
@@ -71,6 +71,11 @@ pub(super) enum UserCommand {
 		room_id: OwnedRoomOrAliasId,
 	},
 
+	/// - Grant server-admin privileges to a user.
+	MakeUserAdmin {
+		user_id: String,
+	},
+
 	/// - Puts a room tag for the specified user and room ID.
 	///
 	/// This is primarily useful if you'd like to set your admin room
@@ -123,6 +128,9 @@ pub(super) async fn process(command: UserCommand, body: Vec<&str>) -> Result<Roo
 			user_id,
 			room_id,
 		} => force_join_room(body, user_id, room_id).await?,
+		UserCommand::MakeUserAdmin {
+			user_id,
+		} => make_user_admin(body, user_id).await?,
 		UserCommand::PutRoomTag {
 			user_id,
 			room_id,