X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Fuser.ts;h=fb4c15aef0e426bf4d8c4879d3507b79ee70f14e;hb=7024e9120b381b5b3201212f5a18f5cdc14e15ff;hp=24b1626e77f17aa490ed31fb2c4372cd674b6b10;hpb=453e83ea5d81d203ba34bc43cd5c2c750ba40568;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 24b1626e7..fb4c15aef 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -1,4 +1,4 @@ -import { FindOptions, literal, Op, QueryTypes } from 'sequelize' +import { FindOptions, literal, Op, QueryTypes, where, fn, col, WhereOptions } from 'sequelize' import { AfterDestroy, AfterUpdate, @@ -19,10 +19,14 @@ import { Table, UpdatedAt } from 'sequelize-typescript' -import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared' +import { hasUserRight, MyUser, USER_ROLE_LABELS, UserRight, VideoPlaylistType, VideoPrivacy } from '../../../shared' import { User, UserRole } from '../../../shared/models/users' import { + isNoInstanceConfigWarningModal, + isNoWelcomeModal, isUserAdminFlagsValid, + isUserAutoPlayNextVideoPlaylistValid, + isUserAutoPlayNextVideoValid, isUserAutoPlayVideoValid, isUserBlockedReasonValid, isUserBlockedValid, @@ -41,6 +45,7 @@ import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { OAuthTokenModel } from '../oauth/oauth-token' import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' +import { VideoPlaylistModel } from '../video/video-playlist' import { AccountModel } from './account' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' import { values } from 'lodash' @@ -55,10 +60,18 @@ import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' import { getThemeOrDefault } from '../../lib/plugins/theme-utils' import * as Bluebird from 'bluebird' -import { MUserChannel, MUserDefault, MUserId, MUserWithNotificationSetting } from '@server/typings/models' +import { + MMyUserFormattable, + MUserDefault, + MUserFormattable, + MUserId, + MUserNotifSettingChannelDefault, + MUserWithNotificationSetting, + MVideoFullLight +} from '@server/typings/models' enum ScopeNames { - WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' + FOR_ME_API = 'FOR_ME_API' } @DefaultScope(() => ({ @@ -74,12 +87,25 @@ enum ScopeNames { ] })) @Scopes(() => ({ - [ScopeNames.WITH_VIDEO_CHANNEL]: { + [ScopeNames.FOR_ME_API]: { include: [ { model: AccountModel, - required: true, - include: [ VideoChannelModel ] + include: [ + { + model: VideoChannelModel + }, + { + attributes: [ 'id', 'name', 'type' ], + model: VideoPlaylistModel.unscoped(), + required: true, + where: { + type: { + [Op.ne]: VideoPlaylistType.REGULAR + } + } + } + ] }, { model: UserNotificationSettingModel, @@ -152,6 +178,21 @@ export class UserModel extends Model { @Column autoPlayVideo: boolean + @AllowNull(false) + @Default(false) + @Is('UserAutoPlayNextVideo', value => throwIfNotValid(value, isUserAutoPlayNextVideoValid, 'auto play next video boolean')) + @Column + autoPlayNextVideo: boolean + + @AllowNull(false) + @Default(true) + @Is( + 'UserAutoPlayNextVideoPlaylist', + value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean') + ) + @Column + autoPlayNextVideoPlaylist: boolean + @AllowNull(true) @Default(null) @Is('UserVideoLanguages', value => throwIfNotValid(value, isUserVideoLanguages, 'video languages')) @@ -197,6 +238,24 @@ export class UserModel extends Model { @Column theme: string + @AllowNull(false) + @Default(false) + @Is( + 'UserNoInstanceConfigWarningModal', + value => throwIfNotValid(value, isNoInstanceConfigWarningModal, 'no instance config warning modal') + ) + @Column + noInstanceConfigWarningModal: boolean + + @AllowNull(false) + @Default(false) + @Is( + 'UserNoInstanceConfigWarningModal', + value => throwIfNotValid(value, isNoWelcomeModal, 'no welcome modal') + ) + @Column + noWelcomeModal: boolean + @CreatedAt createdAt: Date @@ -252,7 +311,8 @@ export class UserModel extends Model { } static listForApi (start: number, count: number, sort: string, search?: string) { - let where = undefined + let where: WhereOptions + if (search) { where = { [Op.or]: [ @@ -263,7 +323,7 @@ export class UserModel extends Model { }, { username: { - [ Op.iLike ]: '%' + search + '%' + [Op.iLike]: '%' + search + '%' } } ] @@ -276,14 +336,14 @@ export class UserModel extends Model { [ literal( '(' + - 'SELECT COALESCE(SUM("size"), 0) ' + - 'FROM (' + - 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + - 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + - 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + - 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + - 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + - ') t' + + 'SELECT COALESCE(SUM("size"), 0) ' + + 'FROM (' + + 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + + 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + + 'WHERE "account"."userId" = "UserModel"."id" GROUP BY "video"."id"' + + ') t' + ')' ), 'videoQuotaUsed' @@ -297,18 +357,18 @@ export class UserModel extends Model { } return UserModel.findAndCountAll(query) - .then(({ rows, count }) => { - return { - data: rows, - total: count - } - }) + .then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) } static listWithRight (right: UserRight): Bluebird { const roles = Object.keys(USER_ROLE_LABELS) - .map(k => parseInt(k, 10) as UserRole) - .filter(role => hasUserRight(role, right)) + .map(k => parseInt(k, 10) as UserRole) + .filter(role => hasUserRight(role, right)) const query = { where: { @@ -334,7 +394,7 @@ export class UserModel extends Model { required: true, include: [ { - attributes: [ ], + attributes: [], model: ActorModel.unscoped(), required: true, where: { @@ -342,7 +402,7 @@ export class UserModel extends Model { }, include: [ { - attributes: [ ], + attributes: [], as: 'ActorFollowings', model: ActorFollowModel.unscoped(), required: true, @@ -377,21 +437,21 @@ export class UserModel extends Model { static loadByUsername (username: string): Bluebird { const query = { where: { - username: { [ Op.iLike ]: username } + username: { [Op.iLike]: username } } } return UserModel.findOne(query) } - static loadByUsernameAndPopulateChannels (username: string): Bluebird { + static loadForMeAPI (username: string): Bluebird { const query = { where: { - username: { [ Op.iLike ]: username } + username: { [Op.iLike]: username } } } - return UserModel.scope(ScopeNames.WITH_VIDEO_CHANNEL).findOne(query) + return UserModel.scope(ScopeNames.FOR_ME_API).findOne(query) } static loadByEmail (email: string): Bluebird { @@ -409,7 +469,11 @@ export class UserModel extends Model { const query = { where: { - [ Op.or ]: [ { username: { [ Op.iLike ]: username } }, { email } ] + [Op.or]: [ + where(fn('lower', col('username')), fn('lower', username)), + + { email } + ] } } @@ -532,7 +596,7 @@ export class UserModel extends Model { const query = { where: { username: { - [ Op.like ]: `%${search}%` + [Op.like]: `%${search}%` } }, limit: 10 @@ -542,6 +606,22 @@ export class UserModel extends Model { .then(u => u.map(u => u.username)) } + canGetVideo (video: MVideoFullLight) { + const videoUserId = video.VideoChannel.Account.userId + + if (video.isBlacklisted()) { + return videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) + } + + if (video.privacy === VideoPrivacy.PRIVATE) { + return video.VideoChannel && videoUserId === this.id || this.hasRight(UserRight.MANAGE_VIDEO_BLACKLIST) + } + + if (video.privacy === VideoPrivacy.INTERNAL) return true + + return false + } + hasRight (right: UserRight) { return hasUserRight(this.role, right) } @@ -554,38 +634,54 @@ export class UserModel extends Model { return comparePassword(password, this.password) } - toFormattedJSON (parameters: { withAdminFlags?: boolean } = {}): User { + toFormattedJSON (this: MUserFormattable, parameters: { withAdminFlags?: boolean } = {}): User { const videoQuotaUsed = this.get('videoQuotaUsed') const videoQuotaUsedDaily = this.get('videoQuotaUsedDaily') - const json = { + const json: User = { id: this.id, username: this.username, email: this.email, + theme: getThemeOrDefault(this.theme, DEFAULT_USER_THEME_NAME), + pendingEmail: this.pendingEmail, emailVerified: this.emailVerified, + nsfwPolicy: this.nsfwPolicy, webTorrentEnabled: this.webTorrentEnabled, videosHistoryEnabled: this.videosHistoryEnabled, autoPlayVideo: this.autoPlayVideo, + autoPlayNextVideo: this.autoPlayNextVideo, + autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist, videoLanguages: this.videoLanguages, + role: this.role, - theme: getThemeOrDefault(this.theme, DEFAULT_USER_THEME_NAME), - roleLabel: USER_ROLE_LABELS[ this.role ], + roleLabel: USER_ROLE_LABELS[this.role], + videoQuota: this.videoQuota, videoQuotaDaily: this.videoQuotaDaily, - createdAt: this.createdAt, + videoQuotaUsed: videoQuotaUsed !== undefined + ? parseInt(videoQuotaUsed + '', 10) + : undefined, + videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined + ? parseInt(videoQuotaUsedDaily + '', 10) + : undefined, + + noInstanceConfigWarningModal: this.noInstanceConfigWarningModal, + noWelcomeModal: this.noWelcomeModal, + blocked: this.blocked, blockedReason: this.blockedReason, + account: this.Account.toFormattedJSON(), - notificationSettings: this.NotificationSetting ? this.NotificationSetting.toFormattedJSON() : undefined, + + notificationSettings: this.NotificationSetting + ? this.NotificationSetting.toFormattedJSON() + : undefined, + videoChannels: [], - videoQuotaUsed: videoQuotaUsed !== undefined - ? parseInt(videoQuotaUsed + '', 10) - : undefined, - videoQuotaUsedDaily: videoQuotaUsedDaily !== undefined - ? parseInt(videoQuotaUsedDaily + '', 10) - : undefined + + createdAt: this.createdAt } if (parameters.withAdminFlags) { @@ -594,18 +690,27 @@ export class UserModel extends Model { if (Array.isArray(this.Account.VideoChannels) === true) { json.videoChannels = this.Account.VideoChannels - .map(c => c.toFormattedJSON()) - .sort((v1, v2) => { - if (v1.createdAt < v2.createdAt) return -1 - if (v1.createdAt === v2.createdAt) return 0 + .map(c => c.toFormattedJSON()) + .sort((v1, v2) => { + if (v1.createdAt < v2.createdAt) return -1 + if (v1.createdAt === v2.createdAt) return 0 - return 1 - }) + return 1 + }) } return json } + toMeFormattedJSON (this: MMyUserFormattable): MyUser { + const formatted = this.toFormattedJSON() + + const specialPlaylists = this.Account.VideoPlaylists + .map(p => ({ id: p.id, name: p.name, type: p.type })) + + return Object.assign(formatted, { specialPlaylists }) + } + async isAbleToUploadVideo (videoFile: { size: number }) { if (this.videoQuota === -1 && this.videoQuotaDaily === -1) return Promise.resolve(true) @@ -628,12 +733,12 @@ export class UserModel extends Model { return 'SELECT SUM("size") AS "total" ' + 'FROM (' + - 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + - 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + - 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + - 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + - 'WHERE "account"."userId" = $userId ' + andWhere + - 'GROUP BY "video"."id"' + + 'SELECT MAX("videoFile"."size") AS "size" FROM "videoFile" ' + + 'INNER JOIN "video" ON "videoFile"."videoId" = "video"."id" ' + + 'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' + + 'INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ' + + 'WHERE "account"."userId" = $userId ' + andWhere + + 'GROUP BY "video"."id"' + ') t' }