2021-07-14 07:07:08 +00:00
|
|
|
use crate::{database::DatabaseGuard, ConduitResult, Database, Error, Result, Ruma, RumaResponse};
|
2020-07-30 16:14:47 +00:00
|
|
|
use ruma::{
|
2021-06-30 07:52:01 +00:00
|
|
|
api::client::r0::{sync::sync_events, uiaa::UiaaResponse},
|
2020-08-21 19:22:59 +00:00
|
|
|
events::{room::member::MembershipState, AnySyncEphemeralRoomEvent, EventType},
|
2020-12-04 23:16:17 +00:00
|
|
|
serde::Raw,
|
2021-06-30 07:52:01 +00:00
|
|
|
DeviceId, RoomId, UserId,
|
2020-07-30 16:14:47 +00:00
|
|
|
};
|
|
|
|
use std::{
|
2021-07-18 18:43:39 +00:00
|
|
|
collections::{hash_map::Entry, BTreeMap, HashMap, HashSet},
|
2021-04-12 10:40:16 +00:00
|
|
|
convert::{TryFrom, TryInto},
|
2021-06-08 16:10:00 +00:00
|
|
|
sync::Arc,
|
2020-07-30 16:14:47 +00:00
|
|
|
time::Duration,
|
|
|
|
};
|
2021-06-30 07:52:01 +00:00
|
|
|
use tokio::sync::watch::Sender;
|
2021-07-29 06:36:01 +00:00
|
|
|
use tracing::{error, warn};
|
2021-06-30 07:52:01 +00:00
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::{get, tokio};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-08-22 19:38:01 +00:00
|
|
|
/// # `GET /_matrix/client/r0/sync`
|
|
|
|
///
|
|
|
|
/// Synchronize the client's state with the latest state on the server.
|
|
|
|
///
|
|
|
|
/// - This endpoint takes a `since` parameter which should be the `next_batch` value from a
|
|
|
|
/// previous request.
|
|
|
|
/// - Calling this endpoint without a `since` parameter will return all recent events, the state
|
|
|
|
/// of all rooms and more data. This should only be called on the initial login of the device.
|
|
|
|
/// - To get incremental updates, you can call this endpoint with a `since` parameter. This will
|
|
|
|
/// return all recent events, state updates and more data that happened since the last /sync
|
|
|
|
/// request.
|
2020-07-30 16:14:47 +00:00
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
get("/_matrix/client/r0/sync", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-07-30 16:14:47 +00:00
|
|
|
pub async fn sync_events_route(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: DatabaseGuard,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<sync_events::Request<'_>>,
|
2021-06-30 07:52:01 +00:00
|
|
|
) -> std::result::Result<RumaResponse<sync_events::Response>, RumaResponse<UiaaResponse>> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
let arc_db = Arc::new(db);
|
|
|
|
|
|
|
|
let mut rx = match arc_db
|
2021-06-30 07:52:01 +00:00
|
|
|
.globals
|
|
|
|
.sync_receivers
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry((sender_user.clone(), sender_device.clone()))
|
|
|
|
{
|
|
|
|
Entry::Vacant(v) => {
|
|
|
|
let (tx, rx) = tokio::sync::watch::channel(None);
|
|
|
|
|
|
|
|
tokio::spawn(sync_helper_wrapper(
|
2021-07-14 07:07:08 +00:00
|
|
|
Arc::clone(&arc_db),
|
2021-06-30 07:52:01 +00:00
|
|
|
sender_user.clone(),
|
|
|
|
sender_device.clone(),
|
|
|
|
body.since.clone(),
|
|
|
|
body.full_state,
|
|
|
|
body.timeout,
|
|
|
|
tx,
|
|
|
|
));
|
|
|
|
|
|
|
|
v.insert((body.since.clone(), rx)).1.clone()
|
|
|
|
}
|
|
|
|
Entry::Occupied(mut o) => {
|
|
|
|
if o.get().0 != body.since {
|
|
|
|
let (tx, rx) = tokio::sync::watch::channel(None);
|
|
|
|
|
|
|
|
tokio::spawn(sync_helper_wrapper(
|
2021-07-14 07:07:08 +00:00
|
|
|
Arc::clone(&arc_db),
|
2021-06-30 07:52:01 +00:00
|
|
|
sender_user.clone(),
|
|
|
|
sender_device.clone(),
|
|
|
|
body.since.clone(),
|
|
|
|
body.full_state,
|
|
|
|
body.timeout,
|
|
|
|
tx,
|
|
|
|
));
|
|
|
|
|
|
|
|
o.insert((body.since.clone(), rx.clone()));
|
|
|
|
|
|
|
|
rx
|
|
|
|
} else {
|
|
|
|
o.get().1.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let we_have_to_wait = rx.borrow().is_none();
|
|
|
|
if we_have_to_wait {
|
2021-06-30 18:31:51 +00:00
|
|
|
if let Err(e) = rx.changed().await {
|
|
|
|
error!("Error waiting for sync: {}", e);
|
|
|
|
}
|
2021-06-30 07:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let result = match rx
|
|
|
|
.borrow()
|
|
|
|
.as_ref()
|
|
|
|
.expect("When sync channel changes it's always set to some")
|
|
|
|
{
|
|
|
|
Ok(response) => Ok(response.clone()),
|
|
|
|
Err(error) => Err(error.to_response()),
|
|
|
|
};
|
|
|
|
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn sync_helper_wrapper(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: Arc<DatabaseGuard>,
|
2021-06-30 07:52:01 +00:00
|
|
|
sender_user: UserId,
|
|
|
|
sender_device: Box<DeviceId>,
|
|
|
|
since: Option<String>,
|
|
|
|
full_state: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
tx: Sender<Option<ConduitResult<sync_events::Response>>>,
|
|
|
|
) {
|
|
|
|
let r = sync_helper(
|
|
|
|
Arc::clone(&db),
|
|
|
|
sender_user.clone(),
|
|
|
|
sender_device.clone(),
|
|
|
|
since.clone(),
|
|
|
|
full_state,
|
|
|
|
timeout,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Ok((_, caching_allowed)) = r {
|
|
|
|
if !caching_allowed {
|
|
|
|
match db
|
|
|
|
.globals
|
|
|
|
.sync_receivers
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry((sender_user, sender_device))
|
|
|
|
{
|
|
|
|
Entry::Occupied(o) => {
|
|
|
|
// Only remove if the device didn't start a different /sync already
|
|
|
|
if o.get().0 == since {
|
|
|
|
o.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Entry::Vacant(_) => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 07:07:08 +00:00
|
|
|
drop(db);
|
|
|
|
|
2021-06-30 07:52:01 +00:00
|
|
|
let _ = tx.send(Some(r.map(|(r, _)| r.into())));
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn sync_helper(
|
2021-07-14 07:07:08 +00:00
|
|
|
db: Arc<DatabaseGuard>,
|
2021-06-30 07:52:01 +00:00
|
|
|
sender_user: UserId,
|
|
|
|
sender_device: Box<DeviceId>,
|
|
|
|
since: Option<String>,
|
|
|
|
full_state: bool,
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
// bool = caching allowed
|
|
|
|
) -> std::result::Result<(sync_events::Response, bool), Error> {
|
2020-07-30 16:14:47 +00:00
|
|
|
// TODO: match body.set_presence {
|
2020-10-18 18:33:12 +00:00
|
|
|
db.rooms.edus.ping_presence(&sender_user)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// Setup watchers, so if there's no response, we can wait for them
|
2021-06-30 07:52:01 +00:00
|
|
|
let watcher = db.watch(&sender_user, &sender_device);
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-06-30 07:52:01 +00:00
|
|
|
let next_batch = db.globals.current_count()?;
|
|
|
|
let next_batch_string = next_batch.to_string();
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let mut joined_rooms = BTreeMap::new();
|
2021-06-30 07:52:01 +00:00
|
|
|
let since = since
|
2020-07-30 16:14:47 +00:00
|
|
|
.clone()
|
|
|
|
.and_then(|string| string.parse().ok())
|
|
|
|
.unwrap_or(0);
|
|
|
|
|
|
|
|
let mut presence_updates = HashMap::new();
|
2020-08-21 19:22:59 +00:00
|
|
|
let mut left_encrypted_users = HashSet::new(); // Users that have left any encrypted rooms the sender was in
|
2020-07-30 16:14:47 +00:00
|
|
|
let mut device_list_updates = HashSet::new();
|
2020-08-21 19:22:59 +00:00
|
|
|
let mut device_list_left = HashSet::new();
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// Look for device list updates of this account
|
|
|
|
device_list_updates.extend(
|
|
|
|
db.users
|
2020-10-18 18:33:12 +00:00
|
|
|
.keys_changed(&sender_user.to_string(), since, None)
|
2020-07-30 16:14:47 +00:00
|
|
|
.filter_map(|r| r.ok()),
|
|
|
|
);
|
|
|
|
|
2020-10-18 18:33:12 +00:00
|
|
|
for room_id in db.rooms.rooms_joined(&sender_user) {
|
2020-07-30 16:14:47 +00:00
|
|
|
let room_id = room_id?;
|
|
|
|
|
2021-07-13 13:44:25 +00:00
|
|
|
// Get and drop the lock to wait for remaining operations to finish
|
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
drop(mutex_lock);
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
let mut non_timeline_pdus = db
|
|
|
|
.rooms
|
2021-06-08 16:10:00 +00:00
|
|
|
.pdus_until(&sender_user, &room_id, u64::MAX)
|
2021-05-28 11:44:40 +00:00
|
|
|
.filter_map(|r| {
|
2021-06-08 16:10:00 +00:00
|
|
|
// Filter out buggy events
|
2021-05-28 11:44:40 +00:00
|
|
|
if r.is_err() {
|
|
|
|
error!("Bad pdu in pdus_since: {:?}", r);
|
|
|
|
}
|
|
|
|
r.ok()
|
2021-06-08 16:10:00 +00:00
|
|
|
})
|
|
|
|
.take_while(|(pduid, _)| {
|
|
|
|
db.rooms
|
|
|
|
.pdu_count(pduid)
|
|
|
|
.map_or(false, |count| count > since)
|
|
|
|
});
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
// Take the last 10 events for the timeline
|
|
|
|
let timeline_pdus = non_timeline_pdus
|
|
|
|
.by_ref()
|
|
|
|
.take(10)
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.into_iter()
|
|
|
|
.rev()
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
let send_notification_counts = !timeline_pdus.is_empty()
|
|
|
|
|| db
|
|
|
|
.rooms
|
|
|
|
.edus
|
2020-10-18 18:33:12 +00:00
|
|
|
.last_privateread_update(&sender_user, &room_id)?
|
2020-08-23 15:29:39 +00:00
|
|
|
> since;
|
2020-08-21 19:22:59 +00:00
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
// They /sync response doesn't always return all messages, so we say the output is
|
|
|
|
// limited unless there are events in non_timeline_pdus
|
2020-12-22 19:08:20 +00:00
|
|
|
let limited = non_timeline_pdus.next().is_some();
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2020-09-17 20:41:43 +00:00
|
|
|
// Database queries:
|
2020-12-19 15:00:11 +00:00
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
let current_shortstatehash = db
|
|
|
|
.rooms
|
|
|
|
.current_shortstatehash(&room_id)?
|
|
|
|
.expect("All rooms have state");
|
2020-08-21 19:22:59 +00:00
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
let first_pdu_before_since = db
|
|
|
|
.rooms
|
|
|
|
.pdus_until(&sender_user, &room_id, since)
|
|
|
|
.next()
|
|
|
|
.transpose()?;
|
2021-06-30 07:52:01 +00:00
|
|
|
|
2021-05-22 09:31:15 +00:00
|
|
|
let pdus_after_since = db
|
|
|
|
.rooms
|
2021-06-30 07:52:01 +00:00
|
|
|
.pdus_after(&sender_user, &room_id, since)
|
2021-05-22 09:31:15 +00:00
|
|
|
.next()
|
|
|
|
.is_some();
|
2020-10-18 06:56:21 +00:00
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
let since_shortstatehash = first_pdu_before_since
|
|
|
|
.as_ref()
|
|
|
|
.map(|pdu| {
|
|
|
|
db.rooms
|
|
|
|
.pdu_shortstatehash(&pdu.1.event_id)
|
|
|
|
.transpose()
|
2021-07-20 16:38:17 +00:00
|
|
|
.ok_or_else(|| {
|
|
|
|
warn!("PDU without state: {}", pdu.1.event_id);
|
|
|
|
Error::bad_database("Found PDU without state")
|
|
|
|
})
|
2021-07-01 17:55:26 +00:00
|
|
|
})
|
2021-07-20 16:45:21 +00:00
|
|
|
.transpose()?
|
|
|
|
.transpose()?;
|
2021-07-01 17:55:26 +00:00
|
|
|
|
|
|
|
// Calculates joined_member_count, invited_member_count and heroes
|
|
|
|
let calculate_counts = || {
|
|
|
|
let joined_member_count = db.rooms.room_members(&room_id).count();
|
|
|
|
let invited_member_count = db.rooms.room_members_invited(&room_id).count();
|
|
|
|
|
|
|
|
// Recalculate heroes (first 5 members)
|
|
|
|
let mut heroes = Vec::new();
|
|
|
|
|
|
|
|
if joined_member_count + invited_member_count <= 5 {
|
|
|
|
// Go through all PDUs and for each member event, check if the user is still joined or
|
|
|
|
// invited until we have 5 or we reach the end
|
|
|
|
|
|
|
|
for hero in db
|
|
|
|
.rooms
|
|
|
|
.all_pdus(&sender_user, &room_id)
|
|
|
|
.filter_map(|pdu| pdu.ok()) // Ignore all broken pdus
|
|
|
|
.filter(|(_, pdu)| pdu.kind == EventType::RoomMember)
|
|
|
|
.map(|(_, pdu)| {
|
|
|
|
let content = serde_json::from_value::<
|
|
|
|
ruma::events::room::member::MemberEventContent,
|
|
|
|
>(pdu.content.clone())
|
|
|
|
.map_err(|_| Error::bad_database("Invalid member event in database."))?;
|
|
|
|
|
|
|
|
if let Some(state_key) = &pdu.state_key {
|
|
|
|
let user_id = UserId::try_from(state_key.clone()).map_err(|_| {
|
|
|
|
Error::bad_database("Invalid UserId in member PDU.")
|
|
|
|
})?;
|
|
|
|
|
|
|
|
// The membership was and still is invite or join
|
|
|
|
if matches!(
|
|
|
|
content.membership,
|
|
|
|
MembershipState::Join | MembershipState::Invite
|
|
|
|
) && (db.rooms.is_joined(&user_id, &room_id)?
|
|
|
|
|| db.rooms.is_invited(&user_id, &room_id)?)
|
|
|
|
{
|
|
|
|
Ok::<_, Error>(Some(state_key.clone()))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// Filter out buggy users
|
|
|
|
.filter_map(|u| u.ok())
|
|
|
|
// Filter for possible heroes
|
|
|
|
.flatten()
|
|
|
|
{
|
|
|
|
if heroes.contains(&hero) || hero == sender_user.as_str() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
heroes.push(hero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(
|
|
|
|
Some(joined_member_count),
|
|
|
|
Some(invited_member_count),
|
|
|
|
heroes,
|
|
|
|
)
|
|
|
|
};
|
2020-09-17 12:44:47 +00:00
|
|
|
|
2021-02-06 14:27:43 +00:00
|
|
|
let (
|
|
|
|
heroes,
|
|
|
|
joined_member_count,
|
|
|
|
invited_member_count,
|
|
|
|
joined_since_last_sync,
|
|
|
|
state_events,
|
2021-07-01 17:55:26 +00:00
|
|
|
) = if since_shortstatehash.is_none() {
|
|
|
|
// Probably since = 0, we will do an initial sync
|
|
|
|
let (joined_member_count, invited_member_count, heroes) = calculate_counts();
|
|
|
|
|
|
|
|
let current_state_ids = db.rooms.state_full_ids(current_shortstatehash)?;
|
|
|
|
let state_events = current_state_ids
|
2021-02-06 14:27:43 +00:00
|
|
|
.iter()
|
2021-07-01 17:55:26 +00:00
|
|
|
.map(|id| db.rooms.get_pdu(id))
|
|
|
|
.filter_map(|r| r.ok().flatten())
|
2021-02-06 14:27:43 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
(
|
|
|
|
heroes,
|
|
|
|
joined_member_count,
|
|
|
|
invited_member_count,
|
|
|
|
true,
|
|
|
|
state_events,
|
|
|
|
)
|
|
|
|
} else if !pdus_after_since || since_shortstatehash == Some(current_shortstatehash) {
|
|
|
|
// No state changes
|
|
|
|
(Vec::new(), None, None, false, Vec::new())
|
|
|
|
} else {
|
|
|
|
// Incremental /sync
|
|
|
|
let since_shortstatehash = since_shortstatehash.unwrap();
|
|
|
|
|
|
|
|
let since_sender_member = db
|
|
|
|
.rooms
|
|
|
|
.state_get(
|
|
|
|
since_shortstatehash,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
sender_user.as_str(),
|
|
|
|
)?
|
|
|
|
.and_then(|pdu| {
|
|
|
|
serde_json::from_value::<Raw<ruma::events::room::member::MemberEventContent>>(
|
|
|
|
pdu.content.clone(),
|
|
|
|
)
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid PDU in database."))
|
|
|
|
.ok()
|
|
|
|
});
|
|
|
|
|
|
|
|
let joined_since_last_sync = since_sender_member
|
|
|
|
.map_or(true, |member| member.membership != MembershipState::Join);
|
|
|
|
|
|
|
|
let current_state_ids = db.rooms.state_full_ids(current_shortstatehash)?;
|
2021-02-06 14:27:43 +00:00
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
let since_state_ids = db.rooms.state_full_ids(since_shortstatehash)?;
|
|
|
|
|
|
|
|
let state_events = if joined_since_last_sync {
|
|
|
|
current_state_ids
|
|
|
|
.iter()
|
|
|
|
.map(|id| db.rooms.get_pdu(id))
|
|
|
|
.filter_map(|r| r.ok().flatten())
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
} else {
|
|
|
|
current_state_ids
|
|
|
|
.difference(&since_state_ids)
|
|
|
|
.filter(|id| {
|
|
|
|
!timeline_pdus
|
2021-02-06 14:27:43 +00:00
|
|
|
.iter()
|
2021-07-01 17:55:26 +00:00
|
|
|
.any(|(_, timeline_pdu)| timeline_pdu.event_id == **id)
|
|
|
|
})
|
|
|
|
.map(|id| db.rooms.get_pdu(id))
|
|
|
|
.filter_map(|r| r.ok().flatten())
|
|
|
|
.collect()
|
|
|
|
};
|
2021-02-06 14:27:43 +00:00
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
let encrypted_room = db
|
|
|
|
.rooms
|
|
|
|
.state_get(current_shortstatehash, &EventType::RoomEncryption, "")?
|
|
|
|
.is_some();
|
|
|
|
|
|
|
|
let since_encryption =
|
|
|
|
db.rooms
|
|
|
|
.state_get(since_shortstatehash, &EventType::RoomEncryption, "")?;
|
|
|
|
|
|
|
|
// Calculations:
|
|
|
|
let new_encrypted_room = encrypted_room && since_encryption.is_none();
|
|
|
|
|
|
|
|
let send_member_count = state_events
|
|
|
|
.iter()
|
2021-07-21 07:39:15 +00:00
|
|
|
.any(|event| event.kind == EventType::RoomMember)
|
|
|
|
|| timeline_pdus.iter().any(|(_, event)| {
|
|
|
|
event.state_key.is_some() && event.kind == EventType::RoomMember
|
|
|
|
});
|
2021-02-06 14:27:43 +00:00
|
|
|
|
|
|
|
if encrypted_room {
|
2021-07-01 17:55:26 +00:00
|
|
|
for (user_id, current_member) in db
|
|
|
|
.rooms
|
|
|
|
.room_members(&room_id)
|
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.filter_map(|user_id| {
|
|
|
|
db.rooms
|
|
|
|
.state_get(
|
|
|
|
current_shortstatehash,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
user_id.as_str(),
|
|
|
|
)
|
|
|
|
.ok()
|
|
|
|
.flatten()
|
|
|
|
.map(|current_member| (user_id, current_member))
|
|
|
|
})
|
|
|
|
{
|
2021-02-06 14:27:43 +00:00
|
|
|
let current_membership = serde_json::from_value::<
|
|
|
|
Raw<ruma::events::room::member::MemberEventContent>,
|
|
|
|
>(current_member.content.clone())
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid PDU in database."))?
|
|
|
|
.membership;
|
|
|
|
|
2021-07-01 17:55:26 +00:00
|
|
|
let since_membership = db
|
|
|
|
.rooms
|
|
|
|
.state_get(
|
|
|
|
since_shortstatehash,
|
|
|
|
&EventType::RoomMember,
|
|
|
|
user_id.as_str(),
|
|
|
|
)?
|
|
|
|
.and_then(|since_member| {
|
|
|
|
serde_json::from_value::<
|
|
|
|
Raw<ruma::events::room::member::MemberEventContent>,
|
|
|
|
>(since_member.content.clone())
|
|
|
|
.expect("Raw::from_value always works")
|
|
|
|
.deserialize()
|
|
|
|
.map_err(|_| Error::bad_database("Invalid PDU in database."))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.map_or(MembershipState::Leave, |member| member.membership);
|
2021-02-06 14:27:43 +00:00
|
|
|
|
|
|
|
let user_id = UserId::try_from(user_id.clone())
|
|
|
|
.map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?;
|
|
|
|
|
|
|
|
match (since_membership, current_membership) {
|
|
|
|
(MembershipState::Leave, MembershipState::Join) => {
|
|
|
|
// A new user joined an encrypted room
|
2021-06-08 16:10:00 +00:00
|
|
|
if !share_encrypted_room(&db, &sender_user, &user_id, &room_id)? {
|
2021-02-06 14:27:43 +00:00
|
|
|
device_list_updates.insert(user_id);
|
|
|
|
}
|
2020-09-17 12:44:47 +00:00
|
|
|
}
|
2021-05-21 20:22:05 +00:00
|
|
|
// TODO: Remove, this should never happen here, right?
|
2021-02-06 14:27:43 +00:00
|
|
|
(MembershipState::Join, MembershipState::Leave) => {
|
|
|
|
// Write down users that have left encrypted rooms we are in
|
|
|
|
left_encrypted_users.insert(user_id);
|
|
|
|
}
|
|
|
|
_ => {}
|
2020-09-17 12:44:47 +00:00
|
|
|
}
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 14:27:43 +00:00
|
|
|
if joined_since_last_sync && encrypted_room || new_encrypted_room {
|
|
|
|
// If the user is in a new encrypted room, give them all joined users
|
|
|
|
device_list_updates.extend(
|
|
|
|
db.rooms
|
|
|
|
.room_members(&room_id)
|
2021-06-17 18:34:14 +00:00
|
|
|
.flatten()
|
2021-02-06 14:27:43 +00:00
|
|
|
.filter(|user_id| {
|
|
|
|
// Don't send key updates from the sender to the sender
|
2021-06-30 07:52:01 +00:00
|
|
|
&sender_user != user_id
|
2021-02-06 14:27:43 +00:00
|
|
|
})
|
|
|
|
.filter(|user_id| {
|
|
|
|
// Only send keys if the sender doesn't share an encrypted room with the target already
|
2021-06-30 07:52:01 +00:00
|
|
|
!share_encrypted_room(&db, &sender_user, user_id, &room_id)
|
2021-06-08 16:10:00 +00:00
|
|
|
.unwrap_or(false)
|
2021-02-06 14:27:43 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-02-06 14:27:43 +00:00
|
|
|
let (joined_member_count, invited_member_count, heroes) = if send_member_count {
|
2021-07-01 17:55:26 +00:00
|
|
|
calculate_counts()
|
2021-02-06 14:27:43 +00:00
|
|
|
} else {
|
|
|
|
(None, None, Vec::new())
|
|
|
|
};
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
(
|
|
|
|
heroes,
|
2021-02-06 14:27:43 +00:00
|
|
|
joined_member_count,
|
|
|
|
invited_member_count,
|
|
|
|
joined_since_last_sync,
|
|
|
|
state_events,
|
2020-07-30 16:14:47 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2021-02-06 14:27:43 +00:00
|
|
|
// 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()),
|
|
|
|
);
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
let notification_count = if send_notification_counts {
|
2021-04-12 10:40:16 +00:00
|
|
|
Some(
|
|
|
|
db.rooms
|
|
|
|
.notification_count(&sender_user, &room_id)?
|
|
|
|
.try_into()
|
|
|
|
.expect("notification count can't go that high"),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let highlight_count = if send_notification_counts {
|
|
|
|
Some(
|
|
|
|
db.rooms
|
|
|
|
.highlight_count(&sender_user, &room_id)?
|
|
|
|
.try_into()
|
|
|
|
.expect("highlight count can't go that high"),
|
|
|
|
)
|
2020-07-30 16:14:47 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2020-09-16 19:11:38 +00:00
|
|
|
let prev_batch = timeline_pdus
|
|
|
|
.first()
|
|
|
|
.map_or(Ok::<_, Error>(None), |(pdu_id, _)| {
|
|
|
|
Ok(Some(db.rooms.pdu_count(pdu_id)?.to_string()))
|
|
|
|
})?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let room_events = timeline_pdus
|
2020-12-22 19:08:20 +00:00
|
|
|
.iter()
|
2020-09-16 19:11:38 +00:00
|
|
|
.map(|(_, pdu)| pdu.to_sync_room_event())
|
2020-07-30 16:14:47 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let mut edus = db
|
|
|
|
.rooms
|
|
|
|
.edus
|
2021-06-08 16:10:00 +00:00
|
|
|
.readreceipts_since(&room_id, since)
|
2020-07-30 16:14:47 +00:00
|
|
|
.filter_map(|r| r.ok()) // Filter out buggy events
|
2021-05-17 08:25:27 +00:00
|
|
|
.map(|(_, _, v)| v)
|
2020-07-30 16:14:47 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
if db.rooms.edus.last_typing_update(&room_id, &db.globals)? > since {
|
2020-07-30 16:14:47 +00:00
|
|
|
edus.push(
|
|
|
|
serde_json::from_str(
|
|
|
|
&serde_json::to_string(&AnySyncEphemeralRoomEvent::Typing(
|
2020-08-23 15:29:39 +00:00
|
|
|
db.rooms.edus.typings_all(&room_id)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
))
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
)
|
|
|
|
.expect("event is valid, we just created it"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let joined_room = sync_events::JoinedRoom {
|
2021-05-20 21:46:52 +00:00
|
|
|
account_data: sync_events::RoomAccountData {
|
2020-07-30 16:14:47 +00:00
|
|
|
events: db
|
|
|
|
.account_data
|
2020-10-18 18:33:12 +00:00
|
|
|
.changes_since(Some(&room_id), &sender_user, since)?
|
2020-07-30 16:14:47 +00:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(_, v)| {
|
|
|
|
serde_json::from_str(v.json().get())
|
|
|
|
.map_err(|_| Error::bad_database("Invalid account event in database."))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
},
|
|
|
|
summary: sync_events::RoomSummary {
|
|
|
|
heroes,
|
|
|
|
joined_member_count: joined_member_count.map(|n| (n as u32).into()),
|
|
|
|
invited_member_count: invited_member_count.map(|n| (n as u32).into()),
|
|
|
|
},
|
|
|
|
unread_notifications: sync_events::UnreadNotificationsCount {
|
2021-04-12 10:40:16 +00:00
|
|
|
highlight_count,
|
2020-07-30 16:14:47 +00:00
|
|
|
notification_count,
|
|
|
|
},
|
|
|
|
timeline: sync_events::Timeline {
|
2020-08-19 16:26:39 +00:00
|
|
|
limited: limited || joined_since_last_sync,
|
2020-07-30 16:14:47 +00:00
|
|
|
prev_batch,
|
|
|
|
events: room_events,
|
|
|
|
},
|
|
|
|
state: sync_events::State {
|
2021-07-01 17:55:26 +00:00
|
|
|
events: state_events
|
|
|
|
.iter()
|
|
|
|
.map(|pdu| pdu.to_sync_state_event())
|
|
|
|
.collect(),
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
|
|
|
ephemeral: sync_events::Ephemeral { events: edus },
|
|
|
|
};
|
|
|
|
|
|
|
|
if !joined_room.is_empty() {
|
|
|
|
joined_rooms.insert(room_id.clone(), joined_room);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take presence updates from this room
|
|
|
|
for (user_id, presence) in
|
|
|
|
db.rooms
|
|
|
|
.edus
|
|
|
|
.presence_since(&room_id, since, &db.rooms, &db.globals)?
|
|
|
|
{
|
|
|
|
match presence_updates.entry(user_id) {
|
2021-07-18 18:43:39 +00:00
|
|
|
Entry::Vacant(v) => {
|
2020-07-30 16:14:47 +00:00
|
|
|
v.insert(presence);
|
|
|
|
}
|
2021-07-18 18:43:39 +00:00
|
|
|
Entry::Occupied(mut o) => {
|
2020-07-30 16:14:47 +00:00
|
|
|
let p = o.get_mut();
|
|
|
|
|
|
|
|
// Update existing presence event with more info
|
|
|
|
p.content.presence = presence.content.presence;
|
|
|
|
if let Some(status_msg) = presence.content.status_msg {
|
|
|
|
p.content.status_msg = Some(status_msg);
|
|
|
|
}
|
|
|
|
if let Some(last_active_ago) = presence.content.last_active_ago {
|
|
|
|
p.content.last_active_ago = Some(last_active_ago);
|
|
|
|
}
|
|
|
|
if let Some(displayname) = presence.content.displayname {
|
|
|
|
p.content.displayname = Some(displayname);
|
|
|
|
}
|
|
|
|
if let Some(avatar_url) = presence.content.avatar_url {
|
|
|
|
p.content.avatar_url = Some(avatar_url);
|
|
|
|
}
|
|
|
|
if let Some(currently_active) = presence.content.currently_active {
|
|
|
|
p.content.currently_active = Some(currently_active);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut left_rooms = BTreeMap::new();
|
2021-04-13 13:00:45 +00:00
|
|
|
for result in db.rooms.rooms_left(&sender_user) {
|
|
|
|
let (room_id, left_state_events) = result?;
|
2021-07-13 13:44:25 +00:00
|
|
|
|
|
|
|
// Get and drop the lock to wait for remaining operations to finish
|
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
drop(mutex_lock);
|
|
|
|
|
2021-04-13 13:00:45 +00:00
|
|
|
let left_count = db.rooms.get_left_count(&room_id, &sender_user)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-04-13 13:00:45 +00:00
|
|
|
// Left before last sync
|
|
|
|
if Some(since) >= left_count {
|
2020-10-27 19:25:43 +00:00
|
|
|
continue;
|
2021-04-13 13:00:45 +00:00
|
|
|
}
|
2020-10-27 19:25:43 +00:00
|
|
|
|
2021-04-13 13:00:45 +00:00
|
|
|
left_rooms.insert(
|
|
|
|
room_id.clone(),
|
2020-10-27 19:25:43 +00:00
|
|
|
sync_events::LeftRoom {
|
2021-05-20 21:46:52 +00:00
|
|
|
account_data: sync_events::RoomAccountData { events: Vec::new() },
|
2020-10-27 19:25:43 +00:00
|
|
|
timeline: sync_events::Timeline {
|
|
|
|
limited: false,
|
2021-06-30 07:52:01 +00:00
|
|
|
prev_batch: Some(next_batch_string.clone()),
|
2020-10-27 19:25:43 +00:00
|
|
|
events: Vec::new(),
|
|
|
|
},
|
2021-04-13 13:00:45 +00:00
|
|
|
state: sync_events::State {
|
|
|
|
events: left_state_events,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut invited_rooms = BTreeMap::new();
|
2021-04-11 19:01:27 +00:00
|
|
|
for result in db.rooms.rooms_invited(&sender_user) {
|
|
|
|
let (room_id, invite_state_events) = result?;
|
2021-07-13 13:44:25 +00:00
|
|
|
|
|
|
|
// Get and drop the lock to wait for remaining operations to finish
|
|
|
|
let mutex = Arc::clone(
|
|
|
|
db.globals
|
|
|
|
.roomid_mutex
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.entry(room_id.clone())
|
|
|
|
.or_default(),
|
|
|
|
);
|
|
|
|
let mutex_lock = mutex.lock().await;
|
|
|
|
drop(mutex_lock);
|
|
|
|
|
2021-04-11 19:01:27 +00:00
|
|
|
let invite_count = db.rooms.get_invite_count(&room_id, &sender_user)?;
|
2020-08-02 07:24:47 +00:00
|
|
|
|
2021-04-11 19:01:27 +00:00
|
|
|
// Invited before last sync
|
|
|
|
if Some(since) >= invite_count {
|
2020-08-02 07:24:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-07-30 16:14:47 +00:00
|
|
|
|
2021-04-11 19:01:27 +00:00
|
|
|
invited_rooms.insert(
|
|
|
|
room_id.clone(),
|
|
|
|
sync_events::InvitedRoom {
|
|
|
|
invite_state: sync_events::InviteState {
|
|
|
|
events: invite_state_events,
|
|
|
|
},
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
2021-04-11 19:01:27 +00:00
|
|
|
);
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
|
2020-08-21 19:22:59 +00:00
|
|
|
for user_id in left_encrypted_users {
|
2020-08-26 15:15:52 +00:00
|
|
|
let still_share_encrypted_room = db
|
2020-08-21 19:22:59 +00:00
|
|
|
.rooms
|
2021-06-08 16:10:00 +00:00
|
|
|
.get_shared_rooms(vec![sender_user.clone(), user_id.clone()])?
|
2020-08-21 19:22:59 +00:00
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.filter_map(|other_room_id| {
|
|
|
|
Some(
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(&other_room_id, &EventType::RoomEncryption, "")
|
|
|
|
.ok()?
|
|
|
|
.is_some(),
|
|
|
|
)
|
|
|
|
})
|
2020-08-21 19:22:59 +00:00
|
|
|
.all(|encrypted| !encrypted);
|
|
|
|
// If the user doesn't share an encrypted room with the target anymore, we need to tell
|
|
|
|
// them
|
2020-08-26 15:15:52 +00:00
|
|
|
if still_share_encrypted_room {
|
2020-08-21 19:22:59 +00:00
|
|
|
device_list_left.insert(user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
// Remove all to-device events the device received *last time*
|
|
|
|
db.users
|
2021-06-30 07:52:01 +00:00
|
|
|
.remove_to_device_events(&sender_user, &sender_device, since)?;
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let response = sync_events::Response {
|
2021-06-30 07:52:01 +00:00
|
|
|
next_batch: next_batch_string,
|
2020-07-30 16:14:47 +00:00
|
|
|
rooms: sync_events::Rooms {
|
|
|
|
leave: left_rooms,
|
|
|
|
join: joined_rooms,
|
|
|
|
invite: invited_rooms,
|
2021-07-15 21:17:58 +00:00
|
|
|
knock: BTreeMap::new(), // TODO
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
|
|
|
presence: sync_events::Presence {
|
|
|
|
events: presence_updates
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, v)| Raw::from(v))
|
|
|
|
.collect(),
|
|
|
|
},
|
2021-05-20 21:46:52 +00:00
|
|
|
account_data: sync_events::GlobalAccountData {
|
2020-07-30 16:14:47 +00:00
|
|
|
events: db
|
|
|
|
.account_data
|
2020-10-18 18:33:12 +00:00
|
|
|
.changes_since(None, &sender_user, since)?
|
2020-07-30 16:14:47 +00:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(_, v)| {
|
|
|
|
serde_json::from_str(v.json().get())
|
|
|
|
.map_err(|_| Error::bad_database("Invalid account event in database."))
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
},
|
|
|
|
device_lists: sync_events::DeviceLists {
|
|
|
|
changed: device_list_updates.into_iter().collect(),
|
2020-08-21 19:22:59 +00:00
|
|
|
left: device_list_left.into_iter().collect(),
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
2021-06-30 07:52:01 +00:00
|
|
|
device_one_time_keys_count: if db.users.last_one_time_keys_update(&sender_user)? > since
|
2020-09-23 10:03:08 +00:00
|
|
|
|| since == 0
|
|
|
|
{
|
2021-06-30 07:52:01 +00:00
|
|
|
db.users.count_one_time_keys(&sender_user, &sender_device)?
|
2020-07-30 16:14:47 +00:00
|
|
|
} else {
|
|
|
|
BTreeMap::new()
|
|
|
|
},
|
|
|
|
to_device: sync_events::ToDevice {
|
2021-06-30 07:52:01 +00:00
|
|
|
events: db
|
|
|
|
.users
|
|
|
|
.get_to_device_events(&sender_user, &sender_device)?,
|
2020-07-30 16:14:47 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// TODO: Retry the endpoint instead of returning (waiting for #118)
|
2021-06-30 07:52:01 +00:00
|
|
|
if !full_state
|
2020-07-30 16:14:47 +00:00
|
|
|
&& response.rooms.is_empty()
|
|
|
|
&& response.presence.is_empty()
|
|
|
|
&& response.account_data.is_empty()
|
|
|
|
&& response.device_lists.is_empty()
|
|
|
|
&& response.device_one_time_keys_count.is_empty()
|
|
|
|
&& response.to_device.is_empty()
|
|
|
|
{
|
|
|
|
// Hang a few seconds so requests are not spammed
|
|
|
|
// Stop hanging if new info arrives
|
2021-06-30 07:52:01 +00:00
|
|
|
let mut duration = timeout.unwrap_or_default();
|
2020-07-30 16:14:47 +00:00
|
|
|
if duration.as_secs() > 30 {
|
|
|
|
duration = Duration::from_secs(30);
|
|
|
|
}
|
2021-03-16 17:00:26 +00:00
|
|
|
let _ = tokio::time::timeout(duration, watcher).await;
|
2021-06-30 07:52:01 +00:00
|
|
|
Ok((response, false))
|
|
|
|
} else {
|
|
|
|
Ok((response, since != next_batch)) // Only cache if we made progress
|
2020-07-30 16:14:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-21 19:22:59 +00:00
|
|
|
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db))]
|
2020-08-21 19:22:59 +00:00
|
|
|
fn share_encrypted_room(
|
|
|
|
db: &Database,
|
2020-10-18 18:33:12 +00:00
|
|
|
sender_user: &UserId,
|
2020-08-21 19:22:59 +00:00
|
|
|
user_id: &UserId,
|
|
|
|
ignore_room: &RoomId,
|
2021-06-08 16:10:00 +00:00
|
|
|
) -> Result<bool> {
|
|
|
|
Ok(db
|
|
|
|
.rooms
|
|
|
|
.get_shared_rooms(vec![sender_user.clone(), user_id.clone()])?
|
2020-08-21 19:22:59 +00:00
|
|
|
.filter_map(|r| r.ok())
|
|
|
|
.filter(|room_id| room_id != ignore_room)
|
|
|
|
.filter_map(|other_room_id| {
|
|
|
|
Some(
|
|
|
|
db.rooms
|
|
|
|
.room_state_get(&other_room_id, &EventType::RoomEncryption, "")
|
|
|
|
.ok()?
|
|
|
|
.is_some(),
|
|
|
|
)
|
|
|
|
})
|
2021-06-08 16:10:00 +00:00
|
|
|
.any(|encrypted| encrypted))
|
2020-08-21 19:22:59 +00:00
|
|
|
}
|