Skip to content

Commit 972b1e8

Browse files
committed
Fix bug in --room-get-state
2 parents de66165 + 14aaecc commit 972b1e8

File tree

5 files changed

+149
-55
lines changed

5 files changed

+149
-55
lines changed

Cargo.lock

Lines changed: 83 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "matrix-commander"
5-
version = "0.10.1"
5+
version = "0.10.2"
66
edition = "2021"
77
description = "simple but convenient CLI-based Matrix client app for sending and receiving"
88
documentation = "https://docs.rs/matrix-commander"
@@ -25,7 +25,7 @@ clap = { version = "4.5", features = [
2525
"unicode",
2626
] }
2727
colored = "3.0"
28-
directories = "5.0"
28+
directories = "6.0"
2929
futures-util = "0.3"
3030
json = "0.12"
3131
# matrix-sdk = { git = "https://github.com/matrix-org/matrix-rust-sdk , features = ["markdown", "anyhow"] }
@@ -49,7 +49,7 @@ rpassword = "7.3"
4949
serde_json = "1.0"
5050
serde = { version = "1.0", features = ["derive"] }
5151
thiserror = "2.0"
52-
tokio = { version = "1.41", default-features = false, features = [
52+
tokio = { version = "1.43", default-features = false, features = [
5353
"rt-multi-thread",
5454
"macros",
5555
] }

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.10.1
1+
0.10.2

src/listen.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -382,31 +382,34 @@ async fn handle_syncroommessageevent(
382382
debug!("Skipping message from itself because --listen-self is not set.");
383383
return;
384384
}
385-
if !context.output.is_text() {
386-
// Serialize it to a JSON string.
387-
let j = match serde_json::to_string("") {
388-
Ok(jsonstr) => {
389-
// this event does not contain the room_id, other events do.
390-
// People are missing the room_id in output.
391-
// Nasty hack: inserting the room_id into the JSON string.
392-
let mut s = jsonstr;
393-
s.insert_str(s.len() - 1, ",\"event_id\":\"\"");
394-
s.insert_str(s.len() - 2, ev.event_id().as_str());
395-
s.insert_str(s.len() - 1, ",\"sender\":\"\"");
396-
s.insert_str(s.len() - 2, ev.sender().as_str());
397-
s.insert_str(s.len() - 1, ",\"origin_server_ts\":\"\"");
398-
s.insert_str(s.len() - 2, &ev.origin_server_ts().0.to_string());
399-
s.insert_str(s.len() - 1, ",\"room_id\":\"\"");
400-
s.insert_str(s.len() - 2, room.room_id().as_str());
401-
s
402-
}
403-
Err(e) => e.to_string(),
404-
};
405-
println!("{}", j);
406-
return;
407-
}
408385
match ev {
409386
SyncMessageLikeEvent::Original(orginialmessagelikeevent) => {
387+
if !context.output.is_text() {
388+
// Serialize it to a JSON string.
389+
let j = match serde_json::to_string(&orginialmessagelikeevent.content) {
390+
Ok(jsonstr) => {
391+
// this event does not contain the room_id, other events do.
392+
// People are missing the room_id in output.
393+
// Nasty hack: inserting the room_id into the JSON string.
394+
let mut s = jsonstr;
395+
s.insert_str(s.len() - 1, ",\"event_id\":\"\"");
396+
s.insert_str(s.len() - 2, orginialmessagelikeevent.event_id.as_str());
397+
s.insert_str(s.len() - 1, ",\"sender\":\"\"");
398+
s.insert_str(s.len() - 2, orginialmessagelikeevent.sender.as_str());
399+
s.insert_str(s.len() - 1, ",\"origin_server_ts\":\"\"");
400+
s.insert_str(
401+
s.len() - 2,
402+
&orginialmessagelikeevent.origin_server_ts.0.to_string(),
403+
);
404+
s.insert_str(s.len() - 1, ",\"room_id\":\"\"");
405+
s.insert_str(s.len() - 2, room.room_id().as_str());
406+
s
407+
}
408+
Err(e) => e.to_string(),
409+
};
410+
println!("{}", j);
411+
return;
412+
}
410413
handle_originalsyncmessagelikeevent(
411414
&orginialmessagelikeevent,
412415
&RoomId::parse(room.room_id()).unwrap(),

src/mclient.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ pub(crate) async fn room_create(
12671267
}
12681268

12691269
request.name = Some(names2[i].clone());
1270-
request.room_alias_name = Some(aliases2[i].clone());
1270+
request.room_alias_name = Some(aliases2[i].clone()).filter(|s| !s.is_empty());
12711271
request.topic = Some(topics2[i].clone());
12721272
request.is_direct = is_dm;
12731273
let usr: OwnedUserId;
@@ -1997,18 +1997,37 @@ async fn print_room_state(room_id: &OwnedRoomId, room: &Room, output: Output) ->
19971997
}
19981998
// Output::JsonSpec => (), // These events should be spec compliant
19991999
_ => {
2000-
println!(
2001-
"{{\"room_id\": {:?}, \
2002-
\"RoomMemberEventContent\": [ {{ {} }} ], \
2003-
\"RoomPowerLevelsEventContent\": [ {{ {} }} ], \
2004-
\"RoomNameEventContent\": [ {{ {} }} ], \
2005-
\"RoomTopicEventContent\": [ {{ {} }} ] }}",
2006-
room_id,
2007-
serde_json::to_string(&member_evs).unwrap_or_else(|_| r#""""#.to_string()),
2008-
serde_json::to_string(&power_level_evs).unwrap_or_else(|_| r#""""#.to_string()),
2009-
serde_json::to_string(&name_evs).unwrap_or_else(|_| r#""""#.to_string()),
2010-
serde_json::to_string(&topic_evs).unwrap_or_else(|_| r#""""#.to_string()),
2011-
);
2000+
// println!(
2001+
// "{{\"room_id\": {:?}, \
2002+
// \"RoomMemberEventContent\": [ {{ \"{}\" }} ], \
2003+
// \"RoomPowerLevelsEventContent\": [ {{ \"{}\" }} ], \
2004+
// \"RoomNameEventContent\": [ {{ \"{}\" }} ], \
2005+
// \"RoomTopicEventContent\": [ {{ \"{}\" }} ] }}",
2006+
// room_id,
2007+
// serde_json::to_string(&member_evs).unwrap_or_else(|_| r#""""#.to_string()),
2008+
// serde_json::to_string(&power_level_evs).unwrap_or_else(|_| r#""""#.to_string()),
2009+
// serde_json::to_string(&name_evs).unwrap_or_else(|_| r#""""#.to_string()),
2010+
// serde_json::to_string(&topic_evs).unwrap_or_else(|_| r#""""#.to_string()),
2011+
// );
2012+
use matrix_sdk::deserialized_responses::RawSyncOrStrippedState;
2013+
#[derive(serde::Serialize)]
2014+
struct MyState<'a> {
2015+
room_id: &'a str,
2016+
room_member_event_content: Vec<RawSyncOrStrippedState<RoomMemberEventContent>>,
2017+
room_power_levels_event_content:
2018+
Vec<RawSyncOrStrippedState<RoomPowerLevelsEventContent>>,
2019+
room_name_event_content: Vec<RawSyncOrStrippedState<RoomNameEventContent>>,
2020+
room_topic_event_content: Vec<RawSyncOrStrippedState<RoomTopicEventContent>>,
2021+
}
2022+
let mystate = MyState {
2023+
room_id: room_id.as_str(),
2024+
room_member_event_content: member_evs,
2025+
room_power_levels_event_content: power_level_evs,
2026+
room_name_event_content: name_evs,
2027+
room_topic_event_content: topic_evs,
2028+
};
2029+
let jsonstr = serde_json::to_string(&mystate).unwrap();
2030+
println!("{}", jsonstr);
20122031
}
20132032
}
20142033
Ok(())
@@ -2091,8 +2110,7 @@ fn print_room_members(room_id: &OwnedRoomId, members: &[RoomMember], output: Out
20912110
}
20922111
Output::JsonSpec => (),
20932112
_ => {
2094-
//zzz
2095-
#[derive(serde::Serialize)]
2113+
#[derive(serde::Serialize, serde::Deserialize)]
20962114
struct MyMember<'a> {
20972115
user_id: &'a str,
20982116
display_name: &'a str,
@@ -2101,7 +2119,7 @@ fn print_room_members(room_id: &OwnedRoomId, members: &[RoomMember], output: Out
21012119
power_level: i64,
21022120
membership: &'a str,
21032121
}
2104-
#[derive(serde::Serialize)]
2122+
#[derive(serde::Serialize, serde::Deserialize)]
21052123
struct MyRoom<'a> {
21062124
room_id: &'a str,
21072125
members: Vec<MyMember<'a>>,
@@ -2122,8 +2140,8 @@ fn print_room_members(room_id: &OwnedRoomId, members: &[RoomMember], output: Out
21222140
room_id: room_id.as_str(),
21232141
members: mymembers,
21242142
};
2125-
let json = serde_json::to_string(&myroom).unwrap();
2126-
println!("{}", json);
2143+
let jsonstr = serde_json::to_string(&myroom).unwrap();
2144+
println!("{}", jsonstr);
21272145
}
21282146
}
21292147
}

0 commit comments

Comments
 (0)