2020-08-30 15:14:05 +00:00
const { render } = require ( "pinski/plugins" )
2020-09-23 12:05:02 +00:00
const db = require ( "../utils/db" )
const { getUser } = require ( "../utils/getuser" )
2021-05-11 12:29:44 +00:00
const { timeToPastText , rewriteVideoDescription , applyVideoFilters } = require ( "../utils/converters" )
2020-10-02 12:32:22 +00:00
const { refresher } = require ( "../background/feed-update" )
2020-08-30 15:14:05 +00:00
module . exports = [
{
2021-05-11 12:29:44 +00:00
route : ` /subscriptions ` , methods : [ "GET" ] , code : async ( { req , url } ) => {
2020-08-30 15:14:05 +00:00
const user = getUser ( req )
let hasSubscriptions = false
let videos = [ ]
let channels = [ ]
2022-01-10 01:18:45 +00:00
let missingChannelCount = 0
2020-09-23 12:48:32 +00:00
let refreshed = null
2020-08-30 15:14:05 +00:00
if ( user . token ) {
2020-10-02 12:32:22 +00:00
// trigger a background refresh, needed if they came back from being inactive
refresher . skipWaiting ( )
2020-09-23 12:48:32 +00:00
// get channels
2021-08-26 11:47:53 +00:00
channels = db . prepare ( ` SELECT Channels.* FROM Channels INNER JOIN Subscriptions ON Channels.ucid = Subscriptions.ucid WHERE token = ? ORDER BY name ` ) . all ( user . token )
2022-01-10 01:18:45 +00:00
missingChannelCount = channels . reduce ( ( a , c ) => a + c . missing , 0 )
2020-09-23 12:48:32 +00:00
// get refreshed status
2021-08-26 11:47:53 +00:00
refreshed = db . prepare ( ` SELECT min(refreshed) as min, max(refreshed) as max, count(refreshed) as count FROM Channels INNER JOIN Subscriptions ON Channels.ucid = Subscriptions.ucid WHERE token = ? ` ) . get ( user . token )
2020-12-28 12:42:25 +00:00
// get watched videos
const watchedVideos = user . getWatchedVideos ( )
2020-09-23 12:48:32 +00:00
// get videos
2021-08-26 11:47:53 +00:00
if ( channels . length ) {
2020-08-30 15:14:05 +00:00
hasSubscriptions = true
2021-08-26 11:47:53 +00:00
videos = db . prepare ( ` SELECT Videos.* FROM Videos INNER JOIN Subscriptions ON Videos.authorID = Subscriptions.ucid WHERE token = ? ORDER BY published DESC LIMIT 60 ` ) . all ( user . token )
2020-09-23 12:05:02 +00:00
. map ( video => {
2020-09-23 12:48:32 +00:00
video . publishedText = timeToPastText ( video . published * 1000 )
2020-12-28 12:42:25 +00:00
video . watched = watchedVideos . includes ( video . videoId )
2021-04-14 11:02:05 +00:00
video . descriptionHtml = rewriteVideoDescription ( video . descriptionHtml , video . videoId )
2020-09-23 12:05:02 +00:00
return video
} )
2020-08-30 15:14:05 +00:00
}
2021-05-11 12:29:44 +00:00
const filters = user . getFilters ( )
; ( { videos } = applyVideoFilters ( videos , filters ) )
2020-08-30 15:14:05 +00:00
}
2020-12-28 12:42:25 +00:00
const settings = user . getSettingsOrDefaults ( )
const instanceOrigin = settings . instance
2023-01-15 09:06:31 +00:00
return render ( 200 , "pug/subscriptions.pug" , { req , url , settings , hasSubscriptions , videos , channels , missingChannelCount , refreshed , timeToPastText , instanceOrigin } )
2020-08-30 15:14:05 +00:00
}
}
]