Order recent requests first and cover inbox authorization and pagination
This commit is contained in:
@@ -1339,31 +1339,61 @@ async fn cancel_http(
|
||||
let p = principal(&s, &h, true).await?;
|
||||
Ok(Json(cancel(&s, &p, t, id, i).await?))
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
enum OrderSort {
|
||||
Recent,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
struct OrderListQuery {
|
||||
sort: Option<OrderSort>,
|
||||
cursor: Option<String>,
|
||||
limit: Option<u16>,
|
||||
}
|
||||
|
||||
async fn operations_http(
|
||||
State(s): State<crate::State>,
|
||||
h: HeaderMap,
|
||||
Path(t): Path<Uuid>,
|
||||
axum::extract::Query(query): axum::extract::Query<crate::pagination::PageQuery>,
|
||||
axum::extract::Query(query): axum::extract::Query<OrderListQuery>,
|
||||
) -> Result<Json<Value>> {
|
||||
let p = principal(&s, &h, false).await?;
|
||||
scope(&p, t, false)?;
|
||||
let mut tx = rmm_persistence::tenant_tx(&s.pool, t).await?;
|
||||
crate::permissions::require_principal(&mut tx, &p, "operations:read").await?;
|
||||
let mut page = crate::pagination::Cursor::parse(&s, &p, "operations:operation_id:asc", query)
|
||||
.map_err(|status| Error(status, "VALIDATION_ERROR"))?;
|
||||
let ids:Vec<Uuid>=sqlx::query_scalar("SELECT operation_id FROM approval_requests WHERE operation_id>$1 AND created_at<=$2 ORDER BY operation_id LIMIT $3").bind(page.last).bind(page.as_of).bind(i64::from(page.limit)+1).fetch_all(&mut *tx).await?;
|
||||
let recent = matches!(query.sort, Some(OrderSort::Recent));
|
||||
let ordering = if recent { "operations:created_at:desc" } else { "operations:operation_id:asc" };
|
||||
let mut page = crate::pagination::Cursor::parse(&s, &p, ordering, crate::pagination::PageQuery {
|
||||
cursor: query.cursor, limit: query.limit,
|
||||
}).map_err(|status| Error(status, "VALIDATION_ERROR"))?;
|
||||
let limit = i64::from(page.limit) + 1;
|
||||
let ids: Vec<(Uuid, DateTime<Utc>)> = if recent {
|
||||
sqlx::query_as("SELECT operation_id,created_at FROM approval_requests WHERE created_at<=$1
|
||||
AND ($2::timestamptz IS NULL OR (created_at,operation_id)<($2,$3))
|
||||
ORDER BY created_at DESC,operation_id DESC LIMIT $4")
|
||||
.bind(page.as_of).bind(page.last_created_at).bind(page.last).bind(limit)
|
||||
.fetch_all(&mut *tx).await?
|
||||
} else {
|
||||
sqlx::query_as("SELECT operation_id,created_at FROM approval_requests WHERE operation_id>$1 AND created_at<=$2
|
||||
ORDER BY operation_id LIMIT $3")
|
||||
.bind(page.last).bind(page.as_of).bind(limit).fetch_all(&mut *tx).await?
|
||||
};
|
||||
tx.commit().await?;
|
||||
let truncated = ids.len() > usize::from(page.limit);
|
||||
let next = if truncated {
|
||||
let (id, date) = ids[usize::from(page.limit) - 1];
|
||||
page.last_created_at = recent.then_some(date);
|
||||
Some(
|
||||
page.next(&s, ids[usize::from(page.limit) - 1])
|
||||
page.next(&s, id)
|
||||
.map_err(|status| Error(status, "VALIDATION_ERROR"))?,
|
||||
)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let mut items = Vec::new();
|
||||
for id in ids.into_iter().take(usize::from(page.limit)) {
|
||||
for (id, _) in ids.into_iter().take(usize::from(page.limit)) {
|
||||
items.push(operation(&s, &p, t, id).await?)
|
||||
}
|
||||
Ok(Json(
|
||||
|
||||
Reference in New Issue
Block a user