Private
Public Access
1
0

Started working on attachment store

This commit is contained in:
2025-05-15 20:11:10 -07:00
parent 77177e07aa
commit 0d4c2e5104
10 changed files with 721 additions and 307 deletions

1
Cargo.lock generated
View File

@@ -1005,6 +1005,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"base64", "base64",
"bytes",
"chrono", "chrono",
"ctor", "ctor",
"env_logger", "env_logger",

View File

@@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
async-trait = "0.1.80" async-trait = "0.1.80"
base64 = "0.22.1" base64 = "0.22.1"
bytes = "1.10.1"
chrono = { version = "0.4.38", features = ["serde"] } chrono = { version = "0.4.38", features = ["serde"] }
ctor = "0.2.8" ctor = "0.2.8"
env_logger = "0.11.5" env_logger = "0.11.5"

View File

@@ -1,10 +1,12 @@
extern crate hyper; extern crate hyper;
extern crate serde; extern crate serde;
use std::{path::PathBuf, str}; use std::{path::PathBuf, pin::Pin, str, task::Poll};
use crate::api::AuthenticationStore;
use crate::api::event_socket::EventSocket; use crate::api::event_socket::EventSocket;
use crate::api::AuthenticationStore;
use bytes::Bytes;
use hyper::body::HttpBody;
use hyper::{Body, Client, Method, Request, Uri}; use hyper::{Body, Client, Method, Request, Uri};
use async_trait::async_trait; use async_trait::async_trait;
@@ -12,26 +14,19 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tokio::net::TcpStream; use tokio::net::TcpStream;
use futures_util::stream::{BoxStream, Stream};
use futures_util::task::Context;
use futures_util::{SinkExt, StreamExt, TryStreamExt}; use futures_util::{SinkExt, StreamExt, TryStreamExt};
use futures_util::stream::{SplitStream, SplitSink, Stream};
use futures_util::stream::BoxStream;
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream}; use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use crate::{ use crate::{
model::{ model::{
Conversation, Conversation, ConversationID, Event, JwtToken, Message, MessageID, OutgoingMessage,
ConversationID,
JwtToken,
Message,
MessageID,
UpdateItem, UpdateItem,
Event,
OutgoingMessage,
}, },
APIInterface,
APIInterface
}; };
type HttpClient = Client<hyper::client::HttpConnector>; type HttpClient = Client<hyper::client::HttpConnector>;
@@ -72,19 +67,19 @@ impl std::fmt::Display for Error {
} }
} }
impl From <hyper::Error> for Error { impl From<hyper::Error> for Error {
fn from(err: hyper::Error) -> Error { fn from(err: hyper::Error) -> Error {
Error::HTTPError(err) Error::HTTPError(err)
} }
} }
impl From <serde_json::Error> for Error { impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error { fn from(err: serde_json::Error) -> Error {
Error::SerdeError(err) Error::SerdeError(err)
} }
} }
impl From <tungstenite::Error> for Error { impl From<tungstenite::Error> for Error {
fn from(err: tungstenite::Error) -> Error { fn from(err: tungstenite::Error) -> Error {
Error::ClientError(err.to_string()) Error::ClientError(err.to_string())
} }
@@ -99,13 +94,17 @@ impl AuthBuilder for hyper::http::request::Builder {
fn with_auth(self, token: &Option<JwtToken>) -> Self { fn with_auth(self, token: &Option<JwtToken>) -> Self {
if let Some(token) = &token { if let Some(token) = &token {
self.header("Authorization", token.to_header_value()) self.header("Authorization", token.to_header_value())
} else { self } } else {
self
}
} }
fn with_auth_string(self, token: &Option<String>) -> Self { fn with_auth_string(self, token: &Option<String>) -> Self {
if let Some(token) = &token { if let Some(token) = &token {
self.header("Authorization", format!("Bearer: {}", token)) self.header("Authorization", format!("Bearer: {}", token))
} else { self } } else {
self
}
} }
} }
@@ -119,7 +118,8 @@ trait AuthSetting {
impl<B> AuthSetting for hyper::http::Request<B> { impl<B> AuthSetting for hyper::http::Request<B> {
fn authenticate(&mut self, token: &Option<JwtToken>) { fn authenticate(&mut self, token: &Option<JwtToken>) {
if let Some(token) = &token { if let Some(token) = &token {
self.headers_mut().insert("Authorization", token.to_header_value()); self.headers_mut()
.insert("Authorization", token.to_header_value());
} }
} }
} }
@@ -156,7 +156,7 @@ impl WebsocketEventSocket {
// Connection was closed cleanly // Connection was closed cleanly
Err(Error::ClientError("WebSocket connection closed".into())) Err(Error::ClientError("WebSocket connection closed".into()))
} }
_ => Ok(None) _ => Ok(None),
} }
}) })
} }
@@ -182,17 +182,40 @@ impl EventSocket for WebsocketEventSocket {
} }
} }
pub struct ResponseStream {
body: hyper::Body,
}
impl Stream for ResponseStream {
type Item = Result<Bytes, Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.body
.poll_next_unpin(cx)
.map_err(|e| Error::HTTPError(e))
}
}
impl From<hyper::Body> for ResponseStream {
fn from(value: hyper::Body) -> Self {
ResponseStream { body: value }
}
}
#[async_trait] #[async_trait]
impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> { impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
type Error = Error; type Error = Error;
type ResponseStream = ResponseStream;
async fn get_version(&mut self) -> Result<String, Self::Error> { async fn get_version(&mut self) -> Result<String, Self::Error> {
let version: String = self.request("version", Method::GET).await?; let version: String = self.deserialized_response("version", Method::GET).await?;
Ok(version) Ok(version)
} }
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error> { async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error> {
let conversations: Vec<Conversation> = self.request("conversations", Method::GET).await?; let conversations: Vec<Conversation> = self
.deserialized_response("conversations", Method::GET)
.await?;
Ok(conversations) Ok(conversations)
} }
@@ -205,7 +228,9 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
log::debug!("Authenticating with username: {:?}", credentials.username); log::debug!("Authenticating with username: {:?}", credentials.username);
let body = || -> Body { serde_json::to_string(&credentials).unwrap().into() }; let body = || -> Body { serde_json::to_string(&credentials).unwrap().into() };
let token: AuthResponse = self.request_with_body_retry("authenticate", Method::POST, body, false).await?; let token: AuthResponse = self
.deserialized_response_with_body_retry("authenticate", Method::POST, body, false)
.await?;
let token = JwtToken::new(&token.jwt).map_err(|e| Error::DecodeError(e.to_string()))?; let token = JwtToken::new(&token.jwt).map_err(|e| Error::DecodeError(e.to_string()))?;
log::debug!("Saving token: {:?}", token); log::debug!("Saving token: {:?}", token);
@@ -235,7 +260,7 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
endpoint.push_str(&format!("&afterMessageGUID={}", after_id)); endpoint.push_str(&format!("&afterMessageGUID={}", after_id));
} }
let messages: Vec<Message> = self.request(&endpoint, Method::GET).await?; let messages: Vec<Message> = self.deserialized_response(&endpoint, Method::GET).await?;
Ok(messages) Ok(messages)
} }
@@ -243,18 +268,32 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
&mut self, &mut self,
outgoing_message: &OutgoingMessage, outgoing_message: &OutgoingMessage,
) -> Result<Message, Self::Error> { ) -> Result<Message, Self::Error> {
let message: Message = self.request_with_body( let message: Message = self
"sendMessage", .deserialized_response_with_body("sendMessage", Method::POST, || {
Method::POST, serde_json::to_string(&outgoing_message).unwrap().into()
|| serde_json::to_string(&outgoing_message).unwrap().into() })
).await?; .await?;
Ok(message) Ok(message)
} }
async fn open_event_socket(&mut self, update_seq: Option<u64>) -> Result<WebsocketEventSocket, Self::Error> { async fn fetch_attachment_data(
use tungstenite::handshake::client::Request as TungsteniteRequest; &mut self,
guid: &String,
) -> Result<ResponseStream, Self::Error> {
let endpoint = format!("attachment?guid={}", guid);
self.response_with_body_retry(&endpoint, Method::GET, Body::empty, true)
.await
.map(hyper::Response::into_body)
.map(ResponseStream::from)
}
async fn open_event_socket(
&mut self,
update_seq: Option<u64>,
) -> Result<WebsocketEventSocket, Self::Error> {
use tungstenite::handshake::client::generate_key; use tungstenite::handshake::client::generate_key;
use tungstenite::handshake::client::Request as TungsteniteRequest;
let endpoint = match update_seq { let endpoint = match update_seq {
Some(seq) => format!("updates?seq={}", seq), Some(seq) => format!("updates?seq={}", seq),
@@ -279,7 +318,10 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
match &auth { match &auth {
Some(token) => { Some(token) => {
request.headers_mut().insert("Authorization", format!("Bearer: {}", token).parse().unwrap()); request.headers_mut().insert(
"Authorization",
format!("Bearer: {}", token).parse().unwrap(),
);
} }
None => { None => {
log::warn!(target: "websocket", "Proceeding without auth token."); log::warn!(target: "websocket", "Proceeding without auth token.");
@@ -306,14 +348,16 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
return Err(Error::Unauthorized); return Err(Error::Unauthorized);
} else { } else {
log::error!("Websocket unauthorized, no credentials provided"); log::error!("Websocket unauthorized, no credentials provided");
return Err(Error::ClientError("Unauthorized, no credentials provided".into())); return Err(Error::ClientError(
"Unauthorized, no credentials provided".into(),
));
} }
} }
_ => Err(e) _ => Err(e),
} },
_ => Err(e) _ => Err(e),
} },
} }
} }
} }
@@ -348,25 +392,67 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
} }
} }
async fn request<T: DeserializeOwned>(&mut self, endpoint: &str, method: Method) -> Result<T, Error> { async fn deserialized_response<T: DeserializeOwned>(
self.request_with_body(endpoint, method, Body::empty).await &mut self,
endpoint: &str,
method: Method,
) -> Result<T, Error> {
self.deserialized_response_with_body(endpoint, method, Body::empty)
.await
} }
async fn request_with_body<T>(&mut self, endpoint: &str, method: Method, body_fn: impl Fn() -> Body) -> Result<T, Error> async fn deserialized_response_with_body<T>(
where T: DeserializeOwned
{
self.request_with_body_retry(endpoint, method, body_fn, true).await
}
async fn request_with_body_retry<T>(
&mut self, &mut self,
endpoint: &str, endpoint: &str,
method: Method, method: Method,
body_fn: impl Fn() -> Body, body_fn: impl Fn() -> Body,
retry_auth: bool) -> Result<T, Error> ) -> Result<T, Error>
where where
T: DeserializeOwned, T: DeserializeOwned,
{ {
self.deserialized_response_with_body_retry(endpoint, method, body_fn, true)
.await
}
async fn deserialized_response_with_body_retry<T>(
&mut self,
endpoint: &str,
method: Method,
body_fn: impl Fn() -> Body,
retry_auth: bool,
) -> Result<T, Error>
where
T: DeserializeOwned,
{
let response = self
.response_with_body_retry(endpoint, method, body_fn, retry_auth)
.await?;
// Read and parse response body
let body = hyper::body::to_bytes(response.into_body()).await?;
let parsed: T = match serde_json::from_slice(&body) {
Ok(result) => Ok(result),
Err(json_err) => {
log::error!("Error deserializing JSON: {:?}", json_err);
log::error!("Body: {:?}", String::from_utf8_lossy(&body));
// If JSON deserialization fails, try to interpret it as plain text
// Unfortunately the server does return things like this...
let s = str::from_utf8(&body).map_err(|e| Error::DecodeError(e.to_string()))?;
serde_plain::from_str(s).map_err(|_| json_err)
}
}?;
Ok(parsed)
}
async fn response_with_body_retry(
&mut self,
endpoint: &str,
method: Method,
body_fn: impl Fn() -> Body,
retry_auth: bool,
) -> Result<hyper::Response<Body>, Error> {
use hyper::StatusCode; use hyper::StatusCode;
let uri = self.uri_for_endpoint(endpoint, None); let uri = self.uri_for_endpoint(endpoint, None);
@@ -389,7 +475,7 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
log::debug!("-> Response: {:}", response.status()); log::debug!("-> Response: {:}", response.status());
match response.status() { match response.status() {
StatusCode::OK => { /* cool */ }, StatusCode::OK => { /* cool */ }
// 401: Unauthorized. Token may have expired or is invalid. Attempt to renew. // 401: Unauthorized. Token may have expired or is invalid. Attempt to renew.
StatusCode::UNAUTHORIZED => { StatusCode::UNAUTHORIZED => {
@@ -398,15 +484,20 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
} }
if let Some(credentials) = &self.auth_store.get_credentials().await { if let Some(credentials) = &self.auth_store.get_credentials().await {
log::debug!("Renewing token using credentials: u: {:?}", credentials.username); log::debug!(
"Renewing token using credentials: u: {:?}",
credentials.username
);
let new_token = self.authenticate(credentials.clone()).await?; let new_token = self.authenticate(credentials.clone()).await?;
let request = build_request(&Some(new_token.to_string())); let request = build_request(&Some(new_token.to_string()));
response = self.client.request(request).await?; response = self.client.request(request).await?;
} else { } else {
return Err(Error::ClientError("Unauthorized, no credentials provided".into())); return Err(Error::ClientError(
"Unauthorized, no credentials provided".into(),
));
} }
}, }
// Other errors: bubble up. // Other errors: bubble up.
_ => { _ => {
@@ -415,22 +506,7 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
} }
} }
// Read and parse response body Ok(response)
let body = hyper::body::to_bytes(response.into_body()).await?;
let parsed: T = match serde_json::from_slice(&body) {
Ok(result) => Ok(result),
Err(json_err) => {
log::error!("Error deserializing JSON: {:?}", json_err);
log::error!("Body: {:?}", String::from_utf8_lossy(&body));
// If JSON deserialization fails, try to interpret it as plain text
// Unfortunately the server does return things like this...
let s = str::from_utf8(&body).map_err(|e| Error::DecodeError(e.to_string()))?;
serde_plain::from_str(s).map_err(|_| json_err)
}
}?;
Ok(parsed)
} }
} }
@@ -447,7 +523,10 @@ mod test {
password: "test".to_string(), password: "test".to_string(),
}; };
HTTPAPIClient::new(base_url, InMemoryAuthenticationStore::new(Some(credentials))) HTTPAPIClient::new(
base_url,
InMemoryAuthenticationStore::new(Some(credentials)),
)
} }
#[cfg(test)] #[cfg(test)]
@@ -498,7 +577,10 @@ mod test {
let mut client = local_mock_client(); let mut client = local_mock_client();
let conversations = client.get_conversations().await.unwrap(); let conversations = client.get_conversations().await.unwrap();
let conversation = conversations.first().unwrap(); let conversation = conversations.first().unwrap();
let messages = client.get_messages(&conversation.guid, None, None, None).await.unwrap(); let messages = client
.get_messages(&conversation.guid, None, None, None)
.await
.unwrap();
assert!(!messages.is_empty()); assert!(!messages.is_empty());
} }

View File

@@ -1,7 +1,8 @@
pub use crate::model::{Conversation, ConversationID, Message, MessageID, OutgoingMessage};
use async_trait::async_trait; use async_trait::async_trait;
pub use crate::model::{ use bytes::Bytes;
Conversation, Message, ConversationID, MessageID, OutgoingMessage, use futures_util::Stream;
};
pub mod auth; pub mod auth;
pub use crate::api::auth::{AuthenticationStore, InMemoryAuthenticationStore}; pub use crate::api::auth::{AuthenticationStore, InMemoryAuthenticationStore};
@@ -15,11 +16,13 @@ pub mod event_socket;
pub use event_socket::EventSocket; pub use event_socket::EventSocket;
use self::http_client::Credentials; use self::http_client::Credentials;
use std::fmt::Debug; use core::error::Error as StdError;
use std::{fmt::Debug, io::BufRead};
#[async_trait] #[async_trait]
pub trait APIInterface { pub trait APIInterface {
type Error: Debug; type Error: Debug;
type ResponseStream: Stream<Item = Result<Bytes, Self::Error>>;
// (GET) /version // (GET) /version
async fn get_version(&mut self) -> Result<String, Self::Error>; async fn get_version(&mut self) -> Result<String, Self::Error>;
@@ -42,9 +45,18 @@ pub trait APIInterface {
outgoing_message: &OutgoingMessage, outgoing_message: &OutgoingMessage,
) -> Result<Message, Self::Error>; ) -> Result<Message, Self::Error>;
// (GET) /attachment
async fn fetch_attachment_data(
&mut self,
guid: &String,
) -> Result<Self::ResponseStream, Self::Error>;
// (POST) /authenticate // (POST) /authenticate
async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>; async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>;
// (WS) /updates // (WS) /updates
async fn open_event_socket(&mut self, update_seq: Option<u64>) -> Result<impl EventSocket, Self::Error>; async fn open_event_socket(
&mut self,
update_seq: Option<u64>,
) -> Result<impl EventSocket, Self::Error>;
} }

View File

@@ -6,13 +6,16 @@ use uuid::Uuid;
pub use crate::APIInterface; pub use crate::APIInterface;
use crate::{ use crate::{
api::http_client::Credentials,
model::{Conversation, ConversationID, JwtToken, Message, MessageID, UpdateItem, Event, OutgoingMessage},
api::event_socket::EventSocket, api::event_socket::EventSocket,
api::http_client::Credentials,
model::{
Conversation, ConversationID, Event, JwtToken, Message, MessageID, OutgoingMessage,
UpdateItem,
},
}; };
use futures_util::StreamExt;
use futures_util::stream::BoxStream; use futures_util::stream::BoxStream;
use futures_util::StreamExt;
pub struct TestClient { pub struct TestClient {
pub version: &'static str, pub version: &'static str,
@@ -82,10 +85,10 @@ impl APIInterface for TestClient {
conversation_id: &ConversationID, conversation_id: &ConversationID,
limit: Option<u32>, limit: Option<u32>,
before: Option<MessageID>, before: Option<MessageID>,
after: Option<MessageID> after: Option<MessageID>,
) -> Result<Vec<Message>, Self::Error> { ) -> Result<Vec<Message>, Self::Error> {
if let Some(messages) = self.messages.get(conversation_id) { if let Some(messages) = self.messages.get(conversation_id) {
return Ok(messages.clone()) return Ok(messages.clone());
} }
Err(TestError::ConversationNotFound) Err(TestError::ConversationNotFound)
@@ -101,13 +104,21 @@ impl APIInterface for TestClient {
.date(OffsetDateTime::now_utc()) .date(OffsetDateTime::now_utc())
.build(); .build();
self.messages.entry(outgoing_message.conversation_id.clone()).or_insert(vec![]).push(message.clone()); self.messages
.entry(outgoing_message.conversation_id.clone())
.or_insert(vec![])
.push(message.clone());
Ok(message) Ok(message)
} }
async fn open_event_socket(&mut self, _update_seq: Option<u64>) -> Result<impl EventSocket, Self::Error> { async fn open_event_socket(
&mut self,
_update_seq: Option<u64>,
) -> Result<impl EventSocket, Self::Error> {
Ok(TestEventSocket::new()) Ok(TestEventSocket::new())
} }
async fn fetch_attachment_data(&mut self, guid: &String) -> Result<Vec<u8>, Self::Error> {
Ok(vec![])
}
} }

View File

@@ -76,6 +76,24 @@
value="Emitted when the list of messages is updated."/> value="Emitted when the list of messages is updated."/>
</signal> </signal>
<!-- Attachments -->
<method name="GetAttachment">
<arg type="s" name="attachment_id" direction="in"/>
<arg type="o" name="attachment_obj" direction="out"/>
<annotation name="org.freedesktop.DBus.DocString"
value="Returns a D-Bus object path for accessing the requested attachment."/>
</method>
</interface>
<interface name="net.buzzert.kordophone.Attachment">
<property name="FilePath" type="s" access="readonly" />
<property name="Downloaded" type="b" access="readonly" />
<method name="Delete">
</method>
<signal name="DownloadedStateChanged" />
</interface> </interface>
<interface name="net.buzzert.kordophone.Settings"> <interface name="net.buzzert.kordophone.Settings">

View File

@@ -0,0 +1,104 @@
use std::{
io::{BufReader, BufWriter, Read, Write},
path::{Path, PathBuf},
};
use anyhow::{Error, Result};
use futures_util::{poll, StreamExt};
use kordophone::APIInterface;
use thiserror::Error;
use tokio::pin;
mod target {
pub static ATTACHMENTS: &str = "attachments";
}
#[derive(Debug, Clone)]
pub struct Attachment {
pub guid: String,
pub path: PathBuf,
pub downloaded: bool,
}
#[derive(Debug, Error)]
enum AttachmentStoreError {
#[error("attachment has already been downloaded")]
AttachmentAlreadyDownloaded,
#[error("Client error: {0}")]
APIClientError(String),
}
pub struct AttachmentStore {
store_path: PathBuf,
}
impl AttachmentStore {
pub fn new(data_dir: &PathBuf) -> AttachmentStore {
let store_path = data_dir.join("attachments");
log::info!(target: target::ATTACHMENTS, "Attachment store path: {}", store_path.display());
// Create the attachment store if it doesn't exist
std::fs::create_dir_all(&store_path)
.expect("Wasn't able to create the attachment store path");
AttachmentStore {
store_path: store_path,
}
}
pub fn get_attachment(&self, guid: &String) -> Attachment {
let path = self.store_path.join(guid);
let path_exists = std::fs::exists(&path).expect(
format!(
"Wasn't able to check for the existence of an attachment file path at {}",
&path.display()
)
.as_str(),
);
Attachment {
guid: guid.to_owned(),
path: path,
downloaded: path_exists,
}
}
pub async fn download_attachent<C, F>(
&mut self,
attachment: &Attachment,
mut client_factory: F,
) -> Result<()>
where
C: APIInterface,
F: AsyncFnMut() -> Result<C>,
{
if attachment.downloaded {
log::error!(target: target::ATTACHMENTS, "Attempted to download existing attachment.");
return Err(AttachmentStoreError::AttachmentAlreadyDownloaded.into());
}
// Create temporary file first, we'll atomically swap later.
assert!(!std::fs::exists(&attachment.path).unwrap());
let file = std::fs::File::create(&attachment.path)?;
let mut writer = BufWriter::new(&file);
log::trace!(target: target::ATTACHMENTS, "Created attachment file at {}", &attachment.path.display());
let mut client = (client_factory)().await?;
let stream = client
.fetch_attachment_data(&attachment.guid)
.await
.map_err(|e| AttachmentStoreError::APIClientError(format!("{:?}", e)))?;
// Since we're async, we need to pin this.
pin!(stream);
log::trace!(target: target::ATTACHMENTS, "Writing attachment data to disk");
while let Some(Ok(data)) = stream.next().await {
writer.write(data.as_ref())?;
}
Ok(())
}
}

View File

@@ -1,10 +1,10 @@
use crate::daemon::SettingsKey; use crate::daemon::SettingsKey;
use keyring::{Entry, Result};
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use keyring::{Entry, Result};
use kordophone::api::{AuthenticationStore, http_client::Credentials}; use kordophone::api::{http_client::Credentials, AuthenticationStore};
use kordophone::model::JwtToken; use kordophone::model::JwtToken;
use kordophone_db::database::{Database, DatabaseAccess}; use kordophone_db::database::{Database, DatabaseAccess};
@@ -25,51 +25,66 @@ impl AuthenticationStore for DatabaseAuthenticationStore {
async fn get_credentials(&mut self) -> Option<Credentials> { async fn get_credentials(&mut self) -> Option<Credentials> {
use keyring::secret_service::SsCredential; use keyring::secret_service::SsCredential;
self.database.lock().await.with_settings(|settings| { self.database
let username: Option<String> = settings.get::<String>(SettingsKey::USERNAME) .lock()
.unwrap_or_else(|e| { .await
log::warn!("error getting username from database: {}", e); .with_settings(|settings| {
None let username: Option<String> = settings
}); .get::<String>(SettingsKey::USERNAME)
.unwrap_or_else(|e| {
log::warn!("error getting username from database: {}", e);
None
});
match username { match username {
Some(username) => { Some(username) => {
let credential = SsCredential::new_with_target(None, "net.buzzert.kordophonecd", &username).unwrap(); let credential = SsCredential::new_with_target(
None,
"net.buzzert.kordophonecd",
&username,
)
.unwrap();
let password: Result<String> = Entry::new_with_credential(Box::new(credential)) let password: Result<String> =
.get_password(); Entry::new_with_credential(Box::new(credential)).get_password();
log::debug!("password: {:?}", password); match password {
Ok(password) => Some(Credentials { username, password }),
match password { Err(e) => {
Ok(password) => Some(Credentials { username, password }), log::error!("error getting password from keyring: {}", e);
Err(e) => { None
log::error!("error getting password from keyring: {}", e); }
None
} }
} }
None => None,
} }
None => None, })
} .await
}).await
} }
async fn get_token(&mut self) -> Option<String> { async fn get_token(&mut self) -> Option<String> {
self.database.lock().await self.database
.with_settings(|settings| { .lock()
match settings.get::<String>(SettingsKey::TOKEN) { .await
.with_settings(
|settings| match settings.get::<String>(SettingsKey::TOKEN) {
Ok(token) => token, Ok(token) => token,
Err(e) => { Err(e) => {
log::warn!("Failed to get token from settings: {}", e); log::warn!("Failed to get token from settings: {}", e);
None None
} }
} },
}).await )
.await
} }
async fn set_token(&mut self, token: String) { async fn set_token(&mut self, token: String) {
self.database.lock().await self.database
.with_settings(|settings| settings.put(SettingsKey::TOKEN, &token)).await.unwrap_or_else(|e| { .lock()
.await
.with_settings(|settings| settings.put(SettingsKey::TOKEN, &token))
.await
.unwrap_or_else(|e| {
log::error!("Failed to set token: {}", e); log::error!("Failed to set token: {}", e);
}); });
} }

View File

@@ -1,6 +1,6 @@
pub mod settings; pub mod settings;
use settings::Settings;
use settings::keys as SettingsKey; use settings::keys as SettingsKey;
use settings::Settings;
pub mod events; pub mod events;
use events::*; use events::*;
@@ -11,13 +11,13 @@ use signals::*;
use anyhow::Result; use anyhow::Result;
use directories::ProjectDirs; use directories::ProjectDirs;
use std::error::Error;
use std::path::PathBuf;
use std::collections::HashMap; use std::collections::HashMap;
use std::error::Error;
use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
use thiserror::Error; use thiserror::Error;
use tokio::sync::mpsc::{Sender, Receiver}; use tokio::sync::mpsc::{Receiver, Sender};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use uuid::Uuid; use uuid::Uuid;
@@ -26,8 +26,8 @@ use kordophone_db::{
models::{Conversation, Message}, models::{Conversation, Message},
}; };
use kordophone::api::APIInterface;
use kordophone::api::http_client::HTTPAPIClient; use kordophone::api::http_client::HTTPAPIClient;
use kordophone::api::APIInterface;
use kordophone::model::outgoing_message::OutgoingMessage; use kordophone::model::outgoing_message::OutgoingMessage;
use kordophone::model::ConversationID; use kordophone::model::ConversationID;
@@ -38,8 +38,12 @@ mod auth_store;
use auth_store::DatabaseAuthenticationStore; use auth_store::DatabaseAuthenticationStore;
mod post_office; mod post_office;
use post_office::PostOffice;
use post_office::Event as PostOfficeEvent; use post_office::Event as PostOfficeEvent;
use post_office::PostOffice;
mod attachment_store;
pub use attachment_store::Attachment;
use attachment_store::AttachmentStore;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum DaemonError { pub enum DaemonError {
@@ -49,6 +53,8 @@ pub enum DaemonError {
pub type DaemonResult<T> = Result<T, Box<dyn Error + Send + Sync>>; pub type DaemonResult<T> = Result<T, Box<dyn Error + Send + Sync>>;
type DaemonClient = HTTPAPIClient<DatabaseAuthenticationStore>;
pub mod target { pub mod target {
pub static SYNC: &str = "sync"; pub static SYNC: &str = "sync";
pub static EVENT: &str = "event"; pub static EVENT: &str = "event";
@@ -68,6 +74,8 @@ pub struct Daemon {
outgoing_messages: HashMap<ConversationID, Vec<OutgoingMessage>>, outgoing_messages: HashMap<ConversationID, Vec<OutgoingMessage>>,
attachment_store: AttachmentStore,
version: String, version: String,
database: Arc<Mutex<Database>>, database: Arc<Mutex<Database>>,
runtime: tokio::runtime::Runtime, runtime: tokio::runtime::Runtime,
@@ -95,6 +103,10 @@ impl Daemon {
let database_impl = Database::new(&database_path.to_string_lossy())?; let database_impl = Database::new(&database_path.to_string_lossy())?;
let database = Arc::new(Mutex::new(database_impl)); let database = Arc::new(Mutex::new(database_impl));
let data_path = Self::get_data_dir().expect("Unable to get data path");
let attachment_store = AttachmentStore::new(&data_path);
Ok(Self { Ok(Self {
version: "0.1.0".to_string(), version: "0.1.0".to_string(),
database, database,
@@ -105,7 +117,8 @@ impl Daemon {
post_office_sink, post_office_sink,
post_office_source: Some(post_office_source), post_office_source: Some(post_office_source),
outgoing_messages: HashMap::new(), outgoing_messages: HashMap::new(),
runtime attachment_store: attachment_store,
runtime,
}) })
} }
@@ -114,7 +127,8 @@ impl Daemon {
log::debug!("Debug logging enabled."); log::debug!("Debug logging enabled.");
// Update monitor // Update monitor
let mut update_monitor = UpdateMonitor::new(self.database.clone(), self.event_sender.clone()); let mut update_monitor =
UpdateMonitor::new(self.database.clone(), self.event_sender.clone());
tokio::spawn(async move { tokio::spawn(async move {
update_monitor.run().await; // should run indefinitely update_monitor.run().await; // should run indefinitely
}); });
@@ -125,7 +139,10 @@ impl Daemon {
let event_sender = self.event_sender.clone(); let event_sender = self.event_sender.clone();
let post_office_source = self.post_office_source.take().unwrap(); let post_office_source = self.post_office_source.take().unwrap();
tokio::spawn(async move { tokio::spawn(async move {
let mut post_office = PostOffice::new(post_office_source, event_sender, async move || Self::get_client_impl(&mut database).await ); let mut post_office =
PostOffice::new(post_office_source, event_sender, async move || {
Self::get_client_impl(&mut database).await
});
post_office.run().await; post_office.run().await;
}); });
} }
@@ -140,7 +157,7 @@ impl Daemon {
match event { match event {
Event::GetVersion(reply) => { Event::GetVersion(reply) => {
reply.send(self.version.clone()).unwrap(); reply.send(self.version.clone()).unwrap();
}, }
Event::SyncConversationList(reply) => { Event::SyncConversationList(reply) => {
let mut db_clone = self.database.clone(); let mut db_clone = self.database.clone();
@@ -154,13 +171,14 @@ impl Daemon {
// This is a background operation, so return right away. // This is a background operation, so return right away.
reply.send(()).unwrap(); reply.send(()).unwrap();
}, }
Event::SyncAllConversations(reply) => { Event::SyncAllConversations(reply) => {
let mut db_clone = self.database.clone(); let mut db_clone = self.database.clone();
let signal_sender = self.signal_sender.clone(); let signal_sender = self.signal_sender.clone();
self.runtime.spawn(async move { self.runtime.spawn(async move {
let result = Self::sync_all_conversations_impl(&mut db_clone, &signal_sender).await; let result =
Self::sync_all_conversations_impl(&mut db_clone, &signal_sender).await;
if let Err(e) = result { if let Err(e) = result {
log::error!(target: target::SYNC, "Error handling sync event: {}", e); log::error!(target: target::SYNC, "Error handling sync event: {}", e);
} }
@@ -168,86 +186,99 @@ impl Daemon {
// This is a background operation, so return right away. // This is a background operation, so return right away.
reply.send(()).unwrap(); reply.send(()).unwrap();
}, }
Event::SyncConversation(conversation_id, reply) => { Event::SyncConversation(conversation_id, reply) => {
let mut db_clone = self.database.clone(); let mut db_clone = self.database.clone();
let signal_sender = self.signal_sender.clone(); let signal_sender = self.signal_sender.clone();
self.runtime.spawn(async move { self.runtime.spawn(async move {
let result = Self::sync_conversation_impl(&mut db_clone, &signal_sender, conversation_id).await; let result = Self::sync_conversation_impl(
&mut db_clone,
&signal_sender,
conversation_id,
)
.await;
if let Err(e) = result { if let Err(e) = result {
log::error!(target: target::SYNC, "Error handling sync event: {}", e); log::error!(target: target::SYNC, "Error handling sync event: {}", e);
} }
}); });
reply.send(()).unwrap(); reply.send(()).unwrap();
}, }
Event::GetAllConversations(limit, offset, reply) => { Event::GetAllConversations(limit, offset, reply) => {
let conversations = self.get_conversations_limit_offset(limit, offset).await; let conversations = self.get_conversations_limit_offset(limit, offset).await;
reply.send(conversations).unwrap(); reply.send(conversations).unwrap();
}, }
Event::GetAllSettings(reply) => { Event::GetAllSettings(reply) => {
let settings = self.get_settings().await let settings = self.get_settings().await.unwrap_or_else(|e| {
.unwrap_or_else(|e| { log::error!(target: target::SETTINGS, "Failed to get settings: {:#?}", e);
log::error!(target: target::SETTINGS, "Failed to get settings: {:#?}", e); Settings::default()
Settings::default() });
});
reply.send(settings).unwrap(); reply.send(settings).unwrap();
}, }
Event::UpdateSettings(settings, reply) => { Event::UpdateSettings(settings, reply) => {
self.update_settings(&settings).await self.update_settings(&settings).await.unwrap_or_else(|e| {
.unwrap_or_else(|e| { log::error!(target: target::SETTINGS, "Failed to update settings: {}", e);
log::error!(target: target::SETTINGS, "Failed to update settings: {}", e); });
});
reply.send(()).unwrap(); reply.send(()).unwrap();
}, }
Event::GetMessages(conversation_id, last_message_id, reply) => { Event::GetMessages(conversation_id, last_message_id, reply) => {
let messages = self.get_messages(conversation_id, last_message_id).await; let messages = self.get_messages(conversation_id, last_message_id).await;
reply.send(messages).unwrap(); reply.send(messages).unwrap();
}, }
Event::DeleteAllConversations(reply) => { Event::DeleteAllConversations(reply) => {
self.delete_all_conversations().await self.delete_all_conversations().await.unwrap_or_else(|e| {
.unwrap_or_else(|e| { log::error!(target: target::SYNC, "Failed to delete all conversations: {}", e);
log::error!(target: target::SYNC, "Failed to delete all conversations: {}", e); });
});
reply.send(()).unwrap(); reply.send(()).unwrap();
}, }
Event::SendMessage(conversation_id, text, reply) => { Event::SendMessage(conversation_id, text, reply) => {
let conversation_id = conversation_id.clone(); let conversation_id = conversation_id.clone();
let uuid = self.enqueue_outgoing_message(text, conversation_id.clone()).await; let uuid = self
.enqueue_outgoing_message(text, conversation_id.clone())
.await;
reply.send(uuid).unwrap(); reply.send(uuid).unwrap();
// Send message updated signal, we have a placeholder message we will return. // Send message updated signal, we have a placeholder message we will return.
self.signal_sender.send(Signal::MessagesUpdated(conversation_id.clone())).await.unwrap(); self.signal_sender
}, .send(Signal::MessagesUpdated(conversation_id.clone()))
.await
.unwrap();
}
Event::MessageSent(message, outgoing_message, conversation_id) => { Event::MessageSent(message, outgoing_message, conversation_id) => {
log::info!(target: target::EVENT, "Daemon: message sent: {}", message.id); log::info!(target: target::EVENT, "Daemon: message sent: {}", message.id);
// Insert the message into the database. // Insert the message into the database.
log::debug!(target: target::EVENT, "inserting sent message into database: {}", message.id); log::debug!(target: target::EVENT, "inserting sent message into database: {}", message.id);
self.database.lock().await self.database
.with_repository(|r| .lock()
r.insert_message( &conversation_id, message) .await
).await.unwrap(); .with_repository(|r| r.insert_message(&conversation_id, message))
.await
.unwrap();
// Remove from outgoing messages. // Remove from outgoing messages.
log::debug!(target: target::EVENT, "Removing message from outgoing messages: {}", outgoing_message.guid); log::debug!(target: target::EVENT, "Removing message from outgoing messages: {}", outgoing_message.guid);
self.outgoing_messages.get_mut(&conversation_id) self.outgoing_messages
.get_mut(&conversation_id)
.map(|messages| messages.retain(|m| m.guid != outgoing_message.guid)); .map(|messages| messages.retain(|m| m.guid != outgoing_message.guid));
// Send message updated signal. // Send message updated signal.
self.signal_sender.send(Signal::MessagesUpdated(conversation_id)).await.unwrap(); self.signal_sender
}, .send(Signal::MessagesUpdated(conversation_id))
.await
.unwrap();
}
} }
} }
@@ -257,27 +288,47 @@ impl Daemon {
} }
async fn get_conversations(&mut self) -> Vec<Conversation> { async fn get_conversations(&mut self) -> Vec<Conversation> {
self.database.lock().await.with_repository(|r| r.all_conversations(i32::MAX, 0).unwrap()).await self.database
.lock()
.await
.with_repository(|r| r.all_conversations(i32::MAX, 0).unwrap())
.await
} }
async fn get_conversations_limit_offset(&mut self, limit: i32, offset: i32) -> Vec<Conversation> { async fn get_conversations_limit_offset(
self.database.lock().await.with_repository(|r| r.all_conversations(limit, offset).unwrap()).await &mut self,
limit: i32,
offset: i32,
) -> Vec<Conversation> {
self.database
.lock()
.await
.with_repository(|r| r.all_conversations(limit, offset).unwrap())
.await
} }
async fn get_messages(&mut self, conversation_id: String, last_message_id: Option<String>) -> Vec<Message> { async fn get_messages(
&mut self,
conversation_id: String,
last_message_id: Option<String>,
) -> Vec<Message> {
// Get outgoing messages for this conversation. // Get outgoing messages for this conversation.
let empty_vec: Vec<OutgoingMessage> = vec![]; let empty_vec: Vec<OutgoingMessage> = vec![];
let outgoing_messages: &Vec<OutgoingMessage> = self.outgoing_messages.get(&conversation_id) let outgoing_messages: &Vec<OutgoingMessage> = self
.outgoing_messages
.get(&conversation_id)
.unwrap_or(&empty_vec); .unwrap_or(&empty_vec);
self.database.lock().await self.database
.with_repository(|r| .lock()
.await
.with_repository(|r| {
r.get_messages_for_conversation(&conversation_id) r.get_messages_for_conversation(&conversation_id)
.unwrap() .unwrap()
.into_iter() .into_iter()
.chain(outgoing_messages.into_iter().map(|m| m.into())) .chain(outgoing_messages.into_iter().map(|m| m.into()))
.collect() .collect()
) })
.await .await
} }
@@ -289,31 +340,41 @@ impl Daemon {
.build(); .build();
// Keep a record of this so we can provide a consistent model to the client. // Keep a record of this so we can provide a consistent model to the client.
self.outgoing_messages.entry(conversation_id) self.outgoing_messages
.entry(conversation_id)
.or_insert(vec![]) .or_insert(vec![])
.push(outgoing_message.clone()); .push(outgoing_message.clone());
let guid = outgoing_message.guid.clone(); let guid = outgoing_message.guid.clone();
self.post_office_sink.send(PostOfficeEvent::EnqueueOutgoingMessage(outgoing_message)).await.unwrap(); self.post_office_sink
.send(PostOfficeEvent::EnqueueOutgoingMessage(outgoing_message))
.await
.unwrap();
guid guid
} }
async fn sync_conversation_list(database: &mut Arc<Mutex<Database>>, signal_sender: &Sender<Signal>) -> Result<()> { async fn sync_conversation_list(
database: &mut Arc<Mutex<Database>>,
signal_sender: &Sender<Signal>,
) -> Result<()> {
log::info!(target: target::SYNC, "Starting list conversation sync"); log::info!(target: target::SYNC, "Starting list conversation sync");
let mut client = Self::get_client_impl(database).await?; let mut client = Self::get_client_impl(database).await?;
// Fetch conversations from server // Fetch conversations from server
let fetched_conversations = client.get_conversations().await?; let fetched_conversations = client.get_conversations().await?;
let db_conversations: Vec<kordophone_db::models::Conversation> = fetched_conversations.into_iter() let db_conversations: Vec<kordophone_db::models::Conversation> = fetched_conversations
.into_iter()
.map(kordophone_db::models::Conversation::from) .map(kordophone_db::models::Conversation::from)
.collect(); .collect();
// Insert each conversation // Insert each conversation
let num_conversations = db_conversations.len(); let num_conversations = db_conversations.len();
for conversation in db_conversations { for conversation in db_conversations {
database.with_repository(|r| r.insert_conversation(conversation)).await?; database
.with_repository(|r| r.insert_conversation(conversation))
.await?;
} }
// Send conversations updated signal // Send conversations updated signal
@@ -323,14 +384,18 @@ impl Daemon {
Ok(()) Ok(())
} }
async fn sync_all_conversations_impl(database: &mut Arc<Mutex<Database>>, signal_sender: &Sender<Signal>) -> Result<()> { async fn sync_all_conversations_impl(
database: &mut Arc<Mutex<Database>>,
signal_sender: &Sender<Signal>,
) -> Result<()> {
log::info!(target: target::SYNC, "Starting full conversation sync"); log::info!(target: target::SYNC, "Starting full conversation sync");
let mut client = Self::get_client_impl(database).await?; let mut client = Self::get_client_impl(database).await?;
// Fetch conversations from server // Fetch conversations from server
let fetched_conversations = client.get_conversations().await?; let fetched_conversations = client.get_conversations().await?;
let db_conversations: Vec<kordophone_db::models::Conversation> = fetched_conversations.into_iter() let db_conversations: Vec<kordophone_db::models::Conversation> = fetched_conversations
.into_iter()
.map(kordophone_db::models::Conversation::from) .map(kordophone_db::models::Conversation::from)
.collect(); .collect();
@@ -340,7 +405,9 @@ impl Daemon {
let conversation_id = conversation.guid.clone(); let conversation_id = conversation.guid.clone();
// Insert the conversation // Insert the conversation
database.with_repository(|r| r.insert_conversation(conversation)).await?; database
.with_repository(|r| r.insert_conversation(conversation))
.await?;
// Sync individual conversation. // Sync individual conversation.
Self::sync_conversation_impl(database, signal_sender, conversation_id).await?; Self::sync_conversation_impl(database, signal_sender, conversation_id).await?;
@@ -353,13 +420,19 @@ impl Daemon {
Ok(()) Ok(())
} }
async fn sync_conversation_impl(database: &mut Arc<Mutex<Database>>, signal_sender: &Sender<Signal>, conversation_id: String) -> Result<()> { async fn sync_conversation_impl(
database: &mut Arc<Mutex<Database>>,
signal_sender: &Sender<Signal>,
conversation_id: String,
) -> Result<()> {
log::debug!(target: target::SYNC, "Starting conversation sync for {}", conversation_id); log::debug!(target: target::SYNC, "Starting conversation sync for {}", conversation_id);
let mut client = Self::get_client_impl(database).await?; let mut client = Self::get_client_impl(database).await?;
// Check if conversation exists in database. // Check if conversation exists in database.
let conversation = database.with_repository(|r| r.get_conversation_by_guid(&conversation_id)).await?; let conversation = database
.with_repository(|r| r.get_conversation_by_guid(&conversation_id))
.await?;
if conversation.is_none() { if conversation.is_none() {
// If the conversation doesn't exist, first do a conversation list sync. // If the conversation doesn't exist, first do a conversation list sync.
log::warn!(target: target::SYNC, "Conversation {} not found, performing list sync", conversation_id); log::warn!(target: target::SYNC, "Conversation {} not found, performing list sync", conversation_id);
@@ -367,28 +440,37 @@ impl Daemon {
} }
// Fetch and sync messages for this conversation // Fetch and sync messages for this conversation
let last_message_id = database.with_repository(|r| -> Option<String> { let last_message_id = database
r.get_last_message_for_conversation(&conversation_id) .with_repository(|r| -> Option<String> {
.unwrap_or(None) r.get_last_message_for_conversation(&conversation_id)
.map(|m| m.id) .unwrap_or(None)
}).await; .map(|m| m.id)
})
.await;
log::debug!(target: target::SYNC, "Fetching messages for conversation {}", &conversation_id); log::debug!(target: target::SYNC, "Fetching messages for conversation {}", &conversation_id);
log::debug!(target: target::SYNC, "Last message id: {:?}", last_message_id); log::debug!(target: target::SYNC, "Last message id: {:?}", last_message_id);
let messages = client.get_messages(&conversation_id, None, None, last_message_id).await?; let messages = client
let db_messages: Vec<kordophone_db::models::Message> = messages.into_iter() .get_messages(&conversation_id, None, None, last_message_id)
.await?;
let db_messages: Vec<kordophone_db::models::Message> = messages
.into_iter()
.map(kordophone_db::models::Message::from) .map(kordophone_db::models::Message::from)
.collect(); .collect();
// Insert each message // Insert each message
let num_messages = db_messages.len(); let num_messages = db_messages.len();
log::debug!(target: target::SYNC, "Inserting {} messages for conversation {}", num_messages, &conversation_id); log::debug!(target: target::SYNC, "Inserting {} messages for conversation {}", num_messages, &conversation_id);
database.with_repository(|r| r.insert_messages(&conversation_id, db_messages)).await?; database
.with_repository(|r| r.insert_messages(&conversation_id, db_messages))
.await?;
// Send messages updated signal, if we actually inserted any messages. // Send messages updated signal, if we actually inserted any messages.
if num_messages > 0 { if num_messages > 0 {
signal_sender.send(Signal::MessagesUpdated(conversation_id.clone())).await?; signal_sender
.send(Signal::MessagesUpdated(conversation_id.clone()))
.await?;
} }
log::debug!(target: target::SYNC, "Synchronized {} messages for conversation {}", num_messages, &conversation_id); log::debug!(target: target::SYNC, "Synchronized {} messages for conversation {}", num_messages, &conversation_id);
@@ -408,35 +490,45 @@ impl Daemon {
Self::get_client_impl(&mut self.database).await Self::get_client_impl(&mut self.database).await
} }
async fn get_client_impl(database: &mut Arc<Mutex<Database>>) -> Result<HTTPAPIClient<DatabaseAuthenticationStore>> { async fn get_client_impl(
database: &mut Arc<Mutex<Database>>,
) -> Result<HTTPAPIClient<DatabaseAuthenticationStore>> {
let settings = database.with_settings(Settings::from_db).await?; let settings = database.with_settings(Settings::from_db).await?;
let server_url = settings.server_url let server_url = settings
.server_url
.ok_or(DaemonError::ClientNotConfigured)?; .ok_or(DaemonError::ClientNotConfigured)?;
let client = HTTPAPIClient::new( let client = HTTPAPIClient::new(
server_url.parse().unwrap(), server_url.parse().unwrap(),
DatabaseAuthenticationStore::new(database.clone()) DatabaseAuthenticationStore::new(database.clone()),
); );
Ok(client) Ok(client)
} }
async fn delete_all_conversations(&mut self) -> Result<()> { async fn delete_all_conversations(&mut self) -> Result<()> {
self.database.with_repository(|r| -> Result<()> { self.database
r.delete_all_conversations()?; .with_repository(|r| -> Result<()> {
r.delete_all_messages()?; r.delete_all_conversations()?;
Ok(()) r.delete_all_messages()?;
}).await?; Ok(())
})
.await?;
self.signal_sender.send(Signal::ConversationsUpdated).await?; self.signal_sender
.send(Signal::ConversationsUpdated)
.await?;
Ok(()) Ok(())
} }
fn get_data_dir() -> Option<PathBuf> {
ProjectDirs::from("net", "buzzert", "kordophonecd").map(|p| PathBuf::from(p.data_dir()))
}
fn get_database_path() -> PathBuf { fn get_database_path() -> PathBuf {
if let Some(proj_dirs) = ProjectDirs::from("net", "buzzert", "kordophonecd") { if let Some(data_dir) = Self::get_data_dir() {
let data_dir = proj_dirs.data_dir();
data_dir.join("database.db") data_dir.join("database.db")
} else { } else {
// Fallback to a local path if we can't get the system directories // Fallback to a local path if we can't get the system directories

View File

@@ -1,16 +1,17 @@
use dbus::arg; use dbus::arg;
use dbus_tree::MethodErr; use dbus_tree::MethodErr;
use tokio::sync::mpsc;
use std::future::Future; use std::future::Future;
use std::thread; use std::thread;
use tokio::sync::mpsc;
use tokio::sync::oneshot; use tokio::sync::oneshot;
use crate::daemon::{ use crate::daemon::{
DaemonResult,
events::{Event, Reply}, events::{Event, Reply},
settings::Settings, settings::Settings,
Attachment, DaemonResult,
}; };
use crate::dbus::interface::NetBuzzertKordophoneAttachment as DbusAttachment;
use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository; use crate::dbus::interface::NetBuzzertKordophoneRepository as DbusRepository;
use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings; use crate::dbus::interface::NetBuzzertKordophoneSettings as DbusSettings;
@@ -29,7 +30,8 @@ impl ServerImpl {
make_event: impl FnOnce(Reply<T>) -> Event, make_event: impl FnOnce(Reply<T>) -> Event,
) -> DaemonResult<T> { ) -> DaemonResult<T> {
let (reply_tx, reply_rx) = oneshot::channel(); let (reply_tx, reply_rx) = oneshot::channel();
self.event_sink.send(make_event(reply_tx)) self.event_sink
.send(make_event(reply_tx))
.await .await
.map_err(|_| "Failed to send event")?; .map_err(|_| "Failed to send event")?;
@@ -51,19 +53,46 @@ impl DbusRepository for ServerImpl {
self.send_event_sync(Event::GetVersion) self.send_event_sync(Event::GetVersion)
} }
fn get_conversations(&mut self, limit: i32, offset: i32) -> Result<Vec<arg::PropMap>, dbus::MethodErr> { fn get_conversations(
&mut self,
limit: i32,
offset: i32,
) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
self.send_event_sync(|r| Event::GetAllConversations(limit, offset, r)) self.send_event_sync(|r| Event::GetAllConversations(limit, offset, r))
.map(|conversations| { .map(|conversations| {
conversations.into_iter().map(|conv| { conversations
let mut map = arg::PropMap::new(); .into_iter()
map.insert("guid".into(), arg::Variant(Box::new(conv.guid))); .map(|conv| {
map.insert("display_name".into(), arg::Variant(Box::new(conv.display_name.unwrap_or_default()))); let mut map = arg::PropMap::new();
map.insert("unread_count".into(), arg::Variant(Box::new(conv.unread_count as i32))); map.insert("guid".into(), arg::Variant(Box::new(conv.guid)));
map.insert("last_message_preview".into(), arg::Variant(Box::new(conv.last_message_preview.unwrap_or_default()))); map.insert(
map.insert("participants".into(), arg::Variant(Box::new(conv.participants.into_iter().map(|p| p.display_name()).collect::<Vec<String>>()))); "display_name".into(),
map.insert("date".into(), arg::Variant(Box::new(conv.date.and_utc().timestamp()))); arg::Variant(Box::new(conv.display_name.unwrap_or_default())),
map );
}).collect() map.insert(
"unread_count".into(),
arg::Variant(Box::new(conv.unread_count as i32)),
);
map.insert(
"last_message_preview".into(),
arg::Variant(Box::new(conv.last_message_preview.unwrap_or_default())),
);
map.insert(
"participants".into(),
arg::Variant(Box::new(
conv.participants
.into_iter()
.map(|p| p.display_name())
.collect::<Vec<String>>(),
)),
);
map.insert(
"date".into(),
arg::Variant(Box::new(conv.date.and_utc().timestamp())),
);
map
})
.collect()
}) })
} }
@@ -79,7 +108,11 @@ impl DbusRepository for ServerImpl {
self.send_event_sync(|r| Event::SyncConversation(conversation_id, r)) self.send_event_sync(|r| Event::SyncConversation(conversation_id, r))
} }
fn get_messages(&mut self, conversation_id: String, last_message_id: String) -> Result<Vec<arg::PropMap>, dbus::MethodErr> { fn get_messages(
&mut self,
conversation_id: String,
last_message_id: String,
) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
let last_message_id_opt = if last_message_id.is_empty() { let last_message_id_opt = if last_message_id.is_empty() {
None None
} else { } else {
@@ -88,14 +121,23 @@ impl DbusRepository for ServerImpl {
self.send_event_sync(|r| Event::GetMessages(conversation_id, last_message_id_opt, r)) self.send_event_sync(|r| Event::GetMessages(conversation_id, last_message_id_opt, r))
.map(|messages| { .map(|messages| {
messages.into_iter().map(|msg| { messages
let mut map = arg::PropMap::new(); .into_iter()
map.insert("id".into(), arg::Variant(Box::new(msg.id))); .map(|msg| {
map.insert("text".into(), arg::Variant(Box::new(msg.text))); let mut map = arg::PropMap::new();
map.insert("date".into(), arg::Variant(Box::new(msg.date.and_utc().timestamp()))); map.insert("id".into(), arg::Variant(Box::new(msg.id)));
map.insert("sender".into(), arg::Variant(Box::new(msg.sender.display_name()))); map.insert("text".into(), arg::Variant(Box::new(msg.text)));
map map.insert(
}).collect() "date".into(),
arg::Variant(Box::new(msg.date.and_utc().timestamp())),
);
map.insert(
"sender".into(),
arg::Variant(Box::new(msg.sender.display_name())),
);
map
})
.collect()
}) })
} }
@@ -103,21 +145,35 @@ impl DbusRepository for ServerImpl {
self.send_event_sync(Event::DeleteAllConversations) self.send_event_sync(Event::DeleteAllConversations)
} }
fn send_message(&mut self, conversation_id: String, text: String) -> Result<String, dbus::MethodErr> { fn send_message(
&mut self,
conversation_id: String,
text: String,
) -> Result<String, dbus::MethodErr> {
self.send_event_sync(|r| Event::SendMessage(conversation_id, text, r)) self.send_event_sync(|r| Event::SendMessage(conversation_id, text, r))
.map(|uuid| uuid.to_string()) .map(|uuid| uuid.to_string())
} }
fn get_attachment(
&mut self,
attachment_id: String,
) -> Result<dbus::Path<'static>, dbus::MethodErr> {
todo!()
}
} }
impl DbusSettings for ServerImpl { impl DbusSettings for ServerImpl {
fn set_server(&mut self, url: String, user: String) -> Result<(), dbus::MethodErr> { fn set_server(&mut self, url: String, user: String) -> Result<(), dbus::MethodErr> {
self.send_event_sync(|r| self.send_event_sync(|r| {
Event::UpdateSettings(Settings { Event::UpdateSettings(
server_url: Some(url), Settings {
username: Some(user), server_url: Some(url),
token: None, username: Some(user),
}, r) token: None,
) },
r,
)
})
} }
fn server_url(&self) -> Result<String, dbus::MethodErr> { fn server_url(&self) -> Result<String, dbus::MethodErr> {
@@ -126,13 +182,16 @@ impl DbusSettings for ServerImpl {
} }
fn set_server_url(&self, value: String) -> Result<(), dbus::MethodErr> { fn set_server_url(&self, value: String) -> Result<(), dbus::MethodErr> {
self.send_event_sync(|r| self.send_event_sync(|r| {
Event::UpdateSettings(Settings { Event::UpdateSettings(
server_url: Some(value), Settings {
username: None, server_url: Some(value),
token: None, username: None,
}, r) token: None,
) },
r,
)
})
} }
fn username(&self) -> Result<String, dbus::MethodErr> { fn username(&self) -> Result<String, dbus::MethodErr> {
@@ -141,13 +200,32 @@ impl DbusSettings for ServerImpl {
} }
fn set_username(&self, value: String) -> Result<(), dbus::MethodErr> { fn set_username(&self, value: String) -> Result<(), dbus::MethodErr> {
self.send_event_sync(|r| self.send_event_sync(|r| {
Event::UpdateSettings(Settings { Event::UpdateSettings(
server_url: None, Settings {
username: Some(value), server_url: None,
token: None, username: Some(value),
}, r) token: None,
) },
r,
)
})
}
}
impl DbusAttachment for Attachment {
fn file_path(&self) -> Result<String, dbus::MethodErr> {
Ok(self.path.as_os_str().to_os_string().into_string().unwrap())
}
fn downloaded(&self) -> Result<bool, dbus::MethodErr> {
Ok(self.downloaded)
}
fn delete(&mut self) -> Result<(), dbus::MethodErr> {
// Mostly a placeholder method because dbuscodegen for some reason barfs on this
// if there are no methods defined.
todo!()
} }
} }