2024-06-14 22:08:44 +00:00
use std ::collections ::BTreeMap ;
2024-06-10 06:02:17 +00:00
2024-08-28 07:05:13 +00:00
use conduit ::{ 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 } ,
RoomAccountDataEventType , TimelineEventType ,
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
} ;
use serde_json ::value ::to_raw_value ;
2024-07-20 23:38:20 +00:00
use crate ::pdu ::PduBuilder ;
2024-06-10 06:02:17 +00:00
2024-07-20 23:38:20 +00:00
impl super ::Service {
/// Invite the user to the conduit admin room.
///
/// In conduit, this is equivalent to granting admin privileges.
2024-09-09 21:10:56 -04:00
pub async fn make_user_admin ( & self , user_id : & UserId ) -> Result < ( ) > {
2024-07-20 23:38:20 +00:00
let Some ( room_id ) = self . get_admin_room ( ) ? else {
return Ok ( ( ) ) ;
} ;
2024-07-18 06:37:47 +00:00
let state_lock = self . services . state . mutex . lock ( & room_id ) . await ;
2024-06-10 06:02:17 +00:00
// Use the server user to grant the new admin's power level
2024-07-18 06:37:47 +00:00
let server_user = & self . services . globals . server_user ;
2024-06-10 06:02:17 +00:00
// Invite and join the real user
2024-07-18 06:37:47 +00:00
self . services
. timeline
2024-06-10 06:02:17 +00:00
. build_and_append_pdu (
PduBuilder {
event_type : TimelineEventType ::RoomMember ,
content : to_raw_value ( & RoomMemberEventContent {
membership : MembershipState ::Invite ,
displayname : None ,
avatar_url : None ,
is_direct : None ,
third_party_invite : None ,
blurhash : None ,
reason : None ,
join_authorized_via_users_server : None ,
} )
. expect ( " event is valid, we just created it " ) ,
unsigned : None ,
state_key : Some ( user_id . to_string ( ) ) ,
redacts : None ,
2024-08-14 20:01:34 -04:00
timestamp : None ,
2024-06-10 06:02:17 +00:00
} ,
server_user ,
& room_id ,
& state_lock ,
)
. await ? ;
2024-07-18 06:37:47 +00:00
self . services
. timeline
2024-06-10 06:02:17 +00:00
. build_and_append_pdu (
PduBuilder {
event_type : TimelineEventType ::RoomMember ,
content : to_raw_value ( & RoomMemberEventContent {
membership : MembershipState ::Join ,
2024-09-09 21:10:56 -04:00
displayname : None ,
2024-06-10 06:02:17 +00:00
avatar_url : None ,
is_direct : None ,
third_party_invite : None ,
blurhash : None ,
reason : None ,
join_authorized_via_users_server : None ,
} )
. expect ( " event is valid, we just created it " ) ,
unsigned : None ,
state_key : Some ( user_id . to_string ( ) ) ,
redacts : None ,
2024-08-14 20:01:34 -04:00
timestamp : None ,
2024-06-10 06:02:17 +00:00
} ,
user_id ,
& room_id ,
& state_lock ,
)
. await ? ;
// Set power level
2024-09-09 21:41:57 -04: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-07-18 06:37:47 +00:00
self . services
. timeline
2024-06-10 06:02:17 +00:00
. build_and_append_pdu (
PduBuilder {
event_type : TimelineEventType ::RoomPowerLevels ,
content : to_raw_value ( & RoomPowerLevelsEventContent {
users ,
.. Default ::default ( )
} )
. expect ( " event is valid, we just created it " ) ,
unsigned : None ,
state_key : Some ( String ::new ( ) ) ,
redacts : None ,
2024-08-14 20:01:34 -04:00
timestamp : None ,
2024-06-10 06:02:17 +00:00
} ,
server_user ,
& room_id ,
& state_lock ,
)
. await ? ;
2024-08-28 07:05:13 +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 ) {
error! ( ? room_id , ? user_id , ? room_tag , ? e , " Failed to set tag for admin grant " ) ;
}
}
2024-06-10 06:02:17 +00:00
// Send welcome message
2024-07-18 06:37:47 +00:00
self . services . timeline . build_and_append_pdu (
2024-06-10 06:02:17 +00:00
PduBuilder {
event_type : TimelineEventType ::RoomMessage ,
2024-09-01 12:11:56 -04:00
content : to_raw_value ( & RoomMessageEventContent ::text_markdown (
String ::from ( " ## Thank you for trying out conduwuit! \n \n conduwuit is a fork of upstream Conduit which is in Beta. This means you can join and participate in most Matrix rooms, but not all features are supported and you might run into bugs from time to time. \n \n Helpful links: \n > Git and Documentation: https://github.com/girlbossceo/conduwuit \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): \n \n conduwuit room (Ask questions and get notified on updates): \n `/join #conduwuit:puppygock.gay` " ) ,
2024-06-10 06:02:17 +00:00
) )
. expect ( " event is valid, we just created it " ) ,
unsigned : None ,
state_key : None ,
redacts : None ,
2024-08-14 20:01:34 -04:00
timestamp : None ,
2024-06-10 06:02:17 +00:00
} ,
server_user ,
& room_id ,
& state_lock ,
) . await ? ;
2024-07-20 23:38:20 +00:00
Ok ( ( ) )
}
2024-06-10 06:02:17 +00:00
}
2024-08-28 07:05:13 +00:00
#[ implement(super::Service) ]
fn set_room_tag ( & self , room_id : & RoomId , user_id : & UserId , tag : & str ) -> Result < ( ) > {
let mut event = self
. services
. account_data
. get ( Some ( room_id ) , user_id , RoomAccountDataEventType ::Tag ) ?
. map ( | event | serde_json ::from_str ( event . get ( ) ) )
. and_then ( Result ::ok )
. unwrap_or_else ( | | TagEvent {
content : TagEventContent {
tags : BTreeMap ::new ( ) ,
} ,
} ) ;
event
. content
. tags
. insert ( tag . to_owned ( ) . into ( ) , TagInfo ::new ( ) ) ;
self . services . account_data . update (
Some ( room_id ) ,
user_id ,
RoomAccountDataEventType ::Tag ,
& serde_json ::to_value ( event ) ? ,
) ? ;
Ok ( ( ) )
}