2020-11-24 16:11:27 +01:00
|
|
|
use matrix_sdk::{
|
2020-12-02 06:52:21 +01:00
|
|
|
events::AnyRoomEvent, events::AnySyncRoomEvent, identifiers::DeviceId, identifiers::UserId,
|
|
|
|
reqwest::Url, Client, ClientConfig, LoopCtrl, SyncSettings,
|
2020-11-24 16:11:27 +01:00
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
2020-11-23 17:18:05 +01:00
|
|
|
|
2020-11-24 16:11:27 +01:00
|
|
|
pub type Error = anyhow::Error;
|
|
|
|
|
|
|
|
// Needed to be able to serialize `Session`s. Should be done with serde remote.
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
2020-11-24 21:49:06 +01:00
|
|
|
pub struct Session {
|
2020-11-24 16:11:27 +01:00
|
|
|
access_token: String,
|
|
|
|
user_id: UserId,
|
|
|
|
device_id: Box<DeviceId>,
|
2020-11-24 21:49:06 +01:00
|
|
|
homeserver: String,
|
2020-11-24 16:11:27 +01:00
|
|
|
}
|
|
|
|
|
2020-11-24 21:49:06 +01:00
|
|
|
impl From<Session> for matrix_sdk::Session {
|
2020-11-24 16:11:27 +01:00
|
|
|
fn from(s: Session) -> Self {
|
|
|
|
Self {
|
|
|
|
access_token: s.access_token,
|
|
|
|
user_id: s.user_id,
|
|
|
|
device_id: s.device_id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-24 09:12:24 +01:00
|
|
|
|
2020-11-24 21:49:06 +01:00
|
|
|
/// Login with credentials, creating a new authentication session
|
2020-11-24 09:12:24 +01:00
|
|
|
pub async fn login(
|
2020-11-23 17:18:05 +01:00
|
|
|
username: &str,
|
|
|
|
password: &str,
|
|
|
|
server: &str,
|
2020-11-24 09:12:24 +01:00
|
|
|
) -> Result<(Client, Session), Error> {
|
2020-11-23 17:18:05 +01:00
|
|
|
let url = Url::parse(server)?;
|
2020-11-24 21:49:06 +01:00
|
|
|
let client = client(url)?;
|
2020-11-24 16:11:27 +01:00
|
|
|
|
2020-11-24 21:49:06 +01:00
|
|
|
let response = client
|
|
|
|
.login(
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
None,
|
|
|
|
Some(&format!("retrix@{}", hostname::get()?.to_string_lossy())),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
let session = Session {
|
|
|
|
access_token: response.access_token,
|
|
|
|
user_id: response.user_id,
|
|
|
|
device_id: response.device_id,
|
|
|
|
homeserver: server.to_owned(),
|
2020-11-24 09:12:24 +01:00
|
|
|
};
|
2020-11-24 21:49:06 +01:00
|
|
|
write_session(&session)?;
|
2020-11-24 16:11:27 +01:00
|
|
|
client.sync_once(SyncSettings::new()).await?;
|
2020-11-23 17:18:05 +01:00
|
|
|
|
2020-11-24 09:12:24 +01:00
|
|
|
Ok((client, session))
|
2020-11-23 17:18:05 +01:00
|
|
|
}
|
2020-11-24 16:11:27 +01:00
|
|
|
|
2020-11-24 21:49:06 +01:00
|
|
|
pub async fn restore_login(session: Session) -> Result<(Client, Session), Error> {
|
|
|
|
let url = Url::parse(&session.homeserver)?;
|
|
|
|
let client = client(url)?;
|
|
|
|
|
|
|
|
client.restore_login(session.clone().into()).await?;
|
|
|
|
client.sync_once(SyncSettings::new()).await?;
|
|
|
|
|
|
|
|
Ok((client, session))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a matrix client handler with the desired configuration
|
|
|
|
fn client(url: Url) -> Result<Client, matrix_sdk::Error> {
|
|
|
|
let config = ClientConfig::new().store_path(&dirs::config_dir().unwrap().join("retrix"));
|
|
|
|
Client::new_with_config(url, config)
|
|
|
|
}
|
|
|
|
|
2020-11-24 16:11:27 +01:00
|
|
|
/// File path to store session data in
|
|
|
|
fn session_path() -> std::path::PathBuf {
|
|
|
|
dirs::config_dir()
|
|
|
|
.unwrap()
|
|
|
|
.join("retrix")
|
|
|
|
.join("session.toml")
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read session data from config file
|
2020-11-24 21:49:06 +01:00
|
|
|
pub fn get_session() -> Result<Option<Session>, Error> {
|
2020-11-24 16:11:27 +01:00
|
|
|
let path = session_path();
|
|
|
|
if !path.is_file() {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2020-11-24 21:49:06 +01:00
|
|
|
let session: Session = toml::from_slice(&std::fs::read(path)?)?;
|
|
|
|
Ok(Some(session))
|
2020-11-24 16:11:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Save session data to config file
|
2020-11-24 21:49:06 +01:00
|
|
|
fn write_session(session: &Session) -> Result<(), Error> {
|
2020-11-24 16:11:27 +01:00
|
|
|
let serialized = toml::to_string(&session)?;
|
2020-11-24 21:49:06 +01:00
|
|
|
std::fs::write(session_path(), serialized)?;
|
2020-11-24 16:11:27 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-12-02 06:52:21 +01:00
|
|
|
|
|
|
|
pub struct MatrixSync {
|
|
|
|
client: matrix_sdk::Client,
|
|
|
|
//id: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MatrixSync {
|
|
|
|
pub fn subscription(client: matrix_sdk::Client) -> iced::Subscription<AnyRoomEvent> {
|
|
|
|
iced::Subscription::from_recipe(MatrixSync { client })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*#[async_trait]
|
|
|
|
impl EventEmitter for Callback {
|
|
|
|
async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) {
|
|
|
|
let room_id = if let matrix_sdk::RoomState::Joined(arc) = room {
|
|
|
|
let room = arc.read().await;
|
|
|
|
room.room_id.clone()
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
self.sender
|
|
|
|
.send(event.clone().into_full_event(room_id))
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
|
|
|
impl<H, I> iced_futures::subscription::Recipe<H, I> for MatrixSync
|
|
|
|
where
|
|
|
|
H: std::hash::Hasher,
|
|
|
|
{
|
|
|
|
type Output = AnyRoomEvent;
|
|
|
|
|
|
|
|
fn hash(&self, state: &mut H) {
|
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
|
|
std::any::TypeId::of::<Self>().hash(state);
|
|
|
|
//self.id.hash(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stream(
|
|
|
|
self: Box<Self>,
|
|
|
|
_input: iced_futures::BoxStream<I>,
|
|
|
|
) -> iced_futures::BoxStream<Self::Output> {
|
|
|
|
let (sender, receiver) = tokio::sync::mpsc::unbounded_channel();
|
|
|
|
let client = self.client.clone();
|
|
|
|
tokio::task::spawn(async move {
|
|
|
|
client
|
|
|
|
.sync_with_callback(SyncSettings::new(), |response| async {
|
|
|
|
for (room_id, room) in response.rooms.join {
|
|
|
|
for event in room.timeline.events {
|
|
|
|
if let Ok(event) = event.deserialize() {
|
|
|
|
let room_id = room_id.clone();
|
|
|
|
let event = match event {
|
|
|
|
AnySyncRoomEvent::Message(e) => {
|
|
|
|
AnyRoomEvent::Message(e.into_full_event(room_id))
|
|
|
|
}
|
|
|
|
AnySyncRoomEvent::State(e) => {
|
|
|
|
AnyRoomEvent::State(e.into_full_event(room_id))
|
|
|
|
}
|
|
|
|
AnySyncRoomEvent::RedactedMessage(e) => {
|
|
|
|
AnyRoomEvent::RedactedMessage(e.into_full_event(room_id))
|
|
|
|
}
|
|
|
|
AnySyncRoomEvent::RedactedState(e) => {
|
|
|
|
AnyRoomEvent::RedactedState(e.into_full_event(room_id))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sender.send(event).ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LoopCtrl::Continue
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
});
|
|
|
|
Box::pin(receiver)
|
|
|
|
}
|
|
|
|
}
|