-
-
Notifications
You must be signed in to change notification settings - Fork 947
Community post tags (part 1) #4997
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
Merged
Changes from 13 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
003613b
partial post tags implementation
phiresky c130bee
fixes
phiresky 4d3b4ba
fix lints
phiresky 3bebdf6
Merge remote-tracking branch 'origin/main' into post-tags
phiresky 8edc63d
Merge remote-tracking branch 'origin/main' into post-tags
phiresky 56ad069
schema fix
phiresky 37246e9
Merge remote-tracking branch 'origin/main' into post-tags
phiresky 89a9068
chore: restructure / rename tag tables
phiresky 211b8b0
chore: fix post view tests
phiresky adea1c1
format
phiresky 803fac8
lint
phiresky 79f2e57
Merge remote-tracking branch 'origin/main' into post-tags
phiresky fcd7c6d
expect used
phiresky 03b850e
chore: update code to maybe final version
phiresky 2de5a49
add ts-rs optionals
phiresky 0cb0bb7
remove error context
phiresky 9572367
clippy
phiresky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| use crate::{ | ||
| newtypes::{CommunityId, TagId}, | ||
| schema::{community_post_tag, post_tag, tag}, | ||
| source::community_post_tag::{ | ||
| CommunityPostTag, | ||
| CommunityPostTagInsertForm, | ||
| PostTagInsertForm, | ||
| Tag, | ||
| TagInsertForm, | ||
| }, | ||
| 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 Tag { | ||
| type InsertForm = TagInsertForm; | ||
|
|
||
| type UpdateForm = TagInsertForm; | ||
|
|
||
| type IdType = TagId; | ||
|
|
||
| async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error> { | ||
| let conn = &mut get_conn(pool).await?; | ||
| insert_into(tag::table) | ||
| .values(form) | ||
| .get_result::<Self>(conn) | ||
| .await | ||
| } | ||
|
|
||
| async fn update( | ||
| pool: &mut DbPool<'_>, | ||
| pid: TagId, | ||
| form: &Self::UpdateForm, | ||
| ) -> Result<Self, Error> { | ||
| let conn = &mut get_conn(pool).await?; | ||
| diesel::update(tag::table.find(pid)) | ||
| .set(form) | ||
| .get_result::<Self>(conn) | ||
| .await | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl Crud for CommunityPostTag { | ||
| type InsertForm = CommunityPostTagInsertForm; | ||
|
|
||
| type UpdateForm = CommunityPostTagInsertForm; | ||
|
|
||
| type IdType = (CommunityId, TagId); | ||
|
|
||
| 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: (CommunityId, TagId), | ||
| 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 PostTagInsertForm { | ||
| pub async fn insert_tag_associations( | ||
| pool: &mut DbPool<'_>, | ||
| tags: &[PostTagInsertForm], | ||
| ) -> LemmyResult<()> { | ||
| let conn = &mut get_conn(pool).await?; | ||
| insert_into(post_tag::table) | ||
| .values(tags) | ||
| .execute(conn) | ||
| .await | ||
| .context("Failed to insert post community tag associations")?; | ||
|
phiresky marked this conversation as resolved.
Outdated
|
||
| Ok(()) | ||
|
phiresky marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| use crate::newtypes::{CommunityId, DbUrl, PostId, TagId}; | ||
| #[cfg(feature = "full")] | ||
| use crate::schema::{community_post_tag, post_tag, 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 = tag))] | ||
| #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] | ||
| #[cfg_attr(feature = "full", ts(export))] | ||
| pub struct Tag { | ||
| pub id: TagId, | ||
| pub ap_id: DbUrl, | ||
| pub name: String, | ||
| pub published: DateTime<Utc>, | ||
| pub updated: Option<DateTime<Utc>>, | ||
| pub deleted: Option<DateTime<Utc>>, | ||
| } | ||
|
|
||
| #[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", diesel(primary_key(community_id, tag_id)))] | ||
| #[cfg_attr(feature = "full", ts(export))] | ||
| pub struct CommunityPostTag { | ||
| pub community_id: CommunityId, | ||
| pub tag_id: TagId, | ||
| pub published: DateTime<Utc>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] | ||
| #[cfg_attr(feature = "full", diesel(table_name = tag))] | ||
| pub struct TagInsertForm { | ||
| pub ap_id: DbUrl, | ||
| pub name: String, | ||
| // default now | ||
| pub published: Option<DateTime<Utc>>, | ||
|
phiresky marked this conversation as resolved.
|
||
| 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 community_id: CommunityId, | ||
| pub tag_id: TagId, | ||
| pub published: Option<DateTime<Utc>>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] | ||
| #[cfg_attr(feature = "full", diesel(table_name = post_tag))] | ||
| pub struct PostTagInsertForm { | ||
| pub post_id: PostId, | ||
| pub tag_id: TagId, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.