X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount.ts;h=61a88524c2d39662557d9ba524450f01b0566981;hb=25ed141c7c7631ef21d8764c1163fbf8a6591391;hp=464105261a463bba2e58b03dae10cce89fc5ab29;hpb=59c857da5961e2bcddcfd07832783c1e4afcd01a;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 464105261..61a88524c 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -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 -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,14 +55,14 @@ 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.') } } }, publicKey: { type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max), - allowNull: false, + allowNull: true, validate: { publicKeyValid: value => { const res = isAccountPublicKeyValid(value) @@ -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, @@ -263,6 +249,7 @@ function associate (models) { name: 'targetAccountId', allowNull: false }, + as: 'followers', onDelete: 'cascade' }) } @@ -327,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 = { attributes: [ 'sharedInboxUrl' ], include: [ @@ -339,7 +326,8 @@ getFollowerSharedInboxUrls = function (this: AccountInstance) { targetAccountId: this.id } } - ] + ], + transaction: t } return Account.findAll(query) @@ -360,16 +348,6 @@ getPublicKeyUrl = function (this: AccountInstance) { // ------------------------------ STATICS ------------------------------ -listOwned = function () { - const query: Sequelize.FindOptions = { - where: { - serverId: null - } - } - - return Account.findAll(query) -} - loadApplication = function () { return Account.findOne({ include: [ @@ -447,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 = { where: { - serverId, - uuid + followersUrl: { + [Sequelize.Op.in]: followersUrls + } }, transaction } - return Account.find(query) + return Account.findAll(query) }