use anyhow::Result;
use clap::Parser;
use futures_util::StreamExt;
use matrix_sdk::{config::SyncSettings, ruma::OwnedRoomId, Client};
use matrix_sdk_ui::timeline::RoomExt;
use url::Url;
#[derive(Parser, Debug)]
struct Cli {
#[clap(value_parser)]
homeserver: Url,
#[clap(value_parser)]
user_name: String,
#[clap(value_parser)]
password: String,
#[clap(short, long)]
proxy: Option<Url>,
#[clap(short, long, action)]
verbose: bool,
#[clap(value_parser)]
room_id: OwnedRoomId,
}
async fn login(cli: Cli) -> Result<Client> {
let mut builder = Client::builder().homeserver_url(cli.homeserver);
if let Some(proxy) = cli.proxy {
builder = builder.proxy(proxy);
}
let client = builder.build().await?;
client
.matrix_auth()
.login_username(&cli.user_name, &cli.password)
.initial_device_display_name("rust-sdk")
.await?;
Ok(client)
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt::init();
let cli = Cli::parse();
let room_id = cli.room_id.clone();
let client = login(cli).await?;
let sync_settings = SyncSettings::default();
println!("Wait for the first sync");
client.sync_once(sync_settings.clone()).await?;
let room = client.get_room(&room_id).unwrap();
let timeline = room.timeline().await.unwrap();
let (timeline_items, mut timeline_stream) = timeline.subscribe().await;
println!("Initial timeline items: {timeline_items:#?}");
tokio::spawn(async move {
while let Some(diff) = timeline_stream.next().await {
println!("Received a timeline diff: {diff:#?}");
}
});
client.sync(sync_settings).await?;
Ok(())
}