1use anyhow::Result;
2use clap::Parser;
3use futures_util::StreamExt;
4use matrix_sdk::{config::SyncSettings, ruma::OwnedRoomId, Client};
5use matrix_sdk_ui::timeline::RoomExt;
6use url::Url;
78#[derive(Parser, Debug)]
9struct Cli {
10/// The homeserver to connect to.
11#[clap(value_parser)]
12homeserver: Url,
1314/// The user name that should be used for the login.
15#[clap(value_parser)]
16user_name: String,
1718/// The password that should be used for the login.
19#[clap(value_parser)]
20password: String,
2122/// Set the proxy that should be used for the connection.
23#[clap(short, long)]
24proxy: Option<Url>,
2526/// Enable verbose logging output.
27#[clap(short, long, action)]
28verbose: bool,
2930/// The room id that we should listen for the,
31#[clap(value_parser)]
32room_id: OwnedRoomId,
33}
3435async fn login(cli: Cli) -> Result<Client> {
36// Note that when encryption is enabled, you should use a persistent store to be
37 // able to restore the session with a working encryption setup.
38 // See the `persist_session` example.
39let mut builder = Client::builder().homeserver_url(cli.homeserver);
4041if let Some(proxy) = cli.proxy {
42 builder = builder.proxy(proxy);
43 }
4445let client = builder.build().await?;
4647 client
48 .matrix_auth()
49 .login_username(&cli.user_name, &cli.password)
50 .initial_device_display_name("rust-sdk")
51 .await?;
5253Ok(client)
54}
5556#[tokio::main]
57async fn main() -> Result<()> {
58 tracing_subscriber::fmt::init();
5960let cli = Cli::parse();
61let room_id = cli.room_id.clone();
62let client = login(cli).await?;
6364let sync_settings = SyncSettings::default();
6566// Wait for the first sync response
67println!("Wait for the first sync");
6869 client.sync_once(sync_settings.clone()).await?;
7071// Get the timeline stream and listen to it.
72let room = client.get_room(&room_id).unwrap();
73let timeline = room.timeline().await.unwrap();
74let (timeline_items, mut timeline_stream) = timeline.subscribe().await;
7576println!("Initial timeline items: {timeline_items:#?}");
77 tokio::spawn(async move {
78while let Some(diffs) = timeline_stream.next().await {
79println!("Received timeline diffs: {diffs:#?}");
80 }
81 });
8283// Sync forever
84client.sync(sync_settings).await?;
8586Ok(())
87}