Files
zurg/internal/torrent/latestState.go
2024-01-31 18:08:48 +01:00

48 lines
1.3 KiB
Go

package torrent
type LibraryState struct {
TotalCount int
ActiveCount int
FirstActiveTorrentId string
}
func (ls *LibraryState) Eq(a LibraryState) bool {
if ls.TotalCount == 0 || ls.FirstActiveTorrentId == "" {
return false
}
if a.TotalCount != ls.TotalCount || a.ActiveCount != ls.ActiveCount || a.FirstActiveTorrentId != ls.FirstActiveTorrentId {
return false
}
return true
}
func (t *TorrentManager) setNewLatestState(checksum LibraryState) {
t.latestState.ActiveCount = checksum.ActiveCount
t.latestState.TotalCount = checksum.TotalCount
t.latestState.FirstActiveTorrentId = checksum.FirstActiveTorrentId
}
// generates a checksum based on the number of torrents, the first torrent id and the number of active torrents
func (t *TorrentManager) getCurrentState() LibraryState {
var state LibraryState
activeTorrents, totalCount, err := t.Api.GetTorrents(1, true)
if err != nil {
t.log.Errorf("Checksum API Error (GetTorrents): %v", err)
return LibraryState{}
}
state.TotalCount = totalCount
if len(activeTorrents) > 0 {
state.FirstActiveTorrentId = activeTorrents[0].ID
}
count, err := t.Api.GetActiveTorrentCount()
if err != nil {
t.log.Errorf("Checksum API Error (GetActiveTorrentCount): %v", err)
return LibraryState{}
}
state.ActiveCount = count.DownloadingCount
return state
}