Ensure chat messages are retrieved in order of id (#3086)

Also, remove logic for implicitly marking chat messages as observed when
they are fetched. I think this is unnecessary, because the client always
explicitly acknowledges messages when they are shown.

Release Notes:

- Fixed a bug where chat messages were shown out of order (preview only)
This commit is contained in:
Max Brunsfeld 2023-10-04 15:10:49 -07:00 committed by GitHub
commit a27be35325
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 58 deletions

View file

@ -9,13 +9,3 @@ pub mod projects;
pub mod rooms;
pub mod servers;
pub mod users;
fn max_assign<T: Ord>(max: &mut Option<T>, val: T) {
if let Some(max_val) = max {
if val > *max_val {
*max = Some(val);
}
} else {
*max = Some(val);
}
}

View file

@ -89,17 +89,14 @@ impl Database {
let mut rows = channel_message::Entity::find()
.filter(condition)
.order_by_asc(channel_message::Column::Id)
.limit(count as u64)
.stream(&*tx)
.await?;
let mut max_id = None;
let mut messages = Vec::new();
while let Some(row) = rows.next().await {
let row = row?;
max_assign(&mut max_id, row.id);
let nonce = row.nonce.as_u64_pair();
messages.push(proto::ChannelMessage {
id: row.id.to_proto(),
@ -113,50 +110,6 @@ impl Database {
});
}
drop(rows);
if let Some(max_id) = max_id {
let has_older_message = observed_channel_messages::Entity::find()
.filter(
observed_channel_messages::Column::UserId
.eq(user_id)
.and(observed_channel_messages::Column::ChannelId.eq(channel_id))
.and(observed_channel_messages::Column::ChannelMessageId.lt(max_id)),
)
.one(&*tx)
.await?
.is_some();
if has_older_message {
observed_channel_messages::Entity::update(
observed_channel_messages::ActiveModel {
user_id: ActiveValue::Unchanged(user_id),
channel_id: ActiveValue::Unchanged(channel_id),
channel_message_id: ActiveValue::Set(max_id),
},
)
.exec(&*tx)
.await?;
} else {
observed_channel_messages::Entity::insert(
observed_channel_messages::ActiveModel {
user_id: ActiveValue::Set(user_id),
channel_id: ActiveValue::Set(channel_id),
channel_message_id: ActiveValue::Set(max_id),
},
)
.on_conflict(
OnConflict::columns([
observed_channel_messages::Column::UserId,
observed_channel_messages::Column::ChannelId,
])
.update_columns([observed_channel_messages::Column::ChannelMessageId])
.to_owned(),
)
.exec(&*tx)
.await?;
}
}
Ok(messages)
})
.await