improvement: better and more efficient message count calculation

This commit is contained in:
Timo Kösters 2021-04-12 12:40:16 +02:00
parent 1dc85895a7
commit 662a0cf1df
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
7 changed files with 169 additions and 68 deletions

View file

@ -596,7 +596,10 @@ async fn join_room_by_id_helper(
if let Some(state_key) = &pdu.state_key {
if pdu.kind == EventType::RoomMember {
let target_user_id = UserId::try_from(state_key.clone()).map_err(|e| {
warn!("Invalid user id in send_join response: {}: {}", state_key, e);
warn!(
"Invalid user id in send_join response: {}: {}",
state_key, e
);
Error::BadServerResponse("Invalid user id in send_join response.")
})?;

View file

@ -47,6 +47,8 @@ pub async fn set_read_marker_route(
))?,
&db.globals,
)?;
db.rooms
.reset_notification_counts(&sender_user, &body.room_id)?;
let mut user_receipts = BTreeMap::new();
user_receipts.insert(
@ -103,6 +105,8 @@ pub async fn create_receipt_route(
))?,
&db.globals,
)?;
db.rooms
.reset_notification_counts(&sender_user, &body.room_id)?;
let mut user_receipts = BTreeMap::new();
user_receipts.insert(

View file

@ -12,7 +12,7 @@ use ruma::{
use rocket::{get, tokio};
use std::{
collections::{hash_map, BTreeMap, HashMap, HashSet},
convert::TryFrom,
convert::{TryFrom, TryInto},
time::Duration,
};
@ -370,23 +370,23 @@ pub async fn sync_events_route(
);
let notification_count = if send_notification_counts {
if let Some(last_read) = db.rooms.edus.private_read_get(&room_id, &sender_user)? {
Some(
(db.rooms
.pdus_since(&sender_user, &room_id, last_read)?
.filter_map(|pdu| pdu.ok()) // Filter out buggy events
.filter(|(_, pdu)| {
matches!(
pdu.kind.clone(),
EventType::RoomMessage | EventType::RoomEncrypted
)
})
.count() as u32)
.into(),
)
} else {
None
}
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"),
)
} else {
None
};
@ -440,7 +440,7 @@ pub async fn sync_events_route(
invited_member_count: invited_member_count.map(|n| (n as u32).into()),
},
unread_notifications: sync_events::UnreadNotificationsCount {
highlight_count: None,
highlight_count,
notification_count,
},
timeline: sync_events::Timeline {