2017-11-01 10:33:08 +00:00
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import $ from 'cafy';
|
2018-03-29 11:32:18 +00:00
|
|
|
import Channel from '../../../../models/channel';
|
|
|
|
import Watching from '../../../../models/channel-watching';
|
2017-11-01 10:33:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unwatch a channel
|
|
|
|
*
|
|
|
|
* @param {any} params
|
|
|
|
* @param {any} user
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
module.exports = (params, user) => new Promise(async (res, rej) => {
|
2018-03-29 05:48:47 +00:00
|
|
|
// Get 'channelId' parameter
|
|
|
|
const [channelId, channelIdErr] = $(params.channelId).id().$;
|
|
|
|
if (channelIdErr) return rej('invalid channelId param');
|
2017-11-01 10:33:08 +00:00
|
|
|
|
|
|
|
//#region Fetch channel
|
|
|
|
const channel = await Channel.findOne({
|
|
|
|
_id: channelId
|
|
|
|
});
|
|
|
|
|
|
|
|
if (channel === null) {
|
|
|
|
return rej('channel not found');
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region Check whether not watching
|
|
|
|
const exist = await Watching.findOne({
|
2018-03-29 05:48:47 +00:00
|
|
|
userId: user._id,
|
|
|
|
channelId: channel._id,
|
|
|
|
deletedAt: { $exists: false }
|
2017-11-01 10:33:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist === null) {
|
|
|
|
return rej('already not watching');
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
// Delete watching
|
|
|
|
await Watching.update({
|
|
|
|
_id: exist._id
|
|
|
|
}, {
|
|
|
|
$set: {
|
2018-03-29 05:48:47 +00:00
|
|
|
deletedAt: new Date()
|
2017-11-01 10:33:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Send response
|
|
|
|
res();
|
|
|
|
|
|
|
|
// Decrement watching count
|
|
|
|
Channel.update(channel._id, {
|
|
|
|
$inc: {
|
2018-03-29 05:48:47 +00:00
|
|
|
watchingCount: -1
|
2017-11-01 10:33:08 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|