2018-03-02 04:48:12 +01:00
|
|
|
/*
|
2018-08-03 04:18:08 +02:00
|
|
|
This file is part of FreeTube.
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
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-08-03 04:18:08 +02:00
|
|
|
* File used for functions related to video history.
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
|
|
|
|
/**
|
2018-08-03 04:18:08 +02:00
|
|
|
* Add a video to the history database file
|
|
|
|
*
|
|
|
|
* @param {string} videoId - The video ID of the video to be saved.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
2019-02-28 21:24:28 +01:00
|
|
|
function addToHistory(data){
|
|
|
|
historyDb.findOne({ videoId: data.videoId }, function (err, doc) {
|
2019-02-25 16:35:49 +01:00
|
|
|
if(doc === null) {
|
|
|
|
historyDb.insert(data, (err, newDoc) => {});
|
|
|
|
} else {
|
|
|
|
historyDb.update(
|
2019-02-28 21:24:28 +01:00
|
|
|
{ videoId: data.videoId },
|
|
|
|
{
|
|
|
|
videoId: data.videoId,
|
|
|
|
author: data.author,
|
|
|
|
authorId: data.authorId,
|
|
|
|
published: data.published,
|
|
|
|
publishedText: data.publishedText,
|
|
|
|
description: data.description,
|
|
|
|
viewCount: data.viewCount,
|
|
|
|
title: data.title,
|
|
|
|
description: data.description,
|
|
|
|
lengthSeconds: data.lengthSeconds,
|
|
|
|
videoThumbnails: data.videoThumbnails,
|
|
|
|
type: 'video',
|
|
|
|
timeWatched: data.timeWatched,
|
2019-02-25 16:35:49 +01:00
|
|
|
}, {}, (err, newDoc) => {});
|
|
|
|
}
|
|
|
|
});
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-03 04:18:08 +02:00
|
|
|
* Remove a video from the history database file
|
|
|
|
*
|
|
|
|
* @param {string} videoId - The video ID of the video to be removed.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function removeFromHistory(videoId){
|
|
|
|
const data = {videoId: videoId};
|
2018-11-09 16:11:25 +01:00
|
|
|
historyDb.remove(data, {}, (err, numRemoved) => {
|
|
|
|
if (!err) {
|
|
|
|
showToast('Video removed from history');
|
|
|
|
}
|
|
|
|
});
|
2018-03-02 04:48:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the videos within the history database.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function showHistory(){
|
|
|
|
console.log('checking history');
|
|
|
|
|
2018-09-19 17:01:21 +02:00
|
|
|
historyView.videoList = [];
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2018-08-03 04:18:08 +02:00
|
|
|
historyDb.find({}).sort({
|
|
|
|
timeWatched: -1
|
|
|
|
}).exec((err, docs) => {
|
2019-02-25 16:37:04 +01:00
|
|
|
docs.forEach((video, index) => {
|
2019-02-28 21:24:28 +01:00
|
|
|
if (video.authorId === undefined) {
|
|
|
|
// History data is from old version of FreeTube, update data for future calls.
|
|
|
|
invidiousAPI('videos', video.videoId, {}, (data) => {
|
|
|
|
let publishedText = new Date(data.published * 1000);
|
|
|
|
publishedText = dateFormat(publishedText, "mmm dS, yyyy");
|
|
|
|
let videoData = {
|
|
|
|
videoId: video.videoId,
|
|
|
|
published: data.published,
|
|
|
|
publishedText: publishedText,
|
|
|
|
description: data.description,
|
|
|
|
viewCount: data.viewCount,
|
|
|
|
title: data.title,
|
|
|
|
lengthSeconds: data.lengthSeconds,
|
|
|
|
videoThumbnails: data.videoThumbnails[4].url,
|
|
|
|
author: data.author,
|
|
|
|
authorId: data.authorId,
|
|
|
|
liveNow: false,
|
|
|
|
paid: false,
|
|
|
|
type: 'video',
|
|
|
|
timeWatched: video.timeWatched,
|
|
|
|
};
|
|
|
|
addToHistory(videoData);
|
|
|
|
videoData.position = index;
|
|
|
|
displayVideo(videoData, 'history');
|
2019-03-19 18:45:25 +01:00
|
|
|
}, () => {
|
|
|
|
removeFromHistory(video.videoId);
|
2019-02-28 21:24:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
video.position = index;
|
|
|
|
displayVideo(video, 'history');
|
|
|
|
}
|
2018-03-02 04:48:12 +01:00
|
|
|
});
|
2018-09-19 17:01:21 +02:00
|
|
|
|
|
|
|
loadingView.seen = false;
|
2018-08-03 04:18:08 +02:00
|
|
|
});
|
|
|
|
}
|