2018-03-02 04:48:12 +01:00
|
|
|
/*
|
2018-07-24 03:11:56 +02:00
|
|
|
This file is part of FreeTube.
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
FreeTube is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
FreeTube is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with FreeTube. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-02 04:48:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2018-03-15 15:59:50 +01:00
|
|
|
* File for all functions related to subscriptions.
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-09-19 17:39:12 +02:00
|
|
|
let subscriptionTimer;
|
2018-11-09 16:11:25 +01:00
|
|
|
let forceTimer;
|
2018-09-19 17:39:12 +02:00
|
|
|
let checkSubscriptions = true;
|
2018-11-09 16:11:25 +01:00
|
|
|
let forceSubs = true;
|
2019-07-26 21:07:33 +02:00
|
|
|
let displaySubsLock = false;
|
2018-06-25 22:04:11 +02:00
|
|
|
|
2018-03-02 04:48:12 +01:00
|
|
|
/**
|
2018-03-15 15:59:50 +01:00
|
|
|
* Add a channel to the user's subscription database.
|
|
|
|
*
|
|
|
|
* @param {string} channelId - The channel ID to add to the subscriptions database.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2019-07-26 21:07:33 +02:00
|
|
|
function addSubscription(data, useToast = true, profile = '') {
|
2019-02-27 20:14:08 +01:00
|
|
|
ft.log('Channel Data: ', data);
|
|
|
|
|
2019-07-26 21:07:33 +02:00
|
|
|
let newSubscription = data;
|
|
|
|
newSubscription.profile = [{
|
|
|
|
value: profile
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (profile === '') {
|
|
|
|
newSubscription.profile = [{
|
|
|
|
value: profileSelectView.activeProfile.name
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
subDb.find({
|
|
|
|
channelId: data.channelId
|
|
|
|
}, (err, docs) => {
|
|
|
|
if (jQuery.isEmptyObject(docs)) {
|
|
|
|
subDb.insert(newSubscription, (err, newDoc) => {
|
|
|
|
if (useToast) {
|
|
|
|
showToast('Added ' + newSubscription.channelName + ' to subscriptions.');
|
|
|
|
}
|
|
|
|
// Refresh the list of subscriptions on the side navigation bar.
|
|
|
|
displaySubs();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
subDb.update({
|
|
|
|
channelId: data.channelId,
|
|
|
|
}, {
|
|
|
|
$push: {
|
|
|
|
profile: {
|
|
|
|
value: newSubscription.profile[0].value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, (err, numAdded) => {
|
|
|
|
// Refresh the list of subscriptions on the side navigation bar.
|
|
|
|
displaySubs();
|
|
|
|
showToast('Added ' + data.channelName + ' to subscriptions.');
|
|
|
|
});
|
2019-02-27 20:14:08 +01:00
|
|
|
}
|
2018-03-02 04:48:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 15:59:50 +01:00
|
|
|
* Remove a channel from the subscriptions database.
|
|
|
|
*
|
|
|
|
* @param {string} channelId - The channel ID to be removed.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
function removeSubscription(channelId) {
|
2019-07-26 21:07:33 +02:00
|
|
|
subDb.find({
|
2018-07-24 03:11:56 +02:00
|
|
|
channelId: channelId
|
2019-07-26 21:07:33 +02:00
|
|
|
}, (err, docs) => {
|
|
|
|
if (docs[0].profile.length > 1) {
|
|
|
|
subDb.update({
|
|
|
|
channelId: channelId,
|
|
|
|
}, {
|
|
|
|
$pull: {
|
|
|
|
profile: {
|
|
|
|
value: profileSelectView.activeProfile.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, (err, numAdded) => {
|
|
|
|
// Refresh the list of subscriptions on the side navigation bar.
|
|
|
|
displaySubs();
|
|
|
|
showToast('Removed channel from subscriptions.');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
subDb.remove({
|
|
|
|
channelId: channelId
|
|
|
|
}, {}, (err, numRemoved) => {
|
|
|
|
// Refresh the list of subscriptions on the side navigation bar.
|
|
|
|
displaySubs();
|
|
|
|
showToast('Removed channel from subscriptions.');
|
|
|
|
});
|
|
|
|
}
|
2018-07-24 03:11:56 +02:00
|
|
|
});
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 15:59:50 +01:00
|
|
|
* Load the recent uploads of the user's subscriptions.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-09-19 17:39:12 +02:00
|
|
|
function loadSubscriptions() {
|
|
|
|
if (checkSubscriptions === false && subscriptionView.videoList.length > 0) {
|
2019-05-20 20:53:14 +02:00
|
|
|
ft.log('Will not load subscriptions. Timer still on.');
|
2018-09-19 17:39:12 +02:00
|
|
|
loadingView.seen = false;
|
|
|
|
return;
|
|
|
|
} else {
|
2019-07-13 16:07:48 +02:00
|
|
|
showToast('Refreshing Subscription List. Please wait…');
|
2018-09-19 17:39:12 +02:00
|
|
|
checkSubscriptions = false;
|
2018-09-21 21:56:59 +02:00
|
|
|
progressView.seen = true;
|
2019-06-12 04:29:56 +02:00
|
|
|
$('#reloadButton')[0].classList.add('fa-spin');
|
2018-09-19 17:39:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let videoList = [];
|
|
|
|
|
|
|
|
const subscriptions = returnSubscriptions();
|
|
|
|
|
|
|
|
subscriptions.then((results) => {
|
|
|
|
let channelId = '';
|
|
|
|
let videoList = [];
|
|
|
|
|
|
|
|
if (results.length > 0) {
|
|
|
|
let counter = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < results.length; i++) {
|
|
|
|
channelId = results[i]['channelId'];
|
|
|
|
|
2019-02-26 17:26:05 +01:00
|
|
|
invidiousAPI('channels/latest', channelId, {}, (data) => {
|
|
|
|
data.forEach((video, index) => {
|
2019-07-26 21:07:33 +02:00
|
|
|
data[index].author = results[i]['channelName'];
|
|
|
|
data[index].profile = results[i]['profile'];
|
|
|
|
historyDb.findOne({
|
|
|
|
videoId: video.videoId
|
|
|
|
}, function (err, doc) {
|
|
|
|
if (doc === null) {
|
|
|
|
data[index].watched = false
|
|
|
|
} else {
|
|
|
|
data[index].watched = true;
|
|
|
|
}
|
|
|
|
});
|
2019-02-26 17:26:05 +01:00
|
|
|
});
|
2018-09-19 17:39:12 +02:00
|
|
|
videoList = videoList.concat(data);
|
|
|
|
counter = counter + 1;
|
|
|
|
progressView.progressWidth = (counter / results.length) * 100;
|
|
|
|
|
|
|
|
if (counter === results.length) {
|
2018-11-09 16:11:25 +01:00
|
|
|
addSubsToView(videoList);
|
|
|
|
}
|
|
|
|
}, (errorData) => {
|
|
|
|
showToast('Unable to load channel: ' + results[i]['channelName']);
|
|
|
|
counter = counter + 1;
|
|
|
|
progressView.progressWidth = (counter / results.length) * 100;
|
|
|
|
if (counter === results.length) {
|
|
|
|
addSubsToView(videoList);
|
2018-09-19 17:39:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// User has no subscriptions. Display message.
|
|
|
|
loadingView.seen = false;
|
|
|
|
headerView.seen = false;
|
|
|
|
noSubscriptions.seen = true;
|
2019-06-12 04:29:56 +02:00
|
|
|
$('#reloadButton')[0].classList.remove('fa-spin');
|
2018-09-19 17:39:12 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-11-09 16:11:25 +01:00
|
|
|
function addSubsToView(videoList) {
|
2019-07-02 21:46:59 +02:00
|
|
|
subscriptionView.fullVideoList = videoList;
|
|
|
|
|
2019-02-25 16:27:31 +01:00
|
|
|
videoList = videoList.filter(a => {
|
|
|
|
return !a.premium;
|
|
|
|
});
|
|
|
|
|
2019-07-02 21:46:59 +02:00
|
|
|
if (hideWatchedSubs) {
|
2019-07-26 21:07:33 +02:00
|
|
|
videoList = videoList.filter(a => {
|
|
|
|
return !a.watched;
|
|
|
|
});
|
2019-07-02 21:46:59 +02:00
|
|
|
}
|
|
|
|
|
2019-07-26 21:07:33 +02:00
|
|
|
videoList = videoList.filter(a => {
|
|
|
|
return a.profile.map(x => x.value).indexOf(profileSelectView.activeProfile.name) !== -1
|
|
|
|
});
|
|
|
|
|
2018-11-09 16:11:25 +01:00
|
|
|
videoList.sort((a, b) => {
|
|
|
|
return b.published - a.published;
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriptionView.videoList = [];
|
|
|
|
|
|
|
|
if (videoList.length > 100) {
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
displayVideo(videoList[i], 'subscriptions');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
videoList.forEach((video) => {
|
|
|
|
displayVideo(video, 'subscriptions');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadingView.seen = false;
|
|
|
|
progressView.seen = false;
|
|
|
|
progressView.progressWidth = 0;
|
2019-06-12 04:29:56 +02:00
|
|
|
$('#reloadButton')[0].classList.remove('fa-spin');
|
2018-11-09 16:11:25 +01:00
|
|
|
|
|
|
|
subscriptionTimer = window.setTimeout(() => {
|
|
|
|
checkSubscriptions = true;
|
|
|
|
}, 7200000);
|
|
|
|
|
2019-05-20 20:53:14 +02:00
|
|
|
ft.log('Done');
|
2018-11-09 16:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function forceSubscriptions() {
|
|
|
|
if (progressView.progressWidth > 0) {
|
|
|
|
showToast('Please wait for subscriptions to finish loading before trying again.');
|
|
|
|
return;
|
|
|
|
} else if (forceSubs === false) {
|
|
|
|
showToast('Too many attempts. Please wait before loading subscriptions again.');
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
window.clearTimeout(subscriptionTimer);
|
|
|
|
checkSubscriptions = true;
|
|
|
|
loadSubscriptions();
|
|
|
|
forceSubs = false;
|
|
|
|
}
|
|
|
|
forceTimer = window.setTimeout(() => {
|
|
|
|
forceSubs = true;
|
|
|
|
}, 300000);
|
2018-11-08 20:21:04 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 04:48:12 +01:00
|
|
|
/**
|
2018-03-15 15:59:50 +01:00
|
|
|
* Get the list of subscriptions from the user's subscription database.
|
|
|
|
*
|
|
|
|
* @return {promise} The list of subscriptions.
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
function returnSubscriptions() {
|
2018-07-24 03:11:56 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
subDb.find({}, (err, subs) => {
|
|
|
|
resolve(subs);
|
|
|
|
});
|
2018-03-02 04:48:12 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 15:59:50 +01:00
|
|
|
* Display the list of subscriptions on the side navigation bar.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
function displaySubs() {
|
2019-07-26 21:07:33 +02:00
|
|
|
if (displaySubsLock) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
displaySubsLock = true;
|
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
const subList = document.getElementById('subscriptions');
|
|
|
|
|
|
|
|
subList.innerHTML = '';
|
|
|
|
|
|
|
|
// Sort alphabetically
|
2019-07-26 21:07:33 +02:00
|
|
|
subDb.find({
|
|
|
|
profile: {
|
|
|
|
$elemMatch: {
|
|
|
|
value: profileSelectView.activeProfile.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).sort({
|
2018-07-24 03:11:56 +02:00
|
|
|
channelName: 1
|
|
|
|
}).exec((err, subs) => {
|
|
|
|
subs.forEach((channel) => {
|
|
|
|
// Grab subscriptions.html to be used as a template.
|
|
|
|
const subsTemplate = require('./templates/subscriptions.html')
|
|
|
|
mustache.parse(subsTemplate);
|
|
|
|
const rendered = mustache.render(subsTemplate, {
|
|
|
|
channelIcon: channel['channelThumbnail'],
|
|
|
|
channelName: channel['channelName'],
|
|
|
|
channelId: channel['channelId'],
|
|
|
|
});
|
|
|
|
// Render template to page.
|
|
|
|
const subscriptionsHtml = $('#subscriptions').html();
|
|
|
|
$('#subscriptions').html(subscriptionsHtml + rendered);
|
|
|
|
});
|
2019-07-26 21:07:33 +02:00
|
|
|
|
|
|
|
displaySubsLock = false;
|
2018-03-02 04:48:12 +01:00
|
|
|
});
|
|
|
|
|
2018-07-24 03:11:56 +02:00
|
|
|
// Add onclick function
|
|
|
|
$('#subscriptions .fa-times').onClick = removeSubscription;
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 15:59:50 +01:00
|
|
|
* Adds / Removes a subscription based on if the channel is in the database or not.
|
|
|
|
* @param {string} channelId - The channel ID to check
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2019-02-27 20:14:08 +01:00
|
|
|
function toggleSubscription(data) {
|
2018-07-24 03:11:56 +02:00
|
|
|
event.stopPropagation();
|
|
|
|
|
2019-02-27 20:14:08 +01:00
|
|
|
const checkIfSubscribed = isSubscribed(data.channelId);
|
2018-07-24 03:11:56 +02:00
|
|
|
|
|
|
|
checkIfSubscribed.then((results) => {
|
|
|
|
|
|
|
|
if (results === false) {
|
2019-02-27 19:46:25 +01:00
|
|
|
playerView.subscribedText = 'UNSUBSCRIBE';
|
2019-02-27 20:14:08 +01:00
|
|
|
channelView.subButtonText = 'UNSUBSCRIBE';
|
|
|
|
addSubscription(data);
|
|
|
|
} else {
|
|
|
|
playerView.subscribedText = 'SUBSCRIBE';
|
|
|
|
channelView.subButtonText = 'SUBSCRIBE';
|
|
|
|
removeSubscription(data.channelId);
|
2018-07-24 03:11:56 +02:00
|
|
|
}
|
|
|
|
});
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-15 15:59:50 +01:00
|
|
|
* Check if the user is subscribed to a channel or not.
|
|
|
|
*
|
|
|
|
* @param {string} channelId - The channel ID to check
|
|
|
|
*
|
|
|
|
* @return {promise} - A boolean value if the channel is currently subscribed or not.
|
|
|
|
*/
|
2019-07-26 21:07:33 +02:00
|
|
|
function isSubscribed(channelId, profile = profileSelectView.activeProfile.name) {
|
2018-07-24 03:11:56 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
subDb.find({
|
2019-07-26 21:07:33 +02:00
|
|
|
$and: [{
|
|
|
|
channelId: channelId
|
|
|
|
},
|
|
|
|
{
|
|
|
|
profile: {
|
|
|
|
$elemMatch: {
|
|
|
|
value: profile
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
2018-07-24 03:11:56 +02:00
|
|
|
}, (err, docs) => {
|
|
|
|
if (jQuery.isEmptyObject(docs)) {
|
|
|
|
resolve(false);
|
|
|
|
} else {
|
|
|
|
resolve(true);
|
|
|
|
}
|
|
|
|
});
|
2018-03-02 04:48:12 +01:00
|
|
|
});
|
2018-08-03 04:18:08 +02:00
|
|
|
}
|