feat: Update device metadata upon hitting hot endpoints

This commit is contained in:
timedout 2025-12-02 13:38:24 +00:00
parent b3fa4705ef
commit f3115e14ab
No known key found for this signature in database
GPG key ID: 0FA334385D0B689F
7 changed files with 113 additions and 10 deletions

View file

@ -1,6 +1,6 @@
use axum::extract::State;
use conduwuit::{
Err, Result, at,
Err, Result, at, err,
matrix::{
event::{Event, Matches},
pdu::PduCount,
@ -22,7 +22,7 @@ use conduwuit_service::{
};
use futures::{FutureExt, StreamExt, TryFutureExt, future::OptionFuture, pin_mut};
use ruma::{
DeviceId, RoomId, UserId,
DeviceId, MilliSecondsSinceUnixEpoch, RoomId, UserId,
api::{
Direction,
client::{filter::RoomEventFilter, message::get_message_events},
@ -78,6 +78,21 @@ pub(crate) async fn get_message_events_route(
let room_id = &body.room_id;
let filter = &body.filter;
if sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.or_else(|_| err!(Request(NotFound("device {device_id} not found?"))))?;
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services
.users
.update_device_metadata(sender_user, device_id, &device)
.await?;
}
if !services.rooms.metadata.exists(room_id).await {
return Err!(Request(Forbidden("Room does not exist to this server")));
}

View file

@ -121,6 +121,20 @@ pub(crate) async fn create_receipt_route(
body: Ruma<create_receipt::v3::Request>,
) -> Result<create_receipt::v3::Response> {
let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.or_else(|_| err!(Request(NotFound("device {device_id} not found?"))))?;
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services
.users
.update_device_metadata(sender_user, device_id, &device)
.await?;
}
if matches!(
&body.receipt_type,

View file

@ -1,7 +1,8 @@
use axum::extract::State;
use conduwuit::{Err, Result, matrix::pdu::PduBuilder};
use conduwuit::{Err, Result, err, matrix::pdu::PduBuilder};
use ruma::{
api::client::redact::redact_event, events::room::redaction::RoomRedactionEventContent,
MilliSecondsSinceUnixEpoch, api::client::redact::redact_event,
events::room::redaction::RoomRedactionEventContent,
};
use crate::Ruma;
@ -16,6 +17,20 @@ pub(crate) async fn redact_event_route(
body: Ruma<redact_event::v3::Request>,
) -> Result<redact_event::v3::Response> {
let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.or_else(|_| err!(Request(NotFound("device {device_id} not found?"))))?;
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services
.users
.update_device_metadata(sender_user, device_id, &device)
.await?;
}
let body = &body.body;
if services.users.is_suspended(sender_user).await? {
// TODO: Users can redact their own messages while suspended

View file

@ -2,7 +2,10 @@ use std::collections::BTreeMap;
use axum::extract::State;
use conduwuit::{Err, Result, err, matrix::pdu::PduBuilder, utils};
use ruma::{api::client::message::send_message_event, events::MessageLikeEventType};
use ruma::{
MilliSecondsSinceUnixEpoch, api::client::message::send_message_event,
events::MessageLikeEventType,
};
use serde_json::from_str;
use crate::Ruma;
@ -27,6 +30,21 @@ pub(crate) async fn send_message_event_route(
return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));
}
if sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = sender_device.clone().unwrap();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.or_else(|_| err!(Request(NotFound("device {device_id} not found?"))))?;
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services
.users
.update_device_metadata(sender_user, device_id, &device)
.await?;
}
// Forbid m.room.encrypted if encryption is disabled
if MessageLikeEventType::RoomEncrypted == body.event_type && !services.config.allow_encryption
{

View file

@ -7,7 +7,7 @@ use conduwuit::{
use conduwuit_service::Services;
use futures::{FutureExt, TryStreamExt};
use ruma::{
OwnedEventId, RoomId, UserId,
MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId,
api::client::state::{get_state_events, get_state_events_for_key, send_state_event},
events::{
AnyStateEventContent, StateEventType,
@ -33,6 +33,20 @@ pub(crate) async fn send_state_event_for_key_route(
body: Ruma<send_state_event::v3::Request>,
) -> Result<send_state_event::v3::Response> {
let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.or_else(|_| err!(Request(NotFound("device {device_id} not found?"))))?;
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services
.users
.update_device_metadata(sender_user, device_id, &device)
.await?;
}
if services.users.is_suspended(sender_user).await? {
return Err!(Request(UserSuspended("You cannot perform this action while suspended.")));

View file

@ -7,7 +7,7 @@ use std::{
use axum::extract::State;
use conduwuit::{
Err, Error, Result, at, error, extract_variant, is_equal_to,
Err, Error, Result, at, err, error, extract_variant, is_equal_to,
matrix::{Event, TypeStateKey, pdu::PduCount},
trace,
utils::{
@ -25,7 +25,7 @@ use futures::{
pin_mut,
};
use ruma::{
DeviceId, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId,
DeviceId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedRoomId, RoomId, UInt, UserId,
api::client::sync::sync_events::{self, DeviceLists, UnreadNotificationsCount},
directory::RoomTypeFilter,
events::{
@ -66,6 +66,19 @@ pub(crate) async fn sync_events_v5_route(
debug_assert!(DEFAULT_BUMP_TYPES.is_sorted(), "DEFAULT_BUMP_TYPES is not sorted");
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
let sender_device = body.sender_device.as_ref().expect("user is authenticated");
// Increment the "device last active" metadata
let mut device = services
.users
.get_device_metadata(sender_user, sender_device)
.await
.or_else(|_| err!(Request(NotFound("device {sender_device} not found?"))))?;
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services
.users
.update_device_metadata(sender_user, sender_device, &device)
.await?;
let mut body = body.body;
// Setup watchers, so if there's no response, we can wait for them

View file

@ -1,6 +1,6 @@
use axum::extract::State;
use conduwuit::{Err, Result, utils, utils::math::Tried};
use ruma::api::client::typing::create_typing_event;
use conduwuit::{Err, Result, err, utils, utils::math::Tried};
use ruma::{MilliSecondsSinceUnixEpoch, api::client::typing::create_typing_event};
use crate::Ruma;
@ -13,6 +13,20 @@ pub(crate) async fn create_typing_event_route(
) -> Result<create_typing_event::v3::Response> {
use create_typing_event::v3::Typing;
let sender_user = body.sender_user();
if body.sender_device.is_some() {
// Increment the "device last active" metadata
let device_id = body.sender_device();
let mut device = services
.users
.get_device_metadata(sender_user, device_id)
.await
.or_else(|_| err!(Request(NotFound("device {device_id} not found?"))))?;
device.last_seen_ts = Some(MilliSecondsSinceUnixEpoch::now());
services
.users
.update_device_metadata(sender_user, device_id, &device)
.await?;
}
if sender_user != body.user_id && body.appservice_info.is_none() {
return Err!(Request(Forbidden("You cannot update typing status of other users.")));