X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount.ts;h=61a88524c2d39662557d9ba524450f01b0566981;hb=25ed141c7c7631ef21d8764c1163fbf8a6591391;hp=00c0aefd4627760b5cde6f3e3db926e4ccde7ffd;hpb=e4f97babf701481b55cc10fb3448feab5f97c867;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 00c0aefd4..61a88524c 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,39 +1,29 @@ 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 { 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 { - AccountInstance, - AccountAttributes, - - AccountMethods -} from './account-interface' +import { AccountAttributes, AccountInstance, AccountMethods } from './account-interface' let Account: Sequelize.Model -let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID let load: AccountMethods.Load +let loadApplication: AccountMethods.LoadApplication let loadByUUID: AccountMethods.LoadByUUID let loadByUrl: AccountMethods.LoadByUrl -let loadLocalAccountByName: AccountMethods.LoadLocalAccountByName -let listOwned: AccountMethods.ListOwned -let listFollowerUrlsForApi: AccountMethods.ListFollowerUrlsForApi -let listFollowingUrlsForApi: AccountMethods.ListFollowingUrlsForApi +let loadLocalByName: AccountMethods.LoadLocalByName +let loadByNameAndHost: AccountMethods.LoadByNameAndHost +let listByFollowersUrls: AccountMethods.ListByFollowersUrls let isOwned: AccountMethods.IsOwned let toActivityPubObject: AccountMethods.ToActivityPubObject +let toFormattedJSON: AccountMethods.ToFormattedJSON let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls let getFollowingUrl: AccountMethods.GetFollowingUrl let getFollowersUrl: AccountMethods.GetFollowersUrl @@ -54,25 +44,25 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes type: DataTypes.STRING, allowNull: false, validate: { - usernameValid: value => { + nameValid: value => { const res = isUserUsernameValid(value) - if (res === false) throw new Error('Username is not valid.') + if (res === false) throw new Error('Name is not valid.') } } }, url: { - type: DataTypes.STRING, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max), 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, - allowNull: false, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max), + allowNull: true, validate: { publicKeyValid: value => { const res = isAccountPublicKeyValid(value) @@ -81,8 +71,8 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes } }, privateKey: { - type: DataTypes.STRING, - allowNull: false, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max), + allowNull: true, validate: { privateKeyValid: value => { const res = isAccountPrivateKeyValid(value) @@ -104,58 +94,58 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes type: DataTypes.INTEGER, allowNull: false, validate: { - followersCountValid: value => { + followingCountValid: value => { const res = isAccountFollowingCountValid(value) if (res === false) throw new Error('Following count is not valid.') } } }, inboxUrl: { - type: DataTypes.STRING, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max), allowNull: false, validate: { inboxUrlValid: value => { - const res = isAccountInboxValid(value) + const res = isActivityPubUrlValid(value) if (res === false) throw new Error('Inbox URL is not valid.') } } }, outboxUrl: { - type: DataTypes.STRING, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max), allowNull: false, validate: { outboxUrlValid: value => { - const res = isAccountOutboxValid(value) + const res = isActivityPubUrlValid(value) if (res === false) throw new Error('Outbox URL is not valid.') } } }, sharedInboxUrl: { - type: DataTypes.STRING, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max), 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.') } } }, followersUrl: { - type: DataTypes.STRING, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max), allowNull: false, validate: { followersUrlValid: value => { - const res = isAccountFollowersValid(value) + const res = isActivityPubUrlValid(value) if (res === false) throw new Error('Followers URL is not valid.') } } }, followingUrl: { - type: DataTypes.STRING, + type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max), allowNull: false, validate: { followingUrlValid: value => { - const res = isAccountFollowingValid(value) + const res = isActivityPubUrlValid(value) if (res === false) throw new Error('Following URL is not valid.') } } @@ -167,7 +157,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes fields: [ 'name' ] }, { - fields: [ 'podId' ] + fields: [ 'serverId' ] }, { fields: [ 'userId' ], @@ -178,7 +168,7 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes unique: true }, { - fields: [ 'name', 'podId' ], + fields: [ 'name', 'serverId', 'applicationId' ], unique: true } ], @@ -188,17 +178,18 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes const classMethods = [ associate, - loadAccountByPodAndUUID, + loadApplication, load, loadByUUID, - loadLocalAccountByName, - listOwned, - listFollowerUrlsForApi, - listFollowingUrlsForApi + loadByUrl, + loadLocalByName, + loadByNameAndHost, + listByFollowersUrls ] const instanceMethods = [ isOwned, toActivityPubObject, + toFormattedJSON, getFollowerSharedInboxUrls, getFollowingUrl, getFollowersUrl, @@ -212,9 +203,9 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes // --------------------------------------------------------------------------- function associate (models) { - Account.belongsTo(models.Pod, { + Account.belongsTo(models.Server, { foreignKey: { - name: 'podId', + name: 'serverId', allowNull: true }, onDelete: 'cascade' @@ -230,7 +221,7 @@ function associate (models) { Account.belongsTo(models.Application, { foreignKey: { - name: 'userId', + name: 'applicationId', allowNull: true }, onDelete: 'cascade' @@ -245,7 +236,7 @@ function associate (models) { hooks: true }) - Account.hasMany(models.AccountFollower, { + Account.hasMany(models.AccountFollow, { foreignKey: { name: 'accountId', allowNull: false @@ -253,29 +244,47 @@ function associate (models) { onDelete: 'cascade' }) - Account.hasMany(models.AccountFollower, { + Account.hasMany(models.AccountFollow, { foreignKey: { name: 'targetAccountId', allowNull: false }, + as: 'followers', onDelete: 'cascade' }) } function afterDestroy (account: AccountInstance) { if (account.isOwned()) { - const removeVideoAccountToFriendsParams = { - uuid: account.uuid - } - - return removeVideoAccountToFriends(removeVideoAccountToFriendsParams) + return sendDeleteAccount(account, undefined) } return undefined } +toFormattedJSON = function (this: AccountInstance) { + let host = CONFIG.WEBSERVER.HOST + let score: number + + if (this.Server) { + host = this.Server.host + score = this.Server.score as number + } + + const json = { + id: this.id, + host, + score, + name: this.name, + createdAt: this.createdAt, + updatedAt: this.updatedAt + } + + return json +} + toActivityPubObject = function (this: AccountInstance) { - const type = this.podId ? 'Application' : 'Person' + const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person' const json = { type, @@ -302,20 +311,23 @@ toActivityPubObject = function (this: AccountInstance) { } isOwned = function (this: AccountInstance) { - return this.podId === null + return this.serverId === null } -getFollowerSharedInboxUrls = function (this: AccountInstance) { +getFollowerSharedInboxUrls = function (this: AccountInstance, t: Sequelize.Transaction) { const query: Sequelize.FindOptions = { attributes: [ 'sharedInboxUrl' ], include: [ { - model: Account['sequelize'].models.AccountFollower, + model: Account['sequelize'].models.AccountFollow, + required: true, + as: 'followers', where: { targetAccountId: this.id } } - ] + ], + transaction: t } return Account.findAll(query) @@ -323,7 +335,7 @@ getFollowerSharedInboxUrls = function (this: AccountInstance) { } getFollowingUrl = function (this: AccountInstance) { - return this.url + '/followers' + return this.url + '/following' } getFollowersUrl = function (this: AccountInstance) { @@ -336,22 +348,15 @@ getPublicKeyUrl = function (this: AccountInstance) { // ------------------------------ STATICS ------------------------------ -listOwned = function () { - const query: Sequelize.FindOptions = { - where: { - podId: null - } - } - - return Account.findAll(query) -} - -listFollowerUrlsForApi = function (name: string, start: number, count: number) { - return createListFollowForApiQuery('followers', name, start, count) -} - -listFollowingUrlsForApi = function (name: string, start: number, count: number) { - return createListFollowForApiQuery('following', name, start, count) +loadApplication = function () { + return Account.findOne({ + include: [ + { + model: Account['sequelize'].models.Application, + required: true + } + ] + }) } load = function (id: number) { @@ -368,77 +373,67 @@ loadByUUID = function (uuid: string) { return Account.findOne(query) } -loadLocalAccountByName = function (name: string) { +loadLocalByName = function (name: string) { const query: Sequelize.FindOptions = { where: { name, - userId: { - [Sequelize.Op.ne]: null - } + [Sequelize.Op.or]: [ + { + userId: { + [Sequelize.Op.ne]: null + } + }, + { + applicationId: { + [Sequelize.Op.ne]: null + } + } + ] } } return Account.findOne(query) } -loadByUrl = function (url: string) { +loadByNameAndHost = function (name: string, host: string) { const query: Sequelize.FindOptions = { where: { - url - } + name + }, + include: [ + { + model: Account['sequelize'].models.Server, + required: true, + where: { + host + } + } + ] } return Account.findOne(query) } -loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Sequelize.Transaction) { +loadByUrl = function (url: string, transaction?: Sequelize.Transaction) { const query: Sequelize.FindOptions = { where: { - podId, - uuid + url }, transaction } - return Account.find(query) + return Account.findOne(query) } -// ------------------------------ UTILS ------------------------------ - -async function createListFollowForApiQuery (type: 'followers' | 'following', name: string, start: number, count: number) { - let firstJoin: string - let secondJoin: string - - if (type === 'followers') { - firstJoin = 'targetAccountId' - secondJoin = 'accountId' - } else { - firstJoin = 'accountId' - secondJoin = 'targetAccountId' - } - - const selections = [ '"Followers"."url" AS "url"', 'COUNT(*) AS "total"' ] - const tasks: Promise[] = [] - - for (const selection of selections) { - const query = 'SELECT ' + selection + ' FROM "Account" ' + - 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' + - 'INNER JOIN "Account" AS "Followers" ON "Followers"."id" = "AccountFollower"."' + secondJoin + '" ' + - 'WHERE "Account"."name" = \'$name\' ' + - 'LIMIT ' + start + ', ' + count - - const options = { - bind: { name }, - type: Sequelize.QueryTypes.SELECT - } - tasks.push(Account['sequelize'].query(query, options)) +listByFollowersUrls = function (followersUrls: string[], transaction?: Sequelize.Transaction) { + const query: Sequelize.FindOptions = { + where: { + followersUrl: { + [Sequelize.Op.in]: followersUrls + } + }, + transaction } - const [ followers, [ { total } ]] = await Promise.all(tasks) - const urls: string[] = followers.map(f => f.url) - - return { - data: urls, - total: parseInt(total, 10) - } + return Account.findAll(query) }