List attachments now return with userinfo

This commit is contained in:
LittleSheep 2025-03-31 00:45:39 +08:00
parent 370ee84b34
commit 11d54c7c78
4 changed files with 51 additions and 8 deletions

View File

@ -5,6 +5,7 @@ import (
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
@ -68,7 +69,8 @@ type Attachment struct {
Boosts []AttachmentBoost `json:"boosts"`
AccountID uint `json:"account_id"`
AccountID uint `json:"account_id"`
Account models.Account `gorm:"-" json:"account"`
// Outdated fields, just for backward compatibility
FileChunks datatypes.JSONMap `json:"file_chunks" gorm:"-"`

View File

@ -4,8 +4,8 @@ import (
"context"
"git.solsynth.dev/hypernet/nexus/pkg/nex"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
"git.solsynth.dev/hypernet/paperclip/pkg/proto"
"github.com/rs/zerolog/log"
@ -33,8 +33,13 @@ func (v *Server) GetAttachment(ctx context.Context, request *proto.GetAttachment
return nil, status.Error(codes.NotFound, "attachment not found")
}
out, err := services.CompleteAttachmentMeta(attachment)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &proto.GetAttachmentResponse{
Attachment: nex.EncodeMap(attachment),
Attachment: nex.EncodeMap(out[0]),
}, nil
}
@ -56,8 +61,13 @@ func (v *Server) ListAttachment(ctx context.Context, request *proto.ListAttachme
return nil, status.Error(codes.Internal, err.Error())
}
out, err := services.CompleteAttachmentMeta(attachments...)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &proto.ListAttachmentResponse{
Attachments: lo.Map(attachments, func(v models.Attachment, _ int) []byte {
Attachments: lo.Map(out, func(v models.Attachment, _ int) []byte {
return nex.EncodeMap(v)
}),
}, nil

View File

@ -9,8 +9,8 @@ import (
"github.com/spf13/viper"
"gorm.io/datatypes"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)
@ -111,8 +111,13 @@ func listAttachment(c *fiber.Ctx) error {
services.CacheAttachment(item)
}
out, err := services.CompleteAttachmentMeta(result...)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": result,
"data": out,
})
}

View File

@ -10,13 +10,16 @@ import (
"git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/database"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/fs"
"git.solsynth.dev/hypernet/paperclip/pkg/internal/gap"
"git.solsynth.dev/hypernet/paperclip/pkg/filekit/models"
amodels "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"github.com/google/uuid"
"github.com/samber/lo"
"gorm.io/gorm"
)
@ -24,6 +27,27 @@ func KgAttachmentCache(rid string) string {
return cachekit.FKey(cachekit.DAAttachment, rid)
}
func CompleteAttachmentMeta(in ...models.Attachment) ([]models.Attachment, error) {
var usersId []uint
for _, item := range in {
usersId = append(usersId, item.AccountID)
}
usersId = lo.Uniq(usersId)
users, err := authkit.ListUser(gap.Nx, usersId)
if err != nil {
return in, fmt.Errorf("failed to list users: %v", err)
}
for idx, item := range in {
item.Account = lo.FindOrElse(users, amodels.Account{}, func(idx amodels.Account) bool {
return item.AccountID == idx.ID
})
in[idx] = item
}
return in, nil
}
func GetAttachmentByID(id uint) (models.Attachment, error) {
var attachment models.Attachment
if err := database.C.
@ -38,7 +62,8 @@ func GetAttachmentByID(id uint) (models.Attachment, error) {
CacheAttachment(attachment)
}
return attachment, nil
out, err := CompleteAttachmentMeta(attachment)
return out[0], err
}
func GetAttachmentByRID(rid string) (models.Attachment, error) {
@ -63,7 +88,8 @@ func GetAttachmentByRID(rid string) (models.Attachment, error) {
CacheAttachment(attachment)
}
return attachment, nil
out, err := CompleteAttachmentMeta(attachment)
return out[0], err
}
func GetAttachmentByHash(hash string) (models.Attachment, error) {