combine service/users data w/ mod unit split sliding sync related out of service/users instrument database entry points remove increment crap from database interface de-wrap all database get() calls de-wrap all database insert() calls de-wrap all database remove() calls refactor database interface for async streaming add query key serializer for database implement Debug for result handle add query deserializer for database add deserialization trait for option handle start a stream utils suite de-wrap/asyncify/type-query count_one_time_keys() de-wrap/asyncify users count add admin query users command suite de-wrap/asyncify users exists de-wrap/partially asyncify user filter related asyncify/de-wrap users device/keys related asyncify/de-wrap user auth/misc related asyncify/de-wrap users blurhash asyncify/de-wrap account_data get; merge Data into Service partial asyncify/de-wrap uiaa; merge Data into Service partially asyncify/de-wrap transaction_ids get; merge Data into Service partially asyncify/de-wrap key_backups; merge Data into Service asyncify/de-wrap pusher service getters; merge Data into Service asyncify/de-wrap rooms alias getters/some iterators asyncify/de-wrap rooms directory getters/iterator partially asyncify/de-wrap rooms lazy-loading partially asyncify/de-wrap rooms metadata asyncify/dewrap rooms outlier asyncify/dewrap rooms pdu_metadata dewrap/partially asyncify rooms read receipt de-wrap rooms search service de-wrap/partially asyncify rooms user service partial de-wrap rooms state_compressor de-wrap rooms state_cache de-wrap room state et al de-wrap rooms timeline service additional users device/keys related de-wrap/asyncify sender asyncify services refactor database to TryFuture/TryStream refactor services for TryFuture/TryStream asyncify api handlers additional asyncification for admin module abstract stream related; support reverse streams additional stream conversions asyncify state-res related Signed-off-by: Jason Volk <jason@zemos.net>
197 lines
5.2 KiB
Rust
197 lines
5.2 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use axum::extract::State;
|
|
use conduit::PduCount;
|
|
use ruma::{
|
|
api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt},
|
|
events::{
|
|
receipt::{ReceiptThread, ReceiptType},
|
|
RoomAccountDataEventType,
|
|
},
|
|
MilliSecondsSinceUnixEpoch,
|
|
};
|
|
|
|
use crate::{Error, Result, Ruma};
|
|
|
|
/// # `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<crate::State>, body: Ruma<set_read_marker::v3::Request>,
|
|
) -> Result<set_read_marker::v3::Response> {
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
if let Some(fully_read) = &body.fully_read {
|
|
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
|
content: ruma::events::fully_read::FullyReadEventContent {
|
|
event_id: fully_read.clone(),
|
|
},
|
|
};
|
|
services
|
|
.account_data
|
|
.update(
|
|
Some(&body.room_id),
|
|
sender_user,
|
|
RoomAccountDataEventType::FullyRead,
|
|
&serde_json::to_value(fully_read_event).expect("to json value always works"),
|
|
)
|
|
.await?;
|
|
}
|
|
|
|
if body.private_read_receipt.is_some() || body.read_receipt.is_some() {
|
|
services
|
|
.rooms
|
|
.user
|
|
.reset_notification_counts(sender_user, &body.room_id);
|
|
}
|
|
|
|
if let Some(event) = &body.private_read_receipt {
|
|
let count = services
|
|
.rooms
|
|
.timeline
|
|
.get_pdu_count(event)
|
|
.await
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Event does not exist."))?;
|
|
|
|
let count = match count {
|
|
PduCount::Backfilled(_) => {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::InvalidParam,
|
|
"Read receipt is in backfilled timeline",
|
|
))
|
|
},
|
|
PduCount::Normal(c) => c,
|
|
};
|
|
services
|
|
.rooms
|
|
.read_receipt
|
|
.private_read_set(&body.room_id, sender_user, count);
|
|
}
|
|
|
|
if let Some(event) = &body.read_receipt {
|
|
let mut user_receipts = BTreeMap::new();
|
|
user_receipts.insert(
|
|
sender_user.clone(),
|
|
ruma::events::receipt::Receipt {
|
|
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
|
thread: ReceiptThread::Unthreaded,
|
|
},
|
|
);
|
|
|
|
let mut receipts = BTreeMap::new();
|
|
receipts.insert(ReceiptType::Read, user_receipts);
|
|
|
|
let mut receipt_content = BTreeMap::new();
|
|
receipt_content.insert(event.to_owned(), receipts);
|
|
|
|
services
|
|
.rooms
|
|
.read_receipt
|
|
.readreceipt_update(
|
|
sender_user,
|
|
&body.room_id,
|
|
&ruma::events::receipt::ReceiptEvent {
|
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
room_id: body.room_id.clone(),
|
|
},
|
|
)
|
|
.await;
|
|
}
|
|
|
|
Ok(set_read_marker::v3::Response {})
|
|
}
|
|
|
|
/// # `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<crate::State>, body: Ruma<create_receipt::v3::Request>,
|
|
) -> Result<create_receipt::v3::Response> {
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
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);
|
|
}
|
|
|
|
match body.receipt_type {
|
|
create_receipt::v3::ReceiptType::FullyRead => {
|
|
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
|
content: ruma::events::fully_read::FullyReadEventContent {
|
|
event_id: body.event_id.clone(),
|
|
},
|
|
};
|
|
services
|
|
.account_data
|
|
.update(
|
|
Some(&body.room_id),
|
|
sender_user,
|
|
RoomAccountDataEventType::FullyRead,
|
|
&serde_json::to_value(fully_read_event).expect("to json value always works"),
|
|
)
|
|
.await?;
|
|
},
|
|
create_receipt::v3::ReceiptType::Read => {
|
|
let mut user_receipts = BTreeMap::new();
|
|
user_receipts.insert(
|
|
sender_user.clone(),
|
|
ruma::events::receipt::Receipt {
|
|
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
|
thread: ReceiptThread::Unthreaded,
|
|
},
|
|
);
|
|
let mut receipts = BTreeMap::new();
|
|
receipts.insert(ReceiptType::Read, user_receipts);
|
|
|
|
let mut receipt_content = BTreeMap::new();
|
|
receipt_content.insert(body.event_id.clone(), receipts);
|
|
|
|
services
|
|
.rooms
|
|
.read_receipt
|
|
.readreceipt_update(
|
|
sender_user,
|
|
&body.room_id,
|
|
&ruma::events::receipt::ReceiptEvent {
|
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
room_id: body.room_id.clone(),
|
|
},
|
|
)
|
|
.await;
|
|
},
|
|
create_receipt::v3::ReceiptType::ReadPrivate => {
|
|
let count = services
|
|
.rooms
|
|
.timeline
|
|
.get_pdu_count(&body.event_id)
|
|
.await
|
|
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Event does not exist."))?;
|
|
|
|
let count = match count {
|
|
PduCount::Backfilled(_) => {
|
|
return Err(Error::BadRequest(
|
|
ErrorKind::InvalidParam,
|
|
"Read receipt is in backfilled timeline",
|
|
))
|
|
},
|
|
PduCount::Normal(c) => c,
|
|
};
|
|
services
|
|
.rooms
|
|
.read_receipt
|
|
.private_read_set(&body.room_id, sender_user, count);
|
|
},
|
|
_ => return Err(Error::bad_database("Unsupported receipt type")),
|
|
}
|
|
|
|
Ok(create_receipt::v3::Response {})
|
|
}
|