start working on routes for posts

This commit is contained in:
SadlyNotSappho 2024-04-05 11:55:00 -07:00
parent 7462a36515
commit 518dd26b0f
5 changed files with 205 additions and 6 deletions

138
Cargo.lock generated
View File

@ -80,6 +80,21 @@ version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "async-stream"
version = "0.3.5"
@ -216,6 +231,12 @@ dependencies = [
"generic-array",
]
[[package]]
name = "bumpalo"
version = "3.15.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ff69b9dd49fd426c69a0db9fc04dd934cdb6645ff000864d98f7e2af8830eaa"
[[package]]
name = "bytemuck"
version = "1.14.0"
@ -249,6 +270,18 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a"
dependencies = [
"android-tzdata",
"iana-time-zone",
"num-traits",
"windows-targets 0.52.0",
]
[[package]]
name = "cipher"
version = "0.4.4"
@ -288,6 +321,12 @@ dependencies = [
"version_check",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "cpufeatures"
version = "0.2.11"
@ -922,6 +961,29 @@ dependencies = [
"want",
]
[[package]]
name = "iana-time-zone"
version = "0.1.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "idna"
version = "0.4.0"
@ -1021,6 +1083,15 @@ dependencies = [
"rayon",
]
[[package]]
name = "js-sys"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
@ -2055,6 +2126,7 @@ dependencies = [
"atoi",
"byteorder",
"bytes",
"chrono",
"crc",
"crossbeam-queue",
"dotenvy",
@ -2139,6 +2211,7 @@ dependencies = [
"bitflags 2.4.1",
"byteorder",
"bytes",
"chrono",
"crc",
"digest",
"dotenvy",
@ -2180,6 +2253,7 @@ dependencies = [
"base64",
"bitflags 2.4.1",
"byteorder",
"chrono",
"crc",
"dotenvy",
"etcetera",
@ -2216,6 +2290,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "210976b7d948c7ba9fced8ca835b11cbb2d677c59c79de41ac0d397e14547490"
dependencies = [
"atoi",
"chrono",
"flume",
"futures-channel",
"futures-core",
@ -2713,6 +2788,60 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn 2.0.39",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.39",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "webpki-roots"
version = "0.25.3"
@ -2762,6 +2891,15 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-core"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-sys"
version = "0.48.0"

View File

@ -12,10 +12,10 @@ markdown = "1.0.0-alpha.16"
rand = "0.8.5"
rand_hc = "0.3.2"
regex = "1.10.3"
rocket = {version="0.5.0",features=["secrets","json"]}
rocket = { version = "0.5.0", features = ["secrets", "json"] }
rocket_cors = "0.6.0"
rocket_db_pools = {version="0.1.0",features=["sqlx_postgres"]}
rocket_db_pools = { version = "0.1.0", features = ["sqlx_postgres"] }
sha256 = "1.5.0"
sqlx = { version = "0.7.3", features = ["macros", "postgres"] }
sqlx = { version = "0.7.3", features = ["macros", "postgres", "chrono"] }
uuid = { version = "1.7.0", features = ["v4"] }
# rocket_sync_db_pools = { version = "0.1.0", features = ["diesel_postgres_pool", "diesel"] }

View File

@ -117,6 +117,12 @@ async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
ADD COLUMN IF NOT EXISTS text_id TEXT NOT NULL UNIQUE",
))
.await;
let _ = conn
.execute(sqlx::query(
"ALTER TABLE posts
ADD COLUMN IF NOT EXISTS timestamp TIMESTAMP DEFAULT localtimestamp NOT NULL",
))
.await;
rocket
}
@ -159,6 +165,7 @@ async fn main() {
routes::images::delete_image,
routes::images::get_images_by_username,
// posts stuff
routes::posts::create,
],
)
.mount("/api", routes![routes::users::api_perms])

View File

@ -0,0 +1,50 @@
/*
* POST /posts: uses json request body to get the post info and, y'know, create it
* GET /posts/<id>: figures out what type of id <id> is, gets post via that, returns it
* UPDATE /posts/<id>: figures out what type of id <id> is, uses json request body to update
* specific data about the post
* DELETE /posts/<id>: you can figure out what this one does
*
* GET /posts: gets all posts, maybe json request body for args?
*/
use crate::tables::{users::LoginStatus, users::User, Db};
use rocket::http::CookieJar;
use rocket::http::Status;
use rocket::post;
use rocket::response::status;
use rocket::serde::json::Json;
use rocket::serde::Deserialize;
use rocket_db_pools::Connection;
#[derive(Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct PostCreateInfo {
pub title: String,
pub text_id: String,
pub body: String,
}
#[post("/posts", data = "<info>")]
pub async fn create(
mut db: Connection<Db>,
info: Json<PostCreateInfo>,
cookies: &CookieJar<'_>,
) -> status::Custom<String> {
match User::login_status(&mut db, cookies).await {
LoginStatus::LoggedIn(user) => {
// if post with same text id exists, fail
// make sure user has perms to do this first tho
// and uhhhhh
// yeah thats it idk
// TODO: implement all that
status::Custom(Status::NotImplemented, "Not implemented yet".to_string())
}
LoginStatus::InvalidToken => {
status::Custom(Status::Unauthorized, "Invalid login token".to_string())
}
LoginStatus::NotLoggedIn => {
status::Custom(Status::Unauthorized, "Not logged in".to_string())
}
}
}

View File

@ -1,8 +1,8 @@
use crate::tables::Db;
use rocket_db_pools::sqlx::Executor;
use rocket_db_pools::sqlx::{self, Row};
use rocket_db_pools::Connection;
use crate::tables::Db;
use sqlx::types::chrono::NaiveDateTime;
use sqlx::FromRow;
#[derive(FromRow)]
@ -13,12 +13,13 @@ pub struct Post {
pub title: String,
pub body: String,
pub published: bool,
pub timestamp: NaiveDateTime,
}
impl Post {
pub async fn create(
mut db: Connection<Db>,
title: String, /*ex: Why Trans People Deserve All Your Money*/
body: String, /*ex: # Because we're cooler than you */
body: String, /*ex: # Because we're cooler than you \n\n![trans flag image](https://sadlynotsappho.dev/pfp.png)*/
published: bool,
uuid: String,
text_id: String, /*ex: why-trans-people-deserve-all-your-money */
@ -57,6 +58,7 @@ impl Post {
title: res.get::<String, _>("title"),
body: res.get::<String, _>("body"),
published: res.get::<bool, _>("published"),
timestamp: res.get::<NaiveDateTime, _>("timestamp")
}
}
pub async fn get_by_uuid(mut db: Connection<Db>, uuid: String) -> Post {
@ -71,6 +73,7 @@ impl Post {
title: res.get::<String, _>("title"),
body: res.get::<String, _>("body"),
published: res.get::<bool, _>("published"),
timestamp: res.get::<NaiveDateTime, _>("timestamp")
}
}
pub async fn get_by_text_id(mut db: Connection<Db>, text_id: String) -> Post {
@ -85,6 +88,7 @@ impl Post {
title: res.get::<String, _>("title"),
body: res.get::<String, _>("body"),
published: res.get::<bool, _>("published"),
timestamp: res.get::<NaiveDateTime, _>("timestamp")
}
}