clone client

master
Devin R 2020-04-14 14:49:29 -04:00
parent 6f2b5194d1
commit 87c9dbdad7
1 changed files with 24 additions and 56 deletions

View File

@ -6,22 +6,17 @@ use url::Url;
use matrix_sdk::{ use matrix_sdk::{
self, self,
events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
identifiers::RoomId,
AsyncClient, AsyncClientConfig, EventEmitter, Room, SyncSettings, AsyncClient, AsyncClientConfig, EventEmitter, Room, SyncSettings,
}; };
use tokio::runtime::Handle; use tokio::sync::Mutex;
use tokio::sync::{
mpsc::{self, Sender},
Mutex,
};
struct CommandBot { struct CommandBot {
send: Sender<(RoomId, String)>, client: AsyncClient,
} }
impl CommandBot { impl CommandBot {
pub fn new(send: Sender<(RoomId, String)>) -> Self { pub fn new(client: AsyncClient) -> Self {
Self { send } Self { client }
} }
} }
@ -35,13 +30,18 @@ impl EventEmitter for CommandBot {
{ {
let room = room.lock().await; let room = room.lock().await;
if msg_body.contains("!party") { if msg_body.contains("!party") {
self.send println!("!party found");
.send(( let content = MessageEventContent::Text(TextMessageEventContent {
room.room_id.clone(), body: "🎉🎊🥳 let's PARTY!! 🥳🎊🎉".to_string(),
"🎉🎊🥳 let's PARTY!! 🥳🎊🎉".to_string(), format: None,
)) formatted_body: None,
relates_to: None,
});
self.client
.room_send(&room.room_id, content, None)
.await .await
.unwrap() .unwrap();
println!("message sent");
} }
} }
} }
@ -52,7 +52,6 @@ async fn login_and_sync(
homeserver_url: String, homeserver_url: String,
username: String, username: String,
password: String, password: String,
exec: Handle,
) -> Result<(), matrix_sdk::Error> { ) -> Result<(), matrix_sdk::Error> {
let client_config = AsyncClientConfig::new(); let client_config = AsyncClientConfig::new();
// .proxy("http://localhost:8080")? // .proxy("http://localhost:8080")?
@ -60,10 +59,10 @@ async fn login_and_sync(
let homeserver_url = Url::parse(&homeserver_url)?; let homeserver_url = Url::parse(&homeserver_url)?;
let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap(); let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap();
let (send, mut recv) = mpsc::channel(100);
client client
.add_event_emitter(Arc::new(Mutex::new(Box::new(CommandBot::new(send))))) .add_event_emitter(Arc::new(Mutex::new(Box::new(CommandBot::new(
client.clone(),
)))))
.await; .await;
client client
@ -75,38 +74,15 @@ async fn login_and_sync(
) )
.await?; .await?;
println!("logged in as user {}", username); println!("logged in as {}", username);
let client = Arc::new(Mutex::new(client)); client.sync(SyncSettings::new()).await.unwrap();
let send_client = Arc::clone(&client);
exec.spawn(async move {
for (id, msg) in recv.recv().await {
let content = MessageEventContent::Text(TextMessageEventContent {
body: msg,
format: None,
formatted_body: None,
relates_to: None,
});
send_client
.lock()
.await
.room_send(&id, content, None)
.await
.unwrap();
}
});
client
.lock()
.await
.sync_forever(SyncSettings::new(), |_| async {})
.await;
Ok(()) Ok(())
} }
fn main() -> Result<(), matrix_sdk::Error> { #[tokio::main]
async fn main() -> Result<(), matrix_sdk::Error> {
let (homeserver_url, username, password) = let (homeserver_url, username, password) =
match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) { match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) {
(Some(a), Some(b), Some(c)) => (a, b, c), (Some(a), Some(b), Some(c)) => (a, b, c),
@ -118,14 +94,6 @@ fn main() -> Result<(), matrix_sdk::Error> {
exit(1) exit(1)
} }
}; };
login_and_sync(homeserver_url, username, password).await?;
let mut runtime = tokio::runtime::Builder::new() Ok(())
.basic_scheduler()
.threaded_scheduler()
.enable_all()
.build()
.unwrap();
let executor = runtime.handle().clone();
runtime.block_on(async { login_and_sync(homeserver_url, username, password, executor).await })
} }