Skip to content

Commit

Permalink
Merge branch 'main' into panel-selector-bug
Browse files Browse the repository at this point in the history
  • Loading branch information
GracefulLemming authored Dec 5, 2024
2 parents dc1d4e0 + f891ac7 commit 85add66
Show file tree
Hide file tree
Showing 25 changed files with 910 additions and 91 deletions.
22 changes: 22 additions & 0 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ type Comment {
An optional classification of the comment's content
"""
commentType: CommentType
"""
Whether the comment has been edited since it was posted
"""
edited: Boolean!
}

"""
Expand All @@ -365,6 +369,20 @@ enum CommentType {
QUESTION
}

"""
Used for updating comments.
All fields except id are optional.
"""
input CommentUpdate {
id: UUID!
"""
The text of the comment
"""
textContent: String
commentType: CommentType
edited: Boolean!
}

"""
A block of content, which may be one of several types.
Each page contains several blocks.
Expand Down Expand Up @@ -800,6 +818,10 @@ type Mutation {
"""
postComment(input: PostCommentInput!): CommentParent!
"""
Update a comment
"""
updateComment(comment: CommentUpdate!): CommentParent!
"""
Mutation for adding/changing contributor attributions
"""
updateContributorAttribution(contribution: UpdateContributorAttribution!): UUID!
Expand Down
25 changes: 24 additions & 1 deletion graphql/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use dailp::{
auth::{AuthGuard, GroupGuard, UserGroup, UserInfo},
comment::{CommentParent, DeleteCommentInput, PostCommentInput},
comment::{CommentParent, CommentUpdate, DeleteCommentInput, PostCommentInput},
slugify_ltree, AnnotatedForm, AttachAudioToWordInput, CollectionChapter, CurateWordAudioInput,
DeleteContributorAttribution, DocumentMetadataUpdate, DocumentParagraph,
UpdateContributorAttribution, Uuid,
Expand Down Expand Up @@ -423,6 +423,29 @@ impl Mutation {
input.parent_type.resolve(db, &input.parent_id).await
}

/// Update a comment
#[graphql(guard = "AuthGuard")]
async fn update_comment(
&self,
context: &Context<'_>,
comment: CommentUpdate,
) -> FieldResult<CommentParent> {
let user = context
.data_opt::<UserInfo>()
.ok_or_else(|| anyhow::format_err!("User is not signed in"))?;
let db = context.data::<DataLoader<Database>>()?.loader();
let comment_object = db.comment_by_id(&comment.id).await?;

if comment_object.posted_by.id.0 != user.id.to_string() {
return Err("User attempted to edit another user's comment".into());
}

db.update_comment(comment).await;

// We return the parent object, for GraphCache interop
return comment_object.parent(context).await;
}

/// Mutation for adding/changing contributor attributions
#[graphql(
guard = "GroupGuard::new(UserGroup::Editors).or(GroupGuard::new(UserGroup::Contributors))"
Expand Down

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

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

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

3 changes: 3 additions & 0 deletions types/migrations/20240916182248_add_comment_edit_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Add migration script here
alter table comment
add column edited boolean not null DEFAULT false;
1 change: 1 addition & 0 deletions types/queries/comment_by_id.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ select
posted_by,
u_posted_by.display_name as "posted_by_name",
text_content,
edited,
comment_type as "comment_type: _",
parent_id,
parent_type as "parent_type: _"
Expand Down
1 change: 1 addition & 0 deletions types/queries/comments_by_parent.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ select
posted_by,
u_posted_by.display_name as "posted_by_name",
text_content,
edited,
comment_type as "comment_type: _",
parent_id,
parent_type as "parent_type: _"
Expand Down
13 changes: 13 additions & 0 deletions types/queries/update_comment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
update comment set
text_content =
case
when $2::text[] != '{}' and $2[1] is not null then $2[1]
else text_content
end,
comment_type =
case
when $3::comment_type_enum[] != '{}' and $3[1] is not null then $3[1]
else comment_type
end,
edited = $4
where id = $1
24 changes: 23 additions & 1 deletion types/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{user::User, AnnotatedForm};
use crate::{Database, DateTime, DocumentParagraph};
use async_graphql::Context;
use async_graphql::{dataloader::DataLoader, FieldResult};
use async_graphql::{dataloader::DataLoader, FieldResult, MaybeUndefined};
use serde::{Deserialize, Serialize};
use sqlx::types::Uuid;

Expand All @@ -25,6 +25,9 @@ pub struct Comment {
/// An optional classification of the comment's content
pub comment_type: Option<CommentType>,

/// Whether the comment has been edited since it was posted
pub edited: bool,

/// The id of the word or paragraph this comment is attached to
#[graphql(skip = true)]
pub parent_id: Uuid,
Expand Down Expand Up @@ -82,6 +85,14 @@ pub enum CommentType {
Question,
}

/// PgHasArrayType for CommentType
impl sqlx::postgres::PgHasArrayType for CommentType {
fn array_type_info() -> sqlx::postgres::PgTypeInfo {
// The array type name in PostgreSQL is prefixed with an underscore
sqlx::postgres::PgTypeInfo::with_name("_comment_type_enum")
}
}

/// Type representing the object that a comment is attached to
#[derive(async_graphql::Union)]
pub enum CommentParent {
Expand Down Expand Up @@ -110,3 +121,14 @@ pub struct DeleteCommentInput {
/// ID of the comment to delete
pub comment_id: Uuid,
}

/// Used for updating comments.
/// All fields except id are optional.
#[derive(async_graphql::InputObject)]
pub struct CommentUpdate {
pub id: Uuid,
/// The text of the comment
pub text_content: MaybeUndefined<String>,
pub comment_type: MaybeUndefined<Option<CommentType>>,
pub edited: bool,
}
22 changes: 21 additions & 1 deletion types/src/database_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::str::FromStr;

use crate::collection::CollectionChapter;
use crate::collection::EditedCollection;
use crate::comment::{Comment, CommentParentType, CommentType};
use crate::comment::{Comment, CommentParentType, CommentType, CommentUpdate};
use crate::user::User;
use crate::user::UserId;
use {
Expand Down Expand Up @@ -717,6 +717,23 @@ impl Database {
Ok(self.paragraph_by_id(&paragraph.id).await?)
}

pub async fn update_comment(&self, comment: CommentUpdate) -> Result<Uuid> {
let text_content = comment.text_content.into_vec();
let comment_type = comment.comment_type.into_vec();

query_file!(
"queries/update_comment.sql",
comment.id,
&text_content as _,
&comment_type as _,
comment.edited
)
.execute(&self.client)
.await?;

Ok(comment.id)
}

pub async fn update_contributor_attribution(
&self,
contribution: UpdateContributorAttribution,
Expand Down Expand Up @@ -2143,6 +2160,8 @@ struct BasicComment {
pub text_content: String,
pub comment_type: Option<CommentType>,

pub edited: bool,

pub parent_id: Uuid,
pub parent_type: CommentParentType,
}
Expand All @@ -2158,6 +2177,7 @@ impl Into<Comment> for BasicComment {
},
text_content: self.text_content,
comment_type: self.comment_type,
edited: self.edited,
parent_id: self.parent_id,
parent_type: self.parent_type,
}
Expand Down
Loading

0 comments on commit 85add66

Please sign in to comment.