add expiration mechanism that deletes entries older than one week when queried
This commit is contained in:
parent
d16138036a
commit
d3fa6abcf5
1 changed files with 34 additions and 11 deletions
|
@ -2,7 +2,9 @@
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate rocket;
|
extern crate rocket;
|
||||||
|
|
||||||
use chrono::Local;
|
use std::str::FromStr;
|
||||||
|
|
||||||
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use libpixiv::PixivAppClient;
|
use libpixiv::PixivAppClient;
|
||||||
use maud::{html, DOCTYPE};
|
use maud::{html, DOCTYPE};
|
||||||
use rocket::{fairing::AdHoc, http::Status, response::content::RawHtml, State};
|
use rocket::{fairing::AdHoc, http::Status, response::content::RawHtml, State};
|
||||||
|
@ -46,7 +48,7 @@ async fn fetch_illust(
|
||||||
) -> Option<Metadata> {
|
) -> Option<Metadata> {
|
||||||
if let Ok(row) = sqlx::query(
|
if let Ok(row) = sqlx::query(
|
||||||
"
|
"
|
||||||
SELECT p.large, i.title, i.desc
|
SELECT p.large, i.title, i.desc, i.expires_on
|
||||||
FROM Illustrations i
|
FROM Illustrations i
|
||||||
JOIN IllustrationPages p ON p.illust_id = i.id
|
JOIN IllustrationPages p ON p.illust_id = i.id
|
||||||
WHERE p.page_number = 0 AND i.id = ?
|
WHERE p.page_number = 0 AND i.id = ?
|
||||||
|
@ -56,11 +58,23 @@ async fn fetch_illust(
|
||||||
.fetch_one(&db.0)
|
.fetch_one(&db.0)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
return Some(Metadata {
|
if Utc::now() >= DateTime::<Utc>::from_str(row.get(0)).unwrap() {
|
||||||
image: row.get(0),
|
return Some(Metadata {
|
||||||
title: row.get(1),
|
image: row.get(0),
|
||||||
desc: row.get(2),
|
title: row.get(1),
|
||||||
});
|
desc: row.get(2),
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
let _ = sqlx::query(
|
||||||
|
"
|
||||||
|
DELETE FROM Illustrations
|
||||||
|
WHERE id = $1
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.bind(illust_id)
|
||||||
|
.execute(&db.0)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(client) = client {
|
if let Some(client) = client {
|
||||||
|
@ -75,7 +89,8 @@ async fn fetch_illust(
|
||||||
.bind(illust.user.name)
|
.bind(illust.user.name)
|
||||||
.fetch_one(&db.0)
|
.fetch_one(&db.0)
|
||||||
.await
|
.await
|
||||||
.ok().expect("failed to insert user");
|
.ok()
|
||||||
|
.expect("failed to insert user");
|
||||||
|
|
||||||
let illust_query = "
|
let illust_query = "
|
||||||
INSERT OR REPLACE INTO Illustrations (id, title, description, user, expires_on)
|
INSERT OR REPLACE INTO Illustrations (id, title, description, user, expires_on)
|
||||||
|
@ -86,7 +101,7 @@ async fn fetch_illust(
|
||||||
.bind(&illust.title)
|
.bind(&illust.title)
|
||||||
.bind(&illust.caption)
|
.bind(&illust.caption)
|
||||||
.bind(illust.user.id)
|
.bind(illust.user.id)
|
||||||
.bind(Local::now().naive_utc().to_string())
|
.bind((Utc::now() + Duration::days(7)).to_string())
|
||||||
.execute(&db.0)
|
.execute(&db.0)
|
||||||
.await
|
.await
|
||||||
.expect("failed to insert illustration");
|
.expect("failed to insert illustration");
|
||||||
|
@ -100,7 +115,15 @@ async fn fetch_illust(
|
||||||
.bind(illust.image_urls.square_medium)
|
.bind(illust.image_urls.square_medium)
|
||||||
.bind(illust.image_urls.medium)
|
.bind(illust.image_urls.medium)
|
||||||
.bind(illust.image_urls.large)
|
.bind(illust.image_urls.large)
|
||||||
.bind(&illust.meta_single_page.as_ref().unwrap().original_image_url.clone().unwrap())
|
.bind(
|
||||||
|
&illust
|
||||||
|
.meta_single_page
|
||||||
|
.as_ref()
|
||||||
|
.unwrap()
|
||||||
|
.original_image_url
|
||||||
|
.clone()
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
.bind(0)
|
.bind(0)
|
||||||
.bind(illust.id)
|
.bind(illust.id)
|
||||||
.execute(&db.0)
|
.execute(&db.0)
|
||||||
|
|
Loading…
Add table
Reference in a new issue