From 22a73cb879a5cc775d4bec3d72fa9c9cf52e5175 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 12 Dec 2019 15:47:47 +0100 Subject: Add internal privacy mode --- server/models/account/user.ts | 18 ++++++++- server/models/video/schedule-video-update.ts | 2 +- server/models/video/video.ts | 55 ++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 7 deletions(-) (limited to 'server/models') diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 38c6d474a..522ea3310 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -19,7 +19,7 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' +import { hasUserRight, USER_ROLE_LABELS, UserRight, VideoPrivacy } from '../../../shared' import { User, UserRole } from '../../../shared/models/users' import { isNoInstanceConfigWarningModal, @@ -63,7 +63,7 @@ import { MUserFormattable, MUserId, MUserNotifSettingChannelDefault, - MUserWithNotificationSetting + MUserWithNotificationSetting, MVideoFullLight } from '@server/typings/models' enum ScopeNames { @@ -575,6 +575,20 @@ export class UserModel extends Model { .then(u => u.map(u => u.username)) } + canGetVideo (video: MVideoFullLight) { + if (video.privacy === VideoPrivacy.INTERNAL) return true + + if (video.privacy === VideoPrivacy.PRIVATE) { + return video.VideoChannel && video.VideoChannel.Account.userId === this.id + } + + if (video.isBlacklisted()) { + return this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) + } + + return false + } + hasRight (right: UserRight) { return hasUserRight(this.role, right) } diff --git a/server/models/video/schedule-video-update.ts b/server/models/video/schedule-video-update.ts index eefc10f14..00b7f5524 100644 --- a/server/models/video/schedule-video-update.ts +++ b/server/models/video/schedule-video-update.ts @@ -26,7 +26,7 @@ export class ScheduleVideoUpdateModel extends Model { @AllowNull(true) @Default(null) @Column - privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED + privacy: VideoPrivacy.PUBLIC | VideoPrivacy.UNLISTED | VideoPrivacy.INTERNAL @CreatedAt createdAt: Date diff --git a/server/models/video/video.ts b/server/models/video/video.ts index af6fae0b6..7e18af497 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -348,9 +348,8 @@ export type AvailableForListIDsOptions = { // Only list public/published videos if (!options.filter || options.filter !== 'all-local') { - const privacyWhere = { - // Always list public videos - privacy: VideoPrivacy.PUBLIC, + + const publishWhere = { // Always list published videos, or videos that are being transcoded but on which we don't want to wait for transcoding [ Op.or ]: [ { @@ -364,8 +363,26 @@ export type AvailableForListIDsOptions = { } ] } + whereAnd.push(publishWhere) - whereAnd.push(privacyWhere) + // List internal videos if the user is logged in + if (options.user) { + const privacyWhere = { + [Op.or]: [ + { + privacy: VideoPrivacy.INTERNAL + }, + { + privacy: VideoPrivacy.PUBLIC + } + ] + } + + whereAnd.push(privacyWhere) + } else { // Or only public videos + const privacyWhere = { privacy: VideoPrivacy.PUBLIC } + whereAnd.push(privacyWhere) + } } if (options.videoPlaylistId) { @@ -1773,6 +1790,10 @@ export class VideoModel extends Model { } } + private static isPrivacyForFederation (privacy: VideoPrivacy) { + return privacy === VideoPrivacy.PUBLIC || privacy === VideoPrivacy.UNLISTED + } + static getCategoryLabel (id: number) { return VIDEO_CATEGORIES[ id ] || 'Misc' } @@ -1980,12 +2001,38 @@ export class VideoModel extends Model { return isOutdated(this, ACTIVITY_PUB.VIDEO_REFRESH_INTERVAL) } + hasPrivacyForFederation () { + return VideoModel.isPrivacyForFederation(this.privacy) + } + + isNewVideo (newPrivacy: VideoPrivacy) { + return this.hasPrivacyForFederation() === false && VideoModel.isPrivacyForFederation(newPrivacy) === true + } + setAsRefreshed () { this.changed('updatedAt', true) return this.save() } + requiresAuth () { + return this.privacy === VideoPrivacy.PRIVATE || this.privacy === VideoPrivacy.INTERNAL || !!this.VideoBlacklist + } + + setPrivacy (newPrivacy: VideoPrivacy) { + if (this.privacy === VideoPrivacy.PRIVATE && newPrivacy !== VideoPrivacy.PRIVATE) { + this.publishedAt = new Date() + } + + this.privacy = newPrivacy + } + + isConfidential () { + return this.privacy === VideoPrivacy.PRIVATE || + this.privacy === VideoPrivacy.UNLISTED || + this.privacy === VideoPrivacy.INTERNAL + } + async publishIfNeededAndSave (t: Transaction) { if (this.state !== VideoState.PUBLISHED) { this.state = VideoState.PUBLISHED -- cgit v1.2.3