client.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. import cors from 'cors'
  2. import express from 'express'
  3. import {
  4. VideoChapterObject,
  5. VideoChaptersObject,
  6. VideoCommentObject,
  7. VideoPlaylistPrivacy,
  8. VideoPrivacy,
  9. VideoRateType
  10. } from '@peertube/peertube-models'
  11. import { activityPubCollectionPagination } from '@server/lib/activitypub/collection.js'
  12. import { getContextFilter } from '@server/lib/activitypub/context.js'
  13. import { getServerActor } from '@server/models/application/application.js'
  14. import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models/index.js'
  15. import { activityPubContextify } from '../../helpers/activity-pub-utils.js'
  16. import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants.js'
  17. import { audiencify, getAudience } from '../../lib/activitypub/audience.js'
  18. import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send/index.js'
  19. import { buildCreateActivity } from '../../lib/activitypub/send/send-create.js'
  20. import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike.js'
  21. import {
  22. getLocalVideoChaptersActivityPubUrl,
  23. getLocalVideoCommentsActivityPubUrl,
  24. getLocalVideoDislikesActivityPubUrl,
  25. getLocalVideoLikesActivityPubUrl,
  26. getLocalVideoSharesActivityPubUrl
  27. } from '../../lib/activitypub/url.js'
  28. import {
  29. apVideoChaptersSetCacheKey,
  30. buildAPVideoChaptersGroupsCache,
  31. cacheRoute,
  32. cacheRouteFactory
  33. } from '../../middlewares/cache/cache.js'
  34. import {
  35. activityPubRateLimiter,
  36. asyncMiddleware,
  37. ensureIsLocalChannel,
  38. executeIfActivityPub,
  39. localAccountValidator,
  40. videoChannelsNameWithHostValidator,
  41. videosCustomGetValidator,
  42. videosShareValidator
  43. } from '../../middlewares/index.js'
  44. import {
  45. getAccountVideoRateValidatorFactory,
  46. getVideoLocalViewerValidator,
  47. videoCommentGetValidator
  48. } from '../../middlewares/validators/index.js'
  49. import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy.js'
  50. import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists.js'
  51. import { AccountVideoRateModel } from '../../models/account/account-video-rate.js'
  52. import { AccountModel } from '../../models/account/account.js'
  53. import { ActorFollowModel } from '../../models/actor/actor-follow.js'
  54. import { VideoCommentModel } from '../../models/video/video-comment.js'
  55. import { VideoPlaylistModel } from '../../models/video/video-playlist.js'
  56. import { VideoShareModel } from '../../models/video/video-share.js'
  57. import { activityPubResponse } from './utils.js'
  58. import { VideoChapterModel } from '@server/models/video/video-chapter.js'
  59. import { InternalEventEmitter } from '@server/lib/internal-event-emitter.js'
  60. const activityPubClientRouter = express.Router()
  61. activityPubClientRouter.use(cors())
  62. // Intercept ActivityPub client requests
  63. activityPubClientRouter.get(
  64. [ '/accounts?/:name', '/accounts?/:name/video-channels', '/a/:name', '/a/:name/video-channels' ],
  65. executeIfActivityPub,
  66. activityPubRateLimiter,
  67. asyncMiddleware(localAccountValidator),
  68. asyncMiddleware(accountController)
  69. )
  70. activityPubClientRouter.get('/accounts?/:name/followers',
  71. executeIfActivityPub,
  72. activityPubRateLimiter,
  73. asyncMiddleware(localAccountValidator),
  74. asyncMiddleware(accountFollowersController)
  75. )
  76. activityPubClientRouter.get('/accounts?/:name/following',
  77. executeIfActivityPub,
  78. activityPubRateLimiter,
  79. asyncMiddleware(localAccountValidator),
  80. asyncMiddleware(accountFollowingController)
  81. )
  82. activityPubClientRouter.get('/accounts?/:name/playlists',
  83. executeIfActivityPub,
  84. activityPubRateLimiter,
  85. asyncMiddleware(localAccountValidator),
  86. asyncMiddleware(accountPlaylistsController)
  87. )
  88. activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
  89. executeIfActivityPub,
  90. activityPubRateLimiter,
  91. cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
  92. asyncMiddleware(getAccountVideoRateValidatorFactory('like')),
  93. asyncMiddleware(getAccountVideoRateFactory('like'))
  94. )
  95. activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
  96. executeIfActivityPub,
  97. activityPubRateLimiter,
  98. cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
  99. asyncMiddleware(getAccountVideoRateValidatorFactory('dislike')),
  100. asyncMiddleware(getAccountVideoRateFactory('dislike'))
  101. )
  102. activityPubClientRouter.get(
  103. [ '/videos/watch/:id', '/w/:id' ],
  104. executeIfActivityPub,
  105. activityPubRateLimiter,
  106. cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
  107. asyncMiddleware(videosCustomGetValidator('all')),
  108. asyncMiddleware(videoController)
  109. )
  110. activityPubClientRouter.get('/videos/watch/:id/activity',
  111. executeIfActivityPub,
  112. activityPubRateLimiter,
  113. asyncMiddleware(videosCustomGetValidator('all')),
  114. asyncMiddleware(videoController)
  115. )
  116. activityPubClientRouter.get('/videos/watch/:id/announces',
  117. executeIfActivityPub,
  118. activityPubRateLimiter,
  119. asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
  120. asyncMiddleware(videoAnnouncesController)
  121. )
  122. activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
  123. executeIfActivityPub,
  124. activityPubRateLimiter,
  125. asyncMiddleware(videosShareValidator),
  126. asyncMiddleware(videoAnnounceController)
  127. )
  128. activityPubClientRouter.get('/videos/watch/:id/likes',
  129. executeIfActivityPub,
  130. activityPubRateLimiter,
  131. asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
  132. asyncMiddleware(videoLikesController)
  133. )
  134. activityPubClientRouter.get('/videos/watch/:id/dislikes',
  135. executeIfActivityPub,
  136. activityPubRateLimiter,
  137. asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
  138. asyncMiddleware(videoDislikesController)
  139. )
  140. activityPubClientRouter.get('/videos/watch/:id/comments',
  141. executeIfActivityPub,
  142. activityPubRateLimiter,
  143. asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
  144. asyncMiddleware(videoCommentsController)
  145. )
  146. activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
  147. executeIfActivityPub,
  148. activityPubRateLimiter,
  149. asyncMiddleware(videoCommentGetValidator),
  150. asyncMiddleware(videoCommentController)
  151. )
  152. activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
  153. executeIfActivityPub,
  154. activityPubRateLimiter,
  155. asyncMiddleware(videoCommentGetValidator),
  156. asyncMiddleware(videoCommentController)
  157. )
  158. // ---------------------------------------------------------------------------
  159. const { middleware: chaptersCacheRouteMiddleware, instance: chaptersApiCache } = cacheRouteFactory()
  160. InternalEventEmitter.Instance.on('chapters-updated', ({ video }) => {
  161. if (video.remote) return
  162. chaptersApiCache.clearGroupSafe(buildAPVideoChaptersGroupsCache({ videoId: video.uuid }))
  163. })
  164. activityPubClientRouter.get('/videos/watch/:id/chapters',
  165. executeIfActivityPub,
  166. activityPubRateLimiter,
  167. apVideoChaptersSetCacheKey,
  168. chaptersCacheRouteMiddleware(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS),
  169. asyncMiddleware(videosCustomGetValidator('only-video')),
  170. asyncMiddleware(videoChaptersController)
  171. )
  172. // ---------------------------------------------------------------------------
  173. activityPubClientRouter.get(
  174. [ '/video-channels/:nameWithHost', '/video-channels/:nameWithHost/videos', '/c/:nameWithHost', '/c/:nameWithHost/videos' ],
  175. executeIfActivityPub,
  176. activityPubRateLimiter,
  177. asyncMiddleware(videoChannelsNameWithHostValidator),
  178. ensureIsLocalChannel,
  179. asyncMiddleware(videoChannelController)
  180. )
  181. activityPubClientRouter.get('/video-channels/:nameWithHost/followers',
  182. executeIfActivityPub,
  183. activityPubRateLimiter,
  184. asyncMiddleware(videoChannelsNameWithHostValidator),
  185. ensureIsLocalChannel,
  186. asyncMiddleware(videoChannelFollowersController)
  187. )
  188. activityPubClientRouter.get('/video-channels/:nameWithHost/following',
  189. executeIfActivityPub,
  190. activityPubRateLimiter,
  191. asyncMiddleware(videoChannelsNameWithHostValidator),
  192. ensureIsLocalChannel,
  193. asyncMiddleware(videoChannelFollowingController)
  194. )
  195. activityPubClientRouter.get('/video-channels/:nameWithHost/playlists',
  196. executeIfActivityPub,
  197. activityPubRateLimiter,
  198. asyncMiddleware(videoChannelsNameWithHostValidator),
  199. ensureIsLocalChannel,
  200. asyncMiddleware(videoChannelPlaylistsController)
  201. )
  202. activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
  203. executeIfActivityPub,
  204. activityPubRateLimiter,
  205. asyncMiddleware(videoFileRedundancyGetValidator),
  206. asyncMiddleware(videoRedundancyController)
  207. )
  208. activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
  209. executeIfActivityPub,
  210. activityPubRateLimiter,
  211. asyncMiddleware(videoPlaylistRedundancyGetValidator),
  212. asyncMiddleware(videoRedundancyController)
  213. )
  214. activityPubClientRouter.get(
  215. [ '/video-playlists/:playlistId', '/videos/watch/playlist/:playlistId', '/w/p/:playlistId' ],
  216. executeIfActivityPub,
  217. activityPubRateLimiter,
  218. asyncMiddleware(videoPlaylistsGetValidator('all')),
  219. asyncMiddleware(videoPlaylistController)
  220. )
  221. activityPubClientRouter.get('/video-playlists/:playlistId/videos/:playlistElementId',
  222. executeIfActivityPub,
  223. activityPubRateLimiter,
  224. asyncMiddleware(videoPlaylistElementAPGetValidator),
  225. asyncMiddleware(videoPlaylistElementController)
  226. )
  227. activityPubClientRouter.get('/videos/local-viewer/:localViewerId',
  228. executeIfActivityPub,
  229. activityPubRateLimiter,
  230. asyncMiddleware(getVideoLocalViewerValidator),
  231. asyncMiddleware(getVideoLocalViewerController)
  232. )
  233. // ---------------------------------------------------------------------------
  234. export {
  235. activityPubClientRouter
  236. }
  237. // ---------------------------------------------------------------------------
  238. async function accountController (req: express.Request, res: express.Response) {
  239. const account = res.locals.account
  240. return activityPubResponse(activityPubContextify(await account.toActivityPubObject(), 'Actor', getContextFilter()), res)
  241. }
  242. async function accountFollowersController (req: express.Request, res: express.Response) {
  243. const account = res.locals.account
  244. const activityPubResult = await actorFollowers(req, account.Actor)
  245. return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
  246. }
  247. async function accountFollowingController (req: express.Request, res: express.Response) {
  248. const account = res.locals.account
  249. const activityPubResult = await actorFollowing(req, account.Actor)
  250. return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
  251. }
  252. async function accountPlaylistsController (req: express.Request, res: express.Response) {
  253. const account = res.locals.account
  254. const activityPubResult = await actorPlaylists(req, { account })
  255. return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
  256. }
  257. async function videoChannelPlaylistsController (req: express.Request, res: express.Response) {
  258. const channel = res.locals.videoChannel
  259. const activityPubResult = await actorPlaylists(req, { channel })
  260. return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
  261. }
  262. function getAccountVideoRateFactory (rateType: VideoRateType) {
  263. return (req: express.Request, res: express.Response) => {
  264. const accountVideoRate = res.locals.accountVideoRate
  265. const byActor = accountVideoRate.Account.Actor
  266. const APObject = rateType === 'like'
  267. ? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
  268. : buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
  269. return activityPubResponse(activityPubContextify(APObject, 'Rate', getContextFilter()), res)
  270. }
  271. }
  272. async function videoController (req: express.Request, res: express.Response) {
  273. const video = res.locals.videoAll
  274. if (redirectIfNotOwned(video.url, res)) return
  275. // We need captions to render AP object
  276. const videoAP = await video.lightAPToFullAP(undefined)
  277. const audience = getAudience(videoAP.VideoChannel.Account.Actor, videoAP.privacy === VideoPrivacy.PUBLIC)
  278. const videoObject = audiencify(await videoAP.toActivityPubObject(), audience)
  279. if (req.path.endsWith('/activity')) {
  280. const data = buildCreateActivity(videoAP.url, video.VideoChannel.Account.Actor, videoObject, audience)
  281. return activityPubResponse(activityPubContextify(data, 'Video', getContextFilter()), res)
  282. }
  283. return activityPubResponse(activityPubContextify(videoObject, 'Video', getContextFilter()), res)
  284. }
  285. async function videoAnnounceController (req: express.Request, res: express.Response) {
  286. const share = res.locals.videoShare
  287. if (redirectIfNotOwned(share.url, res)) return
  288. const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)
  289. return activityPubResponse(activityPubContextify(activity, 'Announce', getContextFilter()), res)
  290. }
  291. async function videoAnnouncesController (req: express.Request, res: express.Response) {
  292. const video = res.locals.onlyImmutableVideo
  293. if (redirectIfNotOwned(video.url, res)) return
  294. const handler = async (start: number, count: number) => {
  295. const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
  296. return {
  297. total: result.total,
  298. data: result.data.map(r => r.url)
  299. }
  300. }
  301. const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)
  302. return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
  303. }
  304. async function videoLikesController (req: express.Request, res: express.Response) {
  305. const video = res.locals.onlyImmutableVideo
  306. if (redirectIfNotOwned(video.url, res)) return
  307. const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))
  308. return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
  309. }
  310. async function videoDislikesController (req: express.Request, res: express.Response) {
  311. const video = res.locals.onlyImmutableVideo
  312. if (redirectIfNotOwned(video.url, res)) return
  313. const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))
  314. return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
  315. }
  316. async function videoCommentsController (req: express.Request, res: express.Response) {
  317. const video = res.locals.onlyImmutableVideo
  318. if (redirectIfNotOwned(video.url, res)) return
  319. const handler = async (start: number, count: number) => {
  320. const result = await VideoCommentModel.listAndCountByVideoForAP({ video, start, count })
  321. return {
  322. total: result.total,
  323. data: result.data.map(r => r.url)
  324. }
  325. }
  326. const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)
  327. return activityPubResponse(activityPubContextify(json, 'Collection', getContextFilter()), res)
  328. }
  329. async function videoChannelController (req: express.Request, res: express.Response) {
  330. const videoChannel = res.locals.videoChannel
  331. return activityPubResponse(activityPubContextify(await videoChannel.toActivityPubObject(), 'Actor', getContextFilter()), res)
  332. }
  333. async function videoChannelFollowersController (req: express.Request, res: express.Response) {
  334. const videoChannel = res.locals.videoChannel
  335. const activityPubResult = await actorFollowers(req, videoChannel.Actor)
  336. return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
  337. }
  338. async function videoChannelFollowingController (req: express.Request, res: express.Response) {
  339. const videoChannel = res.locals.videoChannel
  340. const activityPubResult = await actorFollowing(req, videoChannel.Actor)
  341. return activityPubResponse(activityPubContextify(activityPubResult, 'Collection', getContextFilter()), res)
  342. }
  343. async function videoCommentController (req: express.Request, res: express.Response) {
  344. const videoComment = res.locals.videoCommentFull
  345. if (redirectIfNotOwned(videoComment.url, res)) return
  346. const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
  347. const isPublic = true // Comments are always public
  348. let videoCommentObject = videoComment.toActivityPubObject(threadParentComments)
  349. if (videoComment.Account) {
  350. const audience = getAudience(videoComment.Account.Actor, isPublic)
  351. videoCommentObject = audiencify(videoCommentObject, audience)
  352. if (req.path.endsWith('/activity')) {
  353. const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject as VideoCommentObject, audience)
  354. return activityPubResponse(activityPubContextify(data, 'Comment', getContextFilter()), res)
  355. }
  356. }
  357. return activityPubResponse(activityPubContextify(videoCommentObject, 'Comment', getContextFilter()), res)
  358. }
  359. async function videoChaptersController (req: express.Request, res: express.Response) {
  360. const video = res.locals.onlyVideo
  361. if (redirectIfNotOwned(video.url, res)) return
  362. const chapters = await VideoChapterModel.listChaptersOfVideo(video.id)
  363. const hasPart: VideoChapterObject[] = []
  364. if (chapters.length !== 0) {
  365. for (let i = 0; i < chapters.length - 1; i++) {
  366. hasPart.push(chapters[i].toActivityPubJSON({ video, nextChapter: chapters[i + 1] }))
  367. }
  368. hasPart.push(chapters[chapters.length - 1].toActivityPubJSON({ video: res.locals.onlyVideo, nextChapter: null }))
  369. }
  370. const chaptersObject: VideoChaptersObject = {
  371. id: getLocalVideoChaptersActivityPubUrl(video),
  372. hasPart
  373. }
  374. return activityPubResponse(activityPubContextify(chaptersObject, 'Chapters', getContextFilter()), res)
  375. }
  376. async function videoRedundancyController (req: express.Request, res: express.Response) {
  377. const videoRedundancy = res.locals.videoRedundancy
  378. if (redirectIfNotOwned(videoRedundancy.url, res)) return
  379. const serverActor = await getServerActor()
  380. const audience = getAudience(serverActor)
  381. const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
  382. if (req.path.endsWith('/activity')) {
  383. const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
  384. return activityPubResponse(activityPubContextify(data, 'CacheFile', getContextFilter()), res)
  385. }
  386. return activityPubResponse(activityPubContextify(object, 'CacheFile', getContextFilter()), res)
  387. }
  388. async function videoPlaylistController (req: express.Request, res: express.Response) {
  389. const playlist = res.locals.videoPlaylistFull
  390. if (redirectIfNotOwned(playlist.url, res)) return
  391. // We need more attributes
  392. playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
  393. const json = await playlist.toActivityPubObject(req.query.page, null)
  394. const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
  395. const object = audiencify(json, audience)
  396. return activityPubResponse(activityPubContextify(object, 'Playlist', getContextFilter()), res)
  397. }
  398. function videoPlaylistElementController (req: express.Request, res: express.Response) {
  399. const videoPlaylistElement = res.locals.videoPlaylistElementAP
  400. if (redirectIfNotOwned(videoPlaylistElement.url, res)) return
  401. const json = videoPlaylistElement.toActivityPubObject()
  402. return activityPubResponse(activityPubContextify(json, 'Playlist', getContextFilter()), res)
  403. }
  404. function getVideoLocalViewerController (req: express.Request, res: express.Response) {
  405. const localViewer = res.locals.localViewerFull
  406. return activityPubResponse(activityPubContextify(localViewer.toActivityPubObject(), 'WatchAction', getContextFilter()), res)
  407. }
  408. // ---------------------------------------------------------------------------
  409. function actorFollowing (req: express.Request, actor: MActorId) {
  410. const handler = (start: number, count: number) => {
  411. return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
  412. }
  413. return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
  414. }
  415. function actorFollowers (req: express.Request, actor: MActorId) {
  416. const handler = (start: number, count: number) => {
  417. return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
  418. }
  419. return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
  420. }
  421. function actorPlaylists (req: express.Request, options: { account: MAccountId } | { channel: MChannelId }) {
  422. const handler = (start: number, count: number) => {
  423. return VideoPlaylistModel.listPublicUrlsOfForAP(options, start, count)
  424. }
  425. return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
  426. }
  427. function videoRates (req: express.Request, rateType: VideoRateType, video: MVideoId, url: string) {
  428. const handler = async (start: number, count: number) => {
  429. const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
  430. return {
  431. total: result.total,
  432. data: result.data.map(r => r.url)
  433. }
  434. }
  435. return activityPubCollectionPagination(url, handler, req.query.page)
  436. }
  437. function redirectIfNotOwned (url: string, res: express.Response) {
  438. if (url.startsWith(WEBSERVER.URL) === false) {
  439. res.redirect(url)
  440. return true
  441. }
  442. return false
  443. }