client.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // Intercept ActivityPub client requests
  2. import * as express from 'express'
  3. import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
  4. import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
  5. import { ROUTE_CACHE_LIFETIME, WEBSERVER } from '../../initializers/constants'
  6. import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
  7. import { audiencify, getAudience } from '../../lib/activitypub/audience'
  8. import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
  9. import {
  10. asyncMiddleware,
  11. executeIfActivityPub,
  12. localAccountValidator,
  13. localVideoChannelValidator,
  14. videosCustomGetValidator,
  15. videosShareValidator
  16. } from '../../middlewares'
  17. import { getAccountVideoRateValidator, videoCommentGetValidator } from '../../middlewares/validators'
  18. import { AccountModel } from '../../models/account/account'
  19. import { ActorModel } from '../../models/activitypub/actor'
  20. import { ActorFollowModel } from '../../models/activitypub/actor-follow'
  21. import { VideoModel } from '../../models/video/video'
  22. import { VideoCommentModel } from '../../models/video/video-comment'
  23. import { VideoShareModel } from '../../models/video/video-share'
  24. import { cacheRoute } from '../../middlewares/cache'
  25. import { activityPubResponse } from './utils'
  26. import { AccountVideoRateModel } from '../../models/account/account-video-rate'
  27. import {
  28. getRateUrl,
  29. getVideoCommentsActivityPubUrl,
  30. getVideoDislikesActivityPubUrl,
  31. getVideoLikesActivityPubUrl,
  32. getVideoSharesActivityPubUrl
  33. } from '../../lib/activitypub'
  34. import { VideoCaptionModel } from '../../models/video/video-caption'
  35. import { videoFileRedundancyGetValidator, videoPlaylistRedundancyGetValidator } from '../../middlewares/validators/redundancy'
  36. import { getServerActor } from '../../helpers/utils'
  37. import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
  38. import { videoPlaylistElementAPGetValidator, videoPlaylistsGetValidator } from '../../middlewares/validators/videos/video-playlists'
  39. import { VideoPlaylistModel } from '../../models/video/video-playlist'
  40. import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
  41. const activityPubClientRouter = express.Router()
  42. activityPubClientRouter.get('/accounts?/:name',
  43. executeIfActivityPub,
  44. asyncMiddleware(localAccountValidator),
  45. accountController
  46. )
  47. activityPubClientRouter.get('/accounts?/:name/followers',
  48. executeIfActivityPub,
  49. asyncMiddleware(localAccountValidator),
  50. asyncMiddleware(accountFollowersController)
  51. )
  52. activityPubClientRouter.get('/accounts?/:name/following',
  53. executeIfActivityPub,
  54. asyncMiddleware(localAccountValidator),
  55. asyncMiddleware(accountFollowingController)
  56. )
  57. activityPubClientRouter.get('/accounts?/:name/playlists',
  58. executeIfActivityPub,
  59. asyncMiddleware(localAccountValidator),
  60. asyncMiddleware(accountPlaylistsController)
  61. )
  62. activityPubClientRouter.get('/accounts?/:name/likes/:videoId',
  63. executeIfActivityPub,
  64. asyncMiddleware(getAccountVideoRateValidator('like')),
  65. getAccountVideoRate('like')
  66. )
  67. activityPubClientRouter.get('/accounts?/:name/dislikes/:videoId',
  68. executeIfActivityPub,
  69. asyncMiddleware(getAccountVideoRateValidator('dislike')),
  70. getAccountVideoRate('dislike')
  71. )
  72. activityPubClientRouter.get('/videos/watch/:id',
  73. executeIfActivityPub,
  74. asyncMiddleware(cacheRoute(ROUTE_CACHE_LIFETIME.ACTIVITY_PUB.VIDEOS)),
  75. asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
  76. asyncMiddleware(videoController)
  77. )
  78. activityPubClientRouter.get('/videos/watch/:id/activity',
  79. executeIfActivityPub,
  80. asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
  81. asyncMiddleware(videoController)
  82. )
  83. activityPubClientRouter.get('/videos/watch/:id/announces',
  84. executeIfActivityPub,
  85. asyncMiddleware(videosCustomGetValidator('only-video')),
  86. asyncMiddleware(videoAnnouncesController)
  87. )
  88. activityPubClientRouter.get('/videos/watch/:id/announces/:actorId',
  89. executeIfActivityPub,
  90. asyncMiddleware(videosShareValidator),
  91. asyncMiddleware(videoAnnounceController)
  92. )
  93. activityPubClientRouter.get('/videos/watch/:id/likes',
  94. executeIfActivityPub,
  95. asyncMiddleware(videosCustomGetValidator('only-video')),
  96. asyncMiddleware(videoLikesController)
  97. )
  98. activityPubClientRouter.get('/videos/watch/:id/dislikes',
  99. executeIfActivityPub,
  100. asyncMiddleware(videosCustomGetValidator('only-video')),
  101. asyncMiddleware(videoDislikesController)
  102. )
  103. activityPubClientRouter.get('/videos/watch/:id/comments',
  104. executeIfActivityPub,
  105. asyncMiddleware(videosCustomGetValidator('only-video')),
  106. asyncMiddleware(videoCommentsController)
  107. )
  108. activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId',
  109. executeIfActivityPub,
  110. asyncMiddleware(videoCommentGetValidator),
  111. asyncMiddleware(videoCommentController)
  112. )
  113. activityPubClientRouter.get('/videos/watch/:videoId/comments/:commentId/activity',
  114. executeIfActivityPub,
  115. asyncMiddleware(videoCommentGetValidator),
  116. asyncMiddleware(videoCommentController)
  117. )
  118. activityPubClientRouter.get('/video-channels/:name',
  119. executeIfActivityPub,
  120. asyncMiddleware(localVideoChannelValidator),
  121. asyncMiddleware(videoChannelController)
  122. )
  123. activityPubClientRouter.get('/video-channels/:name/followers',
  124. executeIfActivityPub,
  125. asyncMiddleware(localVideoChannelValidator),
  126. asyncMiddleware(videoChannelFollowersController)
  127. )
  128. activityPubClientRouter.get('/video-channels/:name/following',
  129. executeIfActivityPub,
  130. asyncMiddleware(localVideoChannelValidator),
  131. asyncMiddleware(videoChannelFollowingController)
  132. )
  133. activityPubClientRouter.get('/redundancy/videos/:videoId/:resolution([0-9]+)(-:fps([0-9]+))?',
  134. executeIfActivityPub,
  135. asyncMiddleware(videoFileRedundancyGetValidator),
  136. asyncMiddleware(videoRedundancyController)
  137. )
  138. activityPubClientRouter.get('/redundancy/streaming-playlists/:streamingPlaylistType/:videoId',
  139. executeIfActivityPub,
  140. asyncMiddleware(videoPlaylistRedundancyGetValidator),
  141. asyncMiddleware(videoRedundancyController)
  142. )
  143. activityPubClientRouter.get('/video-playlists/:playlistId',
  144. executeIfActivityPub,
  145. asyncMiddleware(videoPlaylistsGetValidator),
  146. asyncMiddleware(videoPlaylistController)
  147. )
  148. activityPubClientRouter.get('/video-playlists/:playlistId/:videoId',
  149. executeIfActivityPub,
  150. asyncMiddleware(videoPlaylistElementAPGetValidator),
  151. asyncMiddleware(videoPlaylistElementController)
  152. )
  153. // ---------------------------------------------------------------------------
  154. export {
  155. activityPubClientRouter
  156. }
  157. // ---------------------------------------------------------------------------
  158. function accountController (req: express.Request, res: express.Response) {
  159. const account = res.locals.account
  160. return activityPubResponse(activityPubContextify(account.toActivityPubObject()), res)
  161. }
  162. async function accountFollowersController (req: express.Request, res: express.Response) {
  163. const account = res.locals.account
  164. const activityPubResult = await actorFollowers(req, account.Actor)
  165. return activityPubResponse(activityPubContextify(activityPubResult), res)
  166. }
  167. async function accountFollowingController (req: express.Request, res: express.Response) {
  168. const account = res.locals.account
  169. const activityPubResult = await actorFollowing(req, account.Actor)
  170. return activityPubResponse(activityPubContextify(activityPubResult), res)
  171. }
  172. async function accountPlaylistsController (req: express.Request, res: express.Response) {
  173. const account = res.locals.account
  174. const activityPubResult = await actorPlaylists(req, account)
  175. return activityPubResponse(activityPubContextify(activityPubResult), res)
  176. }
  177. function getAccountVideoRate (rateType: VideoRateType) {
  178. return (req: express.Request, res: express.Response) => {
  179. const accountVideoRate = res.locals.accountVideoRate
  180. const byActor = accountVideoRate.Account.Actor
  181. const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
  182. const APObject = rateType === 'like'
  183. ? buildLikeActivity(url, byActor, accountVideoRate.Video)
  184. : buildDislikeActivity(url, byActor, accountVideoRate.Video)
  185. return activityPubResponse(activityPubContextify(APObject), res)
  186. }
  187. }
  188. async function videoController (req: express.Request, res: express.Response) {
  189. // We need more attributes
  190. const video = await VideoModel.loadForGetAPI(res.locals.video.id)
  191. if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
  192. // We need captions to render AP object
  193. video.VideoCaptions = await VideoCaptionModel.listVideoCaptions(video.id)
  194. const audience = getAudience(video.VideoChannel.Account.Actor, video.privacy === VideoPrivacy.PUBLIC)
  195. const videoObject = audiencify(video.toActivityPubObject(), audience)
  196. if (req.path.endsWith('/activity')) {
  197. const data = buildCreateActivity(video.url, video.VideoChannel.Account.Actor, videoObject, audience)
  198. return activityPubResponse(activityPubContextify(data), res)
  199. }
  200. return activityPubResponse(activityPubContextify(videoObject), res)
  201. }
  202. async function videoAnnounceController (req: express.Request, res: express.Response) {
  203. const share = res.locals.videoShare
  204. if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
  205. const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.video, undefined)
  206. return activityPubResponse(activityPubContextify(activity), res)
  207. }
  208. async function videoAnnouncesController (req: express.Request, res: express.Response) {
  209. const video = res.locals.video
  210. const handler = async (start: number, count: number) => {
  211. const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
  212. return {
  213. total: result.count,
  214. data: result.rows.map(r => r.url)
  215. }
  216. }
  217. const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
  218. return activityPubResponse(activityPubContextify(json), res)
  219. }
  220. async function videoLikesController (req: express.Request, res: express.Response) {
  221. const video = res.locals.video
  222. const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))
  223. return activityPubResponse(activityPubContextify(json), res)
  224. }
  225. async function videoDislikesController (req: express.Request, res: express.Response) {
  226. const video = res.locals.video
  227. const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))
  228. return activityPubResponse(activityPubContextify(json), res)
  229. }
  230. async function videoCommentsController (req: express.Request, res: express.Response) {
  231. const video = res.locals.video
  232. const handler = async (start: number, count: number) => {
  233. const result = await VideoCommentModel.listAndCountByVideoId(video.id, start, count)
  234. return {
  235. total: result.count,
  236. data: result.rows.map(r => r.url)
  237. }
  238. }
  239. const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
  240. return activityPubResponse(activityPubContextify(json), res)
  241. }
  242. async function videoChannelController (req: express.Request, res: express.Response) {
  243. const videoChannel = res.locals.videoChannel
  244. return activityPubResponse(activityPubContextify(videoChannel.toActivityPubObject()), res)
  245. }
  246. async function videoChannelFollowersController (req: express.Request, res: express.Response) {
  247. const videoChannel = res.locals.videoChannel
  248. const activityPubResult = await actorFollowers(req, videoChannel.Actor)
  249. return activityPubResponse(activityPubContextify(activityPubResult), res)
  250. }
  251. async function videoChannelFollowingController (req: express.Request, res: express.Response) {
  252. const videoChannel = res.locals.videoChannel
  253. const activityPubResult = await actorFollowing(req, videoChannel.Actor)
  254. return activityPubResponse(activityPubContextify(activityPubResult), res)
  255. }
  256. async function videoCommentController (req: express.Request, res: express.Response) {
  257. const videoComment = res.locals.videoComment
  258. if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
  259. const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
  260. const isPublic = true // Comments are always public
  261. const audience = getAudience(videoComment.Account.Actor, isPublic)
  262. const videoCommentObject = audiencify(videoComment.toActivityPubObject(threadParentComments), audience)
  263. if (req.path.endsWith('/activity')) {
  264. const data = buildCreateActivity(videoComment.url, videoComment.Account.Actor, videoCommentObject, audience)
  265. return activityPubResponse(activityPubContextify(data), res)
  266. }
  267. return activityPubResponse(activityPubContextify(videoCommentObject), res)
  268. }
  269. async function videoRedundancyController (req: express.Request, res: express.Response) {
  270. const videoRedundancy = res.locals.videoRedundancy
  271. if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)
  272. const serverActor = await getServerActor()
  273. const audience = getAudience(serverActor)
  274. const object = audiencify(videoRedundancy.toActivityPubObject(), audience)
  275. if (req.path.endsWith('/activity')) {
  276. const data = buildCreateActivity(videoRedundancy.url, serverActor, object, audience)
  277. return activityPubResponse(activityPubContextify(data), res)
  278. }
  279. return activityPubResponse(activityPubContextify(object), res)
  280. }
  281. async function videoPlaylistController (req: express.Request, res: express.Response) {
  282. const playlist = res.locals.videoPlaylist
  283. // We need more attributes
  284. playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)
  285. const json = await playlist.toActivityPubObject(req.query.page, null)
  286. const audience = getAudience(playlist.OwnerAccount.Actor, playlist.privacy === VideoPlaylistPrivacy.PUBLIC)
  287. const object = audiencify(json, audience)
  288. return activityPubResponse(activityPubContextify(object), res)
  289. }
  290. async function videoPlaylistElementController (req: express.Request, res: express.Response) {
  291. const videoPlaylistElement = res.locals.videoPlaylistElement
  292. const json = videoPlaylistElement.toActivityPubObject()
  293. return activityPubResponse(activityPubContextify(json), res)
  294. }
  295. // ---------------------------------------------------------------------------
  296. async function actorFollowing (req: express.Request, actor: ActorModel) {
  297. const handler = (start: number, count: number) => {
  298. return ActorFollowModel.listAcceptedFollowingUrlsForApi([ actor.id ], undefined, start, count)
  299. }
  300. return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
  301. }
  302. async function actorFollowers (req: express.Request, actor: ActorModel) {
  303. const handler = (start: number, count: number) => {
  304. return ActorFollowModel.listAcceptedFollowerUrlsForAP([ actor.id ], undefined, start, count)
  305. }
  306. return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
  307. }
  308. async function actorPlaylists (req: express.Request, account: AccountModel) {
  309. const handler = (start: number, count: number) => {
  310. return VideoPlaylistModel.listPublicUrlsOfForAP(account.id, start, count)
  311. }
  312. return activityPubCollectionPagination(WEBSERVER.URL + req.path, handler, req.query.page)
  313. }
  314. function videoRates (req: express.Request, rateType: VideoRateType, video: VideoModel, url: string) {
  315. const handler = async (start: number, count: number) => {
  316. const result = await AccountVideoRateModel.listAndCountAccountUrlsByVideoId(rateType, video.id, start, count)
  317. return {
  318. total: result.count,
  319. data: result.rows.map(r => r.url)
  320. }
  321. }
  322. return activityPubCollectionPagination(url, handler, req.query.page)
  323. }