|
1 | | -use std::{env, process::exit, sync::Mutex, time::Duration}; |
2 | | - |
| 1 | +use anyhow::Result; |
| 2 | +use clap::Parser; |
3 | 3 | use futures::StreamExt; |
4 | 4 | use futures_signals::signal_vec::SignalVecExt; |
5 | | -use matrix_sdk::{ |
6 | | - self, |
7 | | - config::SyncSettings, |
8 | | - room::Room, |
9 | | - ruma::{ |
10 | | - api::client::filter::{FilterDefinition, LazyLoadOptions}, |
11 | | - events::{AnySyncMessageLikeEvent, AnySyncTimelineEvent, SyncMessageLikeEvent}, |
12 | | - uint, |
13 | | - }, |
14 | | - Client, LoopCtrl, |
15 | | -}; |
16 | | -use tokio::sync::oneshot; |
| 5 | +use matrix_sdk::{self, config::SyncSettings, ruma::OwnedRoomId, Client}; |
17 | 6 | use url::Url; |
18 | 7 |
|
19 | | -async fn login(homeserver_url: String, username: &str, password: &str) -> Client { |
20 | | - let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); |
21 | | - let client = Client::builder() |
22 | | - .homeserver_url(homeserver_url) |
| 8 | +#[derive(Parser, Debug)] |
| 9 | +struct Cli { |
| 10 | + /// The homeserver to connect to. |
| 11 | + #[clap(value_parser)] |
| 12 | + homeserver: Url, |
| 13 | + |
| 14 | + /// The user name that should be used for the login. |
| 15 | + #[clap(value_parser)] |
| 16 | + user_name: String, |
| 17 | + |
| 18 | + /// The password that should be used for the login. |
| 19 | + #[clap(value_parser)] |
| 20 | + password: String, |
| 21 | + |
| 22 | + /// Set the proxy that should be used for the connection. |
| 23 | + #[clap(short, long)] |
| 24 | + proxy: Option<Url>, |
| 25 | + |
| 26 | + /// Enable verbose logging output. |
| 27 | + #[clap(short, long, action)] |
| 28 | + verbose: bool, |
| 29 | + |
| 30 | + /// The room id that we should listen for the, |
| 31 | + #[clap(value_parser)] |
| 32 | + room_id: OwnedRoomId, |
| 33 | +} |
| 34 | + |
| 35 | +async fn login(cli: Cli) -> Result<Client> { |
| 36 | + let builder = Client::builder() |
| 37 | + .homeserver_url(cli.homeserver) |
23 | 38 | .sled_store("./", Some("some password")) |
24 | 39 | .await |
25 | | - .unwrap() |
26 | | - .build() |
27 | | - .await |
28 | 40 | .unwrap(); |
29 | 41 |
|
| 42 | + let builder = if let Some(proxy) = cli.proxy { builder.proxy(proxy) } else { builder }; |
| 43 | + |
| 44 | + let client = builder.build().await?; |
| 45 | + |
30 | 46 | client |
31 | | - .login_username(username, password) |
| 47 | + .login_username(&cli.user_name, &cli.password) |
32 | 48 | .initial_device_display_name("rust-sdk") |
33 | 49 | .send() |
34 | | - .await |
35 | | - .unwrap(); |
36 | | - client |
37 | | -} |
38 | | - |
39 | | -fn _event_content(event: AnySyncTimelineEvent) -> Option<String> { |
40 | | - if let AnySyncTimelineEvent::MessageLike(AnySyncMessageLikeEvent::RoomMessage( |
41 | | - SyncMessageLikeEvent::Original(event), |
42 | | - )) = event |
43 | | - { |
44 | | - Some(event.content.msgtype.body().to_owned()) |
45 | | - } else { |
46 | | - None |
47 | | - } |
48 | | -} |
| 50 | + .await?; |
49 | 51 |
|
50 | | -async fn print_timeline(room: Room) { |
51 | | - let timeline = room.timeline(); |
52 | | - let mut timeline_stream = timeline.signal().to_stream(); |
53 | | - tokio::spawn(async move { |
54 | | - while let Some(_diff) = timeline_stream.next().await { |
55 | | - // Is a straight-forward CLI example of dynamic timeline items |
56 | | - // possible?? let event = event.unwrap(); |
57 | | - //if let Some(content) = |
58 | | - // event_content(event.event.deserialize().unwrap()) { |
59 | | - // println!("{content}"); |
60 | | - //} |
61 | | - } |
62 | | - }); |
63 | | - |
64 | | - loop { |
65 | | - match timeline.paginate_backwards(uint!(10)).await { |
66 | | - Ok(outcome) if !outcome.more_messages => break, |
67 | | - Ok(_) => {} |
68 | | - Err(e) => { |
69 | | - eprintln!("error paginating: {e}"); |
70 | | - } |
71 | | - } |
72 | | - } |
| 52 | + Ok(client) |
73 | 53 | } |
74 | 54 |
|
75 | 55 | #[tokio::main] |
76 | | -async fn main() -> anyhow::Result<()> { |
| 56 | +async fn main() -> Result<()> { |
77 | 57 | tracing_subscriber::fmt::init(); |
78 | 58 |
|
79 | | - let (homeserver_url, username, password, room_id) = |
80 | | - match (env::args().nth(1), env::args().nth(2), env::args().nth(3), env::args().nth(4)) { |
81 | | - (Some(a), Some(b), Some(c), Some(d)) => (a, b, c, d), |
82 | | - _ => { |
83 | | - eprintln!( |
84 | | - "Usage: {} <homeserver_url> <username> <password> <room_id>", |
85 | | - env::args().next().unwrap() |
86 | | - ); |
87 | | - exit(1) |
88 | | - } |
89 | | - }; |
90 | | - |
91 | | - let client = login(homeserver_url, &username, &password).await; |
92 | | - |
93 | | - let mut filter = FilterDefinition::default(); |
94 | | - filter.room.include_leave = true; |
95 | | - filter.room.state.lazy_load_options = |
96 | | - LazyLoadOptions::Enabled { include_redundant_members: false }; |
97 | | - |
98 | | - let sync_settings = SyncSettings::new().timeout(Duration::from_secs(30)).filter(filter.into()); |
99 | | - let (sender, receiver) = oneshot::channel::<()>(); |
100 | | - let sender = Mutex::new(Some(sender)); |
101 | | - let client_clone = client.clone(); |
102 | | - tokio::spawn(async move { |
103 | | - client_clone |
104 | | - .sync_with_callback(sync_settings, |_| async { |
105 | | - if let Some(sender) = sender.lock().unwrap().take() { |
106 | | - sender.send(()).unwrap(); |
107 | | - } |
108 | | - LoopCtrl::Continue |
109 | | - }) |
110 | | - .await |
111 | | - .unwrap(); |
112 | | - }); |
| 59 | + let cli = Cli::parse(); |
| 60 | + let room_id = cli.room_id.clone(); |
| 61 | + let client = login(cli).await?; |
| 62 | + |
| 63 | + let sync_settings = SyncSettings::default(); |
113 | 64 |
|
114 | 65 | // Wait for the first sync response |
115 | 66 | println!("Wait for the first sync"); |
116 | | - receiver.await.unwrap(); |
117 | 67 |
|
118 | | - let room = client.get_room(room_id.as_str().try_into().unwrap()).unwrap(); |
| 68 | + client.sync_once(sync_settings.clone()).await?; |
| 69 | + |
| 70 | + // Get the timeline stream and listen to it. |
| 71 | + let room = client.get_room(&room_id).unwrap(); |
| 72 | + let timeline = room.timeline(); |
| 73 | + let mut timeline_stream = timeline.signal().to_stream(); |
| 74 | + |
| 75 | + tokio::spawn(async move { |
| 76 | + while let Some(diff) = timeline_stream.next().await { |
| 77 | + println!("Received a timeline diff {diff:#?}"); |
| 78 | + } |
| 79 | + }); |
119 | 80 |
|
120 | | - print_timeline(room).await; |
| 81 | + // Sync forever |
| 82 | + client.sync(sync_settings).await?; |
121 | 83 |
|
122 | 84 | Ok(()) |
123 | 85 | } |
0 commit comments