refactor: cleanup unused code and simplify internal packages
This commit performs a significant cleanup of the codebase by removing unused functions, methods, and entire files across various internal modules. This reduces technical debt and simplifies the project structure. Key changes include: - **cache**: Removed unused cache key generators and metrics snapshots. - **dto**: Removed redundant converter functions and segment creation helpers. - **middleware**: Deleted the unused `logger.go` middleware and simplified `ratelimit.go` and `casbin.go`. - **model**: Removed unused ID helpers, database closing functions, and batch decryption logic. - **pkg**: Cleaned up unused utility functions in `circuitbreaker`, `crypto`, `cursor`, `hook`, and `utils`. - **service**: Deleted `account_cleanup_service.go` and removed unused helper functions in `log_cleanup_service.go` and `sensitive_service.go`. - **repository**: Removed unused private loading methods in `comment_repo.go`.
This commit is contained in:
@@ -1,114 +0,0 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"with_you/internal/model"
|
||||
"with_you/internal/repository"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const AccountDeletionCoolDownDays = 90
|
||||
|
||||
type AccountCleanupService interface {
|
||||
CleanupExpiredAccounts(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
type accountCleanupServiceImpl struct {
|
||||
db *gorm.DB
|
||||
userRepo repository.UserRepository
|
||||
logService *LogService
|
||||
}
|
||||
|
||||
func NewAccountCleanupService(
|
||||
db *gorm.DB,
|
||||
userRepo repository.UserRepository,
|
||||
logService *LogService,
|
||||
) AccountCleanupService {
|
||||
return &accountCleanupServiceImpl{
|
||||
db: db,
|
||||
userRepo: userRepo,
|
||||
logService: logService,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *accountCleanupServiceImpl) CleanupExpiredAccounts(ctx context.Context) (int64, error) {
|
||||
cutoffTime := time.Now().AddDate(0, 0, -AccountDeletionCoolDownDays)
|
||||
|
||||
users, err := s.userRepo.GetPendingDeletionUsers(cutoffTime)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if len(users) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
var deletedCount int64
|
||||
for _, user := range users {
|
||||
if err := s.anonymizeAndDeleteUser(ctx, user); err != nil {
|
||||
zap.L().Error("Failed to cleanup user",
|
||||
zap.String("user_id", user.ID),
|
||||
zap.Error(err),
|
||||
)
|
||||
continue
|
||||
}
|
||||
deletedCount++
|
||||
}
|
||||
|
||||
zap.L().Info("Cleaned up expired accounts",
|
||||
zap.Int64("count", deletedCount),
|
||||
)
|
||||
return deletedCount, nil
|
||||
}
|
||||
|
||||
func (s *accountCleanupServiceImpl) anonymizeAndDeleteUser(ctx context.Context, user *model.User) error {
|
||||
deletedUserID := "deleted_user"
|
||||
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Model(&model.Post{}).Where("user_id = ?", user.ID).Update("user_id", deletedUserID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Model(&model.Comment{}).Where("user_id = ?", user.ID).Update("user_id", deletedUserID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where("follower_id = ? OR following_id = ?", user.ID, user.ID).Delete(&model.Follow{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where("user_id = ?", user.ID).Delete(&model.Favorite{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where("user_id = ?", user.ID).Delete(&model.PostLike{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where("user_id = ?", user.ID).Delete(&model.CommentLike{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Unscoped().Delete(&model.User{}, "id = ?", user.ID).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.logService != nil {
|
||||
s.logService.OperationLog.RecordOperation(&model.OperationLog{
|
||||
UserID: user.ID,
|
||||
UserName: user.Username,
|
||||
Operation: "account_deleted",
|
||||
Module: "user",
|
||||
TargetType: "user",
|
||||
TargetID: user.ID,
|
||||
Status: "success",
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -197,9 +197,3 @@ func NewLogService(
|
||||
}
|
||||
}
|
||||
|
||||
// GetLogCleanupServiceFromApp 从应用中获取日志清理服务
|
||||
func GetLogCleanupServiceFromApp(app any) LogCleanupService {
|
||||
// 这里需要根据实际的app结构来获取
|
||||
// 暂时返回nil,实际使用时需要实现
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,11 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"with_you/internal/model"
|
||||
redisclient "with_you/internal/pkg/redis"
|
||||
@@ -501,51 +499,3 @@ func (s *sensitiveServiceImpl) loadFromRedis(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ==================== 辅助函数 ====================
|
||||
|
||||
// ContainsSensitiveWord 快速检查文本是否包含敏感词
|
||||
func ContainsSensitiveWord(text string, tree *SensitiveWordTree) bool {
|
||||
if tree == nil || text == "" {
|
||||
return false
|
||||
}
|
||||
hasSensitive, _ := tree.Check(text)
|
||||
return hasSensitive
|
||||
}
|
||||
|
||||
// FilterSensitiveWords 过滤敏感词并返回替换后的文本
|
||||
func FilterSensitiveWords(text string, tree *SensitiveWordTree, repl string) string {
|
||||
if tree == nil || text == "" {
|
||||
return text
|
||||
}
|
||||
if repl == "" {
|
||||
repl = "***"
|
||||
}
|
||||
return tree.Replace(text, repl)
|
||||
}
|
||||
|
||||
// ValidateTextLength 验证文本长度是否合法
|
||||
func ValidateTextLength(text string, minLen, maxLen int) bool {
|
||||
length := utf8.RuneCountInString(text)
|
||||
return length >= minLen && length <= maxLen
|
||||
}
|
||||
|
||||
// SanitizeText 清理文本,移除多余空白字符
|
||||
func SanitizeText(text string) string {
|
||||
// 替换多个连续空白字符为单个空格
|
||||
spaceReg := regexp.MustCompile(`\s+`)
|
||||
text = spaceReg.ReplaceAllString(text, " ")
|
||||
// 去除首尾空白
|
||||
return strings.TrimSpace(text)
|
||||
}
|
||||
|
||||
// ==================== 默认敏感词列表 ====================
|
||||
|
||||
// DefaultSensitiveWords 返回默认敏感词列表(示例)
|
||||
func DefaultSensitiveWords() map[string]struct{} {
|
||||
return map[string]struct{}{
|
||||
// 示例敏感词,实际需要从数据库或配置加载
|
||||
"测试敏感词1": {},
|
||||
"测试敏感词2": {},
|
||||
"测试敏感词3": {},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user