Update usage grpc call

This commit is contained in:
2025-03-10 22:15:59 +08:00
parent 69be460e13
commit e3ca50c4ae
6 changed files with 248 additions and 60 deletions

View File

@ -5,6 +5,7 @@ import (
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/models"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
"git.solsynth.dev/hypernet/paperclip/pkg/proto"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
@ -91,6 +92,19 @@ func (v *Server) UpdateVisibility(ctx context.Context, request *proto.UpdateVisi
}, nil
}
func (v *Server) UpdateUsage(ctx context.Context, request *proto.UpdateUsageRequest) (*proto.UpdateUsageResponse, error) {
id := lo.Map(request.GetId(), func(item uint64, _ int) uint {
return uint(item)
})
if rows, err := services.CountAttachmentUsage(id, int(request.GetDelta())); err != nil {
return nil, status.Error(codes.Internal, err.Error())
} else {
return &proto.UpdateUsageResponse{
Count: int32(rows),
}, nil
}
}
func (v *Server) DeleteAttachment(ctx context.Context, request *proto.DeleteAttachmentRequest) (*proto.DeleteAttachmentResponse, error) {
tx := database.C
if len(request.Id) == 0 && len(request.Rid) == 0 {

View File

@ -3,10 +3,11 @@ package models
import (
"context"
"fmt"
"git.solsynth.dev/hypernet/paperclip/pkg/proto"
"strconv"
"time"
"git.solsynth.dev/hypernet/paperclip/pkg/proto"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
@ -56,6 +57,8 @@ type Attachment struct {
IsSelfRef bool `json:"is_self_ref"`
IsIndexable bool `json:"is_indexable"` // Show this attachment in the public directory api or not
// Count the usage of this attachment across all services
// If this number remain 0, it will be deleted during the maintenance
UsedCount int `json:"used_count"`
Thumbnail *Attachment `json:"thumbnail"`

View File

@ -238,3 +238,13 @@ func DeleteAttachment(item models.Attachment, txs ...*gorm.DB) error {
return nil
}
func CountAttachmentUsage(id []uint, delta int) (int64, error) {
if tx := database.C.Model(&models.Attachment{}).
Where("id IN ?", id).
Update("used_count", gorm.Expr("used_count + ?", delta)); tx.Error != nil {
return tx.RowsAffected, tx.Error
} else {
return tx.RowsAffected, nil
}
}