2020-07-30 16:14:47 +00:00
|
|
|
use super::State;
|
|
|
|
use crate::{ConduitResult, Database, Error, Ruma};
|
|
|
|
use ruma::{
|
2020-12-19 15:00:11 +00:00
|
|
|
api::client::{
|
2021-01-27 02:53:03 +00:00
|
|
|
error::ErrorKind,
|
2021-03-18 17:33:43 +00:00
|
|
|
r0::{read_marker::set_read_marker, receipt::create_receipt},
|
2020-12-19 15:00:11 +00:00
|
|
|
},
|
2020-07-30 16:14:47 +00:00
|
|
|
events::{AnyEphemeralRoomEvent, AnyEvent, EventType},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[cfg(feature = "conduit_bin")]
|
|
|
|
use rocket::post;
|
|
|
|
use std::{collections::BTreeMap, time::SystemTime};
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/read_markers", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2020-10-21 19:28:02 +00:00
|
|
|
pub async fn set_read_marker_route(
|
2020-07-30 16:14:47 +00:00
|
|
|
db: State<'_, Database>,
|
2020-09-08 15:32:03 +00:00
|
|
|
body: Ruma<set_read_marker::Request<'_>>,
|
2020-07-30 16:14:47 +00:00
|
|
|
) -> ConduitResult<set_read_marker::Response> {
|
2020-10-18 18:33:12 +00:00
|
|
|
let sender_user = body.sender_user.as_ref().expect("user is authenticated");
|
2020-07-30 16:14:47 +00:00
|
|
|
|
|
|
|
let fully_read_event = ruma::events::fully_read::FullyReadEvent {
|
|
|
|
content: ruma::events::fully_read::FullyReadEventContent {
|
|
|
|
event_id: body.fully_read.clone(),
|
|
|
|
},
|
|
|
|
room_id: body.room_id.clone(),
|
|
|
|
};
|
|
|
|
db.account_data.update(
|
|
|
|
Some(&body.room_id),
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-07-30 16:14:47 +00:00
|
|
|
EventType::FullyRead,
|
|
|
|
&fully_read_event,
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
if let Some(event) = &body.read_receipt {
|
2020-08-23 15:29:39 +00:00
|
|
|
db.rooms.edus.private_read_set(
|
2020-07-30 16:14:47 +00:00
|
|
|
&body.room_id,
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-07-30 16:14:47 +00:00
|
|
|
db.rooms.get_pdu_count(event)?.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Event does not exist.",
|
|
|
|
))?,
|
2020-08-23 15:29:39 +00:00
|
|
|
&db.globals,
|
2020-07-30 16:14:47 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut user_receipts = BTreeMap::new();
|
|
|
|
user_receipts.insert(
|
2020-10-18 18:33:12 +00:00
|
|
|
sender_user.clone(),
|
2020-07-30 16:14:47 +00:00
|
|
|
ruma::events::receipt::Receipt {
|
|
|
|
ts: Some(SystemTime::now()),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let mut receipt_content = BTreeMap::new();
|
|
|
|
receipt_content.insert(
|
2020-09-08 15:32:03 +00:00
|
|
|
event.to_owned(),
|
2020-07-30 16:14:47 +00:00
|
|
|
ruma::events::receipt::Receipts {
|
|
|
|
read: Some(user_receipts),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-08-23 15:29:39 +00:00
|
|
|
db.rooms.edus.readreceipt_update(
|
2020-10-18 18:33:12 +00:00
|
|
|
&sender_user,
|
2020-07-30 16:14:47 +00:00
|
|
|
&body.room_id,
|
|
|
|
AnyEvent::Ephemeral(AnyEphemeralRoomEvent::Receipt(
|
|
|
|
ruma::events::receipt::ReceiptEvent {
|
|
|
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
|
|
room_id: body.room_id.clone(),
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
}
|
2020-10-21 19:28:02 +00:00
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
2020-07-30 16:14:47 +00:00
|
|
|
Ok(set_read_marker::Response.into())
|
|
|
|
}
|
2020-12-19 15:00:11 +00:00
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin",
|
|
|
|
post("/_matrix/client/r0/rooms/<_>/receipt/<_>/<_>", data = "<body>")
|
|
|
|
)]
|
2021-02-28 11:41:03 +00:00
|
|
|
#[tracing::instrument(skip(db, body))]
|
2021-03-02 13:32:30 +00:00
|
|
|
pub async fn create_receipt_route(
|
2020-12-19 15:00:11 +00:00
|
|
|
db: State<'_, Database>,
|
2021-03-02 13:32:30 +00: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,
|
|
|
|
db.rooms
|
|
|
|
.get_pdu_count(&body.event_id)?
|
|
|
|
.ok_or(Error::BadRequest(
|
|
|
|
ErrorKind::InvalidParam,
|
|
|
|
"Event does not exist.",
|
|
|
|
))?,
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut user_receipts = BTreeMap::new();
|
|
|
|
user_receipts.insert(
|
|
|
|
sender_user.clone(),
|
|
|
|
ruma::events::receipt::Receipt {
|
|
|
|
ts: Some(SystemTime::now()),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
let mut receipt_content = BTreeMap::new();
|
|
|
|
receipt_content.insert(
|
|
|
|
body.event_id.to_owned(),
|
|
|
|
ruma::events::receipt::Receipts {
|
|
|
|
read: Some(user_receipts),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
db.rooms.edus.readreceipt_update(
|
|
|
|
&sender_user,
|
|
|
|
&body.room_id,
|
|
|
|
AnyEvent::Ephemeral(AnyEphemeralRoomEvent::Receipt(
|
|
|
|
ruma::events::receipt::ReceiptEvent {
|
|
|
|
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
|
|
|
|
room_id: body.room_id.clone(),
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
&db.globals,
|
|
|
|
)?;
|
2020-12-19 15:00:11 +00:00
|
|
|
|
|
|
|
db.flush().await?;
|
|
|
|
|
2021-03-02 13:32:30 +00:00
|
|
|
Ok(create_receipt::Response.into())
|
2020-12-19 15:00:11 +00:00
|
|
|
}
|