Improve "last activity" calculation in WebAPI (#15339)

Co-authored-by: 秦风 <mayli.he@gmail.com>

Co-authored-by: Chocobo1 <Chocobo1@users.noreply.github.com>
This commit is contained in:
Chocobo1 2021-08-19 11:16:34 +08:00 committed by GitHub
parent d0d0bed333
commit 8d822f2cb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,6 +97,14 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
return (ratio > BitTorrent::Torrent::MAX_RATIO) ? -1 : ratio;
};
const auto getLastActivityTime = [&torrent]() -> qlonglong
{
const qlonglong timeSinceActivity = torrent.timeSinceActivity();
return (timeSinceActivity < 0)
? torrent.addedTime().toSecsSinceEpoch()
: (QDateTime::currentDateTime().toSecsSinceEpoch() - timeSinceActivity);
};
return {
{KEY_TORRENT_ID, torrent.id().toString()},
{KEY_TORRENT_INFOHASHV1, torrent.infoHash().v1().toString()},
@ -145,7 +153,7 @@ QVariantMap serialize(const BitTorrent::Torrent &torrent)
{KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()},
{KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()},
{KEY_TORRENT_SEEDING_TIME, torrent.seedingTime()},
{KEY_TORRENT_LAST_ACTIVITY_TIME, (QDateTime::currentDateTime().toSecsSinceEpoch() - torrent.timeSinceActivity())},
{KEY_TORRENT_LAST_ACTIVITY_TIME, getLastActivityTime()},
{KEY_TORRENT_AVAILABILITY, torrent.distributedCopies()},
{KEY_TORRENT_TOTAL_SIZE, torrent.totalSize()}