use crate::{ naive_now, schema::{post, post_like, post_read, post_saved}, }; use diesel::{result::Error, *}; use serde::Serialize; use url::{ParseError, Url}; #[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] #[table_name = "post"] pub struct Post { pub id: i32, pub name: String, pub url: Option, pub body: Option, pub creator_id: i32, pub community_id: i32, pub removed: bool, pub locked: bool, pub published: chrono::NaiveDateTime, pub updated: Option, pub deleted: bool, pub nsfw: bool, pub stickied: bool, pub embed_title: Option, pub embed_description: Option, pub embed_html: Option, pub thumbnail_url: Option, pub ap_id: String, pub local: bool, } #[derive(Insertable, AsChangeset)] #[table_name = "post"] pub struct PostForm { pub name: String, pub url: Option, pub body: Option, pub creator_id: i32, pub community_id: i32, pub removed: Option, pub locked: Option, pub published: Option, pub updated: Option, pub deleted: Option, pub nsfw: bool, pub stickied: Option, pub embed_title: Option, pub embed_description: Option, pub embed_html: Option, pub thumbnail_url: Option, pub ap_id: Option, pub local: bool, } impl Post { pub fn read(conn: &PgConnection, post_id: i32) -> Result { use crate::schema::post::dsl::*; post.filter(id.eq(post_id)).first::(conn) } pub fn list_for_community( conn: &PgConnection, the_community_id: i32, ) -> Result, Error> { use crate::schema::post::dsl::*; post .filter(community_id.eq(the_community_id)) .then_order_by(published.desc()) .then_order_by(stickied.desc()) .limit(20) .load::(conn) } pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result { use crate::schema::post::dsl::*; diesel::update(post.find(post_id)) .set(ap_id.eq(apub_id)) .get_result::(conn) } pub fn permadelete_for_creator( conn: &PgConnection, for_creator_id: i32, ) -> Result, Error> { use crate::schema::post::dsl::*; let perma_deleted = "*Permananently Deleted*"; let perma_deleted_url = "https://deleted.com"; diesel::update(post.filter(creator_id.eq(for_creator_id))) .set(( name.eq(perma_deleted), url.eq(perma_deleted_url), body.eq(perma_deleted), deleted.eq(true), updated.eq(naive_now()), )) .get_results::(conn) } pub fn update_deleted( conn: &PgConnection, post_id: i32, new_deleted: bool, ) -> Result { use crate::schema::post::dsl::*; diesel::update(post.find(post_id)) .set((deleted.eq(new_deleted), updated.eq(naive_now()))) .get_result::(conn) } pub fn update_removed( conn: &PgConnection, post_id: i32, new_removed: bool, ) -> Result { use crate::schema::post::dsl::*; diesel::update(post.find(post_id)) .set((removed.eq(new_removed), updated.eq(naive_now()))) .get_result::(conn) } pub fn update_removed_for_creator( conn: &PgConnection, for_creator_id: i32, for_community_id: Option, new_removed: bool, ) -> Result, Error> { use crate::schema::post::dsl::*; let mut update = diesel::update(post).into_boxed(); update = update.filter(creator_id.eq(for_creator_id)); if let Some(for_community_id) = for_community_id { update = update.filter(community_id.eq(for_community_id)); } update .set((removed.eq(new_removed), updated.eq(naive_now()))) .get_results::(conn) } pub fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result { use crate::schema::post::dsl::*; diesel::update(post.find(post_id)) .set(locked.eq(new_locked)) .get_result::(conn) } pub fn update_stickied( conn: &PgConnection, post_id: i32, new_stickied: bool, ) -> Result { use crate::schema::post::dsl::*; diesel::update(post.find(post_id)) .set(stickied.eq(new_stickied)) .get_result::(conn) } pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool { user_id == post_creator_id } } impl PostForm { pub fn get_ap_id(&self) -> Result { Url::parse(&self.ap_id.as_ref().unwrap_or(&"not_a_url".to_string())) } } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] #[belongs_to(Post)] #[table_name = "post_like"] pub struct PostLike { pub id: i32, pub post_id: i32, pub user_id: i32, pub score: i16, pub published: chrono::NaiveDateTime, } #[derive(Insertable, AsChangeset, Clone)] #[table_name = "post_like"] pub struct PostLikeForm { pub post_id: i32, pub user_id: i32, pub score: i16, } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] #[belongs_to(Post)] #[table_name = "post_saved"] pub struct PostSaved { pub id: i32, pub post_id: i32, pub user_id: i32, pub published: chrono::NaiveDateTime, } #[derive(Insertable, AsChangeset)] #[table_name = "post_saved"] pub struct PostSavedForm { pub post_id: i32, pub user_id: i32, } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] #[belongs_to(Post)] #[table_name = "post_read"] pub struct PostRead { pub id: i32, pub post_id: i32, pub user_id: i32, pub published: chrono::NaiveDateTime, } #[derive(Insertable, AsChangeset)] #[table_name = "post_read"] pub struct PostReadForm { pub post_id: i32, pub user_id: i32, }