diff --git a/Cargo.lock b/Cargo.lock index 49dc624..9cb4bba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 38b046b..fbe6d73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/main.rs b/src/main.rs index 223c22e..e01053d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,6 +117,12 @@ async fn migrate(rocket: Rocket) -> Rocket { 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]) diff --git a/src/routes/posts.rs b/src/routes/posts.rs index e69de29..c3a51eb 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -0,0 +1,50 @@ +/* +* POST /posts: uses json request body to get the post info and, y'know, create it +* GET /posts/: figures out what type of id is, gets post via that, returns it +* UPDATE /posts/: figures out what type of id is, uses json request body to update +* specific data about the post +* DELETE /posts/: 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 = "")] +pub async fn create( + mut db: Connection, + info: Json, + cookies: &CookieJar<'_>, +) -> status::Custom { + 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()) + } + } +} diff --git a/src/tables/posts.rs b/src/tables/posts.rs index 21cea1e..5d77927 100644 --- a/src/tables/posts.rs +++ b/src/tables/posts.rs @@ -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, 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::("title"), body: res.get::("body"), published: res.get::("published"), + timestamp: res.get::("timestamp") } } pub async fn get_by_uuid(mut db: Connection, uuid: String) -> Post { @@ -71,6 +73,7 @@ impl Post { title: res.get::("title"), body: res.get::("body"), published: res.get::("published"), + timestamp: res.get::("timestamp") } } pub async fn get_by_text_id(mut db: Connection, text_id: String) -> Post { @@ -85,6 +88,7 @@ impl Post { title: res.get::("title"), body: res.get::("body"), published: res.get::("published"), + timestamp: res.get::("timestamp") } }