aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorXe Iaso <me@christine.website>2022-07-24 18:21:35 +0000
committerXe Iaso <me@christine.website>2022-07-24 18:22:51 +0000
commite870ccac818565c5ad92c4b085bc3ce82a9bafab (patch)
treec3ccd93acde518df8965a3d40233f507f62116ee /src
parentecdf2115f83f762c7236f2b6cf4f9655e27aa601 (diff)
downloadxesite-e870ccac818565c5ad92c4b085bc3ce82a9bafab.tar.xz
xesite-e870ccac818565c5ad92c4b085bc3ce82a9bafab.zip
notes: finish features
Pagination and comments fixed Signed-off-by: Xe <me@christine.website>
Diffstat (limited to 'src')
-rw-r--r--src/handlers/notes.rs33
-rw-r--r--src/migrate/base_schema.sql6
2 files changed, 28 insertions, 11 deletions
diff --git a/src/handlers/notes.rs b/src/handlers/notes.rs
index 304a370..1eabcd4 100644
--- a/src/handlers/notes.rs
+++ b/src/handlers/notes.rs
@@ -1,7 +1,12 @@
use std::{net::SocketAddr, sync::Arc};
use crate::{app::State, templates};
-use axum::{extract::Path, http::HeaderMap, response::Html, Extension, Json};
+use axum::{
+ extract::{Path, Query},
+ http::HeaderMap,
+ response::Html,
+ Extension, Json,
+};
use chrono::prelude::*;
use maud::{html, Markup, PreEscaped};
use rusqlite::params;
@@ -122,13 +127,22 @@ impl Into<xe_jsonfeed::Item> for Note {
}
}
+#[derive(Clone, Debug, Default, Serialize, Deserialize)]
+pub struct Pagination {
+ pub page: Option<u64>,
+}
+
#[instrument(err, skip(state))]
-pub async fn index(Extension(state): Extension<Arc<State>>) -> super::Result {
+pub async fn index(
+ Extension(state): Extension<Arc<State>>,
+ Query(pagi): Query<Pagination>,
+) -> super::Result {
let conn = state.pool.get().await?;
+ let page = pagi.page.unwrap_or(0);
- let mut stmt = conn.prepare("SELECT id, content, content_html, created_at, updated_at, deleted_at, reply_to FROM notes WHERE deleted_at IS NULL ORDER BY id DESC LIMIT 25")?;
+ let mut stmt = conn.prepare("SELECT id, content, content_html, created_at, updated_at, deleted_at, reply_to FROM notes WHERE deleted_at IS NULL ORDER BY id DESC LIMIT 25 OFFSET ?")?;
let notes = stmt
- .query_map(params![], |row| {
+ .query_map(params![page * 25], |row| {
Ok(Note {
id: row.get(0)?,
content: row.get(1)?,
@@ -144,19 +158,21 @@ pub async fn index(Extension(state): Extension<Arc<State>>) -> super::Result {
.collect::<Vec<Note>>();
let mut result: Vec<u8> = vec![];
- templates::notesindex_html(&mut result, notes)?;
+ templates::notesindex_html(&mut result, notes, page)?;
Ok(Html(result))
}
#[instrument(err, skip(state))]
pub async fn feed(
Extension(state): Extension<Arc<State>>,
+ Query(pagi): Query<Pagination>,
) -> super::Result<Json<xe_jsonfeed::Feed>> {
let conn = state.pool.get().await?;
+ let page = pagi.page.unwrap_or(0);
- let mut stmt = conn.prepare("SELECT id, content, content_html, created_at, updated_at, deleted_at, reply_to FROM notes WHERE deleted_at IS NULL ORDER BY id DESC LIMIT 25")?;
+ let mut stmt = conn.prepare("SELECT id, content, content_html, created_at, updated_at, deleted_at, reply_to FROM notes WHERE deleted_at IS NULL ORDER BY id DESC LIMIT 25 OFFSET ?")?;
let notes = stmt
- .query_map(params![], |row| {
+ .query_map(params![page * 25], |row| {
Ok(Note {
id: row.get(0)?,
content: row.get(1)?,
@@ -179,7 +195,8 @@ pub async fn feed(
.avatar("https://xeiaso.net/static/img/avatar.png"),
)
.description("Short posts that aren't to the same quality level as mainline blogposts")
- .feed_url("https://xeiaso.net/notes.json")
+ .feed_url(format!("https://xeiaso.net/notes.json?page={page}"))
+ .next_url(format!("https://xeiaso.net/notes.json?page={}", page + 1))
.title("Xe's Notes");
for note in notes {
diff --git a/src/migrate/base_schema.sql b/src/migrate/base_schema.sql
index 65d19d0..3b91ccf 100644
--- a/src/migrate/base_schema.sql
+++ b/src/migrate/base_schema.sql
@@ -2,9 +2,9 @@ CREATE TABLE IF NOT EXISTS notes
( id INTEGER PRIMARY KEY
, content TEXT NOT NULL
, content_html TEXT NOT NULL
- , created_at TEXT NOT NULL -- Unix epoch timestamp
- , updated_at TEXT -- Unix epoch timestamp
- , deleted_at TEXT -- Unix epoch timestamp
+ , created_at TEXT NOT NULL -- RFC 3339 timestamp
+ , updated_at TEXT -- RFC 3339 timestamp
+ , deleted_at TEXT -- RFC 3339 timestamp
, reply_to TEXT
);