daemon: adds conversation list limit, fixes auth saving in db auth store
This commit is contained in:
@@ -11,6 +11,9 @@
|
||||
<!-- Conversations -->
|
||||
|
||||
<method name="GetConversations">
|
||||
<arg type="i" name="limit" direction="in"/>
|
||||
<arg type="i" name="offset" direction="in"/>
|
||||
|
||||
<arg type="aa{sv}" direction="out" name="conversations">
|
||||
<annotation name="org.freedesktop.DBus.DocString"
|
||||
value="Array of dictionaries. Each dictionary has keys:
|
||||
|
||||
@@ -54,10 +54,10 @@ impl AuthenticationStore for DatabaseAuthenticationStore {
|
||||
}).await
|
||||
}
|
||||
|
||||
async fn get_token(&mut self) -> Option<JwtToken> {
|
||||
async fn get_token(&mut self) -> Option<String> {
|
||||
self.database.lock().await
|
||||
.with_settings(|settings| {
|
||||
match settings.get::<JwtToken>(SettingsKey::TOKEN) {
|
||||
match settings.get::<String>(SettingsKey::TOKEN) {
|
||||
Ok(token) => token,
|
||||
Err(e) => {
|
||||
log::warn!("Failed to get token from settings: {}", e);
|
||||
@@ -67,7 +67,7 @@ impl AuthenticationStore for DatabaseAuthenticationStore {
|
||||
}).await
|
||||
}
|
||||
|
||||
async fn set_token(&mut self, token: JwtToken) {
|
||||
async fn set_token(&mut self, token: String) {
|
||||
self.database.lock().await
|
||||
.with_settings(|settings| settings.put(SettingsKey::TOKEN, &token)).await.unwrap_or_else(|e| {
|
||||
log::error!("Failed to set token: {}", e);
|
||||
|
||||
@@ -21,7 +21,10 @@ pub enum Event {
|
||||
SyncConversation(String, Reply<()>),
|
||||
|
||||
/// Returns all known conversations from the database.
|
||||
GetAllConversations(Reply<Vec<Conversation>>),
|
||||
/// Parameters:
|
||||
/// - limit: The maximum number of conversations to return. (-1 for no limit)
|
||||
/// - offset: The offset into the conversation list to start returning conversations from.
|
||||
GetAllConversations(i32, i32, Reply<Vec<Conversation>>),
|
||||
|
||||
/// Returns all known settings from the database.
|
||||
GetAllSettings(Reply<Settings>),
|
||||
|
||||
@@ -175,8 +175,8 @@ impl Daemon {
|
||||
reply.send(()).unwrap();
|
||||
},
|
||||
|
||||
Event::GetAllConversations(reply) => {
|
||||
let conversations = self.get_conversations().await;
|
||||
Event::GetAllConversations(limit, offset, reply) => {
|
||||
let conversations = self.get_conversations_limit_offset(limit, offset).await;
|
||||
reply.send(conversations).unwrap();
|
||||
},
|
||||
|
||||
@@ -226,7 +226,11 @@ impl Daemon {
|
||||
}
|
||||
|
||||
async fn get_conversations(&mut self) -> Vec<Conversation> {
|
||||
self.database.lock().await.with_repository(|r| r.all_conversations().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> {
|
||||
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> {
|
||||
|
||||
@@ -17,14 +17,21 @@ pub struct Settings {
|
||||
|
||||
impl Settings {
|
||||
pub fn from_db(db_settings: &mut DbSettings) -> Result<Self> {
|
||||
let server_url: Option<String> = db_settings.get(keys::SERVER_URL)?;
|
||||
let username: Option<String> = db_settings.get(keys::USERNAME)?;
|
||||
let token: Option<String> = db_settings.get(keys::TOKEN)?;
|
||||
Ok(Self {
|
||||
let server_url = db_settings.get(keys::SERVER_URL)?;
|
||||
let username = db_settings.get(keys::USERNAME)?;
|
||||
let token = db_settings.get(keys::TOKEN)?;
|
||||
|
||||
// Create the settings struct with the results
|
||||
let settings = Self {
|
||||
server_url,
|
||||
username,
|
||||
token,
|
||||
})
|
||||
};
|
||||
|
||||
// Load bearing
|
||||
log::debug!("Loaded settings: {:?}", settings);
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
pub fn save(&self, db_settings: &mut DbSettings) -> Result<()> {
|
||||
|
||||
@@ -42,11 +42,13 @@ impl UpdateMonitor {
|
||||
match update {
|
||||
UpdateEvent::ConversationChanged(conversation) => {
|
||||
log::info!(target: target::UPDATES, "Conversation changed: {:?}", conversation);
|
||||
log::info!(target: target::UPDATES, "Syncing new messages for conversation id: {}", conversation.guid);
|
||||
self.send_event(|r| Event::SyncConversation(conversation.guid, r)).await
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to send daemon event: {}", e);
|
||||
});
|
||||
if conversation.unread_count > 0 {
|
||||
log::info!(target: target::UPDATES, "Syncing new messages for conversation id: {}", conversation.guid);
|
||||
self.send_event(|r| Event::SyncConversation(conversation.guid, r)).await
|
||||
.unwrap_or_else(|e| {
|
||||
log::error!("Failed to send daemon event: {}", e);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
UpdateEvent::MessageReceived(conversation, message) => {
|
||||
|
||||
@@ -51,8 +51,8 @@ impl DbusRepository for ServerImpl {
|
||||
self.send_event_sync(Event::GetVersion)
|
||||
}
|
||||
|
||||
fn get_conversations(&mut self) -> Result<Vec<arg::PropMap>, dbus::MethodErr> {
|
||||
self.send_event_sync(Event::GetAllConversations)
|
||||
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))
|
||||
.map(|conversations| {
|
||||
conversations.into_iter().map(|conv| {
|
||||
let mut map = arg::PropMap::new();
|
||||
|
||||
Reference in New Issue
Block a user