]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/url.ts
50be4fac9772716b7ff68252ba33f55113382794
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / url.ts
1 import { REMOTE_SCHEME, WEBSERVER } from '../../initializers/constants'
2 import {
3 MAbuseFull,
4 MAbuseId,
5 MActor,
6 MActorFollowActors,
7 MActorId,
8 MActorUrl,
9 MCommentId,
10 MVideoId,
11 MVideoPlaylistElement,
12 MVideoUrl,
13 MVideoUUID,
14 MVideoWithHost
15 } from '../../types/models'
16 import { MVideoFileVideoUUID } from '../../types/models/video/video-file'
17 import { MVideoPlaylist, MVideoPlaylistUUID } from '../../types/models/video/video-playlist'
18 import { MStreamingPlaylist } from '../../types/models/video/video-streaming-playlist'
19
20 function getLocalVideoActivityPubUrl (video: MVideoUUID) {
21 return WEBSERVER.URL + '/videos/watch/' + video.uuid
22 }
23
24 function getLocalVideoPlaylistActivityPubUrl (videoPlaylist: MVideoPlaylist) {
25 return WEBSERVER.URL + '/video-playlists/' + videoPlaylist.uuid
26 }
27
28 function getLocalVideoPlaylistElementActivityPubUrl (videoPlaylist: MVideoPlaylistUUID, videoPlaylistElement: MVideoPlaylistElement) {
29 return WEBSERVER.URL + '/video-playlists/' + videoPlaylist.uuid + '/videos/' + videoPlaylistElement.id
30 }
31
32 function getLocalVideoCacheFileActivityPubUrl (videoFile: MVideoFileVideoUUID) {
33 const suffixFPS = videoFile.fps && videoFile.fps !== -1 ? '-' + videoFile.fps : ''
34
35 return `${WEBSERVER.URL}/redundancy/videos/${videoFile.Video.uuid}/${videoFile.resolution}${suffixFPS}`
36 }
37
38 function getLocalVideoCacheStreamingPlaylistActivityPubUrl (video: MVideoUUID, playlist: MStreamingPlaylist) {
39 return `${WEBSERVER.URL}/redundancy/streaming-playlists/${playlist.getStringType()}/${video.uuid}`
40 }
41
42 function getLocalVideoCommentActivityPubUrl (video: MVideoUUID, videoComment: MCommentId) {
43 return WEBSERVER.URL + '/videos/watch/' + video.uuid + '/comments/' + videoComment.id
44 }
45
46 function getLocalVideoChannelActivityPubUrl (videoChannelName: string) {
47 return WEBSERVER.URL + '/video-channels/' + videoChannelName
48 }
49
50 function getLocalAccountActivityPubUrl (accountName: string) {
51 return WEBSERVER.URL + '/accounts/' + accountName
52 }
53
54 function getLocalAbuseActivityPubUrl (abuse: MAbuseId) {
55 return WEBSERVER.URL + '/admin/abuses/' + abuse.id
56 }
57
58 function getLocalVideoViewActivityPubUrl (byActor: MActorUrl, video: MVideoId) {
59 return byActor.url + '/views/videos/' + video.id + '/' + new Date().toISOString()
60 }
61
62 function getVideoLikeActivityPubUrlByLocalActor (byActor: MActorUrl, video: MVideoId) {
63 return byActor.url + '/likes/' + video.id
64 }
65
66 function getVideoDislikeActivityPubUrlByLocalActor (byActor: MActorUrl, video: MVideoId) {
67 return byActor.url + '/dislikes/' + video.id
68 }
69
70 function getLocalVideoSharesActivityPubUrl (video: MVideoUrl) {
71 return video.url + '/announces'
72 }
73
74 function getLocalVideoCommentsActivityPubUrl (video: MVideoUrl) {
75 return video.url + '/comments'
76 }
77
78 function getLocalVideoLikesActivityPubUrl (video: MVideoUrl) {
79 return video.url + '/likes'
80 }
81
82 function getLocalVideoDislikesActivityPubUrl (video: MVideoUrl) {
83 return video.url + '/dislikes'
84 }
85
86 function getLocalActorFollowActivityPubUrl (follower: MActor, following: MActorId) {
87 return follower.url + '/follows/' + following.id
88 }
89
90 function getLocalActorFollowAcceptActivityPubUrl (actorFollow: MActorFollowActors) {
91 const follower = actorFollow.ActorFollower
92 const me = actorFollow.ActorFollowing
93
94 return WEBSERVER.URL + '/accepts/follows/' + follower.id + '/' + me.id
95 }
96
97 function getLocalActorFollowRejectActivityPubUrl (follower: MActorId, following: MActorId) {
98 return WEBSERVER.URL + '/rejects/follows/' + follower.id + '/' + following.id
99 }
100
101 function getLocalVideoAnnounceActivityPubUrl (byActor: MActorId, video: MVideoUrl) {
102 return video.url + '/announces/' + byActor.id
103 }
104
105 function getDeleteActivityPubUrl (originalUrl: string) {
106 return originalUrl + '/delete'
107 }
108
109 function getUpdateActivityPubUrl (originalUrl: string, updatedAt: string) {
110 return originalUrl + '/updates/' + updatedAt
111 }
112
113 function getUndoActivityPubUrl (originalUrl: string) {
114 return originalUrl + '/undo'
115 }
116
117 // ---------------------------------------------------------------------------
118
119 function getAbuseTargetUrl (abuse: MAbuseFull) {
120 return abuse.VideoAbuse?.Video?.url ||
121 abuse.VideoCommentAbuse?.VideoComment?.url ||
122 abuse.FlaggedAccount.Actor.url
123 }
124
125 // ---------------------------------------------------------------------------
126
127 function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) {
128 if (!scheme) scheme = REMOTE_SCHEME.HTTP
129
130 const host = video.VideoChannel.Actor.Server.host
131
132 return scheme + '://' + host + path
133 }
134
135 // ---------------------------------------------------------------------------
136
137 function checkUrlsSameHost (url1: string, url2: string) {
138 const idHost = new URL(url1).host
139 const actorHost = new URL(url2).host
140
141 return idHost && actorHost && idHost.toLowerCase() === actorHost.toLowerCase()
142 }
143
144 // ---------------------------------------------------------------------------
145
146 export {
147 getLocalVideoActivityPubUrl,
148 getLocalVideoPlaylistActivityPubUrl,
149 getLocalVideoPlaylistElementActivityPubUrl,
150 getLocalVideoCacheFileActivityPubUrl,
151 getLocalVideoCacheStreamingPlaylistActivityPubUrl,
152 getLocalVideoCommentActivityPubUrl,
153 getLocalVideoChannelActivityPubUrl,
154 getLocalAccountActivityPubUrl,
155 getLocalAbuseActivityPubUrl,
156 getLocalActorFollowActivityPubUrl,
157 getLocalActorFollowAcceptActivityPubUrl,
158 getLocalVideoAnnounceActivityPubUrl,
159 getUpdateActivityPubUrl,
160 getUndoActivityPubUrl,
161 getVideoLikeActivityPubUrlByLocalActor,
162 getLocalVideoViewActivityPubUrl,
163 getVideoDislikeActivityPubUrlByLocalActor,
164 getLocalActorFollowRejectActivityPubUrl,
165 getDeleteActivityPubUrl,
166 getLocalVideoSharesActivityPubUrl,
167 getLocalVideoCommentsActivityPubUrl,
168 getLocalVideoLikesActivityPubUrl,
169 getLocalVideoDislikesActivityPubUrl,
170
171 getAbuseTargetUrl,
172 checkUrlsSameHost,
173 buildRemoteVideoBaseUrl
174 }