refactor: upgrade to Go 1.26 and modernize code idioms
- Replace `interface{}` with `any` type alias across all packages
- Use built-in `min()`/`max()` for parameter clamping
- Use `slices.SortFunc`, `slices.Min`, `slices.Max` for cleaner code
- Use `strings.Cut()` for simpler string parsing in auth middleware
- Use `errors.Is()` for proper error comparison in handlers
- Update dependencies: golang.org/x/image 0.37.0 -> 0.38.0
- Add Wire code generation guidelines to ARCHITECTURE.md
- Disable Go cache in CI build workflow
This commit is contained in:
@@ -94,7 +94,7 @@ func (r *commentRepository) Delete(id string) error {
|
||||
// 减少帖子的评论数并同步热度分
|
||||
// 评论数属于统计字段,不应影响帖子内容更新时间(updated_at)
|
||||
if err := tx.Model(&model.Post{}).Where("id = ?", comment.PostID).
|
||||
UpdateColumns(map[string]interface{}{
|
||||
UpdateColumns(map[string]any{
|
||||
"comments_count": gorm.Expr("comments_count - 1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error; err != nil {
|
||||
|
||||
@@ -125,7 +125,7 @@ func (r *deviceTokenRepository) Upsert(token *model.DeviceToken) error {
|
||||
}
|
||||
|
||||
// 更新现有记录
|
||||
return r.db.Model(&existing).Updates(map[string]interface{}{
|
||||
return r.db.Model(&existing).Updates(map[string]any{
|
||||
"push_token": token.PushToken,
|
||||
"is_active": true,
|
||||
"device_name": token.DeviceName,
|
||||
@@ -151,7 +151,7 @@ func (r *deviceTokenRepository) Deactivate(deviceID string) error {
|
||||
func (r *deviceTokenRepository) Activate(deviceID string) error {
|
||||
return r.db.Model(&model.DeviceToken{}).
|
||||
Where("device_id = ?", deviceID).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"is_active": true,
|
||||
"last_used_at": time.Now(),
|
||||
}).Error
|
||||
|
||||
@@ -91,7 +91,7 @@ func (r *messageRepository) CreateConversationWithParticipants(conv *model.Conve
|
||||
participant := model.ConversationParticipant{
|
||||
ConversationID: conv.ID,
|
||||
UserID: userID,
|
||||
LastReadSeq: 0,
|
||||
LastReadSeq: 0,
|
||||
}
|
||||
if err := tx.Create(&participant).Error; err != nil {
|
||||
return err
|
||||
@@ -291,7 +291,7 @@ func (r *messageRepository) UpdateLastReadSeq(conversationID string, userID stri
|
||||
{Name: "conversation_id"},
|
||||
{Name: "user_id"},
|
||||
},
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||||
DoUpdates: clause.Assignments(map[string]any{
|
||||
"last_read_seq": lastReadSeq,
|
||||
"updated_at": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
}),
|
||||
@@ -324,7 +324,7 @@ func (r *messageRepository) UpdatePinned(conversationID string, userID string, i
|
||||
{Name: "conversation_id"},
|
||||
{Name: "user_id"},
|
||||
},
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||||
DoUpdates: clause.Assignments(map[string]any{
|
||||
"is_pinned": isPinned,
|
||||
"updated_at": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
}),
|
||||
@@ -358,7 +358,7 @@ func (r *messageRepository) GetUnreadCount(conversationID string, userID string)
|
||||
func (r *messageRepository) UpdateConversationLastSeq(conversationID string, seq int64) error {
|
||||
return r.db.Model(&model.Conversation{}).
|
||||
Where("id = ?", conversationID).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"last_seq": seq,
|
||||
"last_msg_time": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
}).Error
|
||||
@@ -393,7 +393,7 @@ func (r *messageRepository) CreateMessageWithSeq(msg *model.Message) error {
|
||||
// 更新会话的last_seq
|
||||
if err := tx.Model(&model.Conversation{}).
|
||||
Where("id = ?", msg.ConversationID).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"last_seq": msg.Seq,
|
||||
"last_msg_time": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
}).Error; err != nil {
|
||||
@@ -455,7 +455,7 @@ func (r *messageRepository) UpdateMessageStatus(messageID string, status model.M
|
||||
func (r *messageRepository) RecallMessage(messageID string, userID string) error {
|
||||
return r.db.Model(&model.Message{}).
|
||||
Where("id = ? AND sender_id = ?", messageID, userID).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"status": model.MessageStatusRecalled,
|
||||
"segments": model.MessageSegments{},
|
||||
}).Error
|
||||
@@ -526,7 +526,7 @@ func (r *messageRepository) MarkAllSystemMessagesAsRead(userID string) error {
|
||||
{Name: "conversation_id"},
|
||||
{Name: "user_id"},
|
||||
},
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||||
DoUpdates: clause.Assignments(map[string]any{
|
||||
"last_read_seq": maxSeq,
|
||||
"updated_at": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
}),
|
||||
@@ -653,7 +653,7 @@ func (r *messageRepository) BatchUpdateParticipants(ctx context.Context, updates
|
||||
|
||||
var cases []string
|
||||
var whereClauses []string
|
||||
var args []interface{}
|
||||
var args []any
|
||||
|
||||
for _, u := range updates {
|
||||
cases = append(cases, "WHEN (conversation_id = ? AND user_id = ?) THEN ?")
|
||||
@@ -678,7 +678,7 @@ func (r *messageRepository) UpdateConversationLastSeqWithContext(ctx context.Con
|
||||
return r.db.WithContext(ctx).
|
||||
Model(&model.Conversation{}).
|
||||
Where("id = ?", convID).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"last_seq": lastSeq,
|
||||
"last_msg_time": lastMsgTime,
|
||||
"updated_at": time.Now(),
|
||||
@@ -701,7 +701,7 @@ func (r *messageRepository) BatchUpdateParticipantsWithTx(tx *gorm.DB, updates [
|
||||
|
||||
var cases []string
|
||||
var whereClauses []string
|
||||
var args []interface{}
|
||||
var args []any
|
||||
|
||||
for _, u := range updates {
|
||||
cases = append(cases, "WHEN (conversation_id = ? AND user_id = ?) THEN ?")
|
||||
@@ -725,7 +725,7 @@ func (r *messageRepository) BatchUpdateParticipantsWithTx(tx *gorm.DB, updates [
|
||||
func (r *messageRepository) UpdateConversationLastSeqWithTx(tx *gorm.DB, convID string, lastSeq int64, lastMsgTime time.Time) error {
|
||||
return tx.Model(&model.Conversation{}).
|
||||
Where("id = ?", convID).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"last_seq": lastSeq,
|
||||
"last_msg_time": lastMsgTime,
|
||||
"updated_at": time.Now(),
|
||||
|
||||
@@ -166,7 +166,7 @@ func (r *postRepository) UpdateWithImages(post *model.Post, images *[]string) er
|
||||
// UpdateModerationStatus 更新帖子审核状态
|
||||
func (r *postRepository) UpdateModerationStatus(postID string, status model.PostStatus, rejectReason string, reviewedBy string) error {
|
||||
// 审核状态更新属于管理操作,不应影响帖子内容更新时间(updated_at)
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"status": status,
|
||||
"reviewed_at": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
"reviewed_by": reviewedBy,
|
||||
@@ -455,7 +455,7 @@ func (r *postRepository) IsFavoritedBatch(postIDs []string, userID string) map[s
|
||||
// IncrementViews 增加帖子观看量
|
||||
func (r *postRepository) IncrementViews(postID string) error {
|
||||
return r.db.Model(&model.Post{}).Where("id = ?", postID).
|
||||
UpdateColumns(map[string]interface{}{
|
||||
UpdateColumns(map[string]any{
|
||||
"views_count": gorm.Expr("views_count + 1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
}).Error
|
||||
@@ -467,7 +467,7 @@ func (r *postRepository) IncrementShares(postID string) (int, error) {
|
||||
err := r.db.Transaction(func(tx *gorm.DB) error {
|
||||
res := tx.Model(&model.Post{}).
|
||||
Where("id = ? AND status = ?", postID, model.PostStatusPublished).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"shares_count": gorm.Expr("shares_count + 1"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
})
|
||||
@@ -909,11 +909,11 @@ func (r *postRepository) BatchUpdateStatus(ids []string, status model.PostStatus
|
||||
result := r.db.Model(&model.Post{}).
|
||||
Where("id IN ?", ids).
|
||||
Updates(map[string]any{
|
||||
"status": status,
|
||||
"reviewed_at": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
"reviewed_by": reviewedBy,
|
||||
"status": status,
|
||||
"reviewed_at": gorm.Expr("CURRENT_TIMESTAMP"),
|
||||
"reviewed_by": reviewedBy,
|
||||
"reject_reason": "",
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
"updated_at": gorm.Expr("updated_at"),
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
|
||||
@@ -113,7 +113,7 @@ func (r *pushRecordRepository) BatchUpdateStatus(ids []int64, status model.PushS
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"push_status": status,
|
||||
}
|
||||
if status == model.PushStatusPushed {
|
||||
@@ -126,7 +126,7 @@ func (r *pushRecordRepository) BatchUpdateStatus(ids []int64, status model.PushS
|
||||
|
||||
// UpdateStatus 更新单条记录状态
|
||||
func (r *pushRecordRepository) UpdateStatus(id int64, status model.PushStatus) error {
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"push_status": status,
|
||||
}
|
||||
if status == model.PushStatusPushed {
|
||||
@@ -141,7 +141,7 @@ func (r *pushRecordRepository) UpdateStatus(id int64, status model.PushStatus) e
|
||||
func (r *pushRecordRepository) MarkAsFailed(id int64, errMsg string) error {
|
||||
return r.db.Model(&model.PushRecord{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"push_status": model.PushStatusFailed,
|
||||
"error_message": errMsg,
|
||||
"retry_count": gorm.Expr("retry_count + 1"),
|
||||
@@ -152,7 +152,7 @@ func (r *pushRecordRepository) MarkAsFailed(id int64, errMsg string) error {
|
||||
func (r *pushRecordRepository) MarkAsDelivered(id int64) error {
|
||||
return r.db.Model(&model.PushRecord{}).
|
||||
Where("id = ?", id).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"push_status": model.PushStatusDelivered,
|
||||
"delivered_at": time.Now(),
|
||||
}).Error
|
||||
|
||||
@@ -164,7 +164,7 @@ func (r *reportRepository) GetPendingReportsByTarget(targetType model.ReportTarg
|
||||
|
||||
// UpdateStatus 更新举报状态
|
||||
func (r *reportRepository) UpdateStatus(id string, status model.ReportStatus, handledBy string, result string) error {
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"status": status,
|
||||
"handled_by": handledBy,
|
||||
"result": result,
|
||||
@@ -174,7 +174,7 @@ func (r *reportRepository) UpdateStatus(id string, status model.ReportStatus, ha
|
||||
|
||||
// UpdateStatusWithContext 使用上下文更新举报状态
|
||||
func (r *reportRepository) UpdateStatusWithContext(ctx context.Context, id string, status model.ReportStatus, handledBy string, result string) error {
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"status": status,
|
||||
"handled_by": handledBy,
|
||||
"result": result,
|
||||
@@ -184,7 +184,7 @@ func (r *reportRepository) UpdateStatusWithContext(ctx context.Context, id strin
|
||||
|
||||
// BatchUpdateStatus 批量更新举报状态
|
||||
func (r *reportRepository) BatchUpdateStatus(ids []string, status model.ReportStatus, handledBy string, result string) (int64, error) {
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"status": status,
|
||||
"handled_by": handledBy,
|
||||
"result": result,
|
||||
@@ -195,7 +195,7 @@ func (r *reportRepository) BatchUpdateStatus(ids []string, status model.ReportSt
|
||||
|
||||
// BatchUpdateStatusWithContext 使用上下文批量更新举报状态
|
||||
func (r *reportRepository) BatchUpdateStatusWithContext(ctx context.Context, ids []string, status model.ReportStatus, handledBy string, result string) (int64, error) {
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"status": status,
|
||||
"handled_by": handledBy,
|
||||
"result": result,
|
||||
@@ -209,4 +209,4 @@ func (r *reportRepository) GetByIDsWithContext(ctx context.Context, ids []string
|
||||
var reports []*model.Report
|
||||
err := r.getDB(ctx).Where("id IN ?", ids).Find(&reports).Error
|
||||
return reports, err
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (r *systemNotificationRepository) MarkAsRead(id int64, receiverID string) e
|
||||
now := model.SystemNotification{}.UpdatedAt
|
||||
return r.db.Model(&model.SystemNotification{}).
|
||||
Where("id = ? AND receiver_id = ?", id, receiverID).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"is_read": true,
|
||||
"read_at": now,
|
||||
}).Error
|
||||
@@ -102,7 +102,7 @@ func (r *systemNotificationRepository) MarkAllAsRead(receiverID string) error {
|
||||
now := model.SystemNotification{}.UpdatedAt
|
||||
return r.db.Model(&model.SystemNotification{}).
|
||||
Where("receiver_id = ? AND is_read = ?", receiverID, false).
|
||||
Updates(map[string]interface{}{
|
||||
Updates(map[string]any{
|
||||
"is_read": true,
|
||||
"read_at": now,
|
||||
}).Error
|
||||
|
||||
Reference in New Issue
Block a user