use std::collections::BTreeMap; use axum::extract::State; use conduwuit::{Err, Result, err}; use ruma::{ MilliSecondsSinceUnixEpoch, api::client::{read_marker::set_read_marker, receipt::create_receipt}, events::{ RoomAccountDataEventType, fully_read::{FullyReadEvent, FullyReadEventContent}, receipt::{Receipt, ReceiptEvent, ReceiptEventContent, ReceiptThread, ReceiptType}, }, }; use crate::{Ruma, client_ip::ClientIp}; /// # `POST /_matrix/client/r0/rooms/{roomId}/read_markers` /// /// Sets different types of read markers. /// /// - Updates fully-read account data event to `fully_read` /// - If `read_receipt` is set: Update private marker and public read receipt /// EDU pub(crate) async fn set_read_marker_route( State(services): State, body: Ruma, ) -> Result { let sender_user = body.identity.expect_sender_user()?; if !services .rooms .state_cache .is_joined(sender_user, &body.room_id) .await { return Err!(Request(Forbidden("You are not in this room."))); } if let Some(event) = &body.fully_read { let fully_read_event = FullyReadEvent::new(FullyReadEventContent::new(event.to_owned())); services .account_data .update( Some(&body.room_id), sender_user, RoomAccountDataEventType::FullyRead, &serde_json::to_value(fully_read_event)?, ) .await?; } if body.private_read_receipt.is_some() || body.read_receipt.is_some() { services .rooms .user .reset_notification_counts(sender_user, &body.room_id); } // ping presence if services.config.allow_local_presence { services .presence .ping_presence(sender_user, &ruma::presence::PresenceState::Online) .await?; } if let Some(event) = &body.read_receipt { if services.config.allow_local_read_receipts && !services.users.is_suspended(sender_user).await? { let receipt_content = [( event.to_owned(), BTreeMap::from_iter([( ReceiptType::Read, BTreeMap::from_iter([( sender_user.to_owned(), Receipt::new(MilliSecondsSinceUnixEpoch::now()), )]), )]), )]; services .rooms .read_receipt .readreceipt_update( sender_user, &body.room_id, ReceiptEvent::new( body.room_id.clone(), ReceiptEventContent::from_iter(receipt_content), ), ) .await; } } if let Some(event) = &body.private_read_receipt { let count = services .rooms .timeline .get_pdu_count(event) .await .map_err(|_| err!(Request(NotFound("Event not found."))))? .into_unsigned(); services .rooms .read_receipt .private_read_set(&body.room_id, sender_user, event, count) .await; } services.sync.wake(sender_user).await; Ok(set_read_marker::v3::Response::new()) } /// # `POST /_matrix/client/r0/rooms/{roomId}/receipt/{receiptType}/{eventId}` /// /// Sets private read marker and public read receipt EDU. pub(crate) async fn create_receipt_route( State(services): State, ClientIp(client_ip): ClientIp, // NOTE: Required for updating device metadata body: Ruma, ) -> Result { let sender_user = body.identity.expect_sender_user()?; if !services .rooms .state_cache .is_joined(sender_user, &body.room_id) .await { return Err!(Request(Forbidden("You are not in this room."))); } services .users .update_device_last_seen(sender_user, body.identity.sender_device(), client_ip) .await; if body.receipt_type == create_receipt::v3::ReceiptType::FullyRead && body.thread != ReceiptThread::Unthreaded { return Err!(Request(InvalidParam("thread_id cannot be specified for m.fully_read"))); } if matches!( &body.receipt_type, create_receipt::v3::ReceiptType::Read | create_receipt::v3::ReceiptType::ReadPrivate ) { services .rooms .user .reset_notification_counts(sender_user, &body.room_id); } // ping presence if services.config.allow_local_presence { services .presence .ping_presence(sender_user, &ruma::presence::PresenceState::Online) .await?; } match body.receipt_type { | create_receipt::v3::ReceiptType::FullyRead => { let fully_read_event = FullyReadEvent::new(FullyReadEventContent::new(body.event_id.clone())); services .account_data .update( Some(&body.room_id), sender_user, RoomAccountDataEventType::FullyRead, &serde_json::to_value(fully_read_event)?, ) .await?; }, | create_receipt::v3::ReceiptType::Read => if services.config.allow_local_read_receipts && !services.users.is_suspended(sender_user).await? { let mut receipt = Receipt::new(MilliSecondsSinceUnixEpoch::now()); receipt.thread = body.thread.clone(); let receipt_content = [( body.event_id.clone(), BTreeMap::from_iter([( ReceiptType::Read, BTreeMap::from_iter([(sender_user.to_owned(), receipt)]), )]), )]; services .rooms .read_receipt .readreceipt_update( sender_user, &body.room_id, ReceiptEvent::new( body.room_id.clone(), ReceiptEventContent::from_iter(receipt_content), ), ) .await; }, | create_receipt::v3::ReceiptType::ReadPrivate => { let count = services .rooms .timeline .get_pdu_count(&body.event_id) .await .map_err(|_| err!(Request(NotFound("Event not found."))))? .into_unsigned(); services .rooms .read_receipt .private_read_set(&body.room_id, sender_user, &body.event_id, count) .await; }, | _ => { return Err!(Request(InvalidParam(warn!( "Received unknown read receipt type: {}", &body.receipt_type )))); }, } services.sync.wake(sender_user).await; Ok(create_receipt::v3::Response::new()) }