2
0
Files
logikonline 9e6d1d63de
All checks were successful
Build and Release / Create Release (push) Successful in 0s
Build and Release / Unit Tests (push) Successful in 3m10s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 5m5s
Build and Release / Lint (push) Successful in 5m20s
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Successful in 2m54s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Successful in 9h4m29s
Build and Release / Build Binaries (amd64, darwin, macos) (push) Successful in 8m14s
Build and Release / Build Binaries (arm64, darwin, macos) (push) Successful in 8m19s
Build and Release / Build Binary (linux/arm64) (push) Successful in 8m27s
feat(blog): add comments, reactions, and guest verification
Implements threaded comment system with support for authenticated users and verified guests. Adds email verification flow for guest commenters with token-based sessions and 6-digit codes. Includes reaction system (like/love/laugh/etc) for posts and comments. Adds comment count to blog posts, user profile blog tab, and email notifications for comment verification. Implements nested reply support with parent-child relationships.
2026-02-01 22:22:18 -05:00

141 lines
4.0 KiB
Go

// Copyright 2026 MarketAlly. All rights reserved.
// SPDX-License-Identifier: MIT
package blog
import (
"context"
"code.gitcaddy.com/server/v3/models/db"
user_model "code.gitcaddy.com/server/v3/models/user"
"code.gitcaddy.com/server/v3/modules/timeutil"
)
// BlogComment represents a comment or reply on a blog post.
// ParentID = 0 means top-level comment; ParentID > 0 means a reply.
type BlogComment struct { //revive:disable-line:exported
ID int64 `xorm:"pk autoincr"`
BlogPostID int64 `xorm:"INDEX NOT NULL"`
ParentID int64 `xorm:"INDEX NOT NULL DEFAULT 0"`
UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"`
GuestName string `xorm:"VARCHAR(100)"`
GuestEmail string `xorm:"VARCHAR(255)"`
Content string `xorm:"LONGTEXT NOT NULL"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
// Not persisted
User *user_model.User `xorm:"-"`
Replies []*BlogComment `xorm:"-"`
}
func init() {
db.RegisterModel(new(BlogComment))
}
// IsGuest returns true if the comment was posted by an anonymous guest.
func (c *BlogComment) IsGuest() bool {
return c.UserID == 0
}
// DisplayName returns the commenter's display name.
func (c *BlogComment) DisplayName() string {
if c.User != nil {
return c.User.DisplayName()
}
if c.GuestName != "" {
return c.GuestName
}
return "Anonymous"
}
// LoadUser loads the user for non-guest comments.
func (c *BlogComment) LoadUser(ctx context.Context) error {
if c.UserID == 0 || c.User != nil {
return nil
}
u, err := user_model.GetUserByID(ctx, c.UserID)
if err != nil {
return err
}
c.User = u
return nil
}
// CreateBlogComment inserts a new blog comment.
func CreateBlogComment(ctx context.Context, c *BlogComment) error {
_, err := db.GetEngine(ctx).Insert(c)
return err
}
// GetBlogCommentByID returns a single comment by ID.
func GetBlogCommentByID(ctx context.Context, id int64) (*BlogComment, error) {
c := &BlogComment{}
has, err := db.GetEngine(ctx).ID(id).Get(c)
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return c, nil
}
// GetBlogCommentsByPostID returns all comments for a post, organized as top-level with nested replies.
func GetBlogCommentsByPostID(ctx context.Context, blogPostID int64) ([]*BlogComment, error) {
var all []*BlogComment
err := db.GetEngine(ctx).Where("blog_post_id = ?", blogPostID).
OrderBy("created_unix ASC").
Find(&all)
if err != nil {
return nil, err
}
// Load users for non-guest comments
for _, c := range all {
if err := c.LoadUser(ctx); err != nil {
return nil, err
}
}
// Build tree: top-level + replies
commentMap := make(map[int64]*BlogComment, len(all))
topLevel := make([]*BlogComment, 0)
for _, c := range all {
commentMap[c.ID] = c
}
for _, c := range all {
if c.ParentID == 0 {
topLevel = append(topLevel, c)
} else if parent, ok := commentMap[c.ParentID]; ok {
parent.Replies = append(parent.Replies, c)
} else {
// Orphaned reply — show as top-level
topLevel = append(topLevel, c)
}
}
return topLevel, nil
}
// CountBlogComments returns the total count of comments for a blog post.
func CountBlogComments(ctx context.Context, blogPostID int64) (int64, error) {
return db.GetEngine(ctx).Where("blog_post_id = ?", blogPostID).Count(new(BlogComment))
}
// DeleteBlogComment deletes a comment and all its replies.
func DeleteBlogComment(ctx context.Context, commentID int64) error {
// Delete replies first
if _, err := db.GetEngine(ctx).Where("parent_id = ?", commentID).Delete(new(BlogComment)); err != nil {
return err
}
_, err := db.GetEngine(ctx).ID(commentID).Delete(new(BlogComment))
return err
}
// DeleteBlogCommentsByPostID removes all comments for a blog post.
func DeleteBlogCommentsByPostID(ctx context.Context, blogPostID int64) error {
_, err := db.GetEngine(ctx).Where("blog_post_id = ?", blogPostID).Delete(new(BlogComment))
return err
}