refactor!(matrix): move options into a table

This commit is contained in:
Ryan 2025-03-20 18:13:56 -04:00
parent 228b3e3e54
commit c42f228463
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
4 changed files with 29 additions and 32 deletions

View File

@ -2,7 +2,7 @@ Server = "localhost"
Username = "ErrorNoWatcher"
HttpAddress = "127.0.0.1:8080"
Owners = { "ErrorNoInternet" }
MatrixOwners = { "@errornointernet:envs.net" }
MatrixOptions = { Owners = { "@errornointernet:envs.net" } }
for _, module in ipairs({
"lib",

View File

@ -278,15 +278,10 @@ async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()>
#[cfg(feature = "matrix")]
fn matrix_init(client: &Client, state: State) {
let globals = state.lua.globals();
if let Ok(homeserver_url) = globals.get::<String>("MatrixHomeserverUrl")
&& let Ok(username) = globals.get::<String>("MatrixUsername")
&& let Ok(password) = globals.get::<String>("MatrixPassword")
{
if let Ok(options) = globals.get::<Table>("MatrixOptions") {
let name = client.username();
tokio::spawn(async move {
if let Err(error) =
matrix::login(homeserver_url, username, &password, state, globals, name).await
{
if let Err(error) = matrix::login(state, options, globals, name).await {
error!("failed to log into matrix account: {error:?}");
}
});

View File

@ -28,15 +28,7 @@ pub async fn on_regular_room_message(
return Ok(());
};
if ctx
.state
.lua
.globals()
.get::<Vec<String>>("MatrixOwners")
.unwrap_or_default()
.contains(&event.sender.to_string())
&& text_content.body.starts_with(&ctx.name)
{
if ctx.is_owner(&event.sender.to_string()) && text_content.body.starts_with(&ctx.name) {
let body = text_content.body[ctx.name.len()..]
.trim_start_matches(':')
.trim();
@ -101,13 +93,7 @@ pub async fn on_stripped_state_member(
) -> Result<()> {
if let Some(user_id) = client.user_id()
&& member.state_key == user_id
&& ctx
.state
.lua
.globals()
.get::<Vec<String>>("MatrixOwners")
.unwrap_or_default()
.contains(&member.sender.to_string())
&& ctx.is_owner(&member.sender.to_string())
{
debug!("joining room {}", room.room_id());
while let Err(error) = room.join().await {

View File

@ -20,6 +20,23 @@ pub struct Context {
name: String,
}
impl Context {
fn is_owner(&self, name: &String) -> bool {
self.state
.lua
.globals()
.get::<Table>("MatrixOptions")
.ok()
.and_then(|options| {
options
.get::<Vec<String>>("owners")
.ok()
.and_then(|owners| owners.contains(name).then_some(()))
})
.is_some()
}
}
#[derive(Clone, Serialize, Deserialize)]
struct Session {
#[serde(skip_serializing_if = "Option::is_none")]
@ -37,14 +54,13 @@ async fn persist_sync_token(
Ok(())
}
pub async fn login(
homeserver_url: String,
username: String,
password: &str,
state: State,
globals: Table,
name: String,
) -> Result<()> {
pub async fn login(state: State, options: Table, globals: Table, name: String) -> Result<()> {
let (homeserver_url, username, password) = (
options.get::<String>("homeserver_url")?,
options.get::<String>("username")?,
&options.get::<String>("password")?,
);
let root_dir = dirs::data_dir()
.context("no data directory")?
.join("errornowatcher")