add: paginate notifications/requests pages

This commit is contained in:
trisua 2025-06-02 21:39:35 -04:00
parent 4be98ead53
commit 1ac64d34ed
5 changed files with 74 additions and 6 deletions

View file

@ -54,7 +54,9 @@
(text "{{ text \"notifs:label.mark_all_as_unread\" }}")))))))
(div
("class" "card tertiary flex flex-col gap-4")
(text "{% for notification in notifications %} {{ components::notification(notification=notification) }} {% endfor %}"))))
(text "{% for notification in notifications %} {{ components::notification(notification=notification) }} {% endfor %}")))
(text "{{ components::pagination(page=page, items=notifications|length, key=\"&id=\", value=profile.id) }}"))
(script
(text "async function mark_all_as_read(read) {

View file

@ -128,7 +128,9 @@
("class" "red quaternary")
("onclick" "trigger('me::ip_block_question', ['{{ question[0].id }}'])")
(text "{{ text \"auth:action.ip_block\" }}")))))
(text "{% endfor %}"))))
(text "{% endfor %}")))
(text "{{ components::pagination(page=page, items=requests|length, key=\"&id=\", value=profile.id) }}"))
(script
(text "async function remove_request(id, linked_asset) {

View file

@ -361,6 +361,8 @@ pub async fn all_questions_request(
pub struct NotificationsProps {
#[serde(default)]
pub id: usize,
#[serde(default)]
pub page: usize,
}
/// `/notifs`
@ -379,7 +381,7 @@ pub async fn notifications_request(
}
};
let profile = if props.id != 0 {
let profile = if props.id != 0 && user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS) {
match data.0.get_user_by_id(props.id).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &None).await)),
@ -388,7 +390,11 @@ pub async fn notifications_request(
user.clone()
};
let notifications = match data.0.get_notifications_by_owner(profile.id).await {
let notifications = match data
.0
.get_notifications_by_owner_paginated(profile.id, 12, props.page)
.await
{
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
};
@ -396,6 +402,7 @@ pub async fn notifications_request(
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
context.insert("page", &props.page);
context.insert("profile", &profile);
context.insert("notifications", &notifications);
@ -421,7 +428,7 @@ pub async fn requests_request(
}
};
let profile = if props.id != 0 {
let profile = if props.id != 0 && user.permissions.check(FinePermission::MANAGE_REQUESTS) {
match data.0.get_user_by_id(props.id).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &None).await)),
@ -432,7 +439,11 @@ pub async fn requests_request(
let requests = match data
.0
.get_requests_by_owner(if props.id != 0 { props.id } else { user.id })
.get_requests_by_owner_paginated(
if props.id != 0 { props.id } else { user.id },
12,
props.page,
)
.await
{
Ok(p) => p,
@ -481,6 +492,7 @@ pub async fn requests_request(
let lang = get_lang!(jar, data.0);
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
context.insert("page", &props.page);
context.insert("profile", &profile);
context.insert("requests", &requests);
context.insert("questions", &questions);

View file

@ -53,6 +53,32 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all notifications by `owner` (paginated).
pub async fn get_notifications_by_owner_paginated(
&self,
owner: usize,
batch: usize,
page: usize,
) -> Result<Vec<Notification>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM notifications WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_notification_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("notification".to_string()));
}
Ok(res.unwrap())
}
/// Get all notifications by `tag`.
pub async fn get_notifications_by_tag(&self, tag: &str) -> Result<Vec<Notification>> {
let conn = match self.connect().await {

View file

@ -86,6 +86,32 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all action requests by `owner` (paginated).
pub async fn get_requests_by_owner_paginated(
&self,
owner: usize,
batch: usize,
page: usize,
) -> Result<Vec<ActionRequest>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM requests WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_request_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("request".to_string()));
}
Ok(res.unwrap())
}
/// Create a new request in the database.
///
/// # Arguments