1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-06-27 16:35:59 +00:00
conduit/src/client_server/read_marker.rs

133 lines
4 KiB
Rust
Raw Normal View History

2021-07-14 07:07:08 +00:00
use crate::{database::DatabaseGuard, ConduitResult, Error, Ruma};
2020-07-30 18:14:47 +02:00
use ruma::{
2020-12-19 16:00:11 +01:00
api::client::{
2021-01-26 21:53:03 -05:00
error::ErrorKind,
2021-03-18 18:33:43 +01:00
r0::{read_marker::set_read_marker, receipt::create_receipt},
2020-12-19 16:00:11 +01:00
},
events::EventType,
receipt::ReceiptType,
MilliSecondsSinceUnixEpoch,
2020-07-30 18:14:47 +02:00
};
2021-07-14 07:07:08 +00:00
use std::collections::BTreeMap;
2020-07-30 18:14:47 +02:00
2021-08-31 19:14:37 +02:00
/// # `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
2021-02-28 12:41:03 +01:00
#[tracing::instrument(skip(db, body))]
pub async fn set_read_marker_route(
2021-07-14 07:07:08 +00:00
db: DatabaseGuard,
2020-09-08 17:32:03 +02:00
body: Ruma<set_read_marker::Request<'_>>,
2020-07-30 18:14:47 +02:00
) -> ConduitResult<set_read_marker::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
2020-07-30 18:14:47 +02:00
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
content: ruma::events::fully_read::FullyReadEventContent {
event_id: body.fully_read.clone(),
},
};
db.account_data.update(
Some(&body.room_id),
sender_user,
2020-07-30 18:14:47 +02:00
EventType::FullyRead,
&fully_read_event,
&db.globals,
)?;
if let Some(event) = &body.read_receipt {
db.rooms.edus.private_read_set(
2020-07-30 18:14:47 +02:00
&body.room_id,
sender_user,
2020-07-30 18:14:47 +02:00
db.rooms.get_pdu_count(event)?.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Event does not exist.",
))?,
&db.globals,
2020-07-30 18:14:47 +02:00
)?;
db.rooms
.reset_notification_counts(sender_user, &body.room_id)?;
2020-07-30 18:14:47 +02:00
let mut user_receipts = BTreeMap::new();
user_receipts.insert(
sender_user.clone(),
2020-07-30 18:14:47 +02:00
ruma::events::receipt::Receipt {
ts: Some(MilliSecondsSinceUnixEpoch::now()),
2020-07-30 18:14:47 +02:00
},
);
let mut receipts = BTreeMap::new();
receipts.insert(ReceiptType::Read, user_receipts);
2020-07-30 18:14:47 +02:00
let mut receipt_content = BTreeMap::new();
receipt_content.insert(event.to_owned(), receipts);
2020-07-30 18:14:47 +02:00
db.rooms.edus.readreceipt_update(
sender_user,
2020-07-30 18:14:47 +02:00
&body.room_id,
ruma::events::receipt::ReceiptEvent {
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
room_id: body.room_id.clone(),
},
2020-07-30 18:14:47 +02:00
&db.globals,
)?;
}
db.flush()?;
Ok(set_read_marker::Response {}.into())
2020-07-30 18:14:47 +02:00
}
2020-12-19 16:00:11 +01:00
2021-08-31 19:14:37 +02:00
/// # `POST /_matrix/client/r0/rooms/{roomId}/receipt/{receiptType}/{eventId}`
///
/// Sets private read marker and public read receipt EDU.
2021-02-28 12:41:03 +01:00
#[tracing::instrument(skip(db, body))]
2021-03-02 14:32:30 +01:00
pub async fn create_receipt_route(
2021-07-14 07:07:08 +00:00
db: DatabaseGuard,
2021-03-02 14:32:30 +01:00
body: Ruma<create_receipt::Request<'_>>,
) -> ConduitResult<create_receipt::Response> {
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
db.rooms.edus.private_read_set(
&body.room_id,
sender_user,
2021-03-02 14:32:30 +01:00
db.rooms
.get_pdu_count(&body.event_id)?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Event does not exist.",
))?,
&db.globals,
)?;
db.rooms
.reset_notification_counts(sender_user, &body.room_id)?;
2021-03-02 14:32:30 +01:00
let mut user_receipts = BTreeMap::new();
user_receipts.insert(
sender_user.clone(),
ruma::events::receipt::Receipt {
ts: Some(MilliSecondsSinceUnixEpoch::now()),
2021-03-02 14:32:30 +01:00
},
);
let mut receipts = BTreeMap::new();
receipts.insert(ReceiptType::Read, user_receipts);
2021-03-02 14:32:30 +01:00
let mut receipt_content = BTreeMap::new();
receipt_content.insert(body.event_id.to_owned(), receipts);
2021-03-02 14:32:30 +01:00
db.rooms.edus.readreceipt_update(
sender_user,
2021-03-02 14:32:30 +01:00
&body.room_id,
ruma::events::receipt::ReceiptEvent {
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
room_id: body.room_id.clone(),
},
2021-03-02 14:32:30 +01:00
&db.globals,
)?;
2020-12-19 16:00:11 +01:00
db.flush()?;
2020-12-19 16:00:11 +01:00
Ok(create_receipt::Response {}.into())
2020-12-19 16:00:11 +01:00
}