]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user.ts
Add messages about privacy concerns (P2P)
[github/Chocobozzz/PeerTube.git] / server / models / account / user.ts
index e37fd4d3be71ddc29195547187bcf0f83b9071c9..74cf0f4a8f7b0217bdff9dadaebf9fb5cf28c0e8 100644 (file)
@@ -4,7 +4,7 @@ import {
   Scopes, Table, UpdatedAt
 } from 'sequelize-typescript'
 import { hasUserRight, USER_ROLE_LABELS, UserRight } from '../../../shared'
-import { User } from '../../../shared/models/users'
+import { User, UserRole } from '../../../shared/models/users'
 import {
   isUserAutoPlayVideoValid, isUserDisplayNSFWValid, isUserPasswordValid, isUserRoleValid, isUserUsernameValid,
   isUserVideoQuotaValid
@@ -94,7 +94,8 @@ export class UserModel extends Model<UserModel> {
 
   @HasOne(() => AccountModel, {
     foreignKey: 'userId',
-    onDelete: 'cascade'
+    onDelete: 'cascade',
+    hooks: true
   })
   Account: AccountModel
 
@@ -120,22 +121,11 @@ export class UserModel extends Model<UserModel> {
     return this.count()
   }
 
-  static getByUsername (username: string) {
-    const query = {
-      where: {
-        username: username
-      },
-      include: [ { model: AccountModel, required: true } ]
-    }
-
-    return UserModel.findOne(query)
-  }
-
   static listForApi (start: number, count: number, sort: string) {
     const query = {
       offset: start,
       limit: count,
-      order: [ getSort(sort) ]
+      order: getSort(sort)
     }
 
     return UserModel.findAndCountAll(query)
@@ -147,6 +137,27 @@ export class UserModel extends Model<UserModel> {
       })
   }
 
+  static listEmailsWithRight (right: UserRight) {
+    const roles = Object.keys(USER_ROLE_LABELS)
+      .map(k => parseInt(k, 10) as UserRole)
+      .filter(role => hasUserRight(role, right))
+
+    console.log(roles)
+
+    const query = {
+      attribute: [ 'email' ],
+      where: {
+        role: {
+          [Sequelize.Op.in]: roles
+        }
+      }
+    }
+
+    return UserModel.unscoped()
+      .findAll(query)
+      .then(u => u.map(u => u.email))
+  }
+
   static loadById (id: number) {
     return UserModel.findById(id)
   }
@@ -171,7 +182,19 @@ export class UserModel extends Model<UserModel> {
     return UserModel.scope('withVideoChannel').findOne(query)
   }
 
-  static loadByUsernameOrEmail (username: string, email: string) {
+  static loadByEmail (email: string) {
+    const query = {
+      where: {
+        email
+      }
+    }
+
+    return UserModel.findOne(query)
+  }
+
+  static loadByUsernameOrEmail (username: string, email?: string) {
+    if (!email) email = username
+
     const query = {
       where: {
         [ Sequelize.Op.or ]: [ { username }, { email } ]