]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/account.ts
add postgresql96-contrib to the FreeBSD dependencies (#958)
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
index bc7595a0ee63f07d393b2b20b35034b2f5e4e4a3..66f5dcf2ef3d22d269769bc008cde0a782a1a4e5 100644 (file)
@@ -16,7 +16,6 @@ import {
 } from 'sequelize-typescript'
 import { Account } from '../../../shared/models/actors'
 import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
-import { logger } from '../../helpers/logger'
 import { sendDeleteActor } from '../../lib/activitypub/send'
 import { ActorModel } from '../activitypub/actor'
 import { ApplicationModel } from '../application/application'
@@ -46,7 +45,19 @@ import { UserModel } from './user'
   ]
 })
 @Table({
-  tableName: 'account'
+  tableName: 'account',
+  indexes: [
+    {
+      fields: [ 'actorId' ],
+      unique: true
+    },
+    {
+      fields: [ 'applicationId' ]
+    },
+    {
+      fields: [ 'userId' ]
+    }
+  ]
 })
 export class AccountModel extends Model<AccountModel> {
 
@@ -127,7 +138,6 @@ export class AccountModel extends Model<AccountModel> {
     }
 
     if (instance.isOwned()) {
-      logger.debug('Sending delete of actor of account %s.', instance.Actor.url)
       return sendDeleteActor(instance.Actor, options.transaction)
     }
 
@@ -157,7 +167,6 @@ export class AccountModel extends Model<AccountModel> {
   static loadLocalByName (name: string) {
     const query = {
       where: {
-        name,
         [ Sequelize.Op.or ]: [
           {
             userId: {
@@ -170,7 +179,41 @@ export class AccountModel extends Model<AccountModel> {
             }
           }
         ]
-      }
+      },
+      include: [
+        {
+          model: ActorModel,
+          required: true,
+          where: {
+            preferredUsername: name
+          }
+        }
+      ]
+    }
+
+    return AccountModel.findOne(query)
+  }
+
+  static loadLocalByNameAndHost (name: string, host: string) {
+    const query = {
+      include: [
+        {
+          model: ActorModel,
+          required: true,
+          where: {
+            preferredUsername: name
+          },
+          include: [
+            {
+              model: ServerModel,
+              required: true,
+              where: {
+                host
+              }
+            }
+          ]
+        }
+      ]
     }
 
     return AccountModel.findOne(query)
@@ -213,7 +256,7 @@ export class AccountModel extends Model<AccountModel> {
     const actor = this.Actor.toFormattedJSON()
     const account = {
       id: this.id,
-      displayName: this.name,
+      displayName: this.getDisplayName(),
       description: this.description,
       createdAt: this.createdAt,
       updatedAt: this.updatedAt
@@ -233,4 +276,8 @@ export class AccountModel extends Model<AccountModel> {
   isOwned () {
     return this.Actor.isOwned()
   }
+
+  getDisplayName () {
+    return this.name
+  }
 }