fix: sync collectible document role validation
Sync telesrv d8ad5cc (fix(stargifts): validate collectible document roles). Skipped telesrv docs changes per public sync rules.
This commit is contained in:
parent
2848ff0987
commit
ba82fbccd6
9 changed files with 319 additions and 7 deletions
|
|
@ -981,7 +981,8 @@ func validateStarGiftAttributes(attributes []StarGiftCollectibleAttribute, kind
|
|||
len(attribute.Animation.TGS) == 0 || len(attribute.Animation.SHA256) != 32 {
|
||||
return ErrStarGiftCollectibleInvalid
|
||||
}
|
||||
if requireStoredAsset && (attribute.Document == nil || !attribute.Document.IsSticker() ||
|
||||
if requireStoredAsset && (attribute.Document == nil ||
|
||||
!validStarGiftCollectibleDocument(*attribute.Document, kind) ||
|
||||
attribute.Document.MimeType != "application/x-tgsticker" || attribute.Blob == nil) {
|
||||
return ErrStarGiftCollectibleInvalid
|
||||
}
|
||||
|
|
@ -1003,6 +1004,38 @@ func validateStarGiftAttributes(attributes []StarGiftCollectibleAttribute, kind
|
|||
return nil
|
||||
}
|
||||
|
||||
// validStarGiftCollectibleDocument enforces the client-visible document roles
|
||||
// materialized by the Star Gift write boundary. Models are ordinary stickers.
|
||||
// Patterns are text-color custom emoji with an inline PhotoPathSize so Android
|
||||
// can classify and tint the TGS before its full first frame is downloaded.
|
||||
func validStarGiftCollectibleDocument(document Document, kind StarGiftCollectibleAttributeKind) bool {
|
||||
renderAttributes := 0
|
||||
validRenderAttribute := false
|
||||
for _, attribute := range document.Attributes {
|
||||
switch attribute.Kind {
|
||||
case DocAttrSticker:
|
||||
renderAttributes++
|
||||
validRenderAttribute = validRenderAttribute || kind == StarGiftCollectibleModel
|
||||
case DocAttrCustomEmoji:
|
||||
renderAttributes++
|
||||
validRenderAttribute = validRenderAttribute ||
|
||||
(kind == StarGiftCollectiblePattern && attribute.TextColor)
|
||||
}
|
||||
}
|
||||
if renderAttributes != 1 || !validRenderAttribute {
|
||||
return false
|
||||
}
|
||||
if kind == StarGiftCollectibleModel {
|
||||
return true
|
||||
}
|
||||
for _, thumb := range document.Thumbs {
|
||||
if thumb.Kind == PhotoSizeKindPath && strings.TrimSpace(thumb.Type) != "" && len(thumb.Bytes) > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StarGiftCatalogHash 由客户端可见目录字段折叠出稳定 hash,供 getStarGifts NotModified。
|
||||
func StarGiftCatalogHash(catalog []StarGift) int {
|
||||
var h uint64
|
||||
|
|
|
|||
|
|
@ -93,3 +93,55 @@ func TestValidateStarGiftCollectibleDraftRejectsImplicitRarity(t *testing.T) {
|
|||
t.Fatalf("err=%v, want ErrStarGiftCollectibleInvalid", err)
|
||||
}
|
||||
}
|
||||
|
||||
func storedCollectibleWrite() StarGiftCollectibleWrite {
|
||||
write := validCollectibleDraft()
|
||||
for i := range write.Models {
|
||||
write.Models[i].Document = &Document{
|
||||
ID: int64(100 + i), MimeType: "application/x-tgsticker",
|
||||
Attributes: []DocumentAttribute{{Kind: DocAttrSticker, Alt: "🎁"}},
|
||||
}
|
||||
write.Models[i].Blob = &FileBlob{LocationKey: "model"}
|
||||
}
|
||||
write.Patterns[0].Document = &Document{
|
||||
ID: 200, MimeType: "application/x-tgsticker",
|
||||
Attributes: []DocumentAttribute{{Kind: DocAttrCustomEmoji, Alt: "🎁", TextColor: true}},
|
||||
Thumbs: []PhotoSize{{Kind: PhotoSizeKindPath, Type: "j", Bytes: []byte{1}}},
|
||||
}
|
||||
write.Patterns[0].Blob = &FileBlob{LocationKey: "pattern"}
|
||||
return write
|
||||
}
|
||||
|
||||
func TestValidateStarGiftCollectibleWriteRequiresExactDocumentRoles(t *testing.T) {
|
||||
if err := ValidateStarGiftCollectibleWrite(storedCollectibleWrite()); err != nil {
|
||||
t.Fatalf("valid stored collectible: %v", err)
|
||||
}
|
||||
|
||||
tests := map[string]func(*StarGiftCollectibleWrite){
|
||||
"pattern stored as sticker": func(write *StarGiftCollectibleWrite) {
|
||||
write.Patterns[0].Document.Attributes = []DocumentAttribute{{Kind: DocAttrSticker, Alt: "🎁"}}
|
||||
},
|
||||
"pattern custom emoji without text color": func(write *StarGiftCollectibleWrite) {
|
||||
write.Patterns[0].Document.Attributes[0].TextColor = false
|
||||
},
|
||||
"pattern without inline path thumb": func(write *StarGiftCollectibleWrite) {
|
||||
write.Patterns[0].Document.Thumbs = nil
|
||||
},
|
||||
"model stored as custom emoji": func(write *StarGiftCollectibleWrite) {
|
||||
write.Models[0].Document.Attributes = []DocumentAttribute{{Kind: DocAttrCustomEmoji, Alt: "🎁", TextColor: true}}
|
||||
},
|
||||
"ambiguous model render attributes": func(write *StarGiftCollectibleWrite) {
|
||||
write.Models[0].Document.Attributes = append(write.Models[0].Document.Attributes,
|
||||
DocumentAttribute{Kind: DocAttrCustomEmoji, Alt: "🎁", TextColor: true})
|
||||
},
|
||||
}
|
||||
for name, mutate := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
write := storedCollectibleWrite()
|
||||
mutate(&write)
|
||||
if err := ValidateStarGiftCollectibleWrite(write); !errors.Is(err, ErrStarGiftCollectibleInvalid) {
|
||||
t.Fatalf("err=%v, want ErrStarGiftCollectibleInvalid", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue