start working on posts

This commit is contained in:
SadlyNotSappho 2024-03-29 11:39:44 -07:00
parent 314c1bec7d
commit bfb63afa7e
4 changed files with 275 additions and 44 deletions

16
Cargo.lock generated
View File

@ -597,6 +597,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"base64", "base64",
"image", "image",
"markdown",
"rand", "rand",
"rand_hc", "rand_hc",
"regex", "regex",
@ -1095,6 +1096,15 @@ dependencies = [
"tracing-subscriber", "tracing-subscriber",
] ]
[[package]]
name = "markdown"
version = "1.0.0-alpha.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0f0025e8c0d89b84d6dc63e859475e40e8e82ab1a08be0a93ad5731513a508"
dependencies = [
"unicode-id",
]
[[package]] [[package]]
name = "matchers" name = "matchers"
version = "0.1.0" version = "0.1.0"
@ -2589,6 +2599,12 @@ version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f2528f27a9eb2b21e69c95319b30bd0efd85d09c379741b0f78ea1d86be2416" checksum = "6f2528f27a9eb2b21e69c95319b30bd0efd85d09c379741b0f78ea1d86be2416"
[[package]]
name = "unicode-id"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1b6def86329695390197b82c1e244a54a131ceb66c996f2088a3876e2ae083f"
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"

View File

@ -8,6 +8,7 @@ edition = "2021"
[dependencies] [dependencies]
base64 = "0.21.7" base64 = "0.21.7"
image = "0.24.8" image = "0.24.8"
markdown = "1.0.0-alpha.16"
rand = "0.8.5" rand = "0.8.5"
rand_hc = "0.3.2" rand_hc = "0.3.2"
regex = "1.10.3" regex = "1.10.3"

View File

@ -626,6 +626,13 @@ async fn migrate(rocket: Rocket<Build>) -> Rocket<Build> {
)) ))
.await; .await;
let _ = conn
.execute(sqlx::query(
"ALTER TABLE posts
ADD COLUMN IF NOT EXISTS uuid TEXT NOT NULL UNIQUE,
ADD COLUMN IF NOT EXISTS text_id TEXT NOT NULL UNIQUE",
))
.await;
rocket rocket
} }

View File

@ -17,23 +17,34 @@ pub struct Db(PgPool);
#[derive(FromRow)] #[derive(FromRow)]
pub struct Post { pub struct Post {
pub id: i32, pub id: i32,
pub uuid: String,
pub text_id: String,
pub title: String, pub title: String,
pub body: String, pub body: String,
pub published: bool, pub published: bool,
} }
impl Post { impl Post {
pub async fn create(mut db: Connection<Db>, title: String, body: String, published: bool) { 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 */
published: bool,
uuid: String,
text_id: String, /*ex: why-trans-people-deserve-all-your-money */
) {
match db match db
.fetch_all( .fetch_all(
sqlx::query( sqlx::query(
r#" r#"
INSERT INTO posts (title, body, published) INSERT INTO posts (title, body, published, uuid, text_id)
VALUES ($1, $2, $3); VALUES ($1, $2, $3, $4, $5);
"#, "#,
) )
.bind(title) .bind(title)
.bind(body) .bind(body)
.bind(published), .bind(published)
.bind(uuid)
.bind(text_id),
) )
.await .await
{ {
@ -50,12 +61,204 @@ impl Post {
.unwrap(); .unwrap();
Post { Post {
id: res.get::<i32, _>("id"), id: res.get::<i32, _>("id"),
uuid: res.get::<String, _>("uuid"),
text_id: res.get::<String, _>("text_id"),
title: res.get::<String, _>("title"), title: res.get::<String, _>("title"),
body: res.get::<String, _>("body"), body: res.get::<String, _>("body"),
published: res.get::<bool, _>("published"), published: res.get::<bool, _>("published"),
} }
} }
pub async fn get_by_uuid(mut db: Connection<Db>, uuid: String) -> Post {
let res = db
.fetch_one(sqlx::query("SELECT * FROM posts WHERE uuid = $1;").bind(uuid))
.await
.unwrap();
Post {
id: res.get::<i32, _>("id"),
uuid: res.get::<String, _>("uuid"),
text_id: res.get::<String, _>("text_id"),
title: res.get::<String, _>("title"),
body: res.get::<String, _>("body"),
published: res.get::<bool, _>("published"),
}
}
pub async fn get_by_text_id(mut db: Connection<Db>, text_id: String) -> Post {
let res = db
.fetch_one(sqlx::query("SELECT * FROM posts WHERE text_id = $1;").bind(text_id))
.await
.unwrap();
Post {
id: res.get::<i32, _>("id"),
uuid: res.get::<String, _>("uuid"),
text_id: res.get::<String, _>("text_id"),
title: res.get::<String, _>("title"),
body: res.get::<String, _>("body"),
published: res.get::<bool, _>("published"),
}
}
pub async fn edit_body(
mut db: Connection<Db>,
id: i32,
new_body: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET body = $1 WHERE id = $2;")
.bind(new_body)
.bind(id),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
pub async fn edit_body_by_uuid(
mut db: Connection<Db>,
uuid: String,
new_body: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET body = $1 WHERE uuid = $2;")
.bind(new_body)
.bind(uuid),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
pub async fn edit_body_by_text_id(
mut db: Connection<Db>,
text_id: String,
new_body: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET body = $1 WHERE text_id = $2;")
.bind(new_body)
.bind(text_id),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
pub async fn edit_title(
mut db: Connection<Db>,
id: i32,
new_title: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET title = $1 WHERE id = $2;")
.bind(new_title)
.bind(id),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
pub async fn edit_title_by_uuid(
mut db: Connection<Db>,
uuid: String,
new_title: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET title = $1 WHERE uuid = $2;")
.bind(new_title)
.bind(uuid),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
pub async fn edit_title_by_text_id(
mut db: Connection<Db>,
text_id: String,
new_title: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET body = $1 WHERE text_id = $2;")
.bind(new_title)
.bind(text_id),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
pub async fn edit_text_id(
mut db: Connection<Db>,
id: i32,
new_text_id: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET text_id = $1 WHERE id = $2;")
.bind(new_text_id)
.bind(id),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
pub async fn edit_text_id_by_uuid(
mut db: Connection<Db>,
uuid: String,
new_text_id: String,
) -> Result<(), String> {
match db
.execute(
sqlx::query("UPDATE posts SET text_id = $1 WHERE uuid = $2;")
.bind(new_text_id)
.bind(uuid),
)
.await
{
Ok(_) => Ok(()),
Err(why) => {
eprintln!("{why:?}");
Err("Couldn't update post".to_string())
}
}
}
} }
#[derive(FromRow, Debug)] #[derive(FromRow, Debug)]
pub struct User { pub struct User {
pub id: i32, pub id: i32,
@ -70,11 +273,15 @@ pub struct User {
pub enum LoginStatus { pub enum LoginStatus {
InvalidToken, InvalidToken,
NotLoggedIn, NotLoggedIn,
LoggedIn(User) LoggedIn(User),
} }
impl User { impl User {
pub async fn create(db: &mut Connection<Db>, username: &String, password: &String) -> Result<String, String> { pub async fn create(
db: &mut Connection<Db>,
username: &String,
password: &String,
) -> Result<String, String> {
match Regex::new(r"[^A-Za-z0-9_]") { match Regex::new(r"[^A-Za-z0-9_]") {
Ok(r) => { Ok(r) => {
match r.captures(username) { match r.captures(username) {
@ -226,17 +433,11 @@ impl User {
pub async fn login_status(db: &mut Connection<Db>, cookies: &CookieJar<'_>) -> LoginStatus { pub async fn login_status(db: &mut Connection<Db>, cookies: &CookieJar<'_>) -> LoginStatus {
match cookies.get_private("token") { match cookies.get_private("token") {
Some(t) => { Some(t) => match User::get_by_token(db, t).await {
match User::get_by_token(db, t).await { Some(user) => LoginStatus::LoggedIn(user),
Some(user) => { None => LoginStatus::InvalidToken,
LoginStatus::LoggedIn(user)
}, },
None => { None => LoginStatus::NotLoggedIn,
LoginStatus::InvalidToken
}
}
},
None => LoginStatus::NotLoggedIn
} }
} }
} }
@ -262,9 +463,7 @@ impl Image {
) )
.await .await
{ {
Ok(_) => Ok(_) => Ok(()),
Ok(())
,
Err(why) => { Err(why) => {
eprintln!("Couldn't create database entry: {why:?}"); eprintln!("Couldn't create database entry: {why:?}");
Err("Couldn't create image.".to_string()) Err("Couldn't create image.".to_string())
@ -284,18 +483,23 @@ impl Image {
Err(_) => Err("Couldn't get image.".to_string()), Err(_) => Err("Couldn't get image.".to_string()),
} }
} }
pub async fn is_owned_by(db: &mut Connection<Db>, uuid: &String, username: &String) -> Result<bool, String> { pub async fn is_owned_by(
db: &mut Connection<Db>,
uuid: &String,
username: &String,
) -> Result<bool, String> {
match Image::get_by_uuid(db, uuid).await { match Image::get_by_uuid(db, uuid).await {
Ok(img) => { Ok(img) => Ok(&img.owner_name == username),
Ok(&img.owner_name == username)
},
Err(why) => { Err(why) => {
eprintln!("{why:?}"); eprintln!("{why:?}");
Err("couldn't get image".to_string()) Err("couldn't get image".to_string())
} }
} }
} }
pub async fn get_by_username(db: &mut Connection<Db>, owner_name: &String) -> Result<Vec<Image>, String> { pub async fn get_by_username(
db: &mut Connection<Db>,
owner_name: &String,
) -> Result<Vec<Image>, String> {
match db match db
.fetch_all(sqlx::query("SELECT * FROM images WHERE owner_name = $1;").bind(owner_name)) .fetch_all(sqlx::query("SELECT * FROM images WHERE owner_name = $1;").bind(owner_name))
.await .await
@ -310,16 +514,19 @@ impl Image {
}) })
} }
Ok(res) Ok(res)
}, }
Err(_) => Err("Couldn't get image.".to_string()), Err(_) => Err("Couldn't get image.".to_string()),
} }
} }
pub async fn delete(db: &mut Connection<Db>, uuid: &String) -> Result<(), ()> { pub async fn delete(db: &mut Connection<Db>, uuid: &String) -> Result<(), ()> {
match db.execute(sqlx::query("DELETE FROM images WHERE uuid = $1").bind(uuid)).await { match db
.execute(sqlx::query("DELETE FROM images WHERE uuid = $1").bind(uuid))
.await
{
Ok(rows_gone) => { Ok(rows_gone) => {
eprintln!("Deleted {rows_gone:?} rows."); eprintln!("Deleted {rows_gone:?} rows.");
Ok(()) Ok(())
}, }
Err(why) => { Err(why) => {
eprintln!("Couldn't remove database entry: {why:?}"); eprintln!("Couldn't remove database entry: {why:?}");
Err(()) Err(())