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 = [
4635 "chrono", 4635 "chrono",
4636 "clap", 4636 "clap",
4637 "dashmap", 4637 "dashmap",
4638 "futures-util",
4638 "image", 4639 "image",
4639 "matrix-sdk", 4640 "matrix-sdk",
4640 "mime_guess", 4641 "mime_guess",
Cargo.toml +1
@@ -43,6 +43,7 @@ tracing = "0.1.36"
43tracing-subscriber = { version = "0.3.15", features = ["env-filter"] } 43tracing-subscriber = { version = "0.3.15", features = ["env-filter"] }
44uuid = { version = "1.1.2", features = ["v4"] } 44uuid = { version = "1.1.2", features = ["v4"] }
45unicode-segmentation = "1.10.0" 45unicode-segmentation = "1.10.0"
46futures-util = "0.3"
46 47
47# Auto-select version from matrix-sdk 48# Auto-select version from matrix-sdk
48qrcode = { version = "*", default-features = false } 49qrcode = { version = "*", default-features = false }
README.md +22
@@ -170,6 +170,28 @@ The inactivity delay defaults to five minutes and can be changed with:
170 170
171 /set matrix-rust.look.smart_filter_delay 300000 171 /set matrix-rust.look.smart_filter_delay 300000
172 172
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
173# Storage 195# Storage
174 196
175The plugin keeps Matrix state and encryption data in 197The plugin keeps Matrix state and encryption data in
src/room/buffer.rs +82 −2
@@ -1,11 +1,16 @@
1use std::{borrow::Cow, cell::RefCell, rc::Rc}; 1use std::{borrow::Cow, cell::RefCell, rc::Rc};
2 2
3use futures_util::StreamExt;
3use matrix_sdk::{ 4use matrix_sdk::{
5 room::ParentSpace,
4 ruma::{EventId, OwnedRoomAliasId, TransactionId, UserId}, 6 ruma::{EventId, OwnedRoomAliasId, TransactionId, UserId},
5 Room, 7 Error, Room,
6}; 8};
7use tokio::runtime::Handle; 9use tokio::runtime::Handle;
8use weechat::buffer::{Buffer, BufferHandle, BufferLine, LineData}; 10use weechat::{
11 buffer::{Buffer, BufferHandle, BufferLine, LineData},
12 Prefix, Weechat,
13};
9 14
10use crate::{render::RenderedEvent, utils::ToTag}; 15use crate::{render::RenderedEvent, utils::ToTag};
11 16
@@ -193,6 +198,45 @@ impl RoomBuffer {
193 } 198 }
194 } 199 }
195 200
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
196 fn alias(&self) -> Option<OwnedRoomAliasId> { 240 fn alias(&self) -> Option<OwnedRoomAliasId> {
197 self.room.canonical_alias() 241 self.room.canonical_alias()
198 } 242 }
@@ -323,3 +367,39 @@ mod tests {
323 ); 367 );
324 } 368 }
325} 369}
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 {
348 } 348 }
349 349
350 *room.buffer.inner.borrow_mut() = Some(buffer_handle.clone()); 350 *room.buffer.inner.borrow_mut() = Some(buffer_handle.clone());
351 room.buffer.update_parent_spaces();
351 352
352 Self { inner: room } 353 Self { inner: room }
353 } 354 }
@@ -392,6 +393,7 @@ impl RoomHandle {
392 393
393 room_buffer.buffer.update_buffer_name(); 394 room_buffer.buffer.update_buffer_name();
394 room_buffer.buffer.set_topic(); 395 room_buffer.buffer.set_topic();
396 room_buffer.buffer.update_parent_spaces();
395 397
396 Ok(room_buffer) 398 Ok(room_buffer)
397 } 399 }
@@ -453,6 +455,10 @@ impl MatrixRoom {
453 self.buffer.buffer_handle() 455 self.buffer.buffer_handle()
454 } 456 }
455 457
458 pub fn update_parent_spaces(&self) {
459 self.buffer.update_parent_spaces();
460 }
461
456 pub fn accept_verification(&self) { 462 pub fn accept_verification(&self) {
457 let verification = self.verification.clone(); 463 let verification = self.verification.clone();
458 Weechat::spawn(async move { verification.accept().await }).detach(); 464 Weechat::spawn(async move { verification.accept().await }).detach();
@@ -1167,6 +1173,9 @@ impl MatrixRoom {
1167 self.buffer.set_alias(); 1173 self.buffer.set_alias();
1168 self.buffer.update_buffer_name(); 1174 self.buffer.update_buffer_name();
1169 } 1175 }
1176 AnySyncStateEvent::SpaceParent(_) => {
1177 self.buffer.update_parent_spaces()
1178 }
1170 _ => (), 1179 _ => (),
1171 } 1180 }
1172 } 1181 }
src/server.rs +9 −1
@@ -928,8 +928,16 @@ impl InnerServer {
928 room_id: &RoomId, 928 room_id: &RoomId,
929 event: AnySyncStateEvent, 929 event: AnySyncStateEvent,
930 ) { 930 ) {
931 let refresh_parent_spaces =
932 matches!(&event, AnySyncStateEvent::RoomName(_));
931 let room = self.get_or_create_room(room_id); 933 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 }
933 } 941 }
934 942
935 pub async fn receive_joined_timeline_event( 943 pub async fn receive_joined_timeline_event(