]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/url.ts
feature/ability to disable video history by default (#5728)
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / url.ts
index d98561e3328874119c325a5ccb26bcc04885a624..5cdac71bf0248053c46ab78d4e4ca92716837bc8 100644 (file)
-import { CONFIG } from '../../initializers/constants'
-import { VideoInstance } from '../../models/video/video-interface'
-import { VideoChannelInstance } from '../../models/video/video-channel-interface'
-import { VideoAbuseInstance } from '../../models/video/video-abuse-interface'
-import { AccountFollowInstance } from '../../models/account/account-follow-interface'
-import { AccountInstance } from '../../models/account/account-interface'
+import { REMOTE_SCHEME, WEBSERVER } from '../../initializers/constants'
+import {
+  MAbuseFull,
+  MAbuseId,
+  MActor,
+  MActorFollow,
+  MActorId,
+  MActorUrl,
+  MCommentId,
+  MLocalVideoViewer,
+  MVideoId,
+  MVideoPlaylistElement,
+  MVideoUrl,
+  MVideoUUID,
+  MVideoWithHost
+} from '../../types/models'
+import { MVideoFileVideoUUID } from '../../types/models/video/video-file'
+import { MVideoPlaylist, MVideoPlaylistUUID } from '../../types/models/video/video-playlist'
+import { MStreamingPlaylist } from '../../types/models/video/video-streaming-playlist'
 
-function getVideoActivityPubUrl (video: VideoInstance) {
-  return CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
+function getLocalVideoActivityPubUrl (video: MVideoUUID) {
+  return WEBSERVER.URL + '/videos/watch/' + video.uuid
 }
 
-function getVideoChannelActivityPubUrl (videoChannel: VideoChannelInstance) {
-  return CONFIG.WEBSERVER.URL + '/video-channels/' + videoChannel.uuid
+function getLocalVideoPlaylistActivityPubUrl (videoPlaylist: MVideoPlaylist) {
+  return WEBSERVER.URL + '/video-playlists/' + videoPlaylist.uuid
 }
 
-function getAccountActivityPubUrl (accountName: string) {
-  return CONFIG.WEBSERVER.URL + '/account/' + accountName
+function getLocalVideoPlaylistElementActivityPubUrl (videoPlaylist: MVideoPlaylistUUID, videoPlaylistElement: MVideoPlaylistElement) {
+  return WEBSERVER.URL + '/video-playlists/' + videoPlaylist.uuid + '/videos/' + videoPlaylistElement.id
 }
 
-function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseInstance) {
-  return CONFIG.WEBSERVER.URL + '/admin/video-abuses/' + videoAbuse.id
+function getLocalVideoCacheFileActivityPubUrl (videoFile: MVideoFileVideoUUID) {
+  const suffixFPS = videoFile.fps && videoFile.fps !== -1 ? '-' + videoFile.fps : ''
+
+  return `${WEBSERVER.URL}/redundancy/videos/${videoFile.Video.uuid}/${videoFile.resolution}${suffixFPS}`
+}
+
+function getLocalVideoCacheStreamingPlaylistActivityPubUrl (video: MVideoUUID, playlist: MStreamingPlaylist) {
+  return `${WEBSERVER.URL}/redundancy/streaming-playlists/${playlist.getStringType()}/${video.uuid}`
 }
 
-function getVideoViewActivityPubUrl (byAccount: AccountInstance, video: VideoInstance) {
-  return video.url + '#views/' + byAccount.uuid + '/' + new Date().toISOString()
+function getLocalVideoCommentActivityPubUrl (video: MVideoUUID, videoComment: MCommentId) {
+  return WEBSERVER.URL + '/videos/watch/' + video.uuid + '/comments/' + videoComment.id
 }
 
-function getAccountFollowActivityPubUrl (accountFollow: AccountFollowInstance) {
-  const me = accountFollow.AccountFollower
-  const following = accountFollow.AccountFollowing
+function getLocalVideoChannelActivityPubUrl (videoChannelName: string) {
+  return WEBSERVER.URL + '/video-channels/' + videoChannelName
+}
+
+function getLocalAccountActivityPubUrl (accountName: string) {
+  return WEBSERVER.URL + '/accounts/' + accountName
+}
 
-  return me.url + '#follows/' + following.id
+function getLocalAbuseActivityPubUrl (abuse: MAbuseId) {
+  return WEBSERVER.URL + '/admin/abuses/' + abuse.id
 }
 
-function getAccountFollowAcceptActivityPubUrl (accountFollow: AccountFollowInstance) {
-  const follower = accountFollow.AccountFollower
-  const me = accountFollow.AccountFollowing
+function getLocalVideoViewActivityPubUrl (byActor: MActorUrl, video: MVideoId, viewerIdentifier: string) {
+  return byActor.url + '/views/videos/' + video.id + '/' + viewerIdentifier
+}
+
+function getLocalVideoViewerActivityPubUrl (stats: MLocalVideoViewer) {
+  return WEBSERVER.URL + '/videos/local-viewer/' + stats.uuid
+}
+
+function getVideoLikeActivityPubUrlByLocalActor (byActor: MActorUrl, video: MVideoId) {
+  return byActor.url + '/likes/' + video.id
+}
+
+function getVideoDislikeActivityPubUrlByLocalActor (byActor: MActorUrl, video: MVideoId) {
+  return byActor.url + '/dislikes/' + video.id
+}
+
+function getLocalVideoSharesActivityPubUrl (video: MVideoUrl) {
+  return video.url + '/announces'
+}
+
+function getLocalVideoCommentsActivityPubUrl (video: MVideoUrl) {
+  return video.url + '/comments'
+}
+
+function getLocalVideoLikesActivityPubUrl (video: MVideoUrl) {
+  return video.url + '/likes'
+}
+
+function getLocalVideoDislikesActivityPubUrl (video: MVideoUrl) {
+  return video.url + '/dislikes'
+}
 
-  return follower.url + '#accepts/follows/' + me.id
+function getLocalActorFollowActivityPubUrl (follower: MActor, following: MActorId) {
+  return follower.url + '/follows/' + following.id
 }
 
-function getAnnounceActivityPubUrl (originalUrl: string, byAccount: AccountInstance) {
-  return originalUrl + '#announces/' + byAccount.id
+function getLocalActorFollowAcceptActivityPubUrl (actorFollow: MActorFollow) {
+  return WEBSERVER.URL + '/accepts/follows/' + actorFollow.id
+}
+
+function getLocalActorFollowRejectActivityPubUrl () {
+  return WEBSERVER.URL + '/rejects/follows/' + new Date().toISOString()
+}
+
+function getLocalVideoAnnounceActivityPubUrl (byActor: MActorId, video: MVideoUrl) {
+  return video.url + '/announces/' + byActor.id
+}
+
+function getDeleteActivityPubUrl (originalUrl: string) {
+  return originalUrl + '/delete'
 }
 
 function getUpdateActivityPubUrl (originalUrl: string, updatedAt: string) {
-  return originalUrl + '#updates/' + updatedAt
+  return originalUrl + '/updates/' + updatedAt
 }
 
 function getUndoActivityPubUrl (originalUrl: string) {
   return originalUrl + '/undo'
 }
 
+// ---------------------------------------------------------------------------
+
+function getAbuseTargetUrl (abuse: MAbuseFull) {
+  return abuse.VideoAbuse?.Video?.url ||
+    abuse.VideoCommentAbuse?.VideoComment?.url ||
+    abuse.FlaggedAccount.Actor.url
+}
+
+// ---------------------------------------------------------------------------
+
+function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) {
+  if (!scheme) scheme = REMOTE_SCHEME.HTTP
+
+  const host = video.VideoChannel.Actor.Server.host
+
+  return scheme + '://' + host + path
+}
+
+// ---------------------------------------------------------------------------
+
+function checkUrlsSameHost (url1: string, url2: string) {
+  const idHost = new URL(url1).host
+  const actorHost = new URL(url2).host
+
+  return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
+}
+
+// ---------------------------------------------------------------------------
+
 export {
-  getVideoActivityPubUrl,
-  getVideoChannelActivityPubUrl,
-  getAccountActivityPubUrl,
-  getVideoAbuseActivityPubUrl,
-  getAccountFollowActivityPubUrl,
-  getAccountFollowAcceptActivityPubUrl,
-  getAnnounceActivityPubUrl,
+  getLocalVideoActivityPubUrl,
+  getLocalVideoPlaylistActivityPubUrl,
+  getLocalVideoPlaylistElementActivityPubUrl,
+  getLocalVideoCacheFileActivityPubUrl,
+  getLocalVideoCacheStreamingPlaylistActivityPubUrl,
+  getLocalVideoCommentActivityPubUrl,
+  getLocalVideoChannelActivityPubUrl,
+  getLocalAccountActivityPubUrl,
+  getLocalAbuseActivityPubUrl,
+  getLocalActorFollowActivityPubUrl,
+  getLocalActorFollowAcceptActivityPubUrl,
+  getLocalVideoAnnounceActivityPubUrl,
   getUpdateActivityPubUrl,
   getUndoActivityPubUrl,
-  getVideoViewActivityPubUrl
+  getVideoLikeActivityPubUrlByLocalActor,
+  getLocalVideoViewActivityPubUrl,
+  getVideoDislikeActivityPubUrlByLocalActor,
+  getLocalActorFollowRejectActivityPubUrl,
+  getDeleteActivityPubUrl,
+  getLocalVideoSharesActivityPubUrl,
+  getLocalVideoCommentsActivityPubUrl,
+  getLocalVideoLikesActivityPubUrl,
+  getLocalVideoDislikesActivityPubUrl,
+  getLocalVideoViewerActivityPubUrl,
+
+  getAbuseTargetUrl,
+  checkUrlsSameHost,
+  buildRemoteVideoBaseUrl
 }