-
Notifications
You must be signed in to change notification settings - Fork 100
Add edit and delete discussions and comments #8566
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
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f364913
Add edit and delete discussions and comments
nabramow 6412a8b
WIP
nabramow ea2694e
Update migration number and address type error
nabramow 88e0a52
Add version tables for moderation purposes
nabramow d1ff1b9
Run prettier
nabramow fa51cca
Update test
nabramow 51505e9
Add tests and remove stage only flag
nabramow 00c7e21
Renumber migration to 0165 after rebase onto develop
aapeliv 062d459
Resolve rebase conflicts: dedupe discussion methods, formatting
aapeliv 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
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
101 changes: 101 additions & 0 deletions
101
...kend/src/couchers/migrations/versions/0165_add_edit_delete_to_discussions_and_comments.py
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,101 @@ | ||
| """Add edit/delete support to discussions, comments, and replies | ||
|
|
||
| Revision ID: 0165 | ||
| Revises: 0164 | ||
| Create Date: 2026-05-13 00:00:00.000000 | ||
|
|
||
| """ | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "0165" | ||
| down_revision = "0164" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| op.add_column( | ||
| "discussions", | ||
| sa.Column("deleted", sa.DateTime(timezone=True), nullable=True), | ||
| ) | ||
| op.add_column( | ||
| "discussions", | ||
| sa.Column("last_edited", sa.DateTime(timezone=True), nullable=True), | ||
| ) | ||
| op.add_column( | ||
| "comments", | ||
| sa.Column("last_edited", sa.DateTime(timezone=True), nullable=True), | ||
| ) | ||
| op.add_column( | ||
| "replies", | ||
| sa.Column("last_edited", sa.DateTime(timezone=True), nullable=True), | ||
| ) | ||
|
|
||
| op.create_table( | ||
| "discussion_versions", | ||
| sa.Column("id", sa.BigInteger(), nullable=False), | ||
| sa.Column("discussion_id", sa.BigInteger(), nullable=False), | ||
| sa.Column("editor_user_id", sa.BigInteger(), nullable=False), | ||
| sa.Column("change_type", sa.Enum("edit", "delete", name="contentchangetype"), nullable=False), | ||
| sa.Column("old_title", sa.String(), nullable=True), | ||
| sa.Column("new_title", sa.String(), nullable=True), | ||
| sa.Column("old_content", sa.String(), nullable=True), | ||
| sa.Column("new_content", sa.String(), nullable=True), | ||
| sa.Column("created", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), | ||
| sa.ForeignKeyConstraint(["discussion_id"], ["discussions.id"]), | ||
| sa.ForeignKeyConstraint(["editor_user_id"], ["users.id"]), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| ) | ||
| op.create_index("ix_discussion_versions_discussion_id", "discussion_versions", ["discussion_id"]) | ||
| op.create_index("ix_discussion_versions_editor_user_id", "discussion_versions", ["editor_user_id"]) | ||
|
|
||
| op.create_table( | ||
| "comment_versions", | ||
| sa.Column("id", sa.BigInteger(), nullable=False), | ||
| sa.Column("comment_id", sa.BigInteger(), nullable=False), | ||
| sa.Column("editor_user_id", sa.BigInteger(), nullable=False), | ||
| sa.Column( | ||
| "change_type", sa.Enum("edit", "delete", name="contentchangetype", create_type=False), nullable=False | ||
| ), | ||
| sa.Column("old_content", sa.String(), nullable=True), | ||
| sa.Column("new_content", sa.String(), nullable=True), | ||
| sa.Column("created", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), | ||
| sa.ForeignKeyConstraint(["comment_id"], ["comments.id"]), | ||
| sa.ForeignKeyConstraint(["editor_user_id"], ["users.id"]), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| ) | ||
| op.create_index("ix_comment_versions_comment_id", "comment_versions", ["comment_id"]) | ||
| op.create_index("ix_comment_versions_editor_user_id", "comment_versions", ["editor_user_id"]) | ||
|
|
||
| op.create_table( | ||
| "reply_versions", | ||
| sa.Column("id", sa.BigInteger(), nullable=False), | ||
| sa.Column("reply_id", sa.BigInteger(), nullable=False), | ||
| sa.Column("editor_user_id", sa.BigInteger(), nullable=False), | ||
| sa.Column( | ||
| "change_type", sa.Enum("edit", "delete", name="contentchangetype", create_type=False), nullable=False | ||
| ), | ||
| sa.Column("old_content", sa.String(), nullable=True), | ||
| sa.Column("new_content", sa.String(), nullable=True), | ||
| sa.Column("created", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), | ||
| sa.ForeignKeyConstraint(["reply_id"], ["replies.id"]), | ||
| sa.ForeignKeyConstraint(["editor_user_id"], ["users.id"]), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| ) | ||
| op.create_index("ix_reply_versions_reply_id", "reply_versions", ["reply_id"]) | ||
| op.create_index("ix_reply_versions_editor_user_id", "reply_versions", ["editor_user_id"]) | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_table("reply_versions") | ||
| op.drop_table("comment_versions") | ||
| op.drop_table("discussion_versions") | ||
| sa.Enum(name="contentchangetype").drop(op.get_bind(), checkfirst=True) | ||
|
|
||
| op.drop_column("replies", "last_edited") | ||
| op.drop_column("comments", "last_edited") | ||
| op.drop_column("discussions", "last_edited") | ||
| op.drop_column("discussions", "deleted") |
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 |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| import enum | ||
| from datetime import datetime | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from sqlalchemy import BigInteger, DateTime, ForeignKey, String, UniqueConstraint, func | ||
| from sqlalchemy import BigInteger, DateTime, Enum, ForeignKey, String, UniqueConstraint, func | ||
| from sqlalchemy.orm import Mapped, column_property, mapped_column, relationship | ||
|
|
||
| from couchers.models.base import Base, communities_seq | ||
|
|
@@ -29,6 +30,8 @@ class Discussion(Base, kw_only=True): | |
| thread_id: Mapped[int] = mapped_column(ForeignKey("threads.id"), unique=True) | ||
| moderation_state_id: Mapped[int] = mapped_column(ForeignKey("moderation_states.id"), index=True) | ||
| created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) | ||
| deleted: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) | ||
| last_edited: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) | ||
|
|
||
| creator_user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) | ||
| owner_cluster_id: Mapped[int] = mapped_column(ForeignKey("clusters.id"), index=True) | ||
|
|
@@ -96,6 +99,7 @@ class Comment(Base, kw_only=True): | |
| content: Mapped[str] = mapped_column(String) # CommonMark without images | ||
| created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) | ||
| deleted: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) | ||
| last_edited: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. server default func.now |
||
|
|
||
| thread: Mapped[Thread] = relationship(init=False, backref="comments") | ||
|
|
||
|
|
@@ -117,10 +121,84 @@ class Reply(Base, kw_only=True): | |
| content: Mapped[str] = mapped_column(String) # CommonMark without images | ||
| created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) | ||
| deleted: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) | ||
| last_edited: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), default=None) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
||
|
|
||
| comment: Mapped[Comment] = relationship(init=False, backref="replies") | ||
|
|
||
|
|
||
| class ContentChangeType(enum.Enum): | ||
| edit = enum.auto() | ||
| delete = enum.auto() | ||
|
|
||
|
|
||
| class DiscussionVersion(Base, kw_only=True): | ||
| """ | ||
| audit log of edits and deletions to discussions | ||
| """ | ||
|
|
||
| __tablename__ = "discussion_versions" | ||
|
|
||
| id: Mapped[int] = mapped_column(BigInteger, primary_key=True, init=False) | ||
|
|
||
| discussion_id: Mapped[int] = mapped_column(ForeignKey("discussions.id"), index=True) | ||
| editor_user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) | ||
| change_type: Mapped[ContentChangeType] = mapped_column(Enum(ContentChangeType)) | ||
| old_title: Mapped[str | None] = mapped_column(String, default=None) | ||
| new_title: Mapped[str | None] = mapped_column(String, default=None) | ||
| old_content: Mapped[str | None] = mapped_column(String, default=None) | ||
| new_content: Mapped[str | None] = mapped_column(String, default=None) | ||
| created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) | ||
|
|
||
| discussion: Mapped[Discussion] = relationship(init=False, backref="versions") | ||
| editor_user: Mapped[User] = relationship( | ||
| init=False, backref="edited_discussions", foreign_keys="DiscussionVersion.editor_user_id" | ||
| ) | ||
|
|
||
|
|
||
| class CommentVersion(Base, kw_only=True): | ||
| """ | ||
| audit log of edits and deletions to comments | ||
| """ | ||
|
|
||
| __tablename__ = "comment_versions" | ||
|
|
||
| id: Mapped[int] = mapped_column(BigInteger, primary_key=True, init=False) | ||
|
|
||
| comment_id: Mapped[int] = mapped_column(ForeignKey("comments.id"), index=True) | ||
| editor_user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) | ||
| change_type: Mapped[ContentChangeType] = mapped_column(Enum(ContentChangeType)) | ||
| old_content: Mapped[str | None] = mapped_column(String, default=None) | ||
| new_content: Mapped[str | None] = mapped_column(String, default=None) | ||
| created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) | ||
|
|
||
| comment: Mapped[Comment] = relationship(init=False, backref="versions") | ||
| editor_user: Mapped[User] = relationship( | ||
| init=False, backref="edited_comments", foreign_keys="CommentVersion.editor_user_id" | ||
| ) | ||
|
|
||
|
|
||
| class ReplyVersion(Base, kw_only=True): | ||
| """ | ||
| audit log of edits and deletions to replies | ||
| """ | ||
|
|
||
| __tablename__ = "reply_versions" | ||
|
|
||
| id: Mapped[int] = mapped_column(BigInteger, primary_key=True, init=False) | ||
|
|
||
| reply_id: Mapped[int] = mapped_column(ForeignKey("replies.id"), index=True) | ||
| editor_user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), index=True) | ||
| change_type: Mapped[ContentChangeType] = mapped_column(Enum(ContentChangeType)) | ||
| old_content: Mapped[str | None] = mapped_column(String, default=None) | ||
| new_content: Mapped[str | None] = mapped_column(String, default=None) | ||
| created: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now(), init=False) | ||
|
|
||
| reply: Mapped[Reply] = relationship(init=False, backref="versions") | ||
| editor_user: Mapped[User] = relationship( | ||
| init=False, backref="edited_replies", foreign_keys="ReplyVersion.editor_user_id" | ||
| ) | ||
|
|
||
|
|
||
| class ClusterDiscussionAssociation(Base, kw_only=True): | ||
| """ | ||
| discussions related to clusters | ||
|
|
||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
non-nullable