From e4f97babf701481b55cc10fb3448feab5f97c867 Mon Sep 17 00:00:00 2001
From: Chocobozzz <florian.bigard@gmail.com>
Date: Thu, 9 Nov 2017 17:51:58 +0100
Subject: Begin activitypub

---
 server/models/user/index.ts                     |   2 -
 server/models/user/user-interface.ts            |  69 ------
 server/models/user/user-video-rate-interface.ts |  26 --
 server/models/user/user-video-rate.ts           |  78 ------
 server/models/user/user.ts                      | 312 ------------------------
 5 files changed, 487 deletions(-)
 delete mode 100644 server/models/user/index.ts
 delete mode 100644 server/models/user/user-interface.ts
 delete mode 100644 server/models/user/user-video-rate-interface.ts
 delete mode 100644 server/models/user/user-video-rate.ts
 delete mode 100644 server/models/user/user.ts

(limited to 'server/models/user')

diff --git a/server/models/user/index.ts b/server/models/user/index.ts
deleted file mode 100644
index ed3689518..000000000
--- a/server/models/user/index.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from './user-video-rate-interface'
-export * from './user-interface'
diff --git a/server/models/user/user-interface.ts b/server/models/user/user-interface.ts
deleted file mode 100644
index 49c75aa3b..000000000
--- a/server/models/user/user-interface.ts
+++ /dev/null
@@ -1,69 +0,0 @@
-import * as Sequelize from 'sequelize'
-import * as Promise from 'bluebird'
-
-// Don't use barrel, import just what we need
-import { User as FormattedUser } from '../../../shared/models/users/user.model'
-import { ResultList } from '../../../shared/models/result-list.model'
-import { AuthorInstance } from '../video/author-interface'
-import { UserRight } from '../../../shared/models/users/user-right.enum'
-import { UserRole } from '../../../shared/models/users/user-role'
-
-export namespace UserMethods {
-  export type HasRight = (this: UserInstance, right: UserRight) => boolean
-  export type IsPasswordMatch = (this: UserInstance, password: string) => Promise<boolean>
-
-  export type ToFormattedJSON = (this: UserInstance) => FormattedUser
-  export type IsAbleToUploadVideo = (this: UserInstance, videoFile: Express.Multer.File) => Promise<boolean>
-
-  export type CountTotal = () => Promise<number>
-
-  export type GetByUsername = (username: string) => Promise<UserInstance>
-
-  export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<UserInstance> >
-
-  export type LoadById = (id: number) => Promise<UserInstance>
-
-  export type LoadByUsername = (username: string) => Promise<UserInstance>
-  export type LoadByUsernameAndPopulateChannels = (username: string) => Promise<UserInstance>
-
-  export type LoadByUsernameOrEmail = (username: string, email: string) => Promise<UserInstance>
-}
-
-export interface UserClass {
-  isPasswordMatch: UserMethods.IsPasswordMatch,
-  toFormattedJSON: UserMethods.ToFormattedJSON,
-  hasRight: UserMethods.HasRight,
-  isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo,
-
-  countTotal: UserMethods.CountTotal,
-  getByUsername: UserMethods.GetByUsername,
-  listForApi: UserMethods.ListForApi,
-  loadById: UserMethods.LoadById,
-  loadByUsername: UserMethods.LoadByUsername,
-  loadByUsernameAndPopulateChannels: UserMethods.LoadByUsernameAndPopulateChannels,
-  loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
-}
-
-export interface UserAttributes {
-  id?: number
-  password: string
-  username: string
-  email: string
-  displayNSFW?: boolean
-  role: UserRole
-  videoQuota: number
-
-  Author?: AuthorInstance
-}
-
-export interface UserInstance extends UserClass, UserAttributes, Sequelize.Instance<UserAttributes> {
-  id: number
-  createdAt: Date
-  updatedAt: Date
-
-  isPasswordMatch: UserMethods.IsPasswordMatch
-  toFormattedJSON: UserMethods.ToFormattedJSON
-  hasRight: UserMethods.HasRight
-}
-
-export interface UserModel extends UserClass, Sequelize.Model<UserInstance, UserAttributes> {}
diff --git a/server/models/user/user-video-rate-interface.ts b/server/models/user/user-video-rate-interface.ts
deleted file mode 100644
index ea0fdc4d9..000000000
--- a/server/models/user/user-video-rate-interface.ts
+++ /dev/null
@@ -1,26 +0,0 @@
-import * as Sequelize from 'sequelize'
-import * as Promise from 'bluebird'
-
-import { VideoRateType } from '../../../shared/models/videos/video-rate.type'
-
-export namespace UserVideoRateMethods {
-  export type Load = (userId: number, videoId: number, transaction: Sequelize.Transaction) => Promise<UserVideoRateInstance>
-}
-
-export interface UserVideoRateClass {
-  load: UserVideoRateMethods.Load
-}
-
-export interface UserVideoRateAttributes {
-  type: VideoRateType
-  userId: number
-  videoId: number
-}
-
-export interface UserVideoRateInstance extends UserVideoRateClass, UserVideoRateAttributes, Sequelize.Instance<UserVideoRateAttributes> {
-  id: number
-  createdAt: Date
-  updatedAt: Date
-}
-
-export interface UserVideoRateModel extends UserVideoRateClass, Sequelize.Model<UserVideoRateInstance, UserVideoRateAttributes> {}
diff --git a/server/models/user/user-video-rate.ts b/server/models/user/user-video-rate.ts
deleted file mode 100644
index 7d6dd7281..000000000
--- a/server/models/user/user-video-rate.ts
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
-  User rates per video.
-*/
-import { values } from 'lodash'
-import * as Sequelize from 'sequelize'
-
-import { VIDEO_RATE_TYPES } from '../../initializers'
-
-import { addMethodsToModel } from '../utils'
-import {
-  UserVideoRateInstance,
-  UserVideoRateAttributes,
-
-  UserVideoRateMethods
-} from './user-video-rate-interface'
-
-let UserVideoRate: Sequelize.Model<UserVideoRateInstance, UserVideoRateAttributes>
-let load: UserVideoRateMethods.Load
-
-export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
-  UserVideoRate = sequelize.define<UserVideoRateInstance, UserVideoRateAttributes>('UserVideoRate',
-    {
-      type: {
-        type: DataTypes.ENUM(values(VIDEO_RATE_TYPES)),
-        allowNull: false
-      }
-    },
-    {
-      indexes: [
-        {
-          fields: [ 'videoId', 'userId', 'type' ],
-          unique: true
-        }
-      ]
-    }
-  )
-
-  const classMethods = [
-    associate,
-
-    load
-  ]
-  addMethodsToModel(UserVideoRate, classMethods)
-
-  return UserVideoRate
-}
-
-// ------------------------------ STATICS ------------------------------
-
-function associate (models) {
-  UserVideoRate.belongsTo(models.Video, {
-    foreignKey: {
-      name: 'videoId',
-      allowNull: false
-    },
-    onDelete: 'CASCADE'
-  })
-
-  UserVideoRate.belongsTo(models.User, {
-    foreignKey: {
-      name: 'userId',
-      allowNull: false
-    },
-    onDelete: 'CASCADE'
-  })
-}
-
-load = function (userId: number, videoId: number, transaction: Sequelize.Transaction) {
-  const options: Sequelize.FindOptions<UserVideoRateAttributes> = {
-    where: {
-      userId,
-      videoId
-    }
-  }
-  if (transaction) options.transaction = transaction
-
-  return UserVideoRate.findOne(options)
-}
diff --git a/server/models/user/user.ts b/server/models/user/user.ts
deleted file mode 100644
index b974418d4..000000000
--- a/server/models/user/user.ts
+++ /dev/null
@@ -1,312 +0,0 @@
-import * as Sequelize from 'sequelize'
-import * as Promise from 'bluebird'
-
-import { getSort, addMethodsToModel } from '../utils'
-import {
-  cryptPassword,
-  comparePassword,
-  isUserPasswordValid,
-  isUserUsernameValid,
-  isUserDisplayNSFWValid,
-  isUserVideoQuotaValid,
-  isUserRoleValid
-} from '../../helpers'
-import { UserRight, USER_ROLE_LABELS, hasUserRight } from '../../../shared'
-
-import {
-  UserInstance,
-  UserAttributes,
-
-  UserMethods
-} from './user-interface'
-
-let User: Sequelize.Model<UserInstance, UserAttributes>
-let isPasswordMatch: UserMethods.IsPasswordMatch
-let hasRight: UserMethods.HasRight
-let toFormattedJSON: UserMethods.ToFormattedJSON
-let countTotal: UserMethods.CountTotal
-let getByUsername: UserMethods.GetByUsername
-let listForApi: UserMethods.ListForApi
-let loadById: UserMethods.LoadById
-let loadByUsername: UserMethods.LoadByUsername
-let loadByUsernameAndPopulateChannels: UserMethods.LoadByUsernameAndPopulateChannels
-let loadByUsernameOrEmail: UserMethods.LoadByUsernameOrEmail
-let isAbleToUploadVideo: UserMethods.IsAbleToUploadVideo
-
-export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
-  User = sequelize.define<UserInstance, UserAttributes>('User',
-    {
-      password: {
-        type: DataTypes.STRING,
-        allowNull: false,
-        validate: {
-          passwordValid: value => {
-            const res = isUserPasswordValid(value)
-            if (res === false) throw new Error('Password not valid.')
-          }
-        }
-      },
-      username: {
-        type: DataTypes.STRING,
-        allowNull: false,
-        validate: {
-          usernameValid: value => {
-            const res = isUserUsernameValid(value)
-            if (res === false) throw new Error('Username not valid.')
-          }
-        }
-      },
-      email: {
-        type: DataTypes.STRING(400),
-        allowNull: false,
-        validate: {
-          isEmail: true
-        }
-      },
-      displayNSFW: {
-        type: DataTypes.BOOLEAN,
-        allowNull: false,
-        defaultValue: false,
-        validate: {
-          nsfwValid: value => {
-            const res = isUserDisplayNSFWValid(value)
-            if (res === false) throw new Error('Display NSFW is not valid.')
-          }
-        }
-      },
-      role: {
-        type: DataTypes.INTEGER,
-        allowNull: false,
-        validate: {
-          roleValid: value => {
-            const res = isUserRoleValid(value)
-            if (res === false) throw new Error('Role is not valid.')
-          }
-        }
-      },
-      videoQuota: {
-        type: DataTypes.BIGINT,
-        allowNull: false,
-        validate: {
-          videoQuotaValid: value => {
-            const res = isUserVideoQuotaValid(value)
-            if (res === false) throw new Error('Video quota is not valid.')
-          }
-        }
-      }
-    },
-    {
-      indexes: [
-        {
-          fields: [ 'username' ],
-          unique: true
-        },
-        {
-          fields: [ 'email' ],
-          unique: true
-        }
-      ],
-      hooks: {
-        beforeCreate: beforeCreateOrUpdate,
-        beforeUpdate: beforeCreateOrUpdate
-      }
-    }
-  )
-
-  const classMethods = [
-    associate,
-
-    countTotal,
-    getByUsername,
-    listForApi,
-    loadById,
-    loadByUsername,
-    loadByUsernameAndPopulateChannels,
-    loadByUsernameOrEmail
-  ]
-  const instanceMethods = [
-    hasRight,
-    isPasswordMatch,
-    toFormattedJSON,
-    isAbleToUploadVideo
-  ]
-  addMethodsToModel(User, classMethods, instanceMethods)
-
-  return User
-}
-
-function beforeCreateOrUpdate (user: UserInstance) {
-  if (user.changed('password')) {
-    return cryptPassword(user.password)
-      .then(hash => {
-        user.password = hash
-        return undefined
-      })
-  }
-}
-
-// ------------------------------ METHODS ------------------------------
-
-hasRight = function (this: UserInstance, right: UserRight) {
-  return hasUserRight(this.role, right)
-}
-
-isPasswordMatch = function (this: UserInstance, password: string) {
-  return comparePassword(password, this.password)
-}
-
-toFormattedJSON = function (this: UserInstance) {
-  const json = {
-    id: this.id,
-    username: this.username,
-    email: this.email,
-    displayNSFW: this.displayNSFW,
-    role: this.role,
-    roleLabel: USER_ROLE_LABELS[this.role],
-    videoQuota: this.videoQuota,
-    createdAt: this.createdAt,
-    author: {
-      id: this.Author.id,
-      uuid: this.Author.uuid
-    }
-  }
-
-  if (Array.isArray(this.Author.VideoChannels) === true) {
-    const videoChannels = this.Author.VideoChannels
-      .map(c => c.toFormattedJSON())
-      .sort((v1, v2) => {
-        if (v1.createdAt < v2.createdAt) return -1
-        if (v1.createdAt === v2.createdAt) return 0
-
-        return 1
-      })
-
-    json['videoChannels'] = videoChannels
-  }
-
-  return json
-}
-
-isAbleToUploadVideo = function (this: UserInstance, videoFile: Express.Multer.File) {
-  if (this.videoQuota === -1) return Promise.resolve(true)
-
-  return getOriginalVideoFileTotalFromUser(this).then(totalBytes => {
-    return (videoFile.size + totalBytes) < this.videoQuota
-  })
-}
-
-// ------------------------------ STATICS ------------------------------
-
-function associate (models) {
-  User.hasOne(models.Author, {
-    foreignKey: 'userId',
-    onDelete: 'cascade'
-  })
-
-  User.hasMany(models.OAuthToken, {
-    foreignKey: 'userId',
-    onDelete: 'cascade'
-  })
-}
-
-countTotal = function () {
-  return this.count()
-}
-
-getByUsername = function (username: string) {
-  const query = {
-    where: {
-      username: username
-    },
-    include: [ { model: User['sequelize'].models.Author, required: true } ]
-  }
-
-  return User.findOne(query)
-}
-
-listForApi = function (start: number, count: number, sort: string) {
-  const query = {
-    offset: start,
-    limit: count,
-    order: [ getSort(sort) ],
-    include: [ { model: User['sequelize'].models.Author, required: true } ]
-  }
-
-  return User.findAndCountAll(query).then(({ rows, count }) => {
-    return {
-      data: rows,
-      total: count
-    }
-  })
-}
-
-loadById = function (id: number) {
-  const options = {
-    include: [ { model: User['sequelize'].models.Author, required: true } ]
-  }
-
-  return User.findById(id, options)
-}
-
-loadByUsername = function (username: string) {
-  const query = {
-    where: {
-      username
-    },
-    include: [ { model: User['sequelize'].models.Author, required: true } ]
-  }
-
-  return User.findOne(query)
-}
-
-loadByUsernameAndPopulateChannels = function (username: string) {
-  const query = {
-    where: {
-      username
-    },
-    include: [
-      {
-        model: User['sequelize'].models.Author,
-        required: true,
-        include: [ User['sequelize'].models.VideoChannel ]
-      }
-    ]
-  }
-
-  return User.findOne(query)
-}
-
-loadByUsernameOrEmail = function (username: string, email: string) {
-  const query = {
-    include: [ { model: User['sequelize'].models.Author, required: true } ],
-    where: {
-      [Sequelize.Op.or]: [ { username }, { email } ]
-    }
-  }
-
-  // FIXME: https://github.com/DefinitelyTyped/DefinitelyTyped/issues/18387
-  return (User as any).findOne(query)
-}
-
-// ---------------------------------------------------------------------------
-
-function getOriginalVideoFileTotalFromUser (user: UserInstance) {
-  // Don't use sequelize because we need to use a sub query
-  const query = 'SELECT SUM("size") AS "total" FROM ' +
-                '(SELECT MAX("VideoFiles"."size") AS "size" FROM "VideoFiles" ' +
-                'INNER JOIN "Videos" ON "VideoFiles"."videoId" = "Videos"."id" ' +
-                'INNER JOIN "VideoChannels" ON "VideoChannels"."id" = "Videos"."channelId" ' +
-                'INNER JOIN "Authors" ON "VideoChannels"."authorId" = "Authors"."id" ' +
-                'INNER JOIN "Users" ON "Authors"."userId" = "Users"."id" ' +
-                'WHERE "Users"."id" = $userId GROUP BY "Videos"."id") t'
-
-  const options = {
-    bind: { userId: user.id },
-    type: Sequelize.QueryTypes.SELECT
-  }
-  return User['sequelize'].query(query, options).then(([ { total } ]) => {
-    if (total === null) return 0
-
-    return parseInt(total, 10)
-  })
-}
-- 
cgit v1.2.3