X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Furl.ts;h=5cdac71bf0248053c46ab78d4e4ca92716837bc8;hb=HEAD;hp=bcb7a4ee2f59404715218ef30bc9066a23e1fd70;hpb=74dc3bca2b14f5fd3fe80c394dfc34177a46db77;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/url.ts b/server/lib/activitypub/url.ts index bcb7a4ee2..5cdac71bf 100644 --- a/server/lib/activitypub/url.ts +++ b/server/lib/activitypub/url.ts @@ -1,95 +1,106 @@ -import { WEBSERVER } from '../../initializers/constants' -import { ActorModel } from '../../models/activitypub/actor' -import { ActorFollowModel } from '../../models/activitypub/actor-follow' -import { VideoModel } from '../../models/video/video' -import { VideoAbuseModel } from '../../models/video/video-abuse' -import { VideoCommentModel } from '../../models/video/video-comment' -import { VideoFileModel } from '../../models/video/video-file' -import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist' -import { VideoPlaylistModel } from '../../models/video/video-playlist' - -function getVideoActivityPubUrl (video: VideoModel) { +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 getLocalVideoActivityPubUrl (video: MVideoUUID) { return WEBSERVER.URL + '/videos/watch/' + video.uuid } -function getVideoPlaylistActivityPubUrl (videoPlaylist: VideoPlaylistModel) { +function getLocalVideoPlaylistActivityPubUrl (videoPlaylist: MVideoPlaylist) { return WEBSERVER.URL + '/video-playlists/' + videoPlaylist.uuid } -function getVideoPlaylistElementActivityPubUrl (videoPlaylist: VideoPlaylistModel, video: VideoModel) { - return WEBSERVER.URL + '/video-playlists/' + videoPlaylist.uuid + '/' + video.uuid +function getLocalVideoPlaylistElementActivityPubUrl (videoPlaylist: MVideoPlaylistUUID, videoPlaylistElement: MVideoPlaylistElement) { + return WEBSERVER.URL + '/video-playlists/' + videoPlaylist.uuid + '/videos/' + videoPlaylistElement.id } -function getVideoCacheFileActivityPubUrl (videoFile: VideoFileModel) { +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 getVideoCacheStreamingPlaylistActivityPubUrl (video: VideoModel, playlist: VideoStreamingPlaylistModel) { +function getLocalVideoCacheStreamingPlaylistActivityPubUrl (video: MVideoUUID, playlist: MStreamingPlaylist) { return `${WEBSERVER.URL}/redundancy/streaming-playlists/${playlist.getStringType()}/${video.uuid}` } -function getVideoCommentActivityPubUrl (video: VideoModel, videoComment: VideoCommentModel) { +function getLocalVideoCommentActivityPubUrl (video: MVideoUUID, videoComment: MCommentId) { return WEBSERVER.URL + '/videos/watch/' + video.uuid + '/comments/' + videoComment.id } -function getVideoChannelActivityPubUrl (videoChannelName: string) { +function getLocalVideoChannelActivityPubUrl (videoChannelName: string) { return WEBSERVER.URL + '/video-channels/' + videoChannelName } -function getAccountActivityPubUrl (accountName: string) { +function getLocalAccountActivityPubUrl (accountName: string) { return WEBSERVER.URL + '/accounts/' + accountName } -function getVideoAbuseActivityPubUrl (videoAbuse: VideoAbuseModel) { - return WEBSERVER.URL + '/admin/video-abuses/' + videoAbuse.id +function getLocalAbuseActivityPubUrl (abuse: MAbuseId) { + return WEBSERVER.URL + '/admin/abuses/' + abuse.id } -function getVideoViewActivityPubUrl (byActor: ActorModel, video: VideoModel) { - return byActor.url + '/views/videos/' + video.id + '/' + new Date().toISOString() +function getLocalVideoViewActivityPubUrl (byActor: MActorUrl, video: MVideoId, viewerIdentifier: string) { + return byActor.url + '/views/videos/' + video.id + '/' + viewerIdentifier } -function getVideoLikeActivityPubUrl (byActor: ActorModel, video: VideoModel | { id: number }) { +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 getVideoDislikeActivityPubUrl (byActor: ActorModel, video: VideoModel | { id: number }) { +function getVideoDislikeActivityPubUrlByLocalActor (byActor: MActorUrl, video: MVideoId) { return byActor.url + '/dislikes/' + video.id } -function getVideoSharesActivityPubUrl (video: VideoModel) { +function getLocalVideoSharesActivityPubUrl (video: MVideoUrl) { return video.url + '/announces' } -function getVideoCommentsActivityPubUrl (video: VideoModel) { +function getLocalVideoCommentsActivityPubUrl (video: MVideoUrl) { return video.url + '/comments' } -function getVideoLikesActivityPubUrl (video: VideoModel) { +function getLocalVideoLikesActivityPubUrl (video: MVideoUrl) { return video.url + '/likes' } -function getVideoDislikesActivityPubUrl (video: VideoModel) { +function getLocalVideoDislikesActivityPubUrl (video: MVideoUrl) { return video.url + '/dislikes' } -function getActorFollowActivityPubUrl (follower: ActorModel, following: ActorModel) { +function getLocalActorFollowActivityPubUrl (follower: MActor, following: MActorId) { return follower.url + '/follows/' + following.id } -function getActorFollowAcceptActivityPubUrl (actorFollow: ActorFollowModel) { - const follower = actorFollow.ActorFollower - const me = actorFollow.ActorFollowing - - return follower.url + '/accepts/follows/' + me.id +function getLocalActorFollowAcceptActivityPubUrl (actorFollow: MActorFollow) { + return WEBSERVER.URL + '/accepts/follows/' + actorFollow.id } -function getActorFollowRejectActivityPubUrl (follower: ActorModel, following: ActorModel) { - return follower.url + '/rejects/follows/' + following.id +function getLocalActorFollowRejectActivityPubUrl () { + return WEBSERVER.URL + '/rejects/follows/' + new Date().toISOString() } -function getVideoAnnounceActivityPubUrl (byActor: ActorModel, video: VideoModel) { +function getLocalVideoAnnounceActivityPubUrl (byActor: MActorId, video: MVideoUrl) { return video.url + '/announces/' + byActor.id } @@ -105,28 +116,62 @@ 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, - getVideoPlaylistElementActivityPubUrl, - getVideoPlaylistActivityPubUrl, - getVideoCacheStreamingPlaylistActivityPubUrl, - getVideoChannelActivityPubUrl, - getAccountActivityPubUrl, - getVideoAbuseActivityPubUrl, - getActorFollowActivityPubUrl, - getActorFollowAcceptActivityPubUrl, - getVideoAnnounceActivityPubUrl, + getLocalVideoActivityPubUrl, + getLocalVideoPlaylistActivityPubUrl, + getLocalVideoPlaylistElementActivityPubUrl, + getLocalVideoCacheFileActivityPubUrl, + getLocalVideoCacheStreamingPlaylistActivityPubUrl, + getLocalVideoCommentActivityPubUrl, + getLocalVideoChannelActivityPubUrl, + getLocalAccountActivityPubUrl, + getLocalAbuseActivityPubUrl, + getLocalActorFollowActivityPubUrl, + getLocalActorFollowAcceptActivityPubUrl, + getLocalVideoAnnounceActivityPubUrl, getUpdateActivityPubUrl, getUndoActivityPubUrl, - getVideoViewActivityPubUrl, - getVideoLikeActivityPubUrl, - getVideoDislikeActivityPubUrl, - getActorFollowRejectActivityPubUrl, - getVideoCommentActivityPubUrl, + getVideoLikeActivityPubUrlByLocalActor, + getLocalVideoViewActivityPubUrl, + getVideoDislikeActivityPubUrlByLocalActor, + getLocalActorFollowRejectActivityPubUrl, getDeleteActivityPubUrl, - getVideoSharesActivityPubUrl, - getVideoCommentsActivityPubUrl, - getVideoLikesActivityPubUrl, - getVideoDislikesActivityPubUrl, - getVideoCacheFileActivityPubUrl + getLocalVideoSharesActivityPubUrl, + getLocalVideoCommentsActivityPubUrl, + getLocalVideoLikesActivityPubUrl, + getLocalVideoDislikesActivityPubUrl, + getLocalVideoViewerActivityPubUrl, + + getAbuseTargetUrl, + checkUrlsSameHost, + buildRemoteVideoBaseUrl }