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
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2019-08-07 15:49:25 +02:00
|
|
|
* File used for functions related to video history.
|
|
|
|
*/
|
2018-03-02 04:48:12 +01:00
|
|
|
|
2019-05-30 21:30:02 +02:00
|
|
|
/**
|
2019-08-07 15:49:25 +02:00
|
|
|
* Add a video to the history database file
|
|
|
|
*
|
|
|
|
* @param {string} videoId - The video ID of the video to be saved.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function addToHistory(data) {
|
|
|
|
historyDb.findOne({
|
|
|
|
videoId: data.videoId
|
|
|
|
}, function (err, doc) {
|
|
|
|
if (doc === null) {
|
|
|
|
historyDb.insert(data, (err, newDoc) => {});
|
|
|
|
let videoIndex = subscriptionView.fullVideoList.findIndex((video) => {
|
|
|
|
return video.videoId == data.videoId;
|
|
|
|
});
|
2019-05-30 21:30:02 +02:00
|
|
|
|
2019-08-07 15:49:25 +02:00
|
|
|
if (videoIndex >= 0) {
|
|
|
|
subscriptionView.fullVideoList[videoIndex].watched = true;
|
|
|
|
addSubsToView(subscriptionView.fullVideoList);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
historyDb.update({
|
|
|
|
videoId: data.videoId
|
|
|
|
}, {
|
|
|
|
videoId: data.videoId,
|
|
|
|
author: data.author,
|
|
|
|
authorId: data.authorId,
|
2019-02-28 21:24:28 +01:00
|
|
|
published: data.published,
|
2019-08-07 15:49:25 +02:00
|
|
|
publishedText: data.publishedText,
|
2019-02-28 21:24:28 +01:00
|
|
|
description: data.description,
|
|
|
|
viewCount: data.viewCount,
|
|
|
|
title: data.title,
|
|
|
|
lengthSeconds: data.lengthSeconds,
|
2019-08-07 15:49:25 +02:00
|
|
|
videoThumbnails: data.videoThumbnails,
|
2019-02-28 21:24:28 +01:00
|
|
|
liveNow: false,
|
|
|
|
paid: false,
|
|
|
|
type: 'video',
|
2019-08-07 15:49:25 +02:00
|
|
|
timeWatched: data.timeWatched,
|
|
|
|
watchProgress: data.watchProgress,
|
|
|
|
}, {}, (err, newDoc) => {});
|
2019-02-28 21:24:28 +01:00
|
|
|
}
|
2019-08-07 15:49:25 +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, toast = true) {
|
|
|
|
const data = {
|
|
|
|
videoId: videoId
|
|
|
|
};
|
|
|
|
historyDb.remove(data, {}, (err, numRemoved) => {
|
|
|
|
if (toast) {
|
|
|
|
if (!err) {
|
|
|
|
showToast('Video removed from history');
|
|
|
|
}
|
2019-02-28 21:24:28 +01:00
|
|
|
}
|
2018-03-02 04:48:12 +01:00
|
|
|
});
|
2019-08-07 15:49:25 +02:00
|
|
|
}
|
2018-09-19 17:01:21 +02:00
|
|
|
|
2019-08-07 15:49:25 +02:00
|
|
|
/**
|
|
|
|
* Update the watch progress video from the history database file
|
|
|
|
*
|
|
|
|
* @param {string} videoId - The video ID of the video to be removed.
|
|
|
|
*
|
|
|
|
* @param {double} lengthSeconds - The amount of time the video has been watched in seconds.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function updateWatchProgress(videoId, lengthSeconds) {
|
|
|
|
historyDb.findOne({
|
|
|
|
videoId: videoId
|
|
|
|
}, function (err, doc) {
|
|
|
|
if (doc !== null) {
|
|
|
|
historyDb.update({
|
|
|
|
videoId: videoId
|
|
|
|
}, {
|
|
|
|
$set: {
|
|
|
|
watchProgress: lengthSeconds
|
|
|
|
}
|
|
|
|
}, {}, (err, newDoc) => {});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the videos within the history database.
|
|
|
|
*
|
|
|
|
* @return {Void}
|
|
|
|
*/
|
|
|
|
function showHistory() {
|
|
|
|
ft.log('checking history');
|
|
|
|
|
|
|
|
historyView.videoList = [];
|
|
|
|
|
|
|
|
historyDb.find({}).sort({
|
|
|
|
timeWatched: -1
|
|
|
|
}).exec((err, docs) => {
|
|
|
|
docs.forEach((video, index) => {
|
|
|
|
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,
|
|
|
|
watchProgress: 0,
|
|
|
|
};
|
|
|
|
addToHistory(videoData);
|
|
|
|
videoData.position = index;
|
|
|
|
displayVideo(videoData, 'history');
|
|
|
|
}, () => {
|
|
|
|
removeFromHistory(video.videoId, false);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
video.position = index;
|
|
|
|
displayVideo(video, 'history');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
loadingView.seen = false;
|
|
|
|
});
|
2018-08-03 04:18:08 +02:00
|
|
|
}
|