Implement new fsm

This commit is contained in:
Ben Sarmiento
2024-05-21 01:59:53 +02:00
parent d5dd9426ed
commit 2c5e7a1db0
14 changed files with 121 additions and 59 deletions

View File

@@ -159,6 +159,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
OriginalName: info.OriginalName,
Added: info.Added,
Hash: info.Hash,
State: NewTorrentState("ok"),
}
// SelectedFiles is a subset of Files with only the selected ones
@@ -171,17 +172,17 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
continue
}
selectedFiles = append(selectedFiles, &File{
File: file,
Ended: info.Ended,
Link: "", // no link yet, consider it broken
IsBroken: true,
File: file,
Ended: info.Ended,
Link: "", // no link yet, consider it broken
State: NewFileState("broken"),
})
}
if len(selectedFiles) == len(info.Links) {
// all links are still intact! good!
for i, file := range selectedFiles {
file.Link = info.Links[i]
file.IsBroken = false
file.State.SetState("ok")
}
torrent.UnassignedLinks = mapset.NewSet[string]()
} else {
@@ -254,6 +255,8 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) *Torrent {
Components: mergedComponents,
UnassignedLinks: newer.UnassignedLinks.Union(older.UnassignedLinks),
UnrepairableReason: newer.UnrepairableReason,
State: older.State,
}
// unrepairable reason
@@ -268,32 +271,17 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) *Torrent {
// 3. empty - the file is not available
mainTorrent.SelectedFiles = cmap.New[*File]()
newer.SelectedFiles.IterCb(func(key string, newerFile *File) {
if !newerFile.IsBroken {
mainTorrent.SelectedFiles.Set(key, newerFile)
return
}
olderFile, ok := older.SelectedFiles.Get(key)
if ok && !olderFile.IsBroken {
mainTorrent.SelectedFiles.Set(key, olderFile)
return
}
mainTorrent.SelectedFiles.Set(key, newerFile)
})
inconsistentDeletes := false
older.SelectedFiles.IterCb(func(key string, olderFile *File) {
if !mainTorrent.SelectedFiles.Has(key) {
mainTorrent.SelectedFiles.Set(key, olderFile)
return
}
newerFile, _ := mainTorrent.SelectedFiles.Get(key)
if olderFile.IsDeleted && !newerFile.IsDeleted {
newerFile.IsDeleted = true
inconsistentDeletes = true
} else if olderFile.State.Is("deleted") {
newerFile, _ := mainTorrent.SelectedFiles.Get(key)
newerFile.State.SetState("deleted")
}
})
if inconsistentDeletes {
t.CheckDeletedStatus(&mainTorrent)
}
t.CheckDeletedStatus(&mainTorrent)
return &mainTorrent
}