Commit 845ddb4a authored by Daniel Garcia Moreno's avatar Daniel Garcia Moreno

Merge branch 'since' into 'master'

Continue sync when reopeing fractal (makes first sync much faster)

See merge request World/fractal!232
parents 861f8b24 6ee0a66c
Pipeline #32133 canceled with stages
...@@ -18,7 +18,7 @@ impl AppOp { ...@@ -18,7 +18,7 @@ impl AppOp {
pub fn sync(&mut self) { pub fn sync(&mut self) {
if !self.syncing && self.logged_in { if !self.syncing && self.logged_in {
self.syncing = true; self.syncing = true;
self.backend.send(BKCommand::Sync).unwrap(); self.backend.send(BKCommand::Sync(self.since.clone())).unwrap();
} }
} }
......
...@@ -39,7 +39,7 @@ impl Backend { ...@@ -39,7 +39,7 @@ impl Backend {
scalar_token: None, scalar_token: None,
scalar_url: String::from("https://scalar.vector.im"), scalar_url: String::from("https://scalar.vector.im"),
sticker_widget: None, sticker_widget: None,
since: String::from(""), since: None,
rooms_since: String::from(""), rooms_since: String::from(""),
join_to_room: String::from(""), join_to_room: String::from(""),
m_direct: HashMap::new(), m_direct: HashMap::new(),
...@@ -179,8 +179,8 @@ impl Backend { ...@@ -179,8 +179,8 @@ impl Backend {
// Sync module // Sync module
Ok(BKCommand::Sync) => { Ok(BKCommand::Sync(since)) => {
let r = sync::sync(self); let r = sync::sync(self, since);
bkerror!(r, tx, BKResponse::SyncError); bkerror!(r, tx, BKResponse::SyncError);
} }
Ok(BKCommand::SyncForced) => { Ok(BKCommand::SyncForced) => {
......
...@@ -30,7 +30,7 @@ pub fn guest(bk: &Backend, server: String) -> Result<(), Error> { ...@@ -30,7 +30,7 @@ pub fn guest(bk: &Backend, server: String) -> Result<(), Error> {
let tk = String::from(r["access_token"].as_str().unwrap_or("")); let tk = String::from(r["access_token"].as_str().unwrap_or(""));
data.lock().unwrap().user_id = uid.clone(); data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone(); data.lock().unwrap().access_token = tk.clone();
data.lock().unwrap().since = String::from(""); data.lock().unwrap().since = None;
tx.send(BKResponse::Token(uid, tk)).unwrap(); tx.send(BKResponse::Token(uid, tk)).unwrap();
tx.send(BKResponse::Rooms(vec![], None)).unwrap(); tx.send(BKResponse::Rooms(vec![], None)).unwrap();
}, },
...@@ -88,7 +88,7 @@ pub fn login(bk: &Backend, user: String, password: String, server: String) -> Re ...@@ -88,7 +88,7 @@ pub fn login(bk: &Backend, user: String, password: String, server: String) -> Re
} else { } else {
data.lock().unwrap().user_id = uid.clone(); data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone(); data.lock().unwrap().access_token = tk.clone();
data.lock().unwrap().since = String::new(); data.lock().unwrap().since = None;
tx.send(BKResponse::Token(uid, tk)).unwrap(); tx.send(BKResponse::Token(uid, tk)).unwrap();
} }
}, },
...@@ -103,7 +103,7 @@ pub fn set_token(bk: &Backend, token: String, uid: String, server: String) -> Re ...@@ -103,7 +103,7 @@ pub fn set_token(bk: &Backend, token: String, uid: String, server: String) -> Re
bk.data.lock().unwrap().server_url = s; bk.data.lock().unwrap().server_url = s;
bk.data.lock().unwrap().access_token = token.clone(); bk.data.lock().unwrap().access_token = token.clone();
bk.data.lock().unwrap().user_id = uid.clone(); bk.data.lock().unwrap().user_id = uid.clone();
bk.data.lock().unwrap().since = String::new(); bk.data.lock().unwrap().since = None;
bk.tx.send(BKResponse::Token(uid, token)).unwrap(); bk.tx.send(BKResponse::Token(uid, token)).unwrap();
Ok(()) Ok(())
...@@ -119,7 +119,7 @@ pub fn logout(bk: &Backend) -> Result<(), Error> { ...@@ -119,7 +119,7 @@ pub fn logout(bk: &Backend) -> Result<(), Error> {
|_| { |_| {
data.lock().unwrap().user_id = String::new(); data.lock().unwrap().user_id = String::new();
data.lock().unwrap().access_token = String::new(); data.lock().unwrap().access_token = String::new();
data.lock().unwrap().since = String::new(); data.lock().unwrap().since = None;
tx.send(BKResponse::Logout).unwrap(); tx.send(BKResponse::Logout).unwrap();
}, },
|err| { tx.send(BKResponse::LogoutError(err)).unwrap() } |err| { tx.send(BKResponse::LogoutError(err)).unwrap() }
...@@ -148,7 +148,7 @@ pub fn register(bk: &Backend, user: String, password: String, server: String) -> ...@@ -148,7 +148,7 @@ pub fn register(bk: &Backend, user: String, password: String, server: String) ->
data.lock().unwrap().user_id = uid.clone(); data.lock().unwrap().user_id = uid.clone();
data.lock().unwrap().access_token = tk.clone(); data.lock().unwrap().access_token = tk.clone();
data.lock().unwrap().since = String::from(""); data.lock().unwrap().since = None;
tx.send(BKResponse::Token(uid, tk)).unwrap(); tx.send(BKResponse::Token(uid, tk)).unwrap();
}, },
|err| { tx.send(BKResponse::LoginError(err)).unwrap() } |err| { tx.send(BKResponse::LoginError(err)).unwrap() }
......
...@@ -11,13 +11,13 @@ use backend::types::BKResponse; ...@@ -11,13 +11,13 @@ use backend::types::BKResponse;
use backend::types::Backend; use backend::types::Backend;
use types::Room; use types::Room;
pub fn sync(bk: &Backend) -> Result<(), Error> { pub fn sync(bk: &Backend, new_since: Option<String>) -> Result<(), Error> {
let tk = bk.data.lock().unwrap().access_token.clone(); let tk = bk.data.lock().unwrap().access_token.clone();
if tk.is_empty() { if tk.is_empty() {
return Err(Error::BackendError); return Err(Error::BackendError);
} }
let since = bk.data.lock().unwrap().since.clone(); let mut since = bk.data.lock().unwrap().since.clone().or(new_since);
let userid = bk.data.lock().unwrap().user_id.clone(); let userid = bk.data.lock().unwrap().user_id.clone();
let mut params: Vec<(&str, String)> = vec![]; let mut params: Vec<(&str, String)> = vec![];
...@@ -25,7 +25,11 @@ pub fn sync(bk: &Backend) -> Result<(), Error> { ...@@ -25,7 +25,11 @@ pub fn sync(bk: &Backend) -> Result<(), Error> {
let timeout; let timeout;
if since.is_empty() { if let Some(since) = since.clone() {
params.push(("since", since));
params.push(("timeout", String::from("30000")));
timeout = 30;
} else {
let filter = format!("{{ let filter = format!("{{
\"room\": {{ \"room\": {{
\"state\": {{ \"state\": {{
...@@ -46,10 +50,6 @@ pub fn sync(bk: &Backend) -> Result<(), Error> { ...@@ -46,10 +50,6 @@ pub fn sync(bk: &Backend) -> Result<(), Error> {
params.push(("filter", String::from(filter))); params.push(("filter", String::from(filter)));
params.push(("timeout", String::from("0"))); params.push(("timeout", String::from("0")));
timeout = 0; timeout = 0;
} else {
params.push(("since", since.clone()));
params.push(("timeout", String::from("30000")));
timeout = 30;
} }
let baseu = bk.get_base_url()?; let baseu = bk.get_base_url()?;
...@@ -64,26 +64,7 @@ pub fn sync(bk: &Backend) -> Result<(), Error> { ...@@ -64,26 +64,7 @@ pub fn sync(bk: &Backend) -> Result<(), Error> {
match json_q("get", &url, &attrs, timeout) { match json_q("get", &url, &attrs, timeout) {
Ok(r) => { Ok(r) => {
let next_batch = String::from(r["next_batch"].as_str().unwrap_or("")); let next_batch = String::from(r["next_batch"].as_str().unwrap_or(""));
if since.is_empty() { if let Some(since) = since {
data.lock().unwrap().m_direct = parse_m_direct(&r);
let rooms = match get_rooms_from_json(&r, &userid, &baseu) {
Ok(rs) => rs,
Err(err) => {
tx.send(BKResponse::SyncError(err)).unwrap();
vec![]
}
};
let mut def: Option<Room> = None;
let jtr = data.lock().unwrap().join_to_room.clone();
if !jtr.is_empty() {
if let Some(r) = rooms.iter().find(|x| x.id == jtr) {
def = Some(r.clone());
}
}
tx.send(BKResponse::Rooms(rooms, def)).unwrap();
} else {
// New rooms // New rooms
match get_rooms_from_json(&r, &userid, &baseu) { match get_rooms_from_json(&r, &userid, &baseu) {
Ok(rs) => tx.send(BKResponse::NewRooms(rs)).unwrap(), Ok(rs) => tx.send(BKResponse::NewRooms(rs)).unwrap(),
...@@ -134,10 +115,33 @@ pub fn sync(bk: &Backend) -> Result<(), Error> { ...@@ -134,10 +115,33 @@ pub fn sync(bk: &Backend) -> Result<(), Error> {
} }
} }
}; };
} else {
data.lock().unwrap().m_direct = parse_m_direct(&r);
let rooms = match get_rooms_from_json(&r, &userid, &baseu) {
Ok(rs) => rs,
Err(err) => {
tx.send(BKResponse::SyncError(err)).unwrap();
vec![]
}
};
let mut def: Option<Room> = None;
let jtr = data.lock().unwrap().join_to_room.clone();
if !jtr.is_empty() {
if let Some(r) = rooms.iter().find(|x| x.id == jtr) {
def = Some(r.clone());
}
}
tx.send(BKResponse::Rooms(rooms, def)).unwrap();
} }
tx.send(BKResponse::Sync(next_batch.clone())).unwrap(); tx.send(BKResponse::Sync(next_batch.clone())).unwrap();
data.lock().unwrap().since = next_batch; data.lock().unwrap().since = if !next_batch.is_empty() {
Some(next_batch)
} else {
None
}
}, },
Err(err) => { Err(err) => {
// we wait if there's an error to avoid 100% CPU // we wait if there's an error to avoid 100% CPU
...@@ -154,6 +158,6 @@ pub fn sync(bk: &Backend) -> Result<(), Error> { ...@@ -154,6 +158,6 @@ pub fn sync(bk: &Backend) -> Result<(), Error> {
} }
pub fn force_sync(bk: &Backend) -> Result<(), Error> { pub fn force_sync(bk: &Backend) -> Result<(), Error> {
bk.data.lock().unwrap().since = String::from(""); bk.data.lock().unwrap().since = None;
sync(bk) sync(bk, None)
} }
...@@ -37,7 +37,7 @@ pub enum BKCommand { ...@@ -37,7 +37,7 @@ pub enum BKCommand {
AccountDestruction(String, String, bool), AccountDestruction(String, String, bool),
GetAvatar, GetAvatar,
SetUserAvatar(String), SetUserAvatar(String),
Sync, Sync(Option<String>),
SyncForced, SyncForced,
GetRoomMembers(String), GetRoomMembers(String),
GetRoomMessages(String), GetRoomMessages(String),
...@@ -187,7 +187,7 @@ pub struct BackendData { ...@@ -187,7 +187,7 @@ pub struct BackendData {
pub scalar_token: Option<String>, pub scalar_token: Option<String>,
pub scalar_url: String, pub scalar_url: String,
pub sticker_widget: Option<String>, pub sticker_widget: Option<String>,
pub since: String, pub since: Option<String>,
pub rooms_since: String, pub rooms_since: String,
pub join_to_room: String, pub join_to_room: String,
pub m_direct: HashMap<String, Vec<String>>, pub m_direct: HashMap<String, Vec<String>>,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment