X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=server%2Fmodels%2Faccount%2Faccount.ts;h=d674d8d22de3402f5a230a0289324bbd3c8739c9;hb=cf7a61b5a2b68fd966c4a355e37e84b048ed296b;hp=d6758fa109dd144e80747a47ea8a59038bde7f33;hpb=3fd3ab2d34d512b160a5e6084d7609be7b4f4452;p=oweals%2Fpeertube.git diff --git a/server/models/account/account.ts b/server/models/account/account.ts index d6758fa10..d674d8d22 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,126 +1,76 @@ -import { join } from 'path' import * as Sequelize from 'sequelize' import { - AfterDestroy, AllowNull, + BeforeDestroy, BelongsTo, Column, CreatedAt, - DataType, Default, + DefaultScope, ForeignKey, HasMany, Is, - IsUUID, Model, Table, UpdatedAt } from 'sequelize-typescript' -import { Avatar } from '../../../shared/models/avatars/avatar.model' -import { activityPubContextify } from '../../helpers' -import { - isAccountFollowersCountValid, - isAccountFollowingCountValid, - isAccountPrivateKeyValid, - isAccountPublicKeyValid, - isActivityPubUrlValid -} from '../../helpers/custom-validators/activitypub' -import { isUserUsernameValid } from '../../helpers/custom-validators/users' -import { AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' -import { sendDeleteAccount } from '../../lib/activitypub/send' +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' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' -import { throwIfNotValid } from '../utils' +import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' -import { AccountFollowModel } from './account-follow' +import { VideoCommentModel } from '../video/video-comment' import { UserModel } from './user' +@DefaultScope({ + include: [ + { + model: () => ActorModel, + required: true, + include: [ + { + model: () => ServerModel, + required: false + }, + { + model: () => AvatarModel, + required: false + } + ] + } + ] +}) @Table({ tableName: 'account', indexes: [ { - fields: [ 'name' ] - }, - { - fields: [ 'serverId' ] - }, - { - fields: [ 'userId' ], + fields: [ 'actorId' ], unique: true }, { - fields: [ 'applicationId' ], - unique: true + fields: [ 'applicationId' ] }, { - fields: [ 'name', 'serverId', 'applicationId' ], - unique: true + fields: [ 'userId' ] } ] }) -export class AccountModel extends Model { +export class AccountModel extends Model { @AllowNull(false) - @Default(DataType.UUIDV4) - @IsUUID(4) - @Column(DataType.UUID) - uuid: string - - @AllowNull(false) - @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name')) @Column name: string - @AllowNull(false) - @Is('AccountUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max)) - url: string - @AllowNull(true) - @Is('AccountPublicKey', value => throwIfNotValid(value, isAccountPublicKeyValid, 'public key')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max)) - publicKey: string - - @AllowNull(true) - @Is('AccountPublicKey', value => throwIfNotValid(value, isAccountPrivateKeyValid, 'private key')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max)) - privateKey: string - - @AllowNull(false) - @Is('AccountFollowersCount', value => throwIfNotValid(value, isAccountFollowersCountValid, 'followers count')) + @Default(null) + @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description')) @Column - followersCount: number - - @AllowNull(false) - @Is('AccountFollowersCount', value => throwIfNotValid(value, isAccountFollowingCountValid, 'following count')) - @Column - followingCount: number - - @AllowNull(false) - @Is('AccountInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max)) - inboxUrl: string - - @AllowNull(false) - @Is('AccountOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max)) - outboxUrl: string - - @AllowNull(false) - @Is('AccountSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max)) - sharedInboxUrl: string - - @AllowNull(false) - @Is('AccountFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max)) - followersUrl: string - - @AllowNull(false) - @Is('AccountFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max)) - followingUrl: string + description: string @CreatedAt createdAt: Date @@ -128,29 +78,17 @@ export class AccountModel extends Model { @UpdatedAt updatedAt: Date - @ForeignKey(() => AvatarModel) + @ForeignKey(() => ActorModel) @Column - avatarId: number + actorId: number - @BelongsTo(() => AvatarModel, { + @BelongsTo(() => ActorModel, { foreignKey: { - allowNull: true - }, - onDelete: 'cascade' - }) - Avatar: AvatarModel - - @ForeignKey(() => ServerModel) - @Column - serverId: number - - @BelongsTo(() => ServerModel, { - foreignKey: { - allowNull: true + allowNull: false }, onDelete: 'cascade' }) - Server: ServerModel + Actor: ActorModel @ForeignKey(() => UserModel) @Column @@ -185,54 +123,43 @@ export class AccountModel extends Model { }) VideoChannels: VideoChannelModel[] - @HasMany(() => AccountFollowModel, { + @HasMany(() => VideoCommentModel, { foreignKey: { - name: 'accountId', allowNull: false }, - onDelete: 'cascade' + onDelete: 'cascade', + hooks: true }) - AccountFollowing: AccountFollowModel[] + VideoComments: VideoCommentModel[] - @HasMany(() => AccountFollowModel, { - foreignKey: { - name: 'targetAccountId', - allowNull: false - }, - as: 'followers', - onDelete: 'cascade' - }) - AccountFollowers: AccountFollowModel[] + @BeforeDestroy + static async sendDeleteIfOwned (instance: AccountModel, options) { + if (!instance.Actor) { + instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel + } - @AfterDestroy - static sendDeleteIfOwned (instance: AccountModel) { if (instance.isOwned()) { - return sendDeleteAccount(instance, undefined) + return sendDeleteActor(instance.Actor, options.transaction) } return undefined } - static loadApplication () { - return AccountModel.findOne({ - include: [ - { - model: ApplicationModel, - required: true - } - ] - }) - } - static load (id: number) { return AccountModel.findById(id) } static loadByUUID (uuid: string) { const query = { - where: { - uuid - } + include: [ + { + model: ActorModel, + required: true, + where: { + uuid + } + } + ] } return AccountModel.findOne(query) @@ -241,7 +168,6 @@ export class AccountModel extends Model { static loadLocalByName (name: string) { const query = { where: { - name, [ Sequelize.Op.or ]: [ { userId: { @@ -254,24 +180,39 @@ export class AccountModel extends Model { } } ] - } + }, + include: [ + { + model: ActorModel, + required: true, + where: { + preferredUsername: name + } + } + ] } return AccountModel.findOne(query) } - static loadByNameAndHost (name: string, host: string) { + static loadLocalByNameAndHost (name: string, host: string) { const query = { - where: { - name - }, include: [ { - model: ServerModel, + model: ActorModel, required: true, where: { - host - } + preferredUsername: name + }, + include: [ + { + model: ServerModel, + required: true, + where: { + host + } + } + ] } ] } @@ -281,120 +222,63 @@ export class AccountModel extends Model { static loadByUrl (url: string, transaction?: Sequelize.Transaction) { const query = { - where: { - url - }, + include: [ + { + model: ActorModel, + required: true, + where: { + url + } + } + ], transaction } return AccountModel.findOne(query) } - static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { + static listForApi (start: number, count: number, sort: string) { const query = { - where: { - followersUrl: { - [ Sequelize.Op.in ]: followersUrls - } - }, - transaction + offset: start, + limit: count, + order: getSort(sort) } - return AccountModel.findAll(query) + return AccountModel.findAndCountAll(query) + .then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) } - toFormattedJSON () { - let host = CONFIG.WEBSERVER.HOST - let score: number - let avatar: Avatar = null - - if (this.Avatar) { - avatar = { - path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename), - createdAt: this.Avatar.createdAt, - updatedAt: this.Avatar.updatedAt - } - } - - if (this.Server) { - host = this.Server.host - score = this.Server.score - } - - return { + toFormattedJSON (): Account { + const actor = this.Actor.toFormattedJSON() + const account = { id: this.id, - uuid: this.uuid, - host, - score, - name: this.name, - followingCount: this.followingCount, - followersCount: this.followersCount, + displayName: this.getDisplayName(), + description: this.description, createdAt: this.createdAt, - updatedAt: this.updatedAt, - avatar + updatedAt: this.updatedAt } + + return Object.assign(actor, account) } toActivityPubObject () { - const type = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person' - - const json = { - type, - id: this.url, - following: this.getFollowingUrl(), - followers: this.getFollowersUrl(), - inbox: this.inboxUrl, - outbox: this.outboxUrl, - preferredUsername: this.name, - url: this.url, - name: this.name, - endpoints: { - sharedInbox: this.sharedInboxUrl - }, - uuid: this.uuid, - publicKey: { - id: this.getPublicKeyUrl(), - owner: this.url, - publicKeyPem: this.publicKey - } - } + const obj = this.Actor.toActivityPubObject(this.name, 'Account') - return activityPubContextify(json) + return Object.assign(obj, { + summary: this.description + }) } isOwned () { - return this.serverId === null - } - - getFollowerSharedInboxUrls (t: Sequelize.Transaction) { - const query = { - attributes: [ 'sharedInboxUrl' ], - include: [ - { - model: AccountFollowModel, - required: true, - as: 'followers', - where: { - targetAccountId: this.id - } - } - ], - transaction: t - } - - return AccountModel.findAll(query) - .then(accounts => accounts.map(a => a.sharedInboxUrl)) - } - - getFollowingUrl () { - return this.url + '/following' - } - - getFollowersUrl () { - return this.url + '/followers' + return this.Actor.isOwned() } - getPublicKeyUrl () { - return this.url + '#main-key' + getDisplayName () { + return this.name } }