+// ---------------------------------------------------------------------------
+
+async function checkCanSeeVideo (options: {
+ req: Request
+ res: Response
+ paramId: string
+ video: MVideo
+ authenticateInQuery?: boolean // default false
+}) {
+ const { req, res, video, paramId, authenticateInQuery = false } = options
+
+ if (video.requiresAuth()) {
+ return checkCanSeeAuthVideo(req, res, video, authenticateInQuery)
+ }
+
+ if (video.privacy === VideoPrivacy.UNLISTED) {
+ if (isUUIDValid(paramId)) return true
+
+ return checkCanSeeAuthVideo(req, res, video, authenticateInQuery)
+ }
+
+ if (video.privacy === VideoPrivacy.PUBLIC) return true
+
+ throw new Error('Fatal error when checking video right ' + video.url)
+}
+
+async function checkCanSeeAuthVideo (req: Request, res: Response, video: MVideoId | MVideoWithRights, authenticateInQuery = false) {
+ const fail = () => {
+ res.fail({
+ status: HttpStatusCode.FORBIDDEN_403,
+ message: 'Cannot fetch information of private/internal/blocked video'
+ })
+
+ return false
+ }
+
+ await authenticatePromise(req, res, authenticateInQuery)
+
+ const user = res.locals.oauth?.token.User
+ if (!user) return fail()
+
+ const videoWithRights = (video as MVideoWithRights).VideoChannel?.Account?.userId
+ ? video as MVideoWithRights
+ : await VideoModel.loadFull(video.id)
+
+ const privacy = videoWithRights.privacy
+
+ if (privacy === VideoPrivacy.INTERNAL) {
+ // We know we have a user
+ return true
+ }
+
+ const isOwnedByUser = videoWithRights.VideoChannel.Account.userId === user.id
+
+ if (videoWithRights.isBlacklisted()) {
+ if (isOwnedByUser || user.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST)) return true
+
+ return fail()
+ }
+
+ if (privacy === VideoPrivacy.PRIVATE || privacy === VideoPrivacy.UNLISTED) {
+ if (isOwnedByUser || user.hasRight(UserRight.SEE_ALL_VIDEOS)) return true
+
+ return fail()
+ }
+
+ // Should not happen
+ return fail()
+}
+
+// ---------------------------------------------------------------------------
+