Commit 5ce0dc756a

5ce0dc756a30cd11fc80afd440238419bab10c50

parent: 1d0098ded6

Unsigned

Darafei Praliaskouski <me@komzpa.net> · 2026-07-09 07:28 UTC
committer: <strk@kbt.io>

feat: expose room parent spaces

Layout: unified · split

Cargo.lock +1
@@ -4635,6 +4635,7 @@ dependencies = [
46354635 "chrono",
46364636 "clap",
46374637 "dashmap",
4638 "futures-util",
46384639 "image",
46394640 "matrix-sdk",
46404641 "mime_guess",
Cargo.toml +1
@@ -43,6 +43,7 @@ tracing = "0.1.36"
4343tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
4444uuid = { version = "1.1.2", features = ["v4"] }
4545unicode-segmentation = "1.10.0"
46futures-util = "0.3"
4647
4748# Auto-select version from matrix-sdk
4849qrcode = { version = "*", default-features = false }
README.md +22
@@ -170,6 +170,28 @@ The inactivity delay defaults to five minutes and can be changed with:
170170
171171 /set matrix-rust.look.smart_filter_delay 300000
172172
173# Matrix Spaces
174
175Room buffers expose Matrix parent Spaces as WeeChat local variables. Switch to a
176room buffer that belongs to a Space and run:
177
178 /buffer listvar
179
180Look for `space`, `space_id`, `spaces`, and `space_ids`. The singular variables
181hold the first parent Space name and room id; the plural variables contain all
182known parent Spaces as comma-separated lists.
183
184The buflist plugin will not reorganize Matrix rooms under Spaces by itself.
185These variables are available to scripts and custom buflist formats. For
186example, to prefix Matrix room names with their first parent Space:
187
188 /set buflist.format.name "${if:${space}?${space}/${name}:${name}}"
189 /buflist refresh
190
191Remove that prefix again with:
192
193 /unset buflist.format.name
194
173195# Storage
174196
175197The plugin keeps Matrix state and encryption data in
src/room/buffer.rs +82 −2
@@ -1,11 +1,16 @@
11use std::{borrow::Cow, cell::RefCell, rc::Rc};
22
3use futures_util::StreamExt;
34use matrix_sdk::{
5 room::ParentSpace,
46 ruma::{EventId, OwnedRoomAliasId, TransactionId, UserId},
5 Room,
7 Error, Room,
68};
79use tokio::runtime::Handle;
8use weechat::buffer::{Buffer, BufferHandle, BufferLine, LineData};
10use weechat::{
11 buffer::{Buffer, BufferHandle, BufferLine, LineData},
12 Prefix, Weechat,
13};
914
1015use crate::{render::RenderedEvent, utils::ToTag};
1116
@@ -193,6 +198,45 @@ impl RoomBuffer {
193198 }
194199 }
195200
201 pub fn update_parent_spaces(&self) {
202 let spaces = self.runtime.block_on(parent_spaces(self.room.clone()));
203
204 if let Ok(buffer) = self.buffer_handle().upgrade() {
205 match spaces {
206 Ok(spaces) => {
207 let ids = spaces
208 .iter()
209 .map(|s| s.id.as_str())
210 .collect::<Vec<_>>()
211 .join(",");
212 let names = spaces
213 .iter()
214 .map(|s| s.name.as_str())
215 .collect::<Vec<_>>()
216 .join(",");
217
218 buffer.set_localvar("space_ids", &ids);
219 buffer.set_localvar("spaces", &names);
220
221 if let Some(space) = spaces.first() {
222 buffer.set_localvar("space_id", space.id.as_str());
223 buffer.set_localvar("space", &space.name);
224 } else {
225 buffer.set_localvar("space_id", "");
226 buffer.set_localvar("space", "");
227 }
228 }
229 Err(e) => {
230 Weechat::print(&format!(
231 "{}: Error fetching parent spaces from the store: {}",
232 Weechat::prefix(Prefix::Error),
233 e,
234 ));
235 }
236 }
237 }
238 }
239
196240 fn alias(&self) -> Option<OwnedRoomAliasId> {
197241 self.room.canonical_alias()
198242 }
@@ -323,3 +367,39 @@ mod tests {
323367 );
324368 }
325369}
370
371struct ParentSpaceInfo {
372 id: String,
373 name: String,
374}
375
376async fn parent_spaces(room: Room) -> Result<Vec<ParentSpaceInfo>, Error> {
377 let mut stream = room.parent_spaces().await?;
378 let mut spaces = Vec::new();
379
380 while let Some(parent) = stream.next().await {
381 match parent? {
382 ParentSpace::Reciprocal(room)
383 | ParentSpace::WithPowerlevel(room)
384 | ParentSpace::Illegitimate(room) => {
385 let id = room.room_id().to_string();
386 let name = room
387 .display_name()
388 .await
389 .map(|name| name.to_string())
390 .unwrap_or_else(|_| id.clone());
391
392 spaces.push(ParentSpaceInfo { id, name });
393 }
394 ParentSpace::Unverifiable(room_id) => {
395 let id = room_id.to_string();
396 spaces.push(ParentSpaceInfo {
397 id: id.clone(),
398 name: id,
399 });
400 }
401 }
402 }
403
404 Ok(spaces)
405}
src/room/mod.rs +9
@@ -348,6 +348,7 @@ impl RoomHandle {
348348 }
349349
350350 *room.buffer.inner.borrow_mut() = Some(buffer_handle.clone());
351 room.buffer.update_parent_spaces();
351352
352353 Self { inner: room }
353354 }
@@ -392,6 +393,7 @@ impl RoomHandle {
392393
393394 room_buffer.buffer.update_buffer_name();
394395 room_buffer.buffer.set_topic();
396 room_buffer.buffer.update_parent_spaces();
395397
396398 Ok(room_buffer)
397399 }
@@ -453,6 +455,10 @@ impl MatrixRoom {
453455 self.buffer.buffer_handle()
454456 }
455457
458 pub fn update_parent_spaces(&self) {
459 self.buffer.update_parent_spaces();
460 }
461
456462 pub fn accept_verification(&self) {
457463 let verification = self.verification.clone();
458464 Weechat::spawn(async move { verification.accept().await }).detach();
@@ -1167,6 +1173,9 @@ impl MatrixRoom {
11671173 self.buffer.set_alias();
11681174 self.buffer.update_buffer_name();
11691175 }
1176 AnySyncStateEvent::SpaceParent(_) => {
1177 self.buffer.update_parent_spaces()
1178 }
11701179 _ => (),
11711180 }
11721181 }
src/server.rs +9 −1
@@ -928,8 +928,16 @@ impl InnerServer {
928928 room_id: &RoomId,
929929 event: AnySyncStateEvent,
930930 ) {
931 let refresh_parent_spaces =
932 matches!(&event, AnySyncStateEvent::RoomName(_));
931933 let room = self.get_or_create_room(room_id);
932 room.handle_sync_state_event(&event, true).await
934 room.handle_sync_state_event(&event, true).await;
935
936 if refresh_parent_spaces {
937 for room in self.rooms() {
938 room.update_parent_spaces();
939 }
940 }
933941 }
934942
935943 pub async fn receive_joined_timeline_event(