fix: sync latest telesrv fixes
This commit is contained in:
parent
b7269b135f
commit
b1f74185f0
11 changed files with 203 additions and 72 deletions
|
|
@ -84,27 +84,27 @@ func seedEffectsCatalog(parsed seedEffectsFileJSON) ([]domain.AvailableEffect, [
|
|||
docByID[d.ID] = d
|
||||
}
|
||||
required := make(map[int64]struct{}, len(docByID))
|
||||
storageID := func(sourceID int64) int64 {
|
||||
if sourceID == 0 {
|
||||
documentID := func(id int64) int64 {
|
||||
if id == 0 {
|
||||
return 0
|
||||
}
|
||||
if _, ok := docByID[sourceID]; !ok {
|
||||
if _, ok := docByID[id]; !ok {
|
||||
return 0
|
||||
}
|
||||
required[sourceID] = struct{}{}
|
||||
return seedDocumentStorageID(sourceID)
|
||||
required[id] = struct{}{}
|
||||
return id
|
||||
}
|
||||
effects := make([]domain.AvailableEffect, 0, len(parsed.Result.Effects))
|
||||
for i, ej := range parsed.Result.Effects {
|
||||
if ej.ID == 0 || ej.EffectStickerID == 0 {
|
||||
continue
|
||||
}
|
||||
staticID := storageID(ej.StaticIconID)
|
||||
stickerID := storageID(ej.EffectStickerID)
|
||||
staticID := documentID(ej.StaticIconID)
|
||||
stickerID := documentID(ej.EffectStickerID)
|
||||
if stickerID == 0 {
|
||||
continue
|
||||
}
|
||||
animID := storageID(ej.EffectAnimationID)
|
||||
animID := documentID(ej.EffectAnimationID)
|
||||
effects = append(effects, domain.AvailableEffect{
|
||||
ID: ej.ID,
|
||||
Emoticon: ej.Emoticon,
|
||||
|
|
|
|||
|
|
@ -322,15 +322,12 @@ func (s *Service) importStickerSetDir(ctx context.Context, setDir, systemKey str
|
|||
|
||||
docIDs := make([]int64, 0, len(info.Result.Documents))
|
||||
docs := make([]domain.Document, 0, len(info.Result.Documents))
|
||||
docIDBySource := make(map[int64]int64, len(info.Result.Documents))
|
||||
for _, dj := range info.Result.Documents {
|
||||
sourceID := dj.ID
|
||||
doc, err := s.importDocument(ctx, dj, stickersDir, index, stats)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if doc.ID != 0 {
|
||||
docIDBySource[sourceID] = doc.ID
|
||||
docIDs = append(docIDs, doc.ID)
|
||||
docs = append(docs, doc)
|
||||
}
|
||||
|
|
@ -351,12 +348,12 @@ func (s *Service) importStickerSetDir(ctx context.Context, setDir, systemKey str
|
|||
Masks: sj.Masks,
|
||||
Archived: sj.Archived,
|
||||
Installed: seedStickerSetInstalled(kind),
|
||||
ThumbDocumentID: seedDocumentStorageID(derefInt64(sj.ThumbDocumentID)),
|
||||
ThumbDocumentID: derefInt64(sj.ThumbDocumentID),
|
||||
Thumbs: seedStickerSetPhotoSizes(sj.Thumbs),
|
||||
ThumbDCID: s.dc,
|
||||
ThumbVersion: sj.ThumbVersion,
|
||||
DocumentIDs: docIDs,
|
||||
Packs: seedStickerPacks(sj.Packs, info.Result.Packs, docIDBySource),
|
||||
Packs: seedStickerPacks(sj.Packs, info.Result.Packs),
|
||||
SortOrder: order,
|
||||
SystemKey: systemKey,
|
||||
}
|
||||
|
|
@ -377,10 +374,9 @@ func (s *Service) importDocument(ctx context.Context, dj seedDocumentJSON, binDi
|
|||
if dj.ID == 0 {
|
||||
return domain.Document{}, nil
|
||||
}
|
||||
storageID := seedDocumentStorageID(dj.ID)
|
||||
ref, _ := hex.DecodeString(dj.FileReference)
|
||||
doc := domain.Document{
|
||||
ID: storageID,
|
||||
ID: dj.ID,
|
||||
AccessHash: dj.AccessHash,
|
||||
FileReference: ref,
|
||||
Date: parseSeedDate(dj.Date),
|
||||
|
|
@ -390,7 +386,7 @@ func (s *Service) importDocument(ctx context.Context, dj seedDocumentJSON, binDi
|
|||
Attributes: seedDocumentAttributes(dj.Attributes),
|
||||
}
|
||||
|
||||
// 主体 blob:doc:<server-owned-id>
|
||||
// 主体 blob:doc:<document-id>
|
||||
if mainPath, ok := index.main[dj.ID]; ok {
|
||||
data, err := os.ReadFile(mainPath)
|
||||
if err != nil {
|
||||
|
|
@ -401,7 +397,7 @@ func (s *Service) importDocument(ctx context.Context, dj seedDocumentJSON, binDi
|
|||
return domain.Document{}, err
|
||||
}
|
||||
if err := s.media.PutFileBlob(ctx, domain.FileBlob{
|
||||
LocationKey: fmt.Sprintf("doc:%d", storageID),
|
||||
LocationKey: fmt.Sprintf("doc:%d", doc.ID),
|
||||
Backend: domain.MediaBackend(s.blobs.Name()),
|
||||
ObjectKey: objectKey,
|
||||
Size: int64(len(data)),
|
||||
|
|
@ -441,7 +437,7 @@ func (s *Service) importDocument(ctx context.Context, dj seedDocumentJSON, binDi
|
|||
}
|
||||
ps.Size = len(data)
|
||||
if err := s.media.PutFileBlob(ctx, domain.FileBlob{
|
||||
LocationKey: fmt.Sprintf("doc:%d:%s", storageID, ps.Type),
|
||||
LocationKey: fmt.Sprintf("doc:%d:%s", doc.ID, ps.Type),
|
||||
Backend: domain.MediaBackend(s.blobs.Name()),
|
||||
ObjectKey: objectKey,
|
||||
Size: int64(len(data)),
|
||||
|
|
@ -487,24 +483,9 @@ var seedThumbMarker = regexp.MustCompile(`_thumb\d+_`)
|
|||
const seedInlineCachedDocumentThumbMaxBytes = 32 * 1024
|
||||
const seedSyntheticDocumentThumbType = "m"
|
||||
|
||||
// Exported Telegram resources keep their original id in filenames/JSON, but
|
||||
// telesrv owns the document catalog it serves. Imported high source ids are
|
||||
// normalized once at seed time so RPC/storage use one server-owned id.
|
||||
const seedExternalDocumentIDOffset int64 = 4_000_000_000_000_000_000
|
||||
|
||||
var seedThumbType = regexp.MustCompile(`PhotoSize_type([a-z])`)
|
||||
var seedSyntheticTGStickerPreviewThumbPNG = makeSeedSyntheticTGStickerPreviewThumbPNG()
|
||||
|
||||
func seedDocumentStorageID(sourceID int64) int64 {
|
||||
if sourceID <= 0 {
|
||||
return 0
|
||||
}
|
||||
if sourceID > seedExternalDocumentIDOffset {
|
||||
return sourceID - seedExternalDocumentIDOffset
|
||||
}
|
||||
return sourceID
|
||||
}
|
||||
|
||||
func scanSeedDir(dir string) (seedDirIndex, error) {
|
||||
idx := seedDirIndex{main: map[int64]string{}, thumb: map[int64]map[string]string{}}
|
||||
entries, err := os.ReadDir(dir)
|
||||
|
|
@ -826,7 +807,7 @@ func (s *Service) documentsNeedSeedRepair(ctx context.Context, ids []int64) (boo
|
|||
return false, nil
|
||||
}
|
||||
|
||||
func seedStickerPacks(setPacks, resultPacks []seedStickerPackJSON, docIDBySource map[int64]int64) []domain.StickerPack {
|
||||
func seedStickerPacks(setPacks, resultPacks []seedStickerPackJSON) []domain.StickerPack {
|
||||
packs := setPacks
|
||||
if len(packs) == 0 {
|
||||
packs = resultPacks
|
||||
|
|
@ -834,12 +815,8 @@ func seedStickerPacks(setPacks, resultPacks []seedStickerPackJSON, docIDBySource
|
|||
out := make([]domain.StickerPack, 0, len(packs))
|
||||
for _, p := range packs {
|
||||
documents := make([]int64, 0, len(p.Documents))
|
||||
for _, sourceID := range p.Documents {
|
||||
if id, ok := docIDBySource[sourceID]; ok {
|
||||
documents = append(documents, id)
|
||||
continue
|
||||
}
|
||||
if id := seedDocumentStorageID(sourceID); id != 0 {
|
||||
for _, id := range p.Documents {
|
||||
if id > 0 {
|
||||
documents = append(documents, id)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -79,13 +79,12 @@ func writeSeedDirFingerprint(h io.Writer, dir string) error {
|
|||
}
|
||||
|
||||
func seedDocumentJSONLocationKeys(dj seedDocumentJSON, index seedDirIndex) []string {
|
||||
storageID := seedDocumentStorageID(dj.ID)
|
||||
if storageID == 0 {
|
||||
if dj.ID == 0 {
|
||||
return nil
|
||||
}
|
||||
keys := make([]string, 0, 1+len(dj.Thumbs))
|
||||
if _, ok := index.main[dj.ID]; ok {
|
||||
keys = append(keys, fmt.Sprintf("doc:%d", storageID))
|
||||
keys = append(keys, fmt.Sprintf("doc:%d", dj.ID))
|
||||
}
|
||||
for _, tj := range dj.Thumbs {
|
||||
ps, downloadable := seedPhotoSize(tj)
|
||||
|
|
@ -93,11 +92,11 @@ func seedDocumentJSONLocationKeys(dj seedDocumentJSON, index seedDirIndex) []str
|
|||
continue
|
||||
}
|
||||
if _, ok := index.thumb[dj.ID][ps.Type]; ok {
|
||||
keys = append(keys, fmt.Sprintf("doc:%d:%s", storageID, ps.Type))
|
||||
keys = append(keys, fmt.Sprintf("doc:%d:%s", dj.ID, ps.Type))
|
||||
}
|
||||
}
|
||||
if seedDocumentJSONNeedsSyntheticTGStickerPreviewThumb(dj) {
|
||||
keys = append(keys, fmt.Sprintf("doc:%d:%s", storageID, seedSyntheticDocumentThumbType))
|
||||
keys = append(keys, fmt.Sprintf("doc:%d:%s", dj.ID, seedSyntheticDocumentThumbType))
|
||||
}
|
||||
return keys
|
||||
}
|
||||
|
|
@ -112,13 +111,12 @@ func (s *Service) seedDocumentJSONsReady(ctx context.Context, docs []seedDocumen
|
|||
locationKeys := make([]string, 0, len(docs))
|
||||
seenLocationKeys := make(map[string]struct{}, len(docs))
|
||||
for _, dj := range docs {
|
||||
storageID := seedDocumentStorageID(dj.ID)
|
||||
if storageID == 0 {
|
||||
if dj.ID == 0 {
|
||||
continue
|
||||
}
|
||||
if _, ok := expected[storageID]; !ok {
|
||||
expected[storageID] = dj
|
||||
ids = append(ids, storageID)
|
||||
if _, ok := expected[dj.ID]; !ok {
|
||||
expected[dj.ID] = dj
|
||||
ids = append(ids, dj.ID)
|
||||
}
|
||||
for _, key := range seedDocumentJSONLocationKeys(dj, index) {
|
||||
if _, ok := seenLocationKeys[key]; ok {
|
||||
|
|
|
|||
|
|
@ -642,9 +642,6 @@ func TestSeedMediaFromRealExport(t *testing.T) {
|
|||
if d, ok, _ := media.GetDocument(context.Background(), first.SelectAnimationID); !ok {
|
||||
t.Error("reaction select animation document missing")
|
||||
} else {
|
||||
if d.ID > seedExternalDocumentIDOffset {
|
||||
t.Errorf("reaction document kept external source id: %d", d.ID)
|
||||
}
|
||||
if d.DCID != 2 {
|
||||
t.Errorf("document dc_id not rewritten: %d", d.DCID)
|
||||
}
|
||||
|
|
@ -678,9 +675,6 @@ func TestSeedMediaFromRealExport(t *testing.T) {
|
|||
if doc, ok, _ := media.GetDocument(context.Background(), sample.DocumentIDs[0]); !ok {
|
||||
t.Fatalf("sample sticker document %d missing", sample.DocumentIDs[0])
|
||||
} else {
|
||||
if doc.ID > seedExternalDocumentIDOffset {
|
||||
t.Fatalf("sample sticker kept external source id: %d", doc.ID)
|
||||
}
|
||||
thumb, ok := findCachedThumb(doc.Thumbs)
|
||||
if !ok {
|
||||
t.Fatalf("sample sticker document thumbs are not inline cached: %+v", doc.Thumbs)
|
||||
|
|
@ -729,14 +723,37 @@ func writeEffectsSeed(t *testing.T, seedDir string, sourceID int64) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSeedDocumentStorageIDNormalizesExternalIDs(t *testing.T) {
|
||||
func TestSeedDocumentIDsAreImportedVerbatim(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
seedDir := t.TempDir()
|
||||
const sourceID int64 = 5382305375846410902
|
||||
const want int64 = 1382305375846410902
|
||||
if got := seedDocumentStorageID(sourceID); got != want {
|
||||
t.Fatalf("seedDocumentStorageID(%d) = %d, want %d", sourceID, got, want)
|
||||
writeStatusPackWithoutThumbSeed(t, seedDir, sourceID, 17)
|
||||
|
||||
media := newFakeMediaStore()
|
||||
blobs, err := NewLocalFS(t.TempDir())
|
||||
if err != nil {
|
||||
t.Fatalf("local fs: %v", err)
|
||||
}
|
||||
if got := seedDocumentStorageID(2222222); got != 2222222 {
|
||||
t.Fatalf("small server id changed: %d", got)
|
||||
svc := NewService(media, blobs, 2)
|
||||
if _, err := svc.SeedMedia(ctx, seedDir, 0); err != nil {
|
||||
t.Fatalf("seed media: %v", err)
|
||||
}
|
||||
set, ok, err := media.GetStickerSetByShortName(ctx, "StatusPack")
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("StatusPack ok=%v err=%v", ok, err)
|
||||
}
|
||||
if len(set.DocumentIDs) != 1 || set.DocumentIDs[0] != sourceID {
|
||||
t.Fatalf("set document ids = %+v, want [%d]", set.DocumentIDs, sourceID)
|
||||
}
|
||||
doc, ok, err := media.GetDocument(ctx, sourceID)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("GetDocument(%d) ok=%v err=%v", sourceID, ok, err)
|
||||
}
|
||||
if doc.ID != sourceID {
|
||||
t.Fatalf("document id = %d, want %d", doc.ID, sourceID)
|
||||
}
|
||||
if _, ok, err := media.GetFileBlob(ctx, fmt.Sprintf("doc:%d", sourceID)); err != nil || !ok {
|
||||
t.Fatalf("main blob for raw id ok=%v err=%v", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue