Skip to content
This repository has been archived by the owner on Nov 23, 2022. It is now read-only.

Commit

Permalink
Merge pull request #52 from jay3332/patch-1
Browse files Browse the repository at this point in the history
Implement `offset` query parameter
  • Loading branch information
hydrostaticcog authored Nov 14, 2021
2 parents 74c57ee + 6c9779f commit 4f13c9b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions ferrischat_webserver/src/messages/message_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub async fn get_message_history(
let GetMessageHistoryParams {
mut limit,
oldest_first,
mut offset,
} = params.0;

if limit < Some(0) {
Expand All @@ -33,12 +34,17 @@ pub async fn get_message_history(
limit = None;
}

if offset >= Some(9223372036854775807) || offset < Some(0) {
offset = Some(0);
}

let messages = {
if oldest_first == Some(true) {
let resp = sqlx::query!(
"SELECT m.*, a.name AS author_name, a.flags AS author_flags, a.discriminator AS author_discriminator FROM messages m CROSS JOIN LATERAL (SELECT * FROM users WHERE id = m.author_id) as a WHERE channel_id = $1 ORDER BY id ASC LIMIT $2",
"SELECT m.*, a.name AS author_name, a.flags AS author_flags, a.discriminator AS author_discriminator FROM messages m CROSS JOIN LATERAL (SELECT * FROM users WHERE id = m.author_id) as a WHERE channel_id = $1 ORDER BY id ASC LIMIT $2 OFFSET $3",
bigint_channel_id,
limit
limit,
offset,
)
.fetch_all(db)
.await;
Expand Down Expand Up @@ -88,9 +94,10 @@ pub async fn get_message_history(
}
} else {
let resp = sqlx::query!(
"SELECT m.*, a.name AS author_name, a.flags AS author_flags, a.discriminator AS author_discriminator FROM messages m CROSS JOIN LATERAL (SELECT * FROM users WHERE id = m.author_id) as a WHERE channel_id = $1 ORDER BY id DESC LIMIT $2",
"SELECT m.*, a.name AS author_name, a.flags AS author_flags, a.discriminator AS author_discriminator FROM messages m CROSS JOIN LATERAL (SELECT * FROM users WHERE id = m.author_id) as a WHERE channel_id = $1 ORDER BY id DESC LIMIT $2 OFFSET $3",
bigint_channel_id,
limit
limit,
offset,
)
.fetch_all(db)
.await;
Expand Down

0 comments on commit 4f13c9b

Please sign in to comment.