2024-06-14 22:08:44 +00:00
use std ::collections ::BTreeMap ;
2024-06-10 06:02:17 +00:00
2025-04-08 03:17:23 +00:00
use futures ::FutureExt ;
2024-06-10 06:02:17 +00:00
use ruma ::{
2025-02-23 01:17:45 -05:00
RoomId , RoomVersionId ,
2024-10-04 20:25:32 +00:00
events ::room ::{
canonical_alias ::RoomCanonicalAliasEventContent ,
create ::RoomCreateEventContent ,
guest_access ::{ GuestAccess , RoomGuestAccessEventContent } ,
history_visibility ::{ HistoryVisibility , RoomHistoryVisibilityEventContent } ,
join_rules ::{ JoinRule , RoomJoinRulesEventContent } ,
member ::{ MembershipState , RoomMemberEventContent } ,
name ::RoomNameEventContent ,
power_levels ::RoomPowerLevelsEventContent ,
preview_url ::RoomPreviewUrlsEventContent ,
topic ::RoomTopicEventContent ,
2024-06-10 06:02:17 +00:00
} ,
} ;
2025-04-22 01:41:02 +00:00
use tuwunel_core ::{ Result , pdu ::PduBuilder } ;
2024-06-10 06:02:17 +00:00
2024-07-20 23:38:20 +00:00
use crate ::Services ;
2024-06-10 06:02:17 +00:00
/// Create the admin room.
///
2025-03-02 23:16:30 -05:00
/// Users in this room are considered admins by conduwuit, and the room can be
2024-06-10 06:02:17 +00:00
/// used to issue admin commands by talking to the server user inside it.
2025-03-02 23:16:30 -05:00
pub async fn create_admin_room ( services : & Services ) -> Result {
2024-07-20 23:38:20 +00:00
let room_id = RoomId ::new ( services . globals . server_name ( ) ) ;
2025-03-02 23:16:30 -05:00
let room_version = & services . config . default_room_version ;
2024-06-10 06:02:17 +00:00
2024-09-21 16:28:46 -04:00
let _short_id = services
. rooms
. short
. get_or_create_shortroomid ( & room_id )
. await ;
2024-06-10 06:02:17 +00:00
2024-07-20 23:38:20 +00:00
let state_lock = services . rooms . state . mutex . lock ( & room_id ) . await ;
2024-06-10 06:02:17 +00:00
// Create a user for the server
2025-03-02 23:16:30 -05:00
let server_user = services . globals . server_user . as_ref ( ) ;
2025-04-19 18:48:14 +02:00
services . users . create ( server_user , None , None ) . await ? ;
2024-06-10 06:02:17 +00:00
2024-10-04 20:25:32 +00:00
let create_content = {
2024-07-12 01:08:53 +00:00
use RoomVersionId ::* ;
match room_version {
2024-12-15 00:05:47 -05:00
| V1 | V2 | V3 | V4 | V5 | V6 | V7 | V8 | V9 | V10 = >
2025-03-02 23:16:30 -05:00
RoomCreateEventContent ::new_v1 ( server_user . into ( ) ) ,
2024-12-15 00:05:47 -05:00
| _ = > RoomCreateEventContent ::new_v11 ( ) ,
2024-07-12 01:08:53 +00:00
}
2024-06-10 06:02:17 +00:00
} ;
// 1. The room create event
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state ( String ::new ( ) , & RoomCreateEventContent {
federate : true ,
predecessor : None ,
room_version : room_version . clone ( ) ,
.. create_content
} ) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
2024-12-14 21:58:01 -05:00
// 2. Make server user/bot join
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state (
2025-03-02 23:16:30 -05:00
String ::from ( server_user ) ,
2024-12-15 00:05:47 -05:00
& RoomMemberEventContent ::new ( MembershipState ::Join ) ,
) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
// 3. Power levels
2025-03-02 23:16:30 -05:00
let users = BTreeMap ::from_iter ( [ ( server_user . into ( ) , 69420. into ( ) ) ] ) ;
2024-06-10 06:02:17 +00:00
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state ( String ::new ( ) , & RoomPowerLevelsEventContent {
users ,
.. Default ::default ( )
} ) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
// 4.1 Join Rules
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-10-04 20:25:32 +00:00
PduBuilder ::state ( String ::new ( ) , & RoomJoinRulesEventContent ::new ( JoinRule ::Invite ) ) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
// 4.2 History Visibility
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-10-04 20:25:32 +00:00
PduBuilder ::state (
String ::new ( ) ,
& RoomHistoryVisibilityEventContent ::new ( HistoryVisibility ::Shared ) ,
) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
// 4.3 Guest Access
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state (
String ::new ( ) ,
& RoomGuestAccessEventContent ::new ( GuestAccess ::Forbidden ) ,
) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
// 5. Events implied by name and topic
2025-03-02 23:16:30 -05:00
let room_name = format! ( " {} Admin Room " , services . config . server_name ) ;
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-10-04 20:25:32 +00:00
PduBuilder ::state ( String ::new ( ) , & RoomNameEventContent ::new ( room_name ) ) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state ( String ::new ( ) , & RoomTopicEventContent {
2025-03-02 23:16:30 -05:00
topic : format ! ( " Manage {} | Run commands prefixed with `!admin` | Run `!admin -h` for help | Documentation: https://conduwuit.puppyirl.gay/ " , services . config . server_name ) ,
2024-12-15 00:05:47 -05:00
} ) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
// 6. Room alias
2024-07-20 23:38:20 +00:00
let alias = & services . globals . admin_alias ;
2024-06-10 06:02:17 +00:00
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state ( String ::new ( ) , & RoomCanonicalAliasEventContent {
alias : Some ( alias . clone ( ) ) ,
alt_aliases : Vec ::new ( ) ,
} ) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. alias
. set_alias ( alias , & room_id , server_user ) ? ;
2025-03-02 23:16:30 -05:00
// 7. (ad-hoc) Disable room URL previews for everyone by default
2024-07-20 23:38:20 +00:00
services
2024-06-10 06:02:17 +00:00
. rooms
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state ( String ::new ( ) , & RoomPreviewUrlsEventContent { disabled : true } ) ,
2024-06-10 06:02:17 +00:00
server_user ,
& room_id ,
& state_lock ,
)
2025-04-08 03:17:23 +00:00
. boxed ( )
2024-06-10 06:02:17 +00:00
. await ? ;
Ok ( ( ) )
}