From fadf619ad61a016c1c7fc53de5a8f398a4f77519 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Dec 2017 11:18:49 +0100 Subject: Save --- server/models/account/account.ts | 234 +++++---------------------------- server/models/activitypub/actor.ts | 245 +++++++++++++++++++++++++++++++++++ server/models/video/video-channel.ts | 60 ++++----- 3 files changed, 303 insertions(+), 236 deletions(-) create mode 100644 server/models/activitypub/actor.ts (limited to 'server/models') diff --git a/server/models/account/account.ts b/server/models/account/account.ts index d6758fa10..b26395fd4 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -1,4 +1,3 @@ -import { join } from 'path' import * as Sequelize from 'sequelize' import { AfterDestroy, @@ -16,24 +15,13 @@ import { 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 { ActorModel } from '../activitypub/actor' import { ApplicationModel } from '../application/application' -import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' import { throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' -import { AccountFollowModel } from './account-follow' import { UserModel } from './user' @Table({ @@ -59,68 +47,7 @@ import { UserModel } from './user' } ] }) -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')) - @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 +export class AccountModel extends Model { @CreatedAt createdAt: Date @@ -128,29 +55,17 @@ export class AccountModel extends Model { @UpdatedAt updatedAt: Date - @ForeignKey(() => AvatarModel) - @Column - avatarId: number - - @BelongsTo(() => AvatarModel, { - foreignKey: { - allowNull: true - }, - onDelete: 'cascade' - }) - Avatar: AvatarModel - - @ForeignKey(() => ServerModel) + @ForeignKey(() => ActorModel) @Column - serverId: number + actorId: number - @BelongsTo(() => ServerModel, { + @BelongsTo(() => ActorModel, { foreignKey: { - allowNull: true + allowNull: false }, onDelete: 'cascade' }) - Server: ServerModel + Actor: ActorModel @ForeignKey(() => UserModel) @Column @@ -185,25 +100,6 @@ export class AccountModel extends Model { }) VideoChannels: VideoChannelModel[] - @HasMany(() => AccountFollowModel, { - foreignKey: { - name: 'accountId', - allowNull: false - }, - onDelete: 'cascade' - }) - AccountFollowing: AccountFollowModel[] - - @HasMany(() => AccountFollowModel, { - foreignKey: { - name: 'targetAccountId', - allowNull: false - }, - as: 'followers', - onDelete: 'cascade' - }) - AccountFollowers: AccountFollowModel[] - @AfterDestroy static sendDeleteIfOwned (instance: AccountModel) { if (instance.isOwned()) { @@ -281,9 +177,15 @@ export class AccountModel extends Model { static loadByUrl (url: string, transaction?: Sequelize.Transaction) { const query = { - where: { - url - }, + include: [ + { + model: ActorModel, + required: true, + where: { + url + } + } + ], transaction } @@ -292,11 +194,17 @@ export class AccountModel extends Model { static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { const query = { - where: { - followersUrl: { - [ Sequelize.Op.in ]: followersUrls + include: [ + { + model: ActorModel, + required: true, + where: { + followersUrl: { + [ Sequelize.Op.in ]: followersUrls + } + } } - }, + ], transaction } @@ -304,97 +212,21 @@ export class AccountModel extends Model { } 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 { + const actor = this.Actor.toFormattedJSON() + const account = { id: this.id, - uuid: this.uuid, - host, - score, - name: this.name, - followingCount: this.followingCount, - followersCount: this.followersCount, 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 - } - } - - return activityPubContextify(json) + return this.Actor.toActivityPubObject(this.name, this.uuid, 'Account') } 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' - } - - getPublicKeyUrl () { - return this.url + '#main-key' + return this.Actor.isOwned() } } diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts new file mode 100644 index 000000000..4cae6a6ec --- /dev/null +++ b/server/models/activitypub/actor.ts @@ -0,0 +1,245 @@ +import { join } from 'path' +import * as Sequelize from 'sequelize' +import { + AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, ForeignKey, HasMany, Is, IsUUID, Model, Table, + UpdatedAt +} from 'sequelize-typescript' +import { Avatar } from '../../../shared/models/avatars/avatar.model' +import { activityPubContextify } from '../../helpers' +import { + isActivityPubUrlValid, + isActorFollowersCountValid, + isActorFollowingCountValid, isActorPreferredUsernameValid, + isActorPrivateKeyValid, + isActorPublicKeyValid +} from '../../helpers/custom-validators/activitypub' +import { isUserUsernameValid } from '../../helpers/custom-validators/users' +import { AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers' +import { AccountFollowModel } from '../account/account-follow' +import { AvatarModel } from '../avatar/avatar' +import { ServerModel } from '../server/server' +import { throwIfNotValid } from '../utils' + +@Table({ + tableName: 'actor' +}) +export class ActorModel extends Model { + + @AllowNull(false) + @Default(DataType.UUIDV4) + @IsUUID(4) + @Column(DataType.UUID) + uuid: string + + @AllowNull(false) + @Is('ActorName', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor name')) + @Column + name: string + + @AllowNull(false) + @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + url: string + + @AllowNull(true) + @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY.max)) + publicKey: string + + @AllowNull(true) + @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY.max)) + privateKey: string + + @AllowNull(false) + @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count')) + @Column + followersCount: number + + @AllowNull(false) + @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count')) + @Column + followingCount: number + + @AllowNull(false) + @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + inboxUrl: string + + @AllowNull(false) + @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + outboxUrl: string + + @AllowNull(false) + @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + sharedInboxUrl: string + + @AllowNull(false) + @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + followersUrl: string + + @AllowNull(false) + @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url')) + @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max)) + followingUrl: string + + @CreatedAt + createdAt: Date + + @UpdatedAt + updatedAt: Date + + @ForeignKey(() => AvatarModel) + @Column + avatarId: number + + @BelongsTo(() => AvatarModel, { + foreignKey: { + allowNull: true + }, + onDelete: 'cascade' + }) + Avatar: AvatarModel + + @HasMany(() => AccountFollowModel, { + foreignKey: { + name: 'accountId', + allowNull: false + }, + onDelete: 'cascade' + }) + AccountFollowing: AccountFollowModel[] + + @HasMany(() => AccountFollowModel, { + foreignKey: { + name: 'targetAccountId', + allowNull: false + }, + as: 'followers', + onDelete: 'cascade' + }) + AccountFollowers: AccountFollowModel[] + + @ForeignKey(() => ServerModel) + @Column + serverId: number + + @BelongsTo(() => ServerModel, { + foreignKey: { + allowNull: true + }, + onDelete: 'cascade' + }) + Server: ServerModel + + static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) { + const query = { + where: { + followersUrl: { + [ Sequelize.Op.in ]: followersUrls + } + }, + transaction + } + + return ActorModel.findAll(query) + } + + toFormattedJSON () { + let avatar: Avatar = null + if (this.Avatar) { + avatar = { + path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename), + createdAt: this.Avatar.createdAt, + updatedAt: this.Avatar.updatedAt + } + } + + let host = CONFIG.WEBSERVER.HOST + let score: number + if (this.Server) { + host = this.Server.host + score = this.Server.score + } + + return { + id: this.id, + host, + score, + followingCount: this.followingCount, + followersCount: this.followersCount, + avatar + } + } + + toActivityPubObject (name: string, uuid: string, type: 'Account' | 'VideoChannel') { + let activityPubType + if (type === 'Account') { + activityPubType = this.serverId ? 'Application' as 'Application' : 'Person' as 'Person' + } else { // VideoChannel + activityPubType = 'Group' + } + + const json = { + type, + id: this.url, + following: this.getFollowingUrl(), + followers: this.getFollowersUrl(), + inbox: this.inboxUrl, + outbox: this.outboxUrl, + preferredUsername: name, + url: this.url, + name, + endpoints: { + sharedInbox: this.sharedInboxUrl + }, + uuid, + publicKey: { + id: this.getPublicKeyUrl(), + owner: this.url, + publicKeyPem: this.publicKey + } + } + + return activityPubContextify(json) + } + + getFollowerSharedInboxUrls (t: Sequelize.Transaction) { + const query = { + attributes: [ 'sharedInboxUrl' ], + include: [ + { + model: AccountFollowModel, + required: true, + as: 'followers', + where: { + targetAccountId: this.id + } + } + ], + transaction: t + } + + return ActorModel.findAll(query) + .then(accounts => accounts.map(a => a.sharedInboxUrl)) + } + + getFollowingUrl () { + return this.url + '/following' + } + + getFollowersUrl () { + return this.url + '/followers' + } + + getPublicKeyUrl () { + return this.url + '#main-key' + } + + isOwned () { + return this.serverId === null + } +} diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 068c8029d..fe44d3d53 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -11,18 +11,16 @@ import { HasMany, Is, IsUUID, - Model, Scopes, + Model, + Scopes, Table, UpdatedAt } from 'sequelize-typescript' import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' -import { activityPubCollection } from '../../helpers' -import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub' import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' -import { CONSTRAINTS_FIELDS } from '../../initializers' -import { getAnnounceActivityPubUrl } from '../../lib/activitypub' import { sendDeleteVideoChannel } from '../../lib/activitypub/send' import { AccountModel } from '../account/account' +import { ActorModel } from '../activitypub/actor' import { ServerModel } from '../server/server' import { getSort, throwIfNotValid } from '../utils' import { VideoModel } from './video' @@ -78,17 +76,24 @@ export class VideoChannelModel extends Model { @Column remote: boolean - @AllowNull(false) - @Is('VideoChannelUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) - @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.URL.max)) - url: string - @CreatedAt createdAt: Date @UpdatedAt updatedAt: Date + @ForeignKey(() => ActorModel) + @Column + actorId: number + + @BelongsTo(() => ActorModel, { + foreignKey: { + allowNull: false + }, + onDelete: 'cascade' + }) + Actor: ActorModel + @ForeignKey(() => AccountModel) @Column accountId: number @@ -174,9 +179,15 @@ export class VideoChannelModel extends Model { static loadByUrl (url: string, t?: Sequelize.Transaction) { const query: IFindOptions = { - where: { - url - } + include: [ + { + model: ActorModel, + required: true, + where: { + url + } + } + ] } if (t !== undefined) query.transaction = t @@ -264,27 +275,6 @@ export class VideoChannelModel extends Model { } toActivityPubObject () { - let sharesObject - if (Array.isArray(this.VideoChannelShares)) { - const shares: string[] = [] - - for (const videoChannelShare of this.VideoChannelShares) { - const shareUrl = getAnnounceActivityPubUrl(this.url, videoChannelShare.Account) - shares.push(shareUrl) - } - - sharesObject = activityPubCollection(shares) - } - - return { - type: 'VideoChannel' as 'VideoChannel', - id: this.url, - uuid: this.uuid, - content: this.description, - name: this.name, - published: this.createdAt.toISOString(), - updated: this.updatedAt.toISOString(), - shares: sharesObject - } + return this.Actor.toActivityPubObject(this.name, this.uuid, 'VideoChannel') } } -- cgit v1.2.3