From 571c2a953dec3d834b06643818bcdbc764e0ccb2 Mon Sep 17 00:00:00 2001 From: OldHawk Date: Fri, 3 Nov 2017 18:34:51 +0800 Subject: [PATCH] fix(core): fixed torrent set salesType and recommend level issues --- .../torrent-info.client.controller.js | 57 ++++++++++--------- .../controllers/torrents.server.controller.js | 2 + .../server/models/torrent.server.model.js | 45 ++++++++++----- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/modules/torrents/client/controllers/torrent-info.client.controller.js b/modules/torrents/client/controllers/torrent-info.client.controller.js index f56fe2bb..45240045 100644 --- a/modules/torrents/client/controllers/torrent-info.client.controller.js +++ b/modules/torrents/client/controllers/torrent-info.client.controller.js @@ -975,42 +975,47 @@ * setSaleType */ vm.setSaleType = function () { - TorrentsService.setSaleType({ - _torrentId: vm.torrentLocalInfo._id, - _saleType: vm.model_salestype - }, function (res) { - Notification.success({ - message: ' ' + $translate.instant('TORRENT_SETSALETYPE_SUCCESSFULLY') - }); + if (vm.model_salestype && vm.model_salestype !== vm.torrentLocalInfo.torrent_sale_status) { + TorrentsService.setSaleType({ + _torrentId: vm.torrentLocalInfo._id, + _saleType: vm.model_salestype + }, function (res) { + Notification.success({ + message: ' ' + $translate.instant('TORRENT_SETSALETYPE_SUCCESSFULLY') + }); - vm.torrentLocalInfo = res; - }, function (res) { - Notification.error({ - message: res.data.message, - title: ' ' + $translate.instant('TORRENT_SETSALETYPE_ERROR') + mtDebug.info(res); + vm.torrentLocalInfo = res; + }, function (res) { + Notification.error({ + message: res.data.message, + title: ' ' + $translate.instant('TORRENT_SETSALETYPE_ERROR') + }); }); - }); + } }; /** * setRecommendLevel */ vm.setRecommendLevel = function () { - TorrentsService.setRecommendLevel({ - _torrentId: vm.torrentLocalInfo._id, - _rlevel: vm.model_rlevel - }, function (res) { - Notification.success({ - message: ' ' + $translate.instant('TORRENT_SETRLEVEL_SUCCESSFULLY') - }); + if (vm.model_rlevel && vm.model_rlevel !== vm.torrentLocalInfo.torrent_recommended) { + TorrentsService.setRecommendLevel({ + _torrentId: vm.torrentLocalInfo._id, + _rlevel: vm.model_rlevel + }, function (res) { + Notification.success({ + message: ' ' + $translate.instant('TORRENT_SETRLEVEL_SUCCESSFULLY') + }); - vm.torrentLocalInfo = res; - }, function (res) { - Notification.error({ - message: res.data.message, - title: ' ' + $translate.instant('TORRENT_SETRLEVEL_ERROR') + vm.torrentLocalInfo = res; + }, function (res) { + Notification.error({ + message: res.data.message, + title: ' ' + $translate.instant('TORRENT_SETRLEVEL_ERROR') + }); }); - }); + } }; /** diff --git a/modules/torrents/server/controllers/torrents.server.controller.js b/modules/torrents/server/controllers/torrents.server.controller.js index 4298eb2a..2e8f2896 100644 --- a/modules/torrents/server/controllers/torrents.server.controller.js +++ b/modules/torrents/server/controllers/torrents.server.controller.js @@ -873,6 +873,8 @@ exports.setSaleType = function (req, res) { torrent.torrent_sale_status = req.params.saleType; torrent.torrent_sale_expires = Date.now() + gbit * config.meanTorrentConfig.torrentSalesType.expires.time; + mtDebug.debugGreen(torrent); + torrent.save(function (err) { if (err) { return res.status(422).send({ diff --git a/modules/torrents/server/models/torrent.server.model.js b/modules/torrents/server/models/torrent.server.model.js index 2d6328a0..f3b0b769 100644 --- a/modules/torrents/server/models/torrent.server.model.js +++ b/modules/torrents/server/models/torrent.server.model.js @@ -159,6 +159,10 @@ var TorrentSchema = new Schema({ torrent_sale_expires: { type: Date }, + isSaling: { + type: Boolean, + default: false + }, torrent_recommended: { type: String, default: 'none' @@ -193,26 +197,41 @@ var TorrentSchema = new Schema({ } }); + /** - * overwrite toJSON + * Hook a pre save method */ -TorrentSchema.methods.toJSON = function (options) { - var document = this.toObject(options); - document.isSaling = false; +TorrentSchema.pre('save', function (next) { + writeIsSaling(this); + next(); +}); - if (this.torrent_sale_expires > Date.now()) { - document.isSaling = true; +/** + * Hook a pre save method + */ +TorrentSchema.pre('update', function (next) { + writeIsSaling(this); + next(); +}); + +/** + * countRatio + * @param user + */ +function writeIsSaling(torrent) { + torrent.isSaling = false; + + if (torrent.torrent_sale_expires > Date.now()) { + torrent.isSaling = true; } - if (!document.isSaling) { - document.torrent_sale_status = 'U1/D1'; + if (!torrent.isSaling) { + torrent.torrent_sale_status = 'U1/D1'; } - if (document.torrent_sale_status === 'U1/D1') { - document.isSaling = false; + if (torrent.torrent_sale_status === 'U1/D1') { + torrent.isSaling = false; } - - return document; -}; +} TorrentSchema.index({user: -1, createdat: -1}); TorrentSchema.index({info_hash: -1, createdat: -1});