|
|
@ -95,15 +95,7 @@ pub async fn sync_events_route(
|
|
|
|
|
|
|
|
|
|
|
|
// Database queries:
|
|
|
|
// Database queries:
|
|
|
|
|
|
|
|
|
|
|
|
let current_state = db.rooms.room_state_full(&room_id)?;
|
|
|
|
let current_state_hash = db.rooms.current_state_hash(&room_id)?;
|
|
|
|
let current_members = current_state
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter(|(key, _)| key.0 == EventType::RoomMember)
|
|
|
|
|
|
|
|
.map(|(key, value)| (&key.1, value)) // Only keep state key
|
|
|
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let encrypted_room = current_state
|
|
|
|
|
|
|
|
.get(&(EventType::RoomEncryption, "".to_owned()))
|
|
|
|
|
|
|
|
.is_some();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// These type is Option<Option<_>>. The outer Option is None when there is no event between
|
|
|
|
// These type is Option<Option<_>>. The outer Option is None when there is no event between
|
|
|
|
// since and the current room state, meaning there should be no updates.
|
|
|
|
// since and the current room state, meaning there should be no updates.
|
|
|
@ -115,6 +107,22 @@ pub async fn sync_events_route(
|
|
|
|
.as_ref()
|
|
|
|
.as_ref()
|
|
|
|
.map(|pdu| db.rooms.pdu_state_hash(&pdu.as_ref().ok()?.0).ok()?);
|
|
|
|
.map(|pdu| db.rooms.pdu_state_hash(&pdu.as_ref().ok()?.0).ok()?);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (
|
|
|
|
|
|
|
|
heroes,
|
|
|
|
|
|
|
|
joined_member_count,
|
|
|
|
|
|
|
|
invited_member_count,
|
|
|
|
|
|
|
|
joined_since_last_sync,
|
|
|
|
|
|
|
|
state_events,
|
|
|
|
|
|
|
|
) = if since_state_hash != None && Some(¤t_state_hash) != since_state_hash.as_ref() {
|
|
|
|
|
|
|
|
let current_state = db.rooms.room_state_full(&room_id)?;
|
|
|
|
|
|
|
|
let current_members = current_state
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter(|(key, _)| key.0 == EventType::RoomMember)
|
|
|
|
|
|
|
|
.map(|(key, value)| (&key.1, value)) // Only keep state key
|
|
|
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let encrypted_room = current_state
|
|
|
|
|
|
|
|
.get(&(EventType::RoomEncryption, "".to_owned()))
|
|
|
|
|
|
|
|
.is_some();
|
|
|
|
let since_state = since_state_hash.as_ref().map(|state_hash| {
|
|
|
|
let since_state = since_state_hash.as_ref().map(|state_hash| {
|
|
|
|
state_hash
|
|
|
|
state_hash
|
|
|
|
.as_ref()
|
|
|
|
.as_ref()
|
|
|
@ -233,13 +241,6 @@ pub async fn sync_events_route(
|
|
|
|
);
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Look for device list updates in this room
|
|
|
|
|
|
|
|
device_list_updates.extend(
|
|
|
|
|
|
|
|
db.users
|
|
|
|
|
|
|
|
.keys_changed(&room_id.to_string(), since, None)
|
|
|
|
|
|
|
|
.filter_map(|r| r.ok()),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let (joined_member_count, invited_member_count, heroes) = if send_member_count {
|
|
|
|
let (joined_member_count, invited_member_count, heroes) = if send_member_count {
|
|
|
|
let joined_member_count = db.rooms.room_members(&room_id).count();
|
|
|
|
let joined_member_count = db.rooms.room_members(&room_id).count();
|
|
|
|
let invited_member_count = db.rooms.room_members_invited(&room_id).count();
|
|
|
|
let invited_member_count = db.rooms.room_members_invited(&room_id).count();
|
|
|
@ -262,10 +263,13 @@ pub async fn sync_events_route(
|
|
|
|
>(pdu.content.clone())
|
|
|
|
>(pdu.content.clone())
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.deserialize()
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
|
|
|
.map_err(|_| {
|
|
|
|
|
|
|
|
Error::bad_database("Invalid member event in database.")
|
|
|
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(state_key) = &pdu.state_key {
|
|
|
|
if let Some(state_key) = &pdu.state_key {
|
|
|
|
let user_id = UserId::try_from(state_key.clone()).map_err(|_| {
|
|
|
|
let user_id =
|
|
|
|
|
|
|
|
UserId::try_from(state_key.clone()).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid UserId in member PDU.")
|
|
|
|
Error::bad_database("Invalid UserId in member PDU.")
|
|
|
|
})?;
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
|
@ -305,6 +309,53 @@ pub async fn sync_events_route(
|
|
|
|
(None, None, Vec::new())
|
|
|
|
(None, None, Vec::new())
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let state_events = if joined_since_last_sync {
|
|
|
|
|
|
|
|
db.rooms
|
|
|
|
|
|
|
|
.room_state_full(&room_id)?
|
|
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
|
|
.map(|(_, pdu)| pdu.to_sync_state_event())
|
|
|
|
|
|
|
|
.collect()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
match since_state {
|
|
|
|
|
|
|
|
None => Vec::new(),
|
|
|
|
|
|
|
|
Some(Some(since_state)) => current_state
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter(|(key, value)| {
|
|
|
|
|
|
|
|
since_state.get(key).map(|e| &e.event_id) != Some(&value.event_id)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.filter(|(_, value)| {
|
|
|
|
|
|
|
|
!timeline_pdus.iter().any(|(_, timeline_pdu)| {
|
|
|
|
|
|
|
|
timeline_pdu.kind == value.kind
|
|
|
|
|
|
|
|
&& timeline_pdu.state_key == value.state_key
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.map(|(_, pdu)| pdu.to_sync_state_event())
|
|
|
|
|
|
|
|
.collect(),
|
|
|
|
|
|
|
|
Some(None) => current_state
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.map(|(_, pdu)| pdu.to_sync_state_event())
|
|
|
|
|
|
|
|
.collect(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(
|
|
|
|
|
|
|
|
heroes,
|
|
|
|
|
|
|
|
joined_member_count,
|
|
|
|
|
|
|
|
invited_member_count,
|
|
|
|
|
|
|
|
joined_since_last_sync,
|
|
|
|
|
|
|
|
state_events,
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
(Vec::new(), None, None, false, Vec::new())
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Look for device list updates in this room
|
|
|
|
|
|
|
|
device_list_updates.extend(
|
|
|
|
|
|
|
|
db.users
|
|
|
|
|
|
|
|
.keys_changed(&room_id.to_string(), since, None)
|
|
|
|
|
|
|
|
.filter_map(|r| r.ok()),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let notification_count = if send_notification_counts {
|
|
|
|
let notification_count = if send_notification_counts {
|
|
|
|
if let Some(last_read) = db.rooms.edus.private_read_get(&room_id, &sender_user)? {
|
|
|
|
if let Some(last_read) = db.rooms.edus.private_read_get(&room_id, &sender_user)? {
|
|
|
|
Some(
|
|
|
|
Some(
|
|
|
@ -385,34 +436,7 @@ pub async fn sync_events_route(
|
|
|
|
events: room_events,
|
|
|
|
events: room_events,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
state: sync_events::State {
|
|
|
|
state: sync_events::State {
|
|
|
|
events: if joined_since_last_sync {
|
|
|
|
events: state_events,
|
|
|
|
db.rooms
|
|
|
|
|
|
|
|
.room_state_full(&room_id)?
|
|
|
|
|
|
|
|
.into_iter()
|
|
|
|
|
|
|
|
.map(|(_, pdu)| pdu.to_sync_state_event())
|
|
|
|
|
|
|
|
.collect()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
match since_state {
|
|
|
|
|
|
|
|
None => Vec::new(),
|
|
|
|
|
|
|
|
Some(Some(since_state)) => current_state
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter(|(key, value)| {
|
|
|
|
|
|
|
|
since_state.get(key).map(|e| &e.event_id) != Some(&value.event_id)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.filter(|(_, value)| {
|
|
|
|
|
|
|
|
!timeline_pdus.iter().any(|(_, timeline_pdu)| {
|
|
|
|
|
|
|
|
timeline_pdu.kind == value.kind
|
|
|
|
|
|
|
|
&& timeline_pdu.state_key == value.state_key
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.map(|(_, pdu)| pdu.to_sync_state_event())
|
|
|
|
|
|
|
|
.collect(),
|
|
|
|
|
|
|
|
Some(None) => current_state
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.map(|(_, pdu)| pdu.to_sync_state_event())
|
|
|
|
|
|
|
|
.collect(),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ephemeral: sync_events::Ephemeral { events: edus },
|
|
|
|
ephemeral: sync_events::Ephemeral { events: edus },
|
|
|
|
};
|
|
|
|
};
|
|
|
|