-import { FindOptions, literal, Op, QueryTypes } from 'sequelize'
+import { FindOptions, literal, Op, QueryTypes, where, fn, col, WhereOptions } from 'sequelize'
import {
AfterDestroy,
AfterUpdate,
Table,
UpdatedAt
} from 'sequelize-typescript'
-import { hasUserRight, USER_ROLE_LABELS, UserRight, VideoPrivacy } from '../../../shared'
+import { hasUserRight, MyUser, USER_ROLE_LABELS, UserRight, VideoPlaylistType, VideoPrivacy } from '../../../shared'
import { User, UserRole } from '../../../shared/models/users'
import {
isNoInstanceConfigWarningModal,
+ isNoWelcomeModal,
isUserAdminFlagsValid,
- isUserAutoPlayVideoValid,
- isUserAutoPlayNextVideoValid,
isUserAutoPlayNextVideoPlaylistValid,
+ isUserAutoPlayNextVideoValid,
+ isUserAutoPlayVideoValid,
isUserBlockedReasonValid,
isUserBlockedValid,
isUserEmailVerifiedValid,
isUserVideoQuotaDailyValid,
isUserVideoQuotaValid,
isUserVideosHistoryEnabledValid,
- isUserWebTorrentEnabledValid,
- isNoWelcomeModal
+ isUserWebTorrentEnabledValid
} from '../../helpers/custom-validators/users'
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'
-import { DEFAULT_THEME_NAME, DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants'
+import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants'
import { clearCacheByUserId } from '../../lib/oauth-model'
import { UserNotificationSettingModel } from './user-notification-setting'
import { VideoModel } from '../video/video'
import { getThemeOrDefault } from '../../lib/plugins/theme-utils'
import * as Bluebird from 'bluebird'
import {
+ MMyUserFormattable,
MUserDefault,
MUserFormattable,
MUserId,
MUserNotifSettingChannelDefault,
- MUserWithNotificationSetting, MVideoFullLight
+ MUserWithNotificationSetting,
+ MVideoFullLight
} from '@server/typings/models'
enum ScopeNames {
- WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL'
+ FOR_ME_API = 'FOR_ME_API'
}
@DefaultScope(() => ({
]
}))
@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,
@AllowNull(false)
@Default(true)
- @Is('UserAutoPlayNextVideoPlaylist', value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean'))
+ @Is(
+ 'UserAutoPlayNextVideoPlaylist',
+ value => throwIfNotValid(value, isUserAutoPlayNextVideoPlaylistValid, 'auto play next video for playlists boolean')
+ )
@Column
autoPlayNextVideoPlaylist: boolean
videoQuotaDaily: number
@AllowNull(false)
- @Default(DEFAULT_THEME_NAME)
+ @Default(DEFAULT_USER_THEME_NAME)
@Is('UserTheme', value => throwIfNotValid(value, isThemeNameValid, 'theme'))
@Column
theme: string
}
static listForApi (start: number, count: number, sort: string, search?: string) {
- let where = undefined
+ let where: WhereOptions
+
if (search) {
where = {
[Op.or]: [
},
{
username: {
- [ Op.iLike ]: '%' + search + '%'
+ [Op.iLike]: '%' + search + '%'
}
}
]
[
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'
}
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<MUserDefault[]> {
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: {
required: true,
include: [
{
- attributes: [ ],
+ attributes: [],
model: ActorModel.unscoped(),
required: true,
where: {
},
include: [
{
- attributes: [ ],
+ attributes: [],
as: 'ActorFollowings',
model: ActorFollowModel.unscoped(),
required: true,
static loadByUsername (username: string): Bluebird<MUserDefault> {
const query = {
where: {
- username: { [ Op.iLike ]: username }
+ username: { [Op.iLike]: username }
}
}
return UserModel.findOne(query)
}
- static loadByUsernameAndPopulateChannels (username: string): Bluebird<MUserNotifSettingChannelDefault> {
+ static loadForMeAPI (username: string): Bluebird<MUserNotifSettingChannelDefault> {
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<MUserDefault> {
const query = {
where: {
- [ Op.or ]: [ { username: { [ Op.iLike ]: username } }, { email } ]
+ [Op.or]: [
+ where(fn('lower', col('username')), fn('lower', username)),
+
+ { email }
+ ]
}
}
const query = {
where: {
username: {
- [ Op.like ]: `%${search}%`
+ [Op.like]: `%${search}%`
}
},
limit: 10
videoLanguages: this.videoLanguages,
role: this.role,
- roleLabel: USER_ROLE_LABELS[ this.role ],
+ roleLabel: USER_ROLE_LABELS[this.role],
videoQuota: this.videoQuota,
videoQuotaDaily: this.videoQuotaDaily,
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)
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'
}