2022-10-05 20:34:31 +02:00
|
|
|
use crate::{services, Error, Result, Ruma};
|
2020-07-30 18:14:47 +02:00
|
|
|
use ruma::{
|
2022-02-18 15:33:14 +01:00
|
|
|
api::client::{error::ErrorKind, read_marker::set_read_marker, receipt::create_receipt},
|
2022-04-06 21:31:29 +02:00
|
|
|
events::RoomAccountDataEventType,
|
2021-05-20 23:46:52 +02:00
|
|
|
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
|
2020-10-21 21:28:02 +02:00
|
|
|
pub async fn set_read_marker_route(
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<set_read_marker::v3::IncomingRequest>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<set_read_marker::v3::Response> {
|
2020-10-18 20:33:12 +02:00
|
|
|
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(),
|
|
|
|
},
|
|
|
|
};
|
2022-09-06 23:15:09 +02:00
|
|
|
services().account_data.update(
|
2020-07-30 18:14:47 +02:00
|
|
|
Some(&body.room_id),
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
2022-04-06 21:31:29 +02:00
|
|
|
RoomAccountDataEventType::FullyRead,
|
2022-10-05 15:33:57 +02:00
|
|
|
&serde_json::to_value(fully_read_event).expect("to json value always works"),
|
2020-07-30 18:14:47 +02:00
|
|
|
)?;
|
|
|
|
|
|
|
|
if let Some(event) = &body.read_receipt {
|
2022-10-05 09:34:25 +02:00
|
|
|
services().rooms.edus.read_receipt.private_read_set(
|
2020-07-30 18:14:47 +02:00
|
|
|
&body.room_id,
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
2022-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.rooms
|
|
|
|
.timeline
|
|
|
|
.get_pdu_count(event)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Event does not exist.",
|
|
|
|
))?,
|
2020-07-30 18:14:47 +02:00
|
|
|
)?;
|
2022-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.rooms
|
|
|
|
.user
|
2021-09-13 19:45:56 +02:00
|
|
|
.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(
|
2020-10-18 20:33:12 +02:00
|
|
|
sender_user.clone(),
|
2020-07-30 18:14:47 +02:00
|
|
|
ruma::events::receipt::Receipt {
|
2021-05-20 23:46:52 +02:00
|
|
|
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
2020-07-30 18:14:47 +02:00
|
|
|
},
|
|
|
|
);
|
2021-05-20 23:46:52 +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();
|
2021-05-20 23:46:52 +02:00
|
|
|
receipt_content.insert(event.to_owned(), receipts);
|
2020-07-30 18:14:47 +02:00
|
|
|
|
2022-10-05 09:34:25 +02:00
|
|
|
services().rooms.edus.read_receipt.readreceipt_update(
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
2020-07-30 18:14:47 +02:00
|
|
|
&body.room_id,
|
2022-02-12 01:58:36 +01:00
|
|
|
ruma::events::receipt::ReceiptEvent {
|
2021-05-20 23:46:52 +02:00
|
|
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
|
|
room_id: body.room_id.clone(),
|
2022-02-12 01:58:36 +01:00
|
|
|
},
|
2020-07-30 18:14:47 +02:00
|
|
|
)?;
|
|
|
|
}
|
2020-10-21 21:28:02 +02:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(set_read_marker::v3::Response {})
|
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-03-02 14:32:30 +01:00
|
|
|
pub async fn create_receipt_route(
|
2022-04-06 21:31:29 +02:00
|
|
|
body: Ruma<create_receipt::v3::IncomingRequest>,
|
2022-02-18 15:33:14 +01:00
|
|
|
) -> Result<create_receipt::v3::Response> {
|
2021-03-02 14:32:30 +01:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
|
|
|
|
2022-10-05 09:34:25 +02:00
|
|
|
services().rooms.edus.read_receipt.private_read_set(
|
2021-03-02 14:32:30 +01:00
|
|
|
&body.room_id,
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
2022-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.rooms
|
2022-10-05 09:34:25 +02:00
|
|
|
.timeline
|
2021-03-02 14:32:30 +01:00
|
|
|
.get_pdu_count(&body.event_id)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Event does not exist.",
|
|
|
|
))?,
|
|
|
|
)?;
|
2022-10-05 20:34:31 +02:00
|
|
|
services()
|
|
|
|
.rooms
|
|
|
|
.user
|
2021-09-13 19:45:56 +02:00
|
|
|
.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 {
|
2021-05-20 23:46:52 +02:00
|
|
|
ts: Some(MilliSecondsSinceUnixEpoch::now()),
|
2021-03-02 14:32:30 +01:00
|
|
|
},
|
|
|
|
);
|
2021-05-20 23:46:52 +02: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();
|
2021-05-20 23:46:52 +02:00
|
|
|
receipt_content.insert(body.event_id.to_owned(), receipts);
|
2021-03-02 14:32:30 +01:00
|
|
|
|
2022-10-05 09:34:25 +02:00
|
|
|
services().rooms.edus.read_receipt.readreceipt_update(
|
2021-09-13 19:45:56 +02:00
|
|
|
sender_user,
|
2021-03-02 14:32:30 +01:00
|
|
|
&body.room_id,
|
2022-02-12 01:58:36 +01:00
|
|
|
ruma::events::receipt::ReceiptEvent {
|
2021-05-20 23:46:52 +02:00
|
|
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
|
|
room_id: body.room_id.clone(),
|
2022-02-12 01:58:36 +01:00
|
|
|
},
|
2021-03-02 14:32:30 +01:00
|
|
|
)?;
|
2020-12-19 16:00:11 +01:00
|
|
|
|
2022-02-18 15:33:14 +01:00
|
|
|
Ok(create_receipt::v3::Response {})
|
2020-12-19 16:00:11 +01:00
|
|
|
}
|