Refactor torrent fetching

This commit is contained in:
Ben Sarmiento
2024-04-30 00:16:38 +02:00
parent 00bd0763d6
commit 102512f7d3
4 changed files with 77 additions and 90 deletions

View File

@@ -1,23 +1,21 @@
package torrent
type LibraryState struct {
TotalCount int
ActiveCount int
FirstActiveTorrentId string
FirstTorrentId string
TotalCount int
ActiveCount int
FirstTorrentId string
}
func (ls *LibraryState) Eq(a LibraryState) bool {
if ls.TotalCount == 0 || ls.FirstActiveTorrentId == "" || ls.FirstTorrentId == "" {
if ls.TotalCount == 0 || ls.FirstTorrentId == "" {
return false
}
return a.TotalCount == ls.TotalCount && a.ActiveCount == ls.ActiveCount && a.FirstActiveTorrentId == ls.FirstActiveTorrentId && a.FirstTorrentId == ls.FirstTorrentId
return a.TotalCount == ls.TotalCount && a.ActiveCount == ls.ActiveCount && a.FirstTorrentId == ls.FirstTorrentId
}
func (t *TorrentManager) setNewLatestState(checksum LibraryState) {
t.latestState.ActiveCount = checksum.ActiveCount
t.latestState.TotalCount = checksum.TotalCount
t.latestState.FirstActiveTorrentId = checksum.FirstActiveTorrentId
t.latestState.FirstTorrentId = checksum.FirstTorrentId
}
@@ -25,16 +23,7 @@ func (t *TorrentManager) setNewLatestState(checksum LibraryState) {
func (t *TorrentManager) getCurrentState() LibraryState {
var state LibraryState
activeTorrents, _, err := t.Api.GetTorrents(1, true)
if err != nil {
t.log.Errorf("Checksum API Error (GetActiveTorrents): %v", err)
return LibraryState{}
}
if len(activeTorrents) > 0 {
state.FirstActiveTorrentId = activeTorrents[0].ID
}
torrents, totalCount, err := t.Api.GetTorrents(1, true)
torrents, totalCount, err := t.Api.GetTorrents(true)
if err != nil {
t.log.Errorf("Checksum API Error (GetTorrents): %v", err)
return LibraryState{}