Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
conduwuit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
🥺
conduwuit
Commits
02bd67dc
Commit
02bd67dc
authored
9 months ago
by
Jason Volk
Browse files
Options
Downloads
Patches
Plain Diff
admin command path simplifications; fix message to room
Signed-off-by:
Jason Volk
<
jason@zemos.net
>
parent
3813628a
No related branches found
No related tags found
1 merge request
!467
Misc fixes
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/admin/handler.rs
+8
-15
8 additions, 15 deletions
src/admin/handler.rs
src/service/admin/mod.rs
+90
-77
90 additions, 77 deletions
src/service/admin/mod.rs
src/service/rooms/timeline/mod.rs
+1
-1
1 addition, 1 deletion
src/service/rooms/timeline/mod.rs
with
99 additions
and
93 deletions
src/admin/handler.rs
+
8
−
15
View file @
02bd67dc
...
...
@@ -12,8 +12,8 @@
extern
crate
conduit_service
as
service
;
use
conduit
::
Result
;
use
service
::
admin
::
HandlerResult
;
pub
(
crate
)
use
service
::
admin
::{
AdminEvent
,
Service
};
pub
(
crate
)
use
service
::
admin
::
{
Command
,
Service
}
;
use
service
::
admin
::{
CommandResult
,
HandlerResult
};
use
self
::{
fsck
::
FsckCommand
,
tester
::
TesterCommands
};
use
crate
::{
...
...
@@ -68,25 +68,18 @@ pub(crate) enum AdminCommand {
}
#[must_use]
pub
fn
handle
(
event
:
AdminEvent
)
->
HandlerResult
{
Box
::
pin
(
handle_
event
(
event
))
}
pub
fn
handle
(
command
:
Command
)
->
HandlerResult
{
Box
::
pin
(
handle_
command
(
command
))
}
#[tracing::instrument(skip_all,
name
=
"admin"
)]
async
fn
handle_event
(
event
:
AdminEvent
)
->
Result
<
AdminEvent
>
{
Ok
(
AdminEvent
::
Reply
(
process_event
(
event
)
.await
))
}
async
fn
process_event
(
event
:
AdminEvent
)
->
Option
<
RoomMessageEventContent
>
{
let
(
mut
message_content
,
reply_id
)
=
match
event
{
AdminEvent
::
Command
(
room_message
,
reply_id
)
=>
(
Box
::
pin
(
process_admin_message
(
room_message
))
.await
,
reply_id
),
AdminEvent
::
Notice
(
content
)
=>
(
content
,
None
),
AdminEvent
::
Reply
(
_
)
=>
return
None
,
};
message_content
.relates_to
=
reply_id
.map
(|
reply_id
|
Reply
{
async
fn
handle_command
(
command
:
Command
)
->
CommandResult
{
let
mut
content
=
process_admin_message
(
command
.command
)
.await
;
content
.relates_to
=
command
.reply_id
.map
(|
event_id
|
Reply
{
in_reply_to
:
InReplyTo
{
event_id
:
reply_id
.into
()
,
event_id
,
},
});
Some
(
message_
content
)
Ok
(
Some
(
content
)
)
}
// Parse and process a message from the admin room
...
...
This diff is collapsed.
Click to expand it.
src/service/admin/mod.rs
+
90
−
77
View file @
02bd67dc
...
...
@@ -7,12 +7,13 @@
use
conduit
::{
utils
::
mutex_map
,
Error
,
Result
};
pub
use
create
::
create_admin_room
;
pub
use
grant
::
make_user_admin
;
use
loole
::{
Receiver
,
Sender
};
use
ruma
::{
events
::{
room
::
message
::{
Relation
,
RoomMessageEventContent
},
TimelineEventType
,
},
EventId
,
OwnedRoomId
,
RoomId
,
UserId
,
Owned
EventId
,
OwnedRoomId
,
RoomId
,
UserId
,
};
use
serde_json
::
value
::
to_raw_value
;
use
tokio
::{
sync
::
Mutex
,
task
::
JoinHandle
};
...
...
@@ -20,12 +21,16 @@
use
crate
::{
pdu
::
PduBuilder
,
services
,
user_is_local
,
PduEvent
};
pub
type
HandlerResult
=
Pin
<
Box
<
dyn
Future
<
Output
=
Result
<
AdminEvent
,
Error
>>
+
Send
>>
;
pub
type
Handler
=
fn
(
AdminEvent
)
->
HandlerResult
;
const
COMMAND_QUEUE_LIMIT
:
usize
=
512
;
pub
type
CommandOutput
=
Option
<
RoomMessageEventContent
>
;
pub
type
CommandResult
=
Result
<
CommandOutput
,
Error
>
;
pub
type
HandlerResult
=
Pin
<
Box
<
dyn
Future
<
Output
=
CommandResult
>
+
Send
>>
;
pub
type
Handler
=
fn
(
Command
)
->
HandlerResult
;
pub
struct
Service
{
sender
:
loole
::
Sender
<
AdminEvent
>
,
receiver
:
Mutex
<
loole
::
Receiver
<
AdminEvent
>>
,
sender
:
Sender
<
Command
>
,
receiver
:
Mutex
<
Receiver
<
Command
>>
,
handler_join
:
Mutex
<
Option
<
JoinHandle
<
()
>>>
,
pub
handle
:
Mutex
<
Option
<
Handler
>>
,
#[cfg(feature
=
"console"
)]
...
...
@@ -33,16 +38,15 @@ pub struct Service {
}
#[derive(Debug)]
pub
enum
AdminEvent
{
Command
(
String
,
Option
<
Arc
<
EventId
>>
),
Reply
(
Option
<
RoomMessageEventContent
>
),
Notice
(
RoomMessageEventContent
),
pub
struct
Command
{
pub
command
:
String
,
pub
reply_id
:
Option
<
OwnedEventId
>
,
}
impl
Service
{
#[must_use]
pub
fn
build
()
->
Arc
<
Self
>
{
let
(
sender
,
receiver
)
=
loole
::
un
bounded
();
let
(
sender
,
receiver
)
=
loole
::
bounded
(
COMMAND_QUEUE_LIMIT
);
Arc
::
new
(
Self
{
sender
,
receiver
:
Mutex
::
new
(
receiver
),
...
...
@@ -53,6 +57,18 @@ pub fn build() -> Arc<Self> {
})
}
pub
async
fn
start_handler
(
self
:
&
Arc
<
Self
>
)
{
let
self_
=
Arc
::
clone
(
self
);
let
handle
=
services
()
.server
.runtime
()
.spawn
(
async
move
{
self_
.handler
()
.await
.expect
(
"Failed to initialize admin room handler"
);
});
_
=
self
.handler_join
.lock
()
.await
.insert
(
handle
);
}
pub
fn
interrupt
(
&
self
)
{
#[cfg(feature
=
"console"
)]
self
.console
.interrupt
();
...
...
@@ -75,16 +91,39 @@ pub async fn close(&self) {
}
}
pub
async
fn
start_handler
(
self
:
&
Arc
<
Self
>
)
{
let
self_
=
Arc
::
clone
(
self
);
let
handle
=
services
()
.server
.runtime
()
.spawn
(
async
move
{
self_
.handler
()
.await
.expect
(
"Failed to initialize admin room handler"
);
});
pub
async
fn
send_text
(
&
self
,
body
:
&
str
)
{
self
.send_message
(
RoomMessageEventContent
::
text_plain
(
body
))
.await
;
}
_
=
self
.handler_join
.lock
()
.await
.insert
(
handle
);
pub
async
fn
send_message
(
&
self
,
message_content
:
RoomMessageEventContent
)
{
if
let
Ok
(
Some
(
room_id
))
=
Self
::
get_admin_room
()
{
let
user_id
=
&
services
()
.globals.server_user
;
respond_to_room
(
message_content
,
&
room_id
,
user_id
)
.await
;
}
}
pub
async
fn
command
(
&
self
,
command
:
String
,
reply_id
:
Option
<
OwnedEventId
>
)
{
self
.send
(
Command
{
command
,
reply_id
,
})
.await
;
}
pub
async
fn
command_in_place
(
&
self
,
command
:
String
,
reply_id
:
Option
<
OwnedEventId
>
,
)
->
Result
<
Option
<
RoomMessageEventContent
>>
{
self
.process_command
(
Command
{
command
,
reply_id
,
})
.await
}
async
fn
send
(
&
self
,
message
:
Command
)
{
debug_assert!
(
!
self
.sender
.is_closed
(),
"channel closed"
);
self
.sender
.send_async
(
message
)
.await
.expect
(
"message sent"
);
}
async
fn
handler
(
self
:
&
Arc
<
Self
>
)
->
Result
<
()
>
{
...
...
@@ -93,8 +132,8 @@ async fn handler(self: &Arc<Self>) -> Result<()> {
loop
{
debug_assert!
(
!
receiver
.is_closed
(),
"channel closed"
);
tokio
::
select!
{
event
=
receiver
.recv_async
()
=>
match
event
{
Ok
(
event
)
=>
self
.
receive
(
event
)
.await
,
command
=
receiver
.recv_async
()
=>
match
command
{
Ok
(
command
)
=>
self
.
handle_command
(
command
)
.await
,
Err
(
_
)
=>
return
Ok
(()),
},
sig
=
signals
.recv
()
=>
match
sig
{
...
...
@@ -105,55 +144,27 @@ async fn handler(self: &Arc<Self>) -> Result<()> {
}
}
pub
async
fn
send_text
(
&
self
,
body
:
&
str
)
{
self
.send_message
(
RoomMessageEventContent
::
text_plain
(
body
))
.await
;
}
pub
async
fn
send_message
(
&
self
,
message_content
:
RoomMessageEventContent
)
{
self
.send
(
AdminEvent
::
Notice
(
message_content
))
.await
;
}
pub
async
fn
command
(
&
self
,
command
:
String
,
event_id
:
Option
<
Arc
<
EventId
>>
)
{
self
.send
(
AdminEvent
::
Command
(
command
,
event_id
))
.await
;
}
pub
async
fn
command_in_place
(
&
self
,
command
:
String
,
event_id
:
Option
<
Arc
<
EventId
>>
,
)
->
Result
<
Option
<
RoomMessageEventContent
>>
{
match
self
.handle
(
AdminEvent
::
Command
(
command
,
event_id
))
.await
?
{
AdminEvent
::
Reply
(
content
)
=>
Ok
(
content
),
_
=>
Ok
(
None
),
async
fn
handle_signal
(
&
self
,
#[allow(unused_variables)]
sig
:
&
'static
str
)
{
#[cfg(feature
=
"console"
)]
if
sig
==
"SIGINT"
&&
services
()
.server
.running
()
{
self
.console
.start
()
.await
;
}
}
async
fn
send
(
&
self
,
message
:
AdminEvent
)
{
debug_assert!
(
!
self
.sender
.is_full
(),
"channel full"
);
debug_assert!
(
!
self
.sender
.is_closed
(),
"channel closed"
);
self
.sender
.send
(
message
)
.expect
(
"message sent"
);
}
async
fn
receive
(
&
self
,
event
:
AdminEvent
)
{
if
let
Ok
(
AdminEvent
::
Reply
(
content
))
=
self
.handle
(
event
)
.await
{
handle_response
(
content
)
.await
;
async
fn
handle_command
(
&
self
,
command
:
Command
)
{
if
let
Ok
(
Some
(
output
))
=
self
.process_command
(
command
)
.await
{
handle_response
(
output
)
.await
;
}
}
async
fn
h
and
le
(
&
self
,
event
:
AdminEvent
)
->
Result
<
AdminEvent
,
Error
>
{
async
fn
process_comm
and
(
&
self
,
command
:
Command
)
->
CommandResult
{
if
let
Some
(
handle
)
=
self
.handle
.lock
()
.await
.as_ref
()
{
handle
(
event
)
.await
handle
(
command
)
.await
}
else
{
Err
(
Error
::
Err
(
"Admin module is not loaded."
.into
()))
}
}
async
fn
handle_signal
(
&
self
,
#[allow(unused_variables)]
sig
:
&
'static
str
)
{
#[cfg(feature
=
"console"
)]
if
sig
==
"SIGINT"
&&
services
()
.server
.running
()
{
self
.console
.start
()
.await
;
}
}
/// Checks whether a given user is an admin of this server
pub
async
fn
user_is_admin
(
&
self
,
user_id
:
&
UserId
)
->
Result
<
bool
>
{
if
let
Ok
(
Some
(
admin_room
))
=
Self
::
get_admin_room
()
{
...
...
@@ -186,26 +197,28 @@ pub fn get_admin_room() -> Result<Option<OwnedRoomId>> {
}
}
async
fn
handle_response
(
content
:
Option
<
RoomMessageEventContent
>
)
{
if
let
Some
(
content
)
=
content
.as_ref
()
{
if
let
Some
(
Relation
::
Reply
{
in_reply_to
,
})
=
content
.relates_to
.as_ref
()
{
if
let
Ok
(
Some
(
pdu
))
=
services
()
.rooms.timeline
.get_pdu
(
&
in_reply_to
.event_id
)
{
let
response_sender
=
if
is_admin_room
(
&
pdu
.room_id
)
{
&
services
()
.globals.server_user
}
else
{
&
pdu
.sender
};
respond_to_room
(
content
,
&
pdu
.room_id
,
response_sender
)
.await
;
}
}
}
async
fn
handle_response
(
content
:
RoomMessageEventContent
)
{
let
Some
(
Relation
::
Reply
{
in_reply_to
,
})
=
content
.relates_to
.as_ref
()
else
{
return
;
};
let
Ok
(
Some
(
pdu
))
=
services
()
.rooms.timeline
.get_pdu
(
&
in_reply_to
.event_id
)
else
{
return
;
};
let
response_sender
=
if
is_admin_room
(
&
pdu
.room_id
)
{
&
services
()
.globals.server_user
}
else
{
&
pdu
.sender
};
respond_to_room
(
content
,
&
pdu
.room_id
,
response_sender
)
.await
;
}
async
fn
respond_to_room
(
content
:
&
RoomMessageEventContent
,
room_id
:
&
RoomId
,
user_id
:
&
UserId
)
{
async
fn
respond_to_room
(
content
:
RoomMessageEventContent
,
room_id
:
&
RoomId
,
user_id
:
&
UserId
)
{
assert!
(
services
()
.admin
...
...
@@ -218,7 +231,7 @@ async fn respond_to_room(content: &RoomMessageEventContent, room_id: &RoomId, us
let
state_lock
=
services
()
.globals.roomid_mutex_state
.lock
(
room_id
)
.await
;
let
response_pdu
=
PduBuilder
{
event_type
:
TimelineEventType
::
RoomMessage
,
content
:
to_raw_value
(
content
)
.expect
(
"event is valid, we just created it"
),
content
:
to_raw_value
(
&
content
)
.expect
(
"event is valid, we just created it"
),
unsigned
:
None
,
state_key
:
None
,
redacts
:
None
,
...
...
This diff is collapsed.
Click to expand it.
src/service/rooms/timeline/mod.rs
+
1
−
1
View file @
02bd67dc
...
...
@@ -475,7 +475,7 @@ pub async fn append_pdu(
if
admin
::
is_admin_command
(
pdu
,
&
body
)
.await
{
services
()
.admin
.command
(
body
,
Some
(
pdu
.event_id
.clone
()))
.command
(
body
,
Some
(
(
*
pdu
.event_id
)
.into
()))
.await
;
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment