@ -5,11 +5,9 @@ use serde::{Deserialize, Serialize};
use anyhow ::{ anyhow , Error } ;
use std ::cell ::RefCell ;
use std ::fs ::remove_dir_all ;
use std ::sync ::{ Arc , Mutex , MutexGuard } ;
use crate ::model ::{ message ::Message , room ::Room } ;
use crate ::util ::cache_dir_path ;
use matrix_sdk ::identifiers ::{ DeviceId , UserId } ;
@ -26,49 +24,13 @@ pub struct AppState {
pub device_id : Box < DeviceId > ,
}
/// Backend Room model but without the list of messages
/// The list of messages is stored in cache with the Msg
/// struct and this struct defines a convenience method
/// to get a Vec<Msg>
#[ derive(Serialize, Deserialize) ]
pub struct AppRoom {
pub room : RefCell < Room > ,
}
/// Message stored in the cache
#[ derive(Serialize, Deserialize) ]
pub struct AppMsg {
pub msg : Message ,
}
impl Model for AppState {
fn key ( & self ) -> String {
"state" . to_string ( )
}
}
impl Model for AppRoom {
fn key ( & self ) -> String {
format! ( "room:{}" , self . room . borrow ( ) . id )
}
}
impl Model for AppMsg {
fn key ( & self ) -> String {
format! (
"msg:{}:{}" ,
self . msg . room ,
self . msg
. id
. as_ref ( )
. map ( | evid | evid . to_string ( ) )
. unwrap_or_default ( ) ,
)
}
}
// Cache
#[ derive(Clone) ]
pub struct FCache {
cache : Arc < Mutex < Option < Cache > > > ,
@ -99,35 +61,6 @@ impl FCache {
remove_dir_all ( fname ) . or_else ( | _ | Err ( anyhow ! ( "Can't remove cache file" ) ) )
}
pub fn get_rooms ( & self ) -> Result < Vec < Room > , Error > {
let cache = self . get_store ( ) ;
let rooms = AppRoom ::all ( cache . as_ref ( ) . unwrap ( ) , "room" ) ?
. iter ( )
. map ( | r | r . room . borrow ( ) . clone ( ) )
. collect ( ) ;
Ok ( rooms )
}
pub fn save_rooms ( & self , rooms : Vec < Room > ) -> Result < ( ) , Error > {
for r in rooms {
self . save_room ( r ) ? ;
}
Ok ( ( ) )
}
pub fn save_room ( & self , room : Room ) -> Result < ( ) , Error > {
let cache = self . get_store ( ) ;
let mut stored_room = room ;
// Don't store typing notifications
stored_room . typing_users . clear ( ) ;
let approom = AppRoom {
room : RefCell ::new ( stored_room ) ,
} ;
approom . store ( cache . as_ref ( ) . unwrap ( ) ) ? ;
Ok ( ( ) )
}
pub fn get_st ( & self ) -> Result < AppState , Error > {
let cache = self . get_store ( ) ;
AppState ::get ( cache . as_ref ( ) . unwrap ( ) , "state" )