mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
Sitemap: Hide empty accounts/channels and add video tags (#6633)
* feat(sitemap): remove empty accounts/channels closes #6607 * feat(sitemap): add more video tags https://developers.google.com/search/docs/crawling-indexing/sitemaps/video-sitemaps closes #6606 * Chunk videos SQL query * Optimize SQL query --------- Co-authored-by: Chocobozzz <me@florianbigard.com>
This commit is contained in:
parent
ef1732e5b9
commit
41c70a6b35
8 changed files with 138 additions and 51 deletions
|
@ -5,11 +5,11 @@ import { logger } from '@server/helpers/logger.js'
|
|||
import { getServerActor } from '@server/models/application/application.js'
|
||||
import { buildNSFWFilter } from '../helpers/express-utils.js'
|
||||
import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../initializers/constants.js'
|
||||
import { apiRateLimiter, asyncMiddleware } from '../middlewares/index.js'
|
||||
import { cacheRoute } from '../middlewares/cache/cache.js'
|
||||
import { apiRateLimiter, asyncMiddleware, cacheRoute } from '../middlewares/index.js'
|
||||
import { AccountModel } from '../models/account/account.js'
|
||||
import { VideoModel } from '../models/video/video.js'
|
||||
import { VideoChannelModel } from '../models/video/video-channel.js'
|
||||
import { VideoFileStream, VideoInclude } from '@peertube/peertube-models'
|
||||
|
||||
const sitemapRouter = express.Router()
|
||||
|
||||
|
@ -73,32 +73,64 @@ async function getSitemapAccountUrls () {
|
|||
async function getSitemapLocalVideoUrls () {
|
||||
const serverActor = await getServerActor()
|
||||
|
||||
const { data } = await VideoModel.listForApi({
|
||||
start: 0,
|
||||
count: undefined,
|
||||
sort: 'createdAt',
|
||||
displayOnlyForFollower: {
|
||||
actorId: serverActor.id,
|
||||
orLocalVideos: true
|
||||
},
|
||||
isLocal: true,
|
||||
nsfw: buildNSFWFilter(),
|
||||
countVideos: false
|
||||
})
|
||||
let acc: { url: string, video: any[] }[] = []
|
||||
|
||||
return data.map(v => ({
|
||||
url: WEBSERVER.URL + v.getWatchStaticPath(),
|
||||
video: [
|
||||
{
|
||||
// Sitemap title should be < 100 characters
|
||||
title: truncate(v.name, { length: 100, omission: '...' }),
|
||||
// Sitemap description should be < 2000 characters
|
||||
description: truncate(v.description || v.name, { length: 2000, omission: '...' }),
|
||||
player_loc: WEBSERVER.URL + v.getEmbedStaticPath(),
|
||||
thumbnail_loc: WEBSERVER.URL + v.getMiniatureStaticPath()
|
||||
}
|
||||
]
|
||||
}))
|
||||
const chunkSize = 200
|
||||
let hasData = true
|
||||
let i = 0
|
||||
|
||||
while (hasData && i < 1000) {
|
||||
const { data } = await VideoModel.listForApi({
|
||||
start: chunkSize * i,
|
||||
count: chunkSize,
|
||||
sort: 'createdAt',
|
||||
displayOnlyForFollower: {
|
||||
actorId: serverActor.id,
|
||||
orLocalVideos: true
|
||||
},
|
||||
isLocal: true,
|
||||
nsfw: buildNSFWFilter(),
|
||||
countVideos: false,
|
||||
include: VideoInclude.FILES | VideoInclude.TAGS
|
||||
})
|
||||
|
||||
hasData = data.length !== 0
|
||||
i++
|
||||
|
||||
acc = acc.concat(
|
||||
data.map(v => {
|
||||
const contentLoc = v.getHLSPlaylist()?.getMasterPlaylistUrl(v) ||
|
||||
v.getMaxQualityFile(VideoFileStream.VIDEO)?.getFileUrl(v) ||
|
||||
v.getMaxQualityFile(VideoFileStream.AUDIO)?.getFileUrl(v)
|
||||
|
||||
return {
|
||||
url: WEBSERVER.URL + v.getWatchStaticPath(),
|
||||
video: [
|
||||
{
|
||||
// Sitemap title should be < 100 characters
|
||||
'title': truncate(v.name, { length: 100, omission: '...' }),
|
||||
// Sitemap description should be < 2000 characters
|
||||
'description': truncate(v.description || v.name, { length: 2000, omission: '...' }),
|
||||
'player_loc': WEBSERVER.URL + v.getEmbedStaticPath(),
|
||||
'thumbnail_loc': WEBSERVER.URL + v.getMiniatureStaticPath(),
|
||||
'content_loc': contentLoc,
|
||||
'duration': v.duration,
|
||||
'view_count': v.views,
|
||||
'publication_date': v.publishedAt.toISOString(),
|
||||
'uploader': v.VideoChannel.getDisplayName(),
|
||||
'uploader:info': v.VideoChannel.getClientUrl(),
|
||||
'live': v.isLive ? 'YES' : 'NO',
|
||||
'family_friendly': v.nsfw ? 'NO' : 'YES',
|
||||
'rating': (v.likes * 5) / (v.likes + v.dislikes) || 0, // Rating is between 0.0 and 5.0
|
||||
'tag': v.Tags.map(t => t.name)
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
return acc
|
||||
}
|
||||
|
||||
function getSitemapBasicUrls () {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue