matrix: break circular dependency between bot and spaces #122

Merged
JakeHillion merged 1 commits from jj/wqqtutxzvypy into main 2026-04-26 23:15:49 +01:00
4 changed files with 46 additions and 39 deletions
Showing only changes of commit 64dc06408d - Show all commits

No files matched your search

+3 -33
View File
@@ -14,7 +14,6 @@ use futures_util::StreamExt;
use matrix_sdk::Client; use matrix_sdk::Client;
use matrix_sdk::Room; use matrix_sdk::Room;
use matrix_sdk::RoomMemberships; use matrix_sdk::RoomMemberships;
use matrix_sdk::authentication::matrix::MatrixSession;
use matrix_sdk::config::SyncSettings; use matrix_sdk::config::SyncSettings;
use matrix_sdk::room::edit::EditedContent; use matrix_sdk::room::edit::EditedContent;
use matrix_sdk::ruma::OwnedEventId; use matrix_sdk::ruma::OwnedEventId;
@@ -33,8 +32,6 @@ use mime::Mime;
use pulldown_cmark::Options; use pulldown_cmark::Options;
use pulldown_cmark::Parser; use pulldown_cmark::Parser;
use pulldown_cmark::html; use pulldown_cmark::html;
use serde::Deserialize;
use serde::Serialize;
use sha2::Digest; use sha2::Digest;
use sha2::Sha256; use sha2::Sha256;
use tokio::fs; use tokio::fs;
@@ -52,6 +49,8 @@ use crate::config::OpenCodeConfig;
use crate::git::auto_closer; use crate::git::auto_closer;
use crate::git::hooks::HookNotifier; use crate::git::hooks::HookNotifier;
use crate::git::repos::Repo; use crate::git::repos::Repo;
use crate::matrix::StoredSession;
use crate::matrix::persist_sync_token;
use crate::matrix::verification; use crate::matrix::verification;
use crate::naming::NamingClient; use crate::naming::NamingClient;
use crate::opencode::GitEnv; use crate::opencode::GitEnv;
@@ -59,6 +58,7 @@ use crate::opencode::OpenCodeClient;
use crate::opencode::OpenCodeEvent; use crate::opencode::OpenCodeEvent;
use crate::opencode::SseSubscriber; use crate::opencode::SseSubscriber;
use crate::types::PersistedFeature; use crate::types::PersistedFeature;
use crate::types::RepoContext;
use crate::types::save_features; use crate::types::save_features;
/// Configuration for a repository to be cloned/fetched. /// Configuration for a repository to be cloned/fetched.
@@ -70,11 +70,6 @@ struct RepoConfig {
feature_prefix: Option<String>, feature_prefix: Option<String>,
} }
pub struct RepoContext {
pub repo_name: String,
pub child_space_id: OwnedRoomId,
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct ToolUseContext { pub struct ToolUseContext {
tool_name: String, tool_name: String,
@@ -282,13 +277,6 @@ fn markdown_to_html(markdown: &str) -> String {
html_output html_output
} }
#[derive(Serialize, Deserialize)]
struct StoredSession {
session: MatrixSession,
#[serde(skip_serializing_if = "Option::is_none")]
sync_token: Option<String>,
}
pub async fn run(config: Config) -> Result<()> { pub async fn run(config: Config) -> Result<()> {
let matrix_path = config.store_path.join("matrix"); let matrix_path = config.store_path.join("matrix");
fs::create_dir_all(&matrix_path) fs::create_dir_all(&matrix_path)
@@ -741,24 +729,6 @@ async fn sync(
Ok(()) Ok(())
} }
pub async fn persist_sync_token(
session_file: &std::path::Path,
client: &Client,
sync_token: &str,
) -> Result<()> {
let session = client
.matrix_auth()
.session()
.expect("Client should have a session");
let stored = StoredSession {
session,
sync_token: Some(sync_token.to_owned()),
};
let data = serde_json::to_string(&stored)?;
fs::write(session_file, data).await?;
Ok(())
}
async fn leave_stale_dms(client: &Client, trusted_users: &[OwnedUserId]) { async fn leave_stale_dms(client: &Client, trusted_users: &[OwnedUserId]) {
// Collect the canonical DM room ID for each trusted user. // Collect the canonical DM room ID for each trusted user.
let canonical_ids: HashSet<OwnedRoomId> = trusted_users let canonical_ids: HashSet<OwnedRoomId> = trusted_users
+32
View File
@@ -1,2 +1,34 @@
use anyhow::Result;
use matrix_sdk::Client;
use matrix_sdk::authentication::matrix::MatrixSession;
use serde::Deserialize;
use serde::Serialize;
use tokio::fs;
pub mod spaces; pub mod spaces;
pub mod verification; pub mod verification;
#[derive(Serialize, Deserialize)]
pub struct StoredSession {
pub session: MatrixSession,
#[serde(skip_serializing_if = "Option::is_none")]
pub sync_token: Option<String>,
}
pub async fn persist_sync_token(
session_file: &std::path::Path,
client: &Client,
sync_token: &str,
) -> Result<()> {
let session = client
.matrix_auth()
.session()
.expect("Client should have a session");
let stored = StoredSession {
session,
sync_token: Some(sync_token.to_owned()),
};
let data = serde_json::to_string(&stored)?;
fs::write(session_file, data).await?;
Ok(())
}
+6 -6
View File
@@ -21,10 +21,10 @@ use matrix_sdk::ruma::serde::Raw;
use tracing::info; use tracing::info;
use tracing::warn; use tracing::warn;
use crate::bot;
use crate::bot::RepoContext;
use crate::config::Config; use crate::config::Config;
use crate::forges::Forge; use crate::forges::Forge;
use crate::matrix::persist_sync_token;
use crate::types::RepoContext;
pub async fn setup( pub async fn setup(
client: &Client, client: &Client,
@@ -92,7 +92,7 @@ async fn join_root_space(client: &Client, config: &Config, session_file: &Path)
info!("Waiting for invite to root space..."); info!("Waiting for invite to root space...");
loop { loop {
let response = client.sync_once(SyncSettings::default()).await?; let response = client.sync_once(SyncSettings::default()).await?;
bot::persist_sync_token(session_file, client, &response.next_batch).await?; persist_sync_token(session_file, client, &response.next_batch).await?;
if let Some(room) = client.get_room(&config.root_space) if let Some(room) = client.get_room(&config.root_space)
&& room.state() == RoomState::Joined && room.state() == RoomState::Joined
@@ -142,7 +142,7 @@ async fn ensure_admin(client: &Client, config: &Config, session_file: &Path) ->
loop { loop {
let response = client.sync_once(SyncSettings::default()).await?; let response = client.sync_once(SyncSettings::default()).await?;
bot::persist_sync_token(session_file, client, &response.next_batch).await?; persist_sync_token(session_file, client, &response.next_batch).await?;
let power_level = room.get_user_power_level(user_id).await?; let power_level = room.get_user_power_level(user_id).await?;
if power_level >= int!(100) { if power_level >= int!(100) {
@@ -266,7 +266,7 @@ async fn reconcile_child_spaces(
// Sync to pick up the new room state. // Sync to pick up the new room state.
let response = client.sync_once(SyncSettings::default()).await?; let response = client.sync_once(SyncSettings::default()).await?;
bot::persist_sync_token(session_file, client, &response.next_batch).await?; persist_sync_token(session_file, client, &response.next_batch).await?;
existing_children.insert(repo_name.clone(), child_room_id); existing_children.insert(repo_name.clone(), child_room_id);
} }
@@ -470,7 +470,7 @@ async fn reconcile_management_rooms(
// Sync to pick up the new room state. // Sync to pick up the new room state.
let response = client.sync_once(SyncSettings::default()).await?; let response = client.sync_once(SyncSettings::default()).await?;
bot::persist_sync_token(session_file, client, &response.next_batch).await?; persist_sync_token(session_file, client, &response.next_batch).await?;
id id
} }
+5
View File
@@ -9,6 +9,11 @@ use serde::Serialize;
use tokio::fs; use tokio::fs;
use tracing::info; use tracing::info;
pub struct RepoContext {
pub repo_name: String,
pub child_space_id: OwnedRoomId,
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct PersistedFeature { pub struct PersistedFeature {
pub room_id: OwnedRoomId, pub room_id: OwnedRoomId,