1
0
Fork 0
mirror of https://forgejo.ellis.link/continuwuation/continuwuity.git synced 2025-07-28 02:38:30 +00:00
continuwuity/src/api/client/read_marker.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

180 lines
5.1 KiB
Rust
Raw Normal View History

2023-02-20 22:59:45 +01:00
use std::collections::BTreeMap;
use axum::extract::State;
use conduit::PduCount;
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},
events::{
receipt::{ReceiptThread, ReceiptType},
RoomAccountDataEventType,
},
MilliSecondsSinceUnixEpoch,
2020-07-30 18:14:47 +02:00
};
use crate::{Error, Result, Ruma};
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
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");
2022-10-09 17:25:06 +02:00
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(
2022-10-09 17:25:06 +02:00
Some(&body.room_id),
sender_user,
RoomAccountDataEventType::FullyRead,
&serde_json::to_value(fully_read_event).expect("to json value always works"),
)?;
}
2022-10-09 17:25:06 +02:00
if body.private_read_receipt.is_some() || body.read_receipt.is_some() {
services
.rooms
.user
.reset_notification_counts(sender_user, &body.room_id)?;
2022-10-09 17:25:06 +02:00
}
2022-10-09 17:25:06 +02:00
if let Some(event) = &body.private_read_receipt {
let count = services
2023-02-20 22:59:45 +01:00
.rooms
.timeline
.get_pdu_count(event)?
.ok_or(Error::BadRequest(ErrorKind::InvalidParam, "Event does not exist."))?;
let count = match count {
PduCount::Backfilled(_) => {
return Err(Error::BadRequest(
2022-10-05 20:34:31 +02:00
ErrorKind::InvalidParam,
2023-02-20 22:59:45 +01:00
"Read receipt is in backfilled timeline",
))
},
PduCount::Normal(c) => c,
};
services
.rooms
.read_receipt
.private_read_set(&body.room_id, sender_user, count)?;
2022-10-09 17:25:06 +02:00
}
2022-10-09 17:25:06 +02:00
if let Some(event) = &body.read_receipt {
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()),
thread: ReceiptThread::Unthreaded,
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);
services.rooms.read_receipt.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
)?;
}
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.
pub(crate) async fn create_receipt_route(
State(services): State<crate::State>, body: Ruma<create_receipt::v3::Request>,
) -> 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-09 17:25:06 +02:00
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)?;
2022-10-09 17:25:06 +02:00
}
2022-10-09 17:25:06 +02:00
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(
2022-10-09 17:25:06 +02:00
Some(&body.room_id),
sender_user,
RoomAccountDataEventType::FullyRead,
&serde_json::to_value(fully_read_event).expect("to json value always works"),
)?;
},
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,
2022-10-09 17:25:06 +02:00
},
);
let mut receipts = BTreeMap::new();
receipts.insert(ReceiptType::Read, user_receipts);
2022-10-09 17:25:06 +02:00
let mut receipt_content = BTreeMap::new();
receipt_content.insert(body.event_id.clone(), receipts);
services.rooms.read_receipt.readreceipt_update(
2022-10-09 17:25:06 +02:00
sender_user,
&body.room_id,
&ruma::events::receipt::ReceiptEvent {
2022-10-09 17:25:06 +02:00
content: ruma::events::receipt::ReceiptEventContent(receipt_content),
room_id: body.room_id.clone(),
},
)?;
},
create_receipt::v3::ReceiptType::ReadPrivate => {
let count = services
2023-02-20 22:59:45 +01:00
.rooms
.timeline
.get_pdu_count(&body.event_id)?
.ok_or(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)?;
2023-02-20 22:59:45 +01:00
},
2022-10-09 17:25:06 +02:00
_ => return Err(Error::bad_database("Unsupported receipt type")),
}
2022-02-18 15:33:14 +01:00
Ok(create_receipt::v3::Response {})
2020-12-19 16:00:11 +01:00
}