2024-06-14 22:08:44 +00:00
use std ::collections ::BTreeMap ;
2024-06-10 06:02:17 +00:00
2024-12-14 21:58:01 -05:00
use conduwuit ::{ error , implement , Result } ;
2024-06-10 06:02:17 +00:00
use ruma ::{
events ::{
room ::{
member ::{ MembershipState , RoomMemberEventContent } ,
message ::RoomMessageEventContent ,
power_levels ::RoomPowerLevelsEventContent ,
} ,
2024-08-28 07:05:13 +00:00
tag ::{ TagEvent , TagEventContent , TagInfo } ,
2024-10-04 20:25:32 +00:00
RoomAccountDataEventType ,
2024-06-10 06:02:17 +00:00
} ,
2024-08-28 07:05:13 +00:00
RoomId , UserId ,
2024-06-10 06:02:17 +00:00
} ;
2024-07-20 23:38:20 +00:00
use crate ::pdu ::PduBuilder ;
2024-06-10 06:02:17 +00:00
2024-12-14 21:58:01 -05:00
/// Invite the user to the conduwuit admin room.
2024-08-08 17:18:30 +00:00
///
2024-12-14 21:58:01 -05:00
/// This is equivalent to granting server admin privileges.
2024-08-08 17:18:30 +00:00
#[ implement(super::Service) ]
pub async fn make_user_admin ( & self , user_id : & UserId ) -> Result < ( ) > {
let Ok ( room_id ) = self . get_admin_room ( ) . await else {
return Ok ( ( ) ) ;
} ;
2024-07-20 23:38:20 +00:00
2024-08-08 17:18:30 +00:00
let state_lock = self . services . state . mutex . lock ( & room_id ) . await ;
2024-06-10 06:02:17 +00:00
2024-08-08 17:18:30 +00:00
// Use the server user to grant the new admin's power level
let server_user = & self . services . globals . server_user ;
2024-06-10 06:02:17 +00:00
2024-08-08 17:18:30 +00:00
// Invite and join the real user
self . services
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state (
user_id . to_string ( ) ,
& RoomMemberEventContent ::new ( MembershipState ::Invite ) ,
) ,
2024-08-08 17:18:30 +00:00
server_user ,
& room_id ,
& state_lock ,
)
. await ? ;
self . services
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state (
user_id . to_string ( ) ,
& RoomMemberEventContent ::new ( MembershipState ::Join ) ,
) ,
2024-08-08 17:18:30 +00:00
user_id ,
& room_id ,
& state_lock ,
)
. await ? ;
2024-06-10 06:02:17 +00:00
2024-08-08 17:18:30 +00:00
// Set power level
2024-12-15 00:05:47 -05:00
let users = BTreeMap ::from_iter ( [
( server_user . clone ( ) , 100. into ( ) ) ,
( user_id . to_owned ( ) , 100. into ( ) ) ,
] ) ;
2024-06-10 06:02:17 +00:00
2024-08-08 17:18:30 +00:00
self . services
. timeline
. build_and_append_pdu (
2024-12-15 00:05:47 -05:00
PduBuilder ::state ( String ::new ( ) , & RoomPowerLevelsEventContent {
users ,
.. Default ::default ( )
} ) ,
2024-08-08 17:18:30 +00:00
server_user ,
& room_id ,
& state_lock ,
)
. await ? ;
2024-06-10 06:02:17 +00:00
2024-08-08 17:18:30 +00:00
// Set room tag
let room_tag = & self . services . server . config . admin_room_tag ;
if ! room_tag . is_empty ( ) {
if let Err ( e ) = self . set_room_tag ( & room_id , user_id , room_tag ) . await {
error! ( ? room_id , ? user_id , ? room_tag , ? e , " Failed to set tag for admin grant " ) ;
2024-08-28 07:05:13 +00:00
}
2024-08-08 17:18:30 +00:00
}
2024-08-28 07:05:13 +00:00
2024-11-20 20:23:13 -05:00
if self . services . server . config . admin_room_notices {
let welcome_message = String ::from ( " ## Thank you for trying out conduwuit! \n \n conduwuit is technically a hard fork of Conduit, which is in Beta. The Beta status initially was inherited from Conduit, however overtime this Beta status is rapidly becoming less and less relevant as our codebase significantly diverges more and more. conduwuit is quite stable and very usable as a daily driver and for a low-medium sized homeserver. There is still a lot of more work to be done, but it is in a far better place than the project was in early 2024. \n \n Helpful links: \n > GitHub Repo: https://github.com/girlbossceo/conduwuit \n > Documentation: https://conduwuit.puppyirl.gay/ \n > Report issues: https://github.com/girlbossceo/conduwuit/issues \n \n For a list of available commands, send the following message in this room: `!admin --help` \n \n Here are some rooms you can join (by typing the command into your client) - \n \n conduwuit space: `/join #conduwuit-space:puppygock.gay` \n conduwuit main room (Ask questions and get notified on updates): `/join #conduwuit:puppygock.gay` \n conduwuit offtopic room: `/join #conduwuit-offtopic:puppygock.gay` " ) ;
// Send welcome message
self . services
. timeline
. build_and_append_pdu (
PduBuilder ::timeline ( & RoomMessageEventContent ::text_markdown ( welcome_message ) ) ,
server_user ,
& room_id ,
& state_lock ,
)
. await ? ;
}
2024-06-10 06:02:17 +00:00
2024-08-08 17:18:30 +00:00
Ok ( ( ) )
2024-06-10 06:02:17 +00:00
}
2024-08-28 07:05:13 +00:00
#[ implement(super::Service) ]
2024-08-08 17:18:30 +00:00
async fn set_room_tag ( & self , room_id : & RoomId , user_id : & UserId , tag : & str ) -> Result < ( ) > {
2024-08-28 07:05:13 +00:00
let mut event = self
. services
. account_data
2024-10-02 07:57:18 +00:00
. get_room ( room_id , user_id , RoomAccountDataEventType ::Tag )
2024-08-08 17:18:30 +00:00
. await
. unwrap_or_else ( | _ | TagEvent {
2024-12-15 00:05:47 -05:00
content : TagEventContent { tags : BTreeMap ::new ( ) } ,
2024-08-28 07:05:13 +00:00
} ) ;
event
. content
. tags
. insert ( tag . to_owned ( ) . into ( ) , TagInfo ::new ( ) ) ;
2024-08-08 17:18:30 +00:00
self . services
. account_data
. update (
Some ( room_id ) ,
user_id ,
RoomAccountDataEventType ::Tag ,
& serde_json ::to_value ( event ) ? ,
)
. await ? ;
2024-08-28 07:05:13 +00:00
Ok ( ( ) )
}