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 = [ ]
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
2020-08-30 15:14:05 +00:00
const subscriptions = user . getSubscriptions ( )
2020-09-23 12:48:32 +00:00
const template = Array ( subscriptions . length ) . fill ( "?" ) . join ( ", " )
channels = db . prepare ( ` SELECT * FROM Channels WHERE ucid IN ( ${ template } ) ORDER BY name ` ) . all ( subscriptions )
// get refreshed status
refreshed = db . prepare ( ` SELECT min(refreshed) as min, max(refreshed) as max, count(refreshed) as count FROM Channels WHERE ucid IN ( ${ template } ) ` ) . get ( subscriptions )
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
2020-08-30 15:14:05 +00:00
if ( subscriptions . length ) {
hasSubscriptions = true
2020-09-23 11:45:02 +00:00
const template = Array ( subscriptions . length ) . fill ( "?" ) . join ( ", " )
videos = db . prepare ( ` SELECT * FROM Videos WHERE authorId IN ( ${ template } ) ORDER BY published DESC LIMIT 60 ` ) . all ( subscriptions )
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
2021-05-11 12:29:44 +00:00
return render ( 200 , "pug/subscriptions.pug" , { url , settings , hasSubscriptions , videos , channels , refreshed , timeToPastText , instanceOrigin } )
2020-08-30 15:14:05 +00:00
}
}
]