Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/design/data-and-cache.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/design/subscriptions.md

Large diffs are not rendered by default.

18 changes: 15 additions & 3 deletions internal/download/weibo_backfill.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ func (m *Manager) BackfillWeiboNoWMCompanions(
slots := library.WeiboStaticImageSlotsMissingNoWM(meta)
if len(slots) == 0 {
needSave := res.LinksCleaned || assetIDsUpdated || dimsSynced
if meta.WeiboNoWMState == library.WeiboNoWMStatePending ||
(meta.WeiboNoWMState == "" && library.WeiboHasStaticImageSlots(meta)) {
meta.WeiboNoWMState = library.WeiboNoWMStateChecked
res.StampedStandard = true
needSave = true
}
if meta.CacheStandardVersion < targetWeiboStd {
meta.CacheStandardVersion = targetWeiboStd
res.StampedStandard = true
Expand Down Expand Up @@ -284,9 +290,15 @@ func (m *Manager) BackfillWeiboNoWMCompanions(
res.AddedCompanions++
}

if allSlotsAttempted && meta.CacheStandardVersion < targetWeiboStd {
meta.CacheStandardVersion = targetWeiboStd
res.StampedStandard = true
if allSlotsAttempted {
if meta.WeiboNoWMState != library.WeiboNoWMStateChecked {
meta.WeiboNoWMState = library.WeiboNoWMStateChecked
res.StampedStandard = true
}
if meta.CacheStandardVersion < targetWeiboStd {
meta.CacheStandardVersion = targetWeiboStd
res.StampedStandard = true
}
}

if res.AddedCompanions > 0 || res.LinksCleaned || res.StampedStandard || assetIDsUpdated || dimsSynced {
Expand Down
402 changes: 402 additions & 0 deletions internal/download/weibo_media_quality_test.go

Large diffs are not rendered by default.

180 changes: 157 additions & 23 deletions internal/download/weibo_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1824,9 +1824,7 @@ func (m *Manager) executeWeiboPostTask(
EditHistory: editHistory,
}
stampCacheStandardAndAssets(&meta, plan.EditTs, plan.EditCount, buildPlanAssetIDs(plan))
if meta.Status == "complete" && len(meta.Missing) == 0 && weiboQualifiesForStandardV4(plan, anyNoWMDeferred) {
meta.CacheStandardVersion = library.WeiboCacheStandardVersion
}
applyWeiboPostStandardAndNoWMState(&meta, plan, previous, exists && !isConflict && !forceOverwrite, anyNoWMDeferred)

if hasProtectedLocal {
saved, sErr := m.commitStagedItemSwap(itemDir, previous, true, meta, workDir, mode)
Expand Down Expand Up @@ -2803,47 +2801,183 @@ func (m *Manager) materializeWeiboEditHistory(
return library.SanitizeItemEditHistory(out), nil
}

// weiboPlanHasStaticImagePages reports whether this Weibo plan contains at least
// one static (non-GIF, non-video) image page that could carry a watermark.
func weiboPlanHasStaticImagePages(plan WorkDownloadPlan) bool {
// weiboPlanRanNoWMStandard reports whether this Weibo post plan includes at least
// one static (non-GIF, non-video) page whose /oslarge/ watermark-free companion
// URL was evaluated during this cache run (DEC-079).
func weiboPlanRanNoWMStandard(plan WorkDownloadPlan) bool {
for _, p := range plan.Pages {
k := strings.ToLower(strings.TrimSpace(p.Kind))
if k == "gif" || k == "video" {
continue
}
if len(p.ImageURLs) > 0 || strings.TrimSpace(p.WatermarkFreeURL) != "" {
if strings.TrimSpace(p.WatermarkFreeURL) != "" {
return true
}
}
return false
}

// weiboPlanRanNoWMStandard reports whether this Weibo post plan includes at least
// one static (non-GIF, non-video) page whose /oslarge/ watermark-free companion
// URL was evaluated during this cache run (DEC-079).
func weiboPlanRanNoWMStandard(plan WorkDownloadPlan) bool {
for _, p := range plan.Pages {
// weiboQualifiesForStandardV4 reports whether a completed Weibo item qualifies
// for WeiboCacheStandardVersion (v6, DEC-096). Static-image NoWM evaluation is
// tracked independently via ItemMetadata.WeiboNoWMState, so removeWM == false
// never blocks CacheStandardVersion; only retryable /oslarge/ 418/429 deferrals
// hold back stamping on fresh/<v6 runs.
func weiboQualifiesForStandardV4(_ WorkDownloadPlan, anyNoWMDeferred bool) bool {
return !anyNoWMDeferred
}

func applyWeiboPostStandardAndNoWMState(
meta *library.ItemMetadata,
plan WorkDownloadPlan,
previous library.ItemMetadata,
canCarryPrevNoWM bool,
anyNoWMDeferred bool,
) {
if meta == nil {
return
}
hasLocalStatic := library.WeiboHasStaticImageSlots(*meta)
isComplete := meta.Status == "complete" && len(meta.Missing) == 0
ranNoWM := weiboPlanRanNoWMStandard(plan)
missingSlots := library.WeiboStaticImageSlotsMissingNoWM(*meta)

if anyNoWMDeferred {
if hasLocalStatic || len(missingSlots) > 0 {
meta.WeiboNoWMState = library.WeiboNoWMStatePending
}
if canCarryPrevNoWM && isComplete &&
previous.CacheStandardVersion >= library.WeiboCacheStandardVersion {
meta.CacheStandardVersion = library.WeiboCacheStandardVersion
}
return
}

if isComplete {
meta.CacheStandardVersion = library.WeiboCacheStandardVersion
}

if !hasLocalStatic {
// Zero static images on disk (pure text/video/GIF, or a partial stub where
// every planned static image failed to download): never fabricate "checked".
return
}
if ranNoWM {
// Only mark "checked" when all non-permanently-unavailable planned static
// pages were saved and evaluated; transient partial failures stay "" so they
// never override another node's "pending" state during MergeItemMetadata.
if isComplete || weiboAllNonDeadPlannedStaticPagesSaved(plan, *meta) {
meta.WeiboNoWMState = library.WeiboNoWMStateChecked
}
return
}
if len(missingSlots) == 0 {
// All static slots already have companions; only mark "checked" when all
// non-permanently-unavailable planned static pages were saved so a transient
// partial failure never suppresses later merges.
if isComplete || weiboAllNonDeadPlannedStaticPagesSaved(plan, *meta) {
meta.WeiboNoWMState = library.WeiboNoWMStateChecked
}
return
}
if canCarryPrevNoWM && (isComplete || weiboAllNonDeadPlannedStaticPagesSaved(plan, *meta)) &&
weiboPrevEvaluatedAllMissingNoWMSlots(previous, missingSlots) {
meta.WeiboNoWMState = library.WeiboNoWMStateChecked
return
}
meta.WeiboNoWMState = library.WeiboNoWMStatePending
}

func weiboAllNonDeadPlannedStaticPagesSaved(plan WorkDownloadPlan, meta library.ItemMetadata) bool {
if len(plan.Pages) == 0 {
return true
}
savedByIdx := make(map[int]string, len(meta.NotePages))
for i, pg := range meta.NotePages {
idx := pg.Index
if idx <= 0 {
idx = i + 1
}
savedByIdx[idx] = strings.TrimSpace(pg.Image)
}
deadByItem := make(map[string]bool, len(meta.Missing))
for _, m := range meta.Missing {
if strings.Contains(m.Reason, WeiboImageUnavailableReason) {
deadByItem[strings.ToLower(strings.TrimSpace(m.Item))] = true
}
}
for i, p := range plan.Pages {
k := strings.ToLower(strings.TrimSpace(p.Kind))
if k == "gif" || k == "video" {
continue
}
if strings.TrimSpace(p.WatermarkFreeURL) != "" {
return true
if len(p.ImageURLs) == 0 && strings.TrimSpace(p.WatermarkFreeURL) == "" {
continue
}
idx := p.Index
if idx <= 0 {
idx = i + 1
}
if savedByIdx[idx] != "" {
continue
}
if !deadByItem[fmt.Sprintf("page_%02d", idx)] {
return false
}
}
return false
return true
}

// weiboQualifiesForStandardV4 reports whether a completed Weibo item qualifies
// for WeiboCacheStandardVersion (v4, DEC-096): pure text, single video, and pure
// GIF posts qualify directly; static-image posts qualify once /oslarge/ was
// evaluated without any 418/429 deferral.
func weiboQualifiesForStandardV4(plan WorkDownloadPlan, anyNoWMDeferred bool) bool {
if anyNoWMDeferred {
func weiboPrevEvaluatedAllMissingNoWMSlots(previous library.ItemMetadata, missingSlots []library.WeiboStaticImageSlot) bool {
if !library.WeiboNoWMAlreadyEvaluated(previous) || len(missingSlots) == 0 {
return false
}
if weiboPlanHasStaticImagePages(plan) && !weiboPlanRanNoWMStandard(plan) {
return false
prevPicIDs := make(map[string]bool)
prevPaths := make(map[string]bool)
if len(previous.NotePages) > 0 {
for i, pg := range previous.NotePages {
imgPath := strings.ToLower(strings.TrimSpace(pg.Image))
if imgPath == "" || pg.GIF {
continue
}
kind := strings.ToLower(strings.TrimSpace(pg.Kind))
if kind == "video" || kind == "gif" || strings.HasSuffix(imgPath, ".gif") {
continue
}
prevPaths[imgPath] = true
pid := library.SanitizeMediaToken(pg.MediaID)
if pid == "" && len(previous.AssetIDs) == len(previous.NotePages) {
pid = library.SanitizeMediaToken(previous.AssetIDs[i])
}
if pid != "" {
prevPicIDs[pid] = true
}
}
} else {
var imgMedias []library.MediaFile
for _, mf := range previous.Media {
if mf.Role == "image" && strings.TrimSpace(mf.Path) != "" && !mf.GIF && !strings.HasSuffix(strings.ToLower(mf.Path), ".gif") {
imgMedias = append(imgMedias, mf)
}
}
for i, mf := range imgMedias {
prevPaths[strings.ToLower(strings.TrimSpace(mf.Path))] = true
if len(previous.AssetIDs) == len(imgMedias) {
if pid := library.SanitizeMediaToken(previous.AssetIDs[i]); pid != "" {
prevPicIDs[pid] = true
}
}
}
}
for _, slot := range missingSlots {
if slot.PicID != "" && len(prevPicIDs) > 0 {
if !prevPicIDs[slot.PicID] {
return false
}
continue
}
if !prevPaths[strings.ToLower(strings.TrimSpace(slot.PrimaryPath))] {
return false
}
}
return true
}
Expand Down
139 changes: 139 additions & 0 deletions internal/library/manifest_conflict_fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,142 @@ func TestWeiboV6VideoDynamicRangeAndCheckedCeilingSanitizeMergeRoundTrip(t *test
t.Fatalf("PortableManifest round-trip lost v6 fields: dv=%+v np=%+v", reparsed.DouyinVideo, reparsed.NotePages[0])
}
}

func TestWeiboNoWMStateSanitizeMergeAndPortableRoundTrip(t *testing.T) {
// 1. SanitizeItemMetadata allows only "", "pending", "checked" on Weibo and wipes non-Weibo or invalid values.
for _, tc := range []struct {
name string
site string
raw string
want string
}{
{"weibo-pending", "weibo", " PENDING ", WeiboNoWMStatePending},
{"weibo-checked", "weibo", "Checked", WeiboNoWMStateChecked},
{"weibo-empty", "weibo", "", ""},
{"weibo-invalid-url", "weibo", "https://evil.example/?ssig=leak", ""},
{"weibo-invalid-token", "weibo", "unknown_state", ""},
{"bilibili-wiped", "bilibili", WeiboNoWMStatePending, ""},
{"douyin-wiped", "douyin", WeiboNoWMStateChecked, ""},
{"x-wiped", "x", WeiboNoWMStatePending, ""},
} {
meta := ItemMetadata{
Version: 1,
ID: "dynamic:5099900000000001",
Site: tc.site,
Kind: "dynamic",
Status: "complete",
WeiboNoWMState: tc.raw,
}
SanitizeItemMetadata(&meta)
if meta.WeiboNoWMState != tc.want {
t.Fatalf("%s: WeiboNoWMState = %q, want %q", tc.name, meta.WeiboNoWMState, tc.want)
}
}

// 2. MergeItemMetadata precedence: "checked" > "pending" > "".
base := ItemMetadata{
Version: 1, ID: "dynamic:5099900000000002", Site: "weibo", Kind: "dynamic", Status: "complete",
}
withState := func(st string) ItemMetadata {
cp := base
cp.WeiboNoWMState = st
return cp
}
for _, tc := range []struct {
local, imported, want string
}{
{"", WeiboNoWMStatePending, WeiboNoWMStatePending},
{WeiboNoWMStatePending, "", WeiboNoWMStatePending},
{WeiboNoWMStatePending, WeiboNoWMStateChecked, WeiboNoWMStateChecked},
{WeiboNoWMStateChecked, WeiboNoWMStatePending, WeiboNoWMStateChecked},
{"", WeiboNoWMStateChecked, WeiboNoWMStateChecked},
{WeiboNoWMStateChecked, "", WeiboNoWMStateChecked},
} {
merged, _ := MergeItemMetadata(withState(tc.local), withState(tc.imported), MergeKeepLocal, nil)
if merged.WeiboNoWMState != tc.want {
t.Fatalf("MergeKeepLocal(%q, %q) = %q, want %q", tc.local, tc.imported, merged.WeiboNoWMState, tc.want)
}
}
// MergeUseImported carries valid state and scrubs poisoned values.
useImp, _ := MergeItemMetadata(withState(WeiboNoWMStateChecked), withState(WeiboNoWMStatePending), MergeUseImported, nil)
if useImp.WeiboNoWMState != WeiboNoWMStatePending {
t.Fatalf("MergeUseImported WeiboNoWMState = %q, want %q", useImp.WeiboNoWMState, WeiboNoWMStatePending)
}
useImpBad, _ := MergeItemMetadata(withState(WeiboNoWMStateChecked), withState("https://evil.example/?sub=bad"), MergeUseImported, nil)
if useImpBad.WeiboNoWMState != "" {
t.Fatalf("MergeUseImported did not scrub invalid WeiboNoWMState: %q", useImpBad.WeiboNoWMState)
}

// 2b. Legacy v3-v6 (WeiboNoWMState=="") static-image item merged with v6+"pending" preserves "checked" in both directions,
// while v2+"" or 0-image partial stub merged with v6+"pending" preserves "pending".
staticPages := []NotePageRecord{{Index: 1, Kind: "image", Image: "images/01.jpg", Width: 1080, Height: 1440}}
legacyV5 := ItemMetadata{
Version: 1, ID: "dynamic:5099900000000003", Site: "weibo", Kind: "dynamic", Status: "complete",
CacheStandardVersion: 5, WeiboNoWMState: "", NotePages: staticPages,
}
v6Pending := ItemMetadata{
Version: 1, ID: "dynamic:5099900000000003", Site: "weibo", Kind: "dynamic", Status: "complete",
CacheStandardVersion: WeiboCacheStandardVersion, WeiboNoWMState: WeiboNoWMStatePending, NotePages: staticPages,
}
v2Unchecked := ItemMetadata{
Version: 1, ID: "dynamic:5099900000000003", Site: "weibo", Kind: "dynamic", Status: "complete",
CacheStandardVersion: 2, WeiboNoWMState: "", NotePages: staticPages,
}
partialZeroImg := ItemMetadata{
Version: 1, ID: "dynamic:5099900000000003", Site: "weibo", Kind: "dynamic", Status: "partial",
CacheStandardVersion: 0, WeiboNoWMState: "", NotePages: []NotePageRecord{{Index: 1, Kind: "image", Image: ""}},
}
for _, mode := range []string{MergeKeepLocal, MergeUnion} {
m1, _ := MergeItemMetadata(legacyV5, v6Pending, mode, nil)
if m1.CacheStandardVersion != WeiboCacheStandardVersion || m1.WeiboNoWMState != WeiboNoWMStateChecked {
t.Fatalf("%s(legacyV5, v6Pending) = std=%d state=%q, want std=%d state=%q",
mode, m1.CacheStandardVersion, m1.WeiboNoWMState, WeiboCacheStandardVersion, WeiboNoWMStateChecked)
}
m2, _ := MergeItemMetadata(v6Pending, legacyV5, mode, nil)
if m2.CacheStandardVersion != WeiboCacheStandardVersion || m2.WeiboNoWMState != WeiboNoWMStateChecked {
t.Fatalf("%s(v6Pending, legacyV5) = std=%d state=%q, want std=%d state=%q",
mode, m2.CacheStandardVersion, m2.WeiboNoWMState, WeiboCacheStandardVersion, WeiboNoWMStateChecked)
}
m3, _ := MergeItemMetadata(v2Unchecked, v6Pending, mode, nil)
if m3.CacheStandardVersion != WeiboCacheStandardVersion || m3.WeiboNoWMState != WeiboNoWMStatePending {
t.Fatalf("%s(v2Unchecked, v6Pending) = std=%d state=%q, want std=%d state=%q",
mode, m3.CacheStandardVersion, m3.WeiboNoWMState, WeiboCacheStandardVersion, WeiboNoWMStatePending)
}
m4, _ := MergeItemMetadata(partialZeroImg, v6Pending, mode, nil)
if m4.WeiboNoWMState != WeiboNoWMStatePending {
t.Fatalf("%s(partialZeroImg, v6Pending) = state=%q, want %q", mode, m4.WeiboNoWMState, WeiboNoWMStatePending)
}
}

// 3. PortableManifest round-trip preserves valid Weibo state and cleans invalid/non-Weibo values.
for _, st := range []string{WeiboNoWMStatePending, WeiboNoWMStateChecked} {
src := withState(st)
src.CacheStandardVersion = WeiboCacheStandardVersion
raw, err := PortableManifest(src)
if err != nil {
t.Fatalf("PortableManifest(%q): %v", st, err)
}
reparsed, err := ParseItemMetadata(raw)
if err != nil {
t.Fatalf("ParseItemMetadata(%q): %v", st, err)
}
if reparsed.WeiboNoWMState != st || reparsed.Version != 1 {
t.Fatalf("PortableManifest round-trip(%q): got state=%q version=%d", st, reparsed.WeiboNoWMState, reparsed.Version)
}
}
nonWeibo := ItemMetadata{
Version: 1, ID: "dynamic:1800000000000000009", Site: "x", Kind: "dynamic", Status: "complete",
WeiboNoWMState: WeiboNoWMStatePending,
}
rawNonWeibo, err := PortableManifest(nonWeibo)
if err != nil {
t.Fatalf("PortableManifest(x): %v", err)
}
reparsedNonWeibo, err := ParseItemMetadata(rawNonWeibo)
if err != nil {
t.Fatalf("ParseItemMetadata(x): %v", err)
}
if reparsedNonWeibo.WeiboNoWMState != "" {
t.Fatalf("PortableManifest did not strip WeiboNoWMState on non-Weibo item: %q", reparsedNonWeibo.WeiboNoWMState)
}
}
Loading