Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Community post tags (part 1) #4997

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use lemmy_db_schema::{
newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId},
newtypes::{CommentId, CommunityId, CommunityPostTagId, DbUrl, LanguageId, PostId, PostReportId},
ListingType,
PostFeatureType,
SortType,
Expand Down Expand Up @@ -30,6 +30,7 @@ pub struct CreatePost {
pub language_id: Option<LanguageId>,
/// Instead of fetching a thumbnail, use a custom one.
pub custom_thumbnail: Option<String>,
pub community_post_tags: Option<Vec<CommunityPostTagId>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -124,6 +125,7 @@ pub struct EditPost {
pub language_id: Option<LanguageId>,
/// Instead of fetching a thumbnail, use a custom one.
pub custom_thumbnail: Option<String>,
pub community_post_tags: Option<Vec<CommunityPostTagId>>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)]
Expand Down
59 changes: 59 additions & 0 deletions crates/db_schema/src/impls/community_post_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use crate::{
newtypes::CommunityPostTagId,
schema::{community_post_tag, post_community_post_tag},
source::community_post_tag::{
CommunityPostTag,
CommunityPostTagInsertForm,
PostCommunityPostTagInsertForm,
},
traits::Crud,
utils::{get_conn, DbPool},
};
use anyhow::Context;
use diesel::{insert_into, result::Error, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_utils::error::LemmyResult;

#[async_trait]
impl Crud for CommunityPostTag {
type InsertForm = CommunityPostTagInsertForm;

type UpdateForm = CommunityPostTagInsertForm;

type IdType = CommunityPostTagId;

async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
insert_into(community_post_tag::table)
.values(form)
.get_result::<Self>(conn)
.await
}

async fn update(
pool: &mut DbPool<'_>,
pid: CommunityPostTagId,
form: &Self::UpdateForm,
) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
diesel::update(community_post_tag::table.find(pid))
.set(form)
.get_result::<Self>(conn)
.await
}
}

impl PostCommunityPostTagInsertForm {
pub async fn insert_tag_associations(
pool: &mut DbPool<'_>,
tags: &[PostCommunityPostTagInsertForm],
) -> LemmyResult<()> {
let conn = &mut get_conn(pool).await?;
insert_into(post_community_post_tag::table)
.values(tags)
.execute(conn)
.await
.context("Failed to insert post community tag associations")?;
Ok(())
}
}
1 change: 1 addition & 0 deletions crates/db_schema/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod comment_reply;
pub mod comment_report;
pub mod community;
pub mod community_block;
pub mod community_post_tag;
pub mod custom_emoji;
pub mod email_verification;
pub mod federation_allowlist;
Expand Down
6 changes: 6 additions & 0 deletions crates/db_schema/src/newtypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,9 @@ impl InstanceId {
self.0
}
}

#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Default, Serialize, Deserialize)]
#[cfg_attr(feature = "full", derive(DieselNewType, TS))]
#[cfg_attr(feature = "full", ts(export))]
/// The post id.
pub struct CommunityPostTagId(pub i32);
24 changes: 24 additions & 0 deletions crates/db_schema/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,18 @@ diesel::table! {
}
}

diesel::table! {
community_post_tag (id) {
id -> Int4,
ap_id -> Text,
community_id -> Int4,
name -> Text,
published -> Timestamptz,
updated -> Nullable<Timestamptz>,
deleted -> Nullable<Timestamptz>,
}
}

diesel::table! {
custom_emoji (id) {
id -> Int4,
Expand Down Expand Up @@ -759,6 +771,13 @@ diesel::table! {
}
}

diesel::table! {
post_community_post_tag (post_id, community_post_tag_id) {
post_id -> Int4,
community_post_tag_id -> Int4,
}
}

diesel::table! {
post_hide (person_id, post_id) {
post_id -> Int4,
Expand Down Expand Up @@ -974,6 +993,7 @@ diesel::joinable!(community_moderator -> community (community_id));
diesel::joinable!(community_moderator -> person (person_id));
diesel::joinable!(community_person_ban -> community (community_id));
diesel::joinable!(community_person_ban -> person (person_id));
diesel::joinable!(community_post_tag -> community (community_id));
diesel::joinable!(custom_emoji -> local_site (local_site_id));
diesel::joinable!(custom_emoji_keyword -> custom_emoji (custom_emoji_id));
diesel::joinable!(email_verification -> local_user (local_user_id));
Expand Down Expand Up @@ -1020,6 +1040,8 @@ diesel::joinable!(post_aggregates -> community (community_id));
diesel::joinable!(post_aggregates -> instance (instance_id));
diesel::joinable!(post_aggregates -> person (creator_id));
diesel::joinable!(post_aggregates -> post (post_id));
diesel::joinable!(post_community_post_tag -> community_post_tag (community_post_tag_id));
diesel::joinable!(post_community_post_tag -> post (post_id));
diesel::joinable!(post_hide -> person (person_id));
diesel::joinable!(post_hide -> post (post_id));
diesel::joinable!(post_like -> person (person_id));
Expand Down Expand Up @@ -1057,6 +1079,7 @@ diesel::allow_tables_to_appear_in_same_query!(
community_language,
community_moderator,
community_person_ban,
community_post_tag,
custom_emoji,
custom_emoji_keyword,
email_verification,
Expand Down Expand Up @@ -1096,6 +1119,7 @@ diesel::allow_tables_to_appear_in_same_query!(
person_post_aggregates,
post,
post_aggregates,
post_community_post_tag,
post_hide,
post_like,
post_read,
Expand Down
48 changes: 48 additions & 0 deletions crates/db_schema/src/source/community_post_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use crate::newtypes::{CommunityId, CommunityPostTagId, DbUrl, PostId};
#[cfg(feature = "full")]
use crate::schema::{community_post_tag, post_community_post_tag};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
#[cfg(feature = "full")]
use ts_rs::TS;

/// A tag that can be assigned to a post within a community.
/// The tag object is created by the community moderators.
/// The assignment happens by the post creator and can be updated by the community moderators.
#[skip_serializing_none]
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
#[cfg_attr(feature = "full", derive(TS, Queryable, Selectable, Identifiable))]
#[cfg_attr(feature = "full", diesel(table_name = community_post_tag))]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
pub struct CommunityPostTag {
pub id: CommunityPostTagId,
pub ap_id: DbUrl,
pub community_id: CommunityId,
pub name: String,
pub published: DateTime<Utc>,
pub updated: Option<DateTime<Utc>>,
pub deleted: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = community_post_tag))]
pub struct CommunityPostTagInsertForm {
pub ap_id: DbUrl,
pub community_id: CommunityId,
pub name: String,
// default now
pub published: Option<DateTime<Utc>>,
pub updated: Option<DateTime<Utc>>,
pub deleted: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = post_community_post_tag))]
pub struct PostCommunityPostTagInsertForm {
pub post_id: PostId,
pub community_post_tag_id: CommunityPostTagId,
}
1 change: 1 addition & 0 deletions crates/db_schema/src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod comment_reply;
pub mod comment_report;
pub mod community;
pub mod community_block;
pub mod community_post_tag;
pub mod custom_emoji;
pub mod custom_emoji_keyword;
pub mod email_verification;
Expand Down
5 changes: 5 additions & 0 deletions crates/db_schema/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ pub mod functions {
sql_function!(fn coalesce<T: diesel::sql_types::SqlType + diesel::sql_types::SingleValue>(x: diesel::sql_types::Nullable<T>, y: T) -> T);

sql_function!(fn set_config(setting_name: Text, new_value: Text, is_local: Bool) -> Text);

sql_function! {
#[aggregate]
fn json_agg<T: diesel::sql_types::SqlType + diesel::sql_types::SingleValue>(obj: T) -> Json
}
}

pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*";
Expand Down
1 change: 1 addition & 0 deletions crates/db_views/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ diesel-async = { workspace = true, optional = true }
diesel_ltree = { workspace = true, optional = true }
serde = { workspace = true }
serde_with = { workspace = true }
serde_json = { workspace = true }
tracing = { workspace = true, optional = true }
ts-rs = { workspace = true, optional = true }
actix-web = { workspace = true, optional = true }
Expand Down
29 changes: 29 additions & 0 deletions crates/db_views/src/community_post_tags_view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::structs::PostCommunityPostTags;
use diesel::{
deserialize::FromSql,
pg::{Pg, PgValue},
serialize::ToSql,
sql_types::{self, Nullable},
};

impl FromSql<Nullable<sql_types::Json>, Pg> for PostCommunityPostTags {
fn from_sql(bytes: PgValue) -> diesel::deserialize::Result<Self> {
let value = <serde_json::Value as FromSql<sql_types::Json, Pg>>::from_sql(bytes)?;
Ok(serde_json::from_value::<PostCommunityPostTags>(value)?)
}
fn from_nullable_sql(
bytes: Option<<Pg as diesel::backend::Backend>::RawValue<'_>>,
) -> diesel::deserialize::Result<Self> {
match bytes {
Some(bytes) => Self::from_sql(bytes),
None => Ok(Self { tags: vec![] }),
}
}
}

impl ToSql<Nullable<sql_types::Json>, Pg> for PostCommunityPostTags {
fn to_sql(&self, out: &mut diesel::serialize::Output<Pg>) -> diesel::serialize::Result {
let value = serde_json::to_value(self)?;
<serde_json::Value as ToSql<sql_types::Json, Pg>>::to_sql(&value, &mut out.reborrow())
}
}
2 changes: 2 additions & 0 deletions crates/db_views/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub mod comment_report_view;
#[cfg(feature = "full")]
pub mod comment_view;
#[cfg(feature = "full")]
pub mod community_post_tags_view;
#[cfg(feature = "full")]
pub mod custom_emoji_view;
#[cfg(feature = "full")]
pub mod local_image_view;
Expand Down
Loading