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

102 lines
3.3 KiB
Go

// Copyright 2026 MarketAlly. All rights reserved.
// SPDX-License-Identifier: MIT
package blog
import (
"context"
"code.gitcaddy.com/server/v3/models/db"
"code.gitcaddy.com/server/v3/modules/timeutil"
)
// BlogReaction represents a thumbs up/down reaction on a blog post.
type BlogReaction struct { //revive:disable-line:exported
ID int64 `xorm:"pk autoincr"`
BlogPostID int64 `xorm:"INDEX NOT NULL"`
UserID int64 `xorm:"INDEX NOT NULL DEFAULT 0"`
GuestIP string `xorm:"VARCHAR(45) NOT NULL DEFAULT ''"`
IsLike bool `xorm:"NOT NULL DEFAULT true"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
}
func init() {
db.RegisterModel(new(BlogReaction))
}
// BlogReactionCounts holds aggregated reaction counts for a blog post.
type BlogReactionCounts struct { //revive:disable-line:exported
Likes int64
Dislikes int64
}
// ToggleBlogReaction creates, updates, or removes a reaction.
// If the user already reacted with the same type, it removes the reaction (toggle off).
// If the user reacted with a different type, it switches.
// If no reaction exists, it creates one.
func ToggleBlogReaction(ctx context.Context, blogPostID, userID int64, guestIP string, isLike bool) (reacted bool, err error) {
existing, err := GetUserBlogReaction(ctx, blogPostID, userID, guestIP)
if err != nil {
return false, err
}
if existing != nil {
if existing.IsLike == isLike {
// Same type — toggle off (remove)
_, err = db.GetEngine(ctx).ID(existing.ID).Delete(new(BlogReaction))
return false, err
}
// Different type — switch
existing.IsLike = isLike
_, err = db.GetEngine(ctx).ID(existing.ID).Cols("is_like").Update(existing)
return true, err
}
// No existing reaction — create
_, err = db.GetEngine(ctx).Insert(&BlogReaction{
BlogPostID: blogPostID,
UserID: userID,
GuestIP: guestIP,
IsLike: isLike,
})
return true, err
}
// GetBlogReactionCounts returns aggregated like/dislike counts for a blog post.
func GetBlogReactionCounts(ctx context.Context, blogPostID int64) (*BlogReactionCounts, error) {
likes, err := db.GetEngine(ctx).Where("blog_post_id = ? AND is_like = ?", blogPostID, true).Count(new(BlogReaction))
if err != nil {
return nil, err
}
dislikes, err := db.GetEngine(ctx).Where("blog_post_id = ? AND is_like = ?", blogPostID, false).Count(new(BlogReaction))
if err != nil {
return nil, err
}
return &BlogReactionCounts{Likes: likes, Dislikes: dislikes}, nil
}
// GetUserBlogReaction returns the existing reaction for a user/guest on a blog post, or nil.
func GetUserBlogReaction(ctx context.Context, blogPostID, userID int64, guestIP string) (*BlogReaction, error) {
r := &BlogReaction{}
var has bool
var err error
if userID > 0 {
has, err = db.GetEngine(ctx).Where("blog_post_id = ? AND user_id = ?", blogPostID, userID).Get(r)
} else {
has, err = db.GetEngine(ctx).Where("blog_post_id = ? AND user_id = 0 AND guest_ip = ?", blogPostID, guestIP).Get(r)
}
if err != nil {
return nil, err
}
if !has {
return nil, nil
}
return r, nil
}
// DeleteBlogReactionsByPostID removes all reactions for a blog post (used when deleting a post).
func DeleteBlogReactionsByPostID(ctx context.Context, blogPostID int64) error {
_, err := db.GetEngine(ctx).Where("blog_post_id = ?", blogPostID).Delete(new(BlogReaction))
return err
}