[fractal] Make SyncRet more natural and self-explanatory
- From: Alejandro Domínguez <aledomu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [fractal] Make SyncRet more natural and self-explanatory
- Date: Wed, 2 Dec 2020 16:16:55 +0000 (UTC)
commit 56611346577638abc6d59b45308c9674009cc93b
Author: Alejandro Domínguez <adomu net-c com>
Date: Wed Dec 2 09:42:55 2020 +0100
Make SyncRet more natural and self-explanatory
fractal-gtk/src/appop/sync.rs | 128 ++++++++++++++++++----------------------
fractal-gtk/src/backend/sync.rs | 90 ++++++++++------------------
2 files changed, 91 insertions(+), 127 deletions(-)
---
diff --git a/fractal-gtk/src/appop/sync.rs b/fractal-gtk/src/appop/sync.rs
index dbd78817..9f036fbc 100644
--- a/fractal-gtk/src/appop/sync.rs
+++ b/fractal-gtk/src/appop/sync.rs
@@ -3,7 +3,7 @@ use crate::util::i18n::i18n;
use crate::app::{App, RUNTIME};
use crate::appop::AppOp;
use crate::backend::{
- sync::{self, RoomElement, SyncRet},
+ sync::{self, RoomElement},
HandleError,
};
@@ -29,7 +29,6 @@ impl AppOp {
let query = sync::sync(
login_data.session_client,
login_data.uid,
- join_to_room,
since,
initial,
number_tries,
@@ -37,79 +36,70 @@ impl AppOp {
.await;
match query {
- Ok(SyncRet::NoSince {
- rooms,
- default,
- next_batch,
- }) => {
- let clear_room_list = true;
- APPOP!(set_rooms, (rooms, clear_room_list));
- // Open the newly joined room
- let jtr = default.as_ref().map(|r| r.id.clone());
- APPOP!(set_join_to_room, (jtr));
- if let Some(room) = default {
- let room_id = room.id;
- APPOP!(set_active_room_by_id, (room_id));
- }
-
- let s = Some(next_batch);
- APPOP!(synced, (s));
- }
- Ok(SyncRet::WithSince {
- update_rooms,
- room_notifications,
- update_rooms_2,
- other,
- next_batch,
- }) => {
- let clear_room_list = false;
- let msgs: Vec<_> = update_rooms
- .iter()
- .flat_map(|r| &r.messages)
- .cloned()
- .collect();
- APPOP!(set_rooms, (update_rooms, clear_room_list));
- APPOP!(show_room_messages, (msgs));
-
- let clear_room_list = false;
- APPOP!(set_rooms, (update_rooms_2, clear_room_list));
+ Ok(sync_ret) => {
+ let clear_room_list = sync_ret.updates.is_none();
+ if let Some(updates) = sync_ret.updates {
+ let rooms = sync_ret.rooms;
+ let msgs: Vec<_> =
+ rooms.iter().flat_map(|r| &r.messages).cloned().collect();
+ APPOP!(set_rooms, (rooms, clear_room_list));
+ APPOP!(show_room_messages, (msgs));
+ let typing_events_as_rooms = updates.typing_events_as_rooms;
+ APPOP!(set_rooms, (typing_events_as_rooms, clear_room_list));
- for (room_id, unread_notifications) in room_notifications {
- let r = room_id;
- let n: u64 = unread_notifications
- .notification_count
- .map(Into::into)
- .unwrap_or_default();
- let h: u64 = unread_notifications
- .highlight_count
- .map(Into::into)
- .unwrap_or_default();
- APPOP!(set_room_notifications, (r, n, h));
- }
+ for (room_id, unread_notifications) in updates.room_notifications {
+ let r = room_id;
+ let n: u64 = unread_notifications
+ .notification_count
+ .map(Into::into)
+ .unwrap_or_default();
+ let h: u64 = unread_notifications
+ .highlight_count
+ .map(Into::into)
+ .unwrap_or_default();
+ APPOP!(set_room_notifications, (r, n, h));
+ }
- for room_element in other {
- match room_element {
- RoomElement::Name(room_id, name) => {
- let n = Some(name);
- APPOP!(room_name_change, (room_id, n));
- }
- RoomElement::Topic(room_id, topic) => {
- let t = Some(topic);
- APPOP!(room_topic_change, (room_id, t));
- }
- RoomElement::NewAvatar(room_id) => {
- APPOP!(new_room_avatar, (room_id));
- }
- RoomElement::MemberEvent(event) => {
- APPOP!(room_member_event, (event));
- }
- RoomElement::RemoveMessage(room_id, msg_id) => {
- APPOP!(remove_message, (room_id, msg_id));
+ for room_element in updates.new_events {
+ match room_element {
+ RoomElement::Name(room_id, name) => {
+ let n = Some(name);
+ APPOP!(room_name_change, (room_id, n));
+ }
+ RoomElement::Topic(room_id, topic) => {
+ let t = Some(topic);
+ APPOP!(room_topic_change, (room_id, t));
+ }
+ RoomElement::NewAvatar(room_id) => {
+ APPOP!(new_room_avatar, (room_id));
+ }
+ RoomElement::MemberEvent(event) => {
+ APPOP!(room_member_event, (event));
+ }
+ RoomElement::RemoveMessage(room_id, msg_id) => {
+ APPOP!(remove_message, (room_id, msg_id));
+ }
}
}
+ } else {
+ let rooms = sync_ret.rooms;
+ let jtr = join_to_room.and_then(|jtr| {
+ rooms
+ .iter()
+ .map(|room| &room.id)
+ .find(|rid| **rid == jtr)
+ .cloned()
+ });
+ APPOP!(set_rooms, (rooms, clear_room_list));
+ // Open the newly joined room
+ let jtr_ = jtr.clone();
+ APPOP!(set_join_to_room, (jtr_));
+ if let Some(room_id) = jtr {
+ APPOP!(set_active_room_by_id, (room_id));
+ }
}
- let s = Some(next_batch);
+ let s = Some(sync_ret.next_batch);
APPOP!(synced, (s));
}
Err(err) => {
diff --git a/fractal-gtk/src/backend/sync.rs b/fractal-gtk/src/backend/sync.rs
index 3d4121e3..a5d58f4d 100644
--- a/fractal-gtk/src/backend/sync.rs
+++ b/fractal-gtk/src/backend/sync.rs
@@ -9,6 +9,7 @@ use matrix_sdk::api::r0::filter::LazyLoadOptions;
use matrix_sdk::api::r0::filter::RoomEventFilter;
use matrix_sdk::api::r0::filter::RoomFilter;
use matrix_sdk::api::r0::sync::sync_events::Filter;
+use matrix_sdk::api::r0::sync::sync_events::JoinedRoom;
use matrix_sdk::api::r0::sync::sync_events::Response;
use matrix_sdk::api::r0::sync::sync_events::UnreadNotificationsCount;
use matrix_sdk::assign;
@@ -24,7 +25,10 @@ use log::{error, warn};
use matrix_sdk::identifiers::{EventId, RoomId, UserId};
use matrix_sdk::Client as MatrixClient;
use matrix_sdk::Error as MatrixError;
-use std::{collections::HashMap, time::Duration};
+use std::{
+ collections::{BTreeMap, HashMap},
+ time::Duration,
+};
use super::{get_ruma_client_error, remove_matrix_access_token_if_present, HandleError};
use crate::app::App;
@@ -53,25 +57,24 @@ impl HandleError for SyncError {
}
}
-pub enum SyncRet {
- NoSince {
- rooms: Vec<Room>,
- default: Option<Room>,
- next_batch: String,
- },
- WithSince {
- update_rooms: Vec<Room>,
- room_notifications: HashMap<RoomId, UnreadNotificationsCount>,
- update_rooms_2: Vec<Room>,
- other: Vec<RoomElement>,
- next_batch: String,
- },
+pub struct SyncRet {
+ // Only new rooms if it's an initial sync
+ pub rooms: Vec<Room>,
+ pub next_batch: String,
+ // None if it's an initial sync
+ pub updates: Option<SyncUpdates>,
+}
+
+pub struct SyncUpdates {
+ pub room_notifications: HashMap<RoomId, UnreadNotificationsCount>,
+ // TODO: Typing events should become RoomElements
+ pub typing_events_as_rooms: Vec<Room>,
+ pub new_events: Vec<RoomElement>,
}
pub async fn sync(
session_client: MatrixClient,
user_id: UserId,
- join_to_room: Option<RoomId>,
since: Option<String>,
initial: bool,
number_tries: u32,
@@ -115,12 +118,7 @@ pub async fn sync(
};
match session_client.sync_once(sync_settings).await {
- Ok(response) => Ok(transform_sync_response(
- response,
- since.is_some(),
- user_id,
- join_to_room,
- )),
+ Ok(response) => Ok(transform_sync_response(response, since.is_some(), user_id)),
Err(err) => {
// we wait if there's an error to avoid 100% CPU
// we wait even longer, if it's a 429 (Too Many Requests) error
@@ -141,47 +139,25 @@ pub async fn sync(
}
}
-fn transform_sync_response(
- response: Response,
- has_since: bool,
- user_id: UserId,
- join_to_room: Option<RoomId>,
-) -> SyncRet {
- if has_since {
- transform_sync_response_with_since(response, user_id)
- } else {
- transform_sync_response_no_since(response, user_id, join_to_room)
- }
-}
-
-fn transform_sync_response_no_since(
- response: Response,
- user_id: UserId,
- join_to_room: Option<RoomId>,
-) -> SyncRet {
- let rooms = Room::from_sync_response(&response, user_id);
- let default = join_to_room.and_then(|jtr| rooms.iter().find(|x| x.id == jtr).cloned());
-
- SyncRet::NoSince {
- rooms,
- default,
+fn transform_sync_response(response: Response, has_since: bool, user_id: UserId) -> SyncRet {
+ SyncRet {
+ rooms: Room::from_sync_response(&response, user_id.clone()),
next_batch: response.next_batch,
+ updates: if !has_since {
+ None
+ } else {
+ Some(get_sync_updates(&response.rooms.join, &user_id))
+ },
}
}
-fn transform_sync_response_with_since(response: Response, user_id: UserId) -> SyncRet {
- let join = &response.rooms.join;
-
- SyncRet::WithSince {
- // New rooms
- update_rooms: Room::from_sync_response(&response, user_id.clone()),
- // Room notifications
+fn get_sync_updates(join: &BTreeMap<RoomId, JoinedRoom>, user_id: &UserId) -> SyncUpdates {
+ SyncUpdates {
room_notifications: join
.iter()
.map(|(k, room)| (k.clone(), room.unread_notifications.clone()))
.collect(),
- // Typing notifications
- update_rooms_2: join
+ typing_events_as_rooms: join
.iter()
.map(|(k, room)| {
let typing: Vec<Member> = room.ephemeral.events
@@ -199,7 +175,7 @@ fn transform_sync_response_with_since(response: Response, user_id: UserId) -> Sy
})
.flatten()
// ignoring the user typing notifications
- .filter(|user| *user != user_id)
+ .filter(|user| user != user_id)
.map(|uid| Member {
uid,
alias: None,
@@ -213,8 +189,7 @@ fn transform_sync_response_with_since(response: Response, user_id: UserId) -> Sy
}
})
.collect(),
- // Other events
- other: join
+ new_events: join
.iter()
.flat_map(|(room_id, room)| {
let room_id = room_id.clone();
@@ -259,6 +234,5 @@ fn transform_sync_response_with_since(response: Response, user_id: UserId) -> Sy
}
})
.collect(),
- next_batch: response.next_batch,
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]