matrix-sdk: Initial support to upload filters.

master
Damir Jelić 2020-11-22 21:25:31 +01:00
parent a98f23e2a7
commit c1383402ed
3 changed files with 50 additions and 1 deletions

View File

@ -67,6 +67,7 @@ use matrix_sdk_common::{
account::register, account::register,
device::{delete_devices, get_devices}, device::{delete_devices, get_devices},
directory::{get_public_rooms, get_public_rooms_filtered}, directory::{get_public_rooms, get_public_rooms_filtered},
filter::{create_filter::Request as FilterUploadRequest, FilterDefinition},
media::create_content, media::create_content,
membership::{ membership::{
ban_user, forget_room, get_member_events, ban_user, forget_room, get_member_events,
@ -606,6 +607,27 @@ impl Client {
self.send(request).await self.send(request).await
} }
/// Get or upload a sync filter.
pub async fn get_or_upload_filter(
&self,
filter_name: &str,
definition: FilterDefinition<'_>,
) -> Result<String> {
if let Some(filter) = self.base_client.get_filter(filter_name).await {
Ok(filter)
} else {
let user_id = self.user_id().await.ok_or(Error::AuthenticationRequired)?;
let request = FilterUploadRequest::new(&user_id, definition);
let response = self.send(request).await?;
self.base_client
.receive_filter_upload(filter_name, &response)
.await;
Ok(response.filter_id)
}
}
/// Join a room by `RoomId`. /// Join a room by `RoomId`.
/// ///
/// Returns a `join_room_by_id::Response` consisting of the /// Returns a `join_room_by_id::Response` consisting of the

View File

@ -49,7 +49,7 @@ use matrix_sdk_crypto::{
Device, EncryptionSettings, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas, Device, EncryptionSettings, IncomingResponse, OlmError, OlmMachine, OutgoingRequest, Sas,
ToDeviceRequest, UserDevices, ToDeviceRequest, UserDevices,
}; };
use tracing::{info, warn}; use tracing::info;
use zeroize::Zeroizing; use zeroize::Zeroizing;
use crate::{ use crate::{
@ -629,6 +629,20 @@ impl BaseClient {
Ok(()) Ok(())
} }
pub async fn receive_filter_upload(
&self,
filter_name: &str,
response: &api::filter::create_filter::Response,
) {
self.store
.save_filter(filter_name, &response.filter_id)
.await;
}
pub async fn get_filter(&self, filter_name: &str) -> Option<String> {
self.store.get_filter(filter_name).await
}
/// Should the client share a group session for the given room. /// Should the client share a group session for the given room.
/// ///
/// Returns true if a session needs to be shared before room messages can be /// Returns true if a session needs to be shared before room messages can be

View File

@ -452,6 +452,19 @@ impl Store {
Store::open_helper(db) Store::open_helper(db)
} }
pub async fn save_filter(&self, filter_name: &str, filter_id: &str) {
self.session
.insert(&format!("filter{}", filter_name), filter_id)
.unwrap();
}
pub async fn get_filter(&self, filter_name: &str) -> Option<String> {
self.session
.get(&format!("filter{}", filter_name))
.unwrap()
.map(|f| String::from_utf8_lossy(&f).to_string())
}
pub async fn save_changes(&self, changes: &StateChanges) { pub async fn save_changes(&self, changes: &StateChanges) {
let ret: TransactionResult<()> = ( let ret: TransactionResult<()> = (
&self.session, &self.session,