import * as Sequelize from 'sequelize'
-
-import {
- isUserUsernameValid,
- isAccountPublicKeyValid,
- isAccountUrlValid,
- isAccountPrivateKeyValid,
- isAccountFollowersCountValid,
- isAccountFollowingCountValid,
- isAccountInboxValid,
- isAccountOutboxValid,
- isAccountSharedInboxValid,
- isAccountFollowersValid,
- isAccountFollowingValid,
- activityPubContextify
-} from '../../helpers'
-
-import { addMethodsToModel, getSort } from '../utils'
import {
- AccountInstance,
- AccountAttributes,
-
- AccountMethods
-} from './account-interface'
-import { sendDeleteAccount } from '../../lib/activitypub/send-request'
-import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers/constants'
-
-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 isOwned: AccountMethods.IsOwned
-let toActivityPubObject: AccountMethods.ToActivityPubObject
-let toFormattedJSON: AccountMethods.ToFormattedJSON
-let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
-let getFollowingUrl: AccountMethods.GetFollowingUrl
-let getFollowersUrl: AccountMethods.GetFollowersUrl
-let getPublicKeyUrl: AccountMethods.GetPublicKeyUrl
-
-export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
- Account = sequelize.define<AccountInstance, AccountAttributes>('Account',
- {
- uuid: {
- type: DataTypes.UUID,
- defaultValue: DataTypes.UUIDV4,
- allowNull: false,
- validate: {
- isUUID: 4
- }
- },
- name: {
- type: DataTypes.STRING,
- allowNull: false,
- validate: {
- nameValid: value => {
- const res = isUserUsernameValid(value)
- if (res === false) throw new Error('Name is not valid.')
- }
- }
- },
- url: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
- allowNull: false,
- validate: {
- urlValid: value => {
- const res = isAccountUrlValid(value)
- if (res === false) throw new Error('URL is not valid.')
- }
- }
- },
- publicKey: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PUBLIC_KEY.max),
- allowNull: false,
- validate: {
- publicKeyValid: value => {
- const res = isAccountPublicKeyValid(value)
- if (res === false) throw new Error('Public key is not valid.')
- }
- }
- },
- privateKey: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.PRIVATE_KEY.max),
- allowNull: true,
- validate: {
- privateKeyValid: value => {
- const res = isAccountPrivateKeyValid(value)
- if (res === false) throw new Error('Private key is not valid.')
- }
- }
- },
- followersCount: {
- type: DataTypes.INTEGER,
- allowNull: false,
- validate: {
- followersCountValid: value => {
- const res = isAccountFollowersCountValid(value)
- if (res === false) throw new Error('Followers count is not valid.')
- }
- }
- },
- followingCount: {
- type: DataTypes.INTEGER,
- allowNull: false,
- validate: {
- followingCountValid: value => {
- const res = isAccountFollowingCountValid(value)
- if (res === false) throw new Error('Following count is not valid.')
- }
- }
- },
- inboxUrl: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
- allowNull: false,
- validate: {
- inboxUrlValid: value => {
- const res = isAccountInboxValid(value)
- if (res === false) throw new Error('Inbox URL is not valid.')
- }
- }
- },
- outboxUrl: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
- allowNull: false,
- validate: {
- outboxUrlValid: value => {
- const res = isAccountOutboxValid(value)
- if (res === false) throw new Error('Outbox URL is not valid.')
- }
- }
- },
- sharedInboxUrl: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
- allowNull: false,
- validate: {
- sharedInboxUrlValid: value => {
- const res = isAccountSharedInboxValid(value)
- if (res === false) throw new Error('Shared inbox URL is not valid.')
- }
- }
- },
- followersUrl: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
- allowNull: false,
- validate: {
- followersUrlValid: value => {
- const res = isAccountFollowersValid(value)
- if (res === false) throw new Error('Followers URL is not valid.')
- }
- }
- },
- followingUrl: {
- type: DataTypes.STRING(CONSTRAINTS_FIELDS.ACCOUNTS.URL.max),
- allowNull: false,
- validate: {
- followingUrlValid: value => {
- const res = isAccountFollowingValid(value)
- if (res === false) throw new Error('Following URL is not valid.')
- }
- }
- }
- },
+ AfterDestroy,
+ AllowNull,
+ BelongsTo,
+ Column,
+ CreatedAt,
+ DefaultScope,
+ ForeignKey,
+ HasMany,
+ Is,
+ Model,
+ Table,
+ UpdatedAt
+} from 'sequelize-typescript'
+import { isUserUsernameValid } from '../../helpers/custom-validators/users'
+import { sendDeleteActor } from '../../lib/activitypub/send'
+import { ActorModel } from '../activitypub/actor'
+import { ApplicationModel } from '../application/application'
+import { ServerModel } from '../server/server'
+import { throwIfNotValid } from '../utils'
+import { VideoChannelModel } from '../video/video-channel'
+import { UserModel } from './user'
+
+@DefaultScope({
+ include: [
{
- indexes: [
- {
- fields: [ 'name' ]
- },
- {
- fields: [ 'serverId' ]
- },
+ model: () => ActorModel,
+ required: true,
+ include: [
{
- fields: [ 'userId' ],
- unique: true
- },
- {
- fields: [ 'applicationId' ],
- unique: true
- },
- {
- fields: [ 'name', 'serverId', 'applicationId' ],
- unique: true
+ model: () => ServerModel,
+ required: false
}
- ],
- hooks: { afterDestroy }
+ ]
}
- )
-
- const classMethods = [
- associate,
- loadAccountByServerAndUUID,
- loadApplication,
- load,
- loadByUUID,
- loadByUrl,
- loadLocalByName,
- loadByNameAndHost,
- listOwned
- ]
- const instanceMethods = [
- isOwned,
- toActivityPubObject,
- toFormattedJSON,
- getFollowerSharedInboxUrls,
- getFollowingUrl,
- getFollowersUrl,
- getPublicKeyUrl
]
- addMethodsToModel(Account, classMethods, instanceMethods)
+})
+@Table({
+ tableName: 'account'
+})
+export class AccountModel extends Model<AccountModel> {
- return Account
-}
+ @AllowNull(false)
+ @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name'))
+ @Column
+ name: string
+
+ @CreatedAt
+ createdAt: Date
-// ---------------------------------------------------------------------------
+ @UpdatedAt
+ updatedAt: Date
-function associate (models) {
- Account.belongsTo(models.Server, {
+ @ForeignKey(() => ActorModel)
+ @Column
+ actorId: number
+
+ @BelongsTo(() => ActorModel, {
foreignKey: {
- name: 'serverId',
- allowNull: true
+ allowNull: false
},
onDelete: 'cascade'
})
+ Actor: ActorModel
- Account.belongsTo(models.User, {
+ @ForeignKey(() => UserModel)
+ @Column
+ userId: number
+
+ @BelongsTo(() => UserModel, {
foreignKey: {
- name: 'userId',
allowNull: true
},
onDelete: 'cascade'
})
+ User: UserModel
+
+ @ForeignKey(() => ApplicationModel)
+ @Column
+ applicationId: number
- Account.belongsTo(models.Application, {
+ @BelongsTo(() => ApplicationModel, {
foreignKey: {
- name: 'applicationId',
allowNull: true
},
onDelete: 'cascade'
})
+ Account: ApplicationModel
- Account.hasMany(models.VideoChannel, {
+ @HasMany(() => VideoChannelModel, {
foreignKey: {
- name: 'accountId',
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
+ VideoChannels: VideoChannelModel[]
- Account.hasMany(models.AccountFollow, {
- foreignKey: {
- name: 'accountId',
- allowNull: false
- },
- as: 'following',
- onDelete: 'cascade'
- })
-
- Account.hasMany(models.AccountFollow, {
- foreignKey: {
- name: 'targetAccountId',
- allowNull: false
- },
- as: 'followers',
- onDelete: 'cascade'
- })
-}
-
-function afterDestroy (account: AccountInstance) {
- if (account.isOwned()) {
- return sendDeleteAccount(account, undefined)
- }
-
- return undefined
-}
-
-toFormattedJSON = function (this: AccountInstance) {
- let host = CONFIG.WEBSERVER.HOST
- let score: number
+ @AfterDestroy
+ static sendDeleteIfOwned (instance: AccountModel) {
+ if (instance.isOwned()) {
+ return sendDeleteActor(instance.Actor, undefined)
+ }
- if (this.Server) {
- host = this.Server.host
- score = this.Server.score as number
+ return undefined
}
- const json = {
- id: this.id,
- host,
- score,
- name: this.name,
- createdAt: this.createdAt,
- updatedAt: this.updatedAt
+ static load (id: number) {
+ return AccountModel.findById(id)
}
- return json
-}
-
-toActivityPubObject = function (this: AccountInstance) {
- 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)
-}
-
-isOwned = function (this: AccountInstance) {
- return this.serverId === null
-}
-
-getFollowerSharedInboxUrls = function (this: AccountInstance) {
- const query: Sequelize.FindOptions<AccountAttributes> = {
- attributes: [ 'sharedInboxUrl' ],
- include: [
- {
- model: Account['sequelize'].models.AccountFollow,
- required: true,
- as: 'followers',
- where: {
- targetAccountId: this.id
+ static loadByUUID (uuid: string) {
+ const query = {
+ include: [
+ {
+ model: ActorModel,
+ required: true,
+ where: {
+ uuid
+ }
}
- }
- ]
- }
-
- return Account.findAll(query)
- .then(accounts => accounts.map(a => a.sharedInboxUrl))
-}
-
-getFollowingUrl = function (this: AccountInstance) {
- return this.url + '/following'
-}
-
-getFollowersUrl = function (this: AccountInstance) {
- return this.url + '/followers'
-}
-
-getPublicKeyUrl = function (this: AccountInstance) {
- return this.url + '#main-key'
-}
-
-// ------------------------------ STATICS ------------------------------
-
-listOwned = function () {
- const query: Sequelize.FindOptions<AccountAttributes> = {
- where: {
- serverId: null
+ ]
}
- }
- return Account.findAll(query)
-}
+ return AccountModel.findOne(query)
+ }
-loadApplication = function () {
- return Account.findOne({
- include: [
- {
- model: Account['sequelize'].models.Application,
- required: true
+ static loadLocalByName (name: string) {
+ const query = {
+ where: {
+ name,
+ [ Sequelize.Op.or ]: [
+ {
+ userId: {
+ [ Sequelize.Op.ne ]: null
+ }
+ },
+ {
+ applicationId: {
+ [ Sequelize.Op.ne ]: null
+ }
+ }
+ ]
}
- ]
- })
-}
-
-load = function (id: number) {
- return Account.findById(id)
-}
-
-loadByUUID = function (uuid: string) {
- const query: Sequelize.FindOptions<AccountAttributes> = {
- where: {
- uuid
}
- }
- return Account.findOne(query)
-}
+ return AccountModel.findOne(query)
+ }
-loadLocalByName = function (name: string) {
- const query: Sequelize.FindOptions<AccountAttributes> = {
- where: {
- name,
- [Sequelize.Op.or]: [
- {
- userId: {
- [Sequelize.Op.ne]: null
- }
- },
+ static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
+ const query = {
+ include: [
{
- applicationId: {
- [Sequelize.Op.ne]: null
+ model: ActorModel,
+ required: true,
+ where: {
+ url
}
}
- ]
+ ],
+ transaction
}
- }
-
- return Account.findOne(query)
-}
-loadByNameAndHost = function (name: string, host: string) {
- const query: Sequelize.FindOptions<AccountAttributes> = {
- where: {
- name
- },
- include: [
- {
- model: Account['sequelize'].models.Server,
- required: true,
- where: {
- host
- }
- }
- ]
+ return AccountModel.findOne(query)
}
- return Account.findOne(query)
-}
+ toFormattedJSON () {
+ const actor = this.Actor.toFormattedJSON()
+ const account = {
+ id: this.id,
+ name: this.name,
+ createdAt: this.createdAt,
+ updatedAt: this.updatedAt
+ }
-loadByUrl = function (url: string, transaction?: Sequelize.Transaction) {
- const query: Sequelize.FindOptions<AccountAttributes> = {
- where: {
- url
- },
- transaction
+ return Object.assign(actor, account)
}
- return Account.findOne(query)
-}
-
-loadAccountByServerAndUUID = function (uuid: string, serverId: number, transaction: Sequelize.Transaction) {
- const query: Sequelize.FindOptions<AccountAttributes> = {
- where: {
- serverId,
- uuid
- },
- transaction
+ toActivityPubObject () {
+ return this.Actor.toActivityPubObject(this.name, 'Account')
}
- return Account.find(query)
+ isOwned () {
+ return this.Actor.isOwned()
+ }
}