]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/account/account.ts
Put activity pub sends inside transactions
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
index c370e17096cb90c6f8e49ffb5d0c64faeb035411..61a88524c2d39662557d9ba524450f01b0566981 100644 (file)
@@ -1,39 +1,26 @@
 import * as Sequelize from 'sequelize'
-
 import {
-  isUserUsernameValid,
-  isAccountPublicKeyValid,
-  isAccountUrlValid,
-  isAccountPrivateKeyValid,
+  activityPubContextify,
   isAccountFollowersCountValid,
   isAccountFollowingCountValid,
-  isAccountInboxValid,
-  isAccountOutboxValid,
-  isAccountSharedInboxValid,
-  isAccountFollowersValid,
-  isAccountFollowingValid,
-  activityPubContextify
+  isAccountPrivateKeyValid,
+  isAccountPublicKeyValid,
+  isUserUsernameValid
 } from '../../helpers'
-
-import { addMethodsToModel, getSort } from '../utils'
-import {
-  AccountInstance,
-  AccountAttributes,
-
-  AccountMethods
-} from './account-interface'
-import { sendDeleteAccount } from '../../lib/activitypub/send-request'
+import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
 import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
+import { sendDeleteAccount } from '../../lib/activitypub/send/send-delete'
+import { addMethodsToModel } from '../utils'
+import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface'
 
 let Account: Sequelize.Model<AccountInstance, AccountAttributes>
-let loadAccountByServerAndUUID: AccountMethods.LoadAccountByServerAndUUID
 let load: AccountMethods.Load
 let loadApplication: AccountMethods.LoadApplication
 let loadByUUID: AccountMethods.LoadByUUID
 let loadByUrl: AccountMethods.LoadByUrl
 let loadLocalByName: AccountMethods.LoadLocalByName
 let loadByNameAndHost: AccountMethods.LoadByNameAndHost
-let listOwned: AccountMethods.ListOwned
+let listByFollowersUrls: AccountMethods.ListByFollowersUrls
 let isOwned: AccountMethods.IsOwned
 let toActivityPubObject: AccountMethods.ToActivityPubObject
 let toFormattedJSON: AccountMethods.ToFormattedJSON
@@ -68,7 +55,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
         allowNull: false,
         validate: {
           urlValid: value => {
-            const res = isAccountUrlValid(value)
+            const res = isActivityPubUrlValid(value)
             if (res === false) throw new Error('URL is not valid.')
           }
         }
@@ -118,7 +105,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
         allowNull: false,
         validate: {
           inboxUrlValid: value => {
-            const res = isAccountInboxValid(value)
+            const res = isActivityPubUrlValid(value)
             if (res === false) throw new Error('Inbox URL is not valid.')
           }
         }
@@ -128,7 +115,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
         allowNull: false,
         validate: {
           outboxUrlValid: value => {
-            const res = isAccountOutboxValid(value)
+            const res = isActivityPubUrlValid(value)
             if (res === false) throw new Error('Outbox URL is not valid.')
           }
         }
@@ -138,7 +125,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
         allowNull: false,
         validate: {
           sharedInboxUrlValid: value => {
-            const res = isAccountSharedInboxValid(value)
+            const res = isActivityPubUrlValid(value)
             if (res === false) throw new Error('Shared inbox URL is not valid.')
           }
         }
@@ -148,7 +135,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
         allowNull: false,
         validate: {
           followersUrlValid: value => {
-            const res = isAccountFollowersValid(value)
+            const res = isActivityPubUrlValid(value)
             if (res === false) throw new Error('Followers URL is not valid.')
           }
         }
@@ -158,7 +145,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
         allowNull: false,
         validate: {
           followingUrlValid: value => {
-            const res = isAccountFollowingValid(value)
+            const res = isActivityPubUrlValid(value)
             if (res === false) throw new Error('Following URL is not valid.')
           }
         }
@@ -191,14 +178,13 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
 
   const classMethods = [
     associate,
-    loadAccountByServerAndUUID,
     loadApplication,
     load,
     loadByUUID,
     loadByUrl,
     loadLocalByName,
     loadByNameAndHost,
-    listOwned
+    listByFollowersUrls
   ]
   const instanceMethods = [
     isOwned,
@@ -328,7 +314,7 @@ isOwned = function (this: AccountInstance) {
   return this.serverId === null
 }
 
-getFollowerSharedInboxUrls = function (this: AccountInstance) {
+getFollowerSharedInboxUrls = function (this: AccountInstance, t: Sequelize.Transaction) {
   const query: Sequelize.FindOptions<AccountAttributes> = {
     attributes: [ 'sharedInboxUrl' ],
     include: [
@@ -340,7 +326,8 @@ getFollowerSharedInboxUrls = function (this: AccountInstance) {
           targetAccountId: this.id
         }
       }
-    ]
+    ],
+    transaction: t
   }
 
   return Account.findAll(query)
@@ -361,16 +348,6 @@ getPublicKeyUrl = function (this: AccountInstance) {
 
 // ------------------------------ STATICS ------------------------------
 
-listOwned = function () {
-  const query: Sequelize.FindOptions<AccountAttributes> = {
-    where: {
-      serverId: null
-    }
-  }
-
-  return Account.findAll(query)
-}
-
 loadApplication = function () {
   return Account.findOne({
     include: [
@@ -448,14 +425,15 @@ loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
   return Account.findOne(query)
 }
 
-loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
+listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) {
   const query: Sequelize.FindOptions<AccountAttributes> = {
     where: {
-      serverId,
-      uuid
+      followersUrl: {
+        [Sequelize.Op.in]: followersUrls
+      }
     },
     transaction
   }
 
-  return Account.find(query)
+  return Account.findAll(query)
 }