]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/user.ts
Add ability for users to block an account/instance on server side
[github/Chocobozzz/PeerTube.git] / server / models / account / user.ts
index 680b1d52d9debb806818162f9de09ebf5972ec38..39654cfcf2f51326f71dc538913aa911942fdb44 100644 (file)
@@ -1,5 +1,7 @@
 import * as Sequelize from 'sequelize'
 import {
+  AfterDelete,
+  AfterUpdate,
   AllowNull,
   BeforeCreate,
   BeforeUpdate,
@@ -39,6 +41,7 @@ import { AccountModel } from './account'
 import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type'
 import { values } from 'lodash'
 import { NSFW_POLICY_TYPES } from '../../initializers'
+import { clearCacheByUserId } from '../../lib/oauth-model'
 
 enum ScopeNames {
   WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL'
@@ -168,11 +171,35 @@ export class UserModel extends Model<UserModel> {
     }
   }
 
+  @AfterUpdate
+  @AfterDelete
+  static removeTokenCache (instance: UserModel) {
+    return clearCacheByUserId(instance.id)
+  }
+
   static countTotal () {
     return this.count()
   }
 
-  static listForApi (start: number, count: number, sort: string) {
+  static listForApi (start: number, count: number, sort: string, search?: string) {
+    let where = undefined
+    if (search) {
+      where = {
+        [Sequelize.Op.or]: [
+          {
+            email: {
+              [Sequelize.Op.iLike]: '%' + search + '%'
+            }
+          },
+          {
+            username: {
+              [ Sequelize.Op.iLike ]: '%' + search + '%'
+            }
+          }
+        ]
+      }
+    }
+
     const query = {
       attributes: {
         include: [
@@ -195,7 +222,8 @@ export class UserModel extends Model<UserModel> {
       },
       offset: start,
       limit: count,
-      order: getSort(sort)
+      order: getSort(sort),
+      where
     }
 
     return UserModel.findAndCountAll(query)