2017-11-09 19:51:58 +03:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-12-12 19:53:50 +03:00
|
|
|
import {
|
|
|
|
AfterDestroy,
|
|
|
|
AllowNull,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
DataType,
|
|
|
|
Default,
|
|
|
|
ForeignKey,
|
|
|
|
HasMany,
|
|
|
|
Is,
|
|
|
|
IsUUID,
|
|
|
|
Model,
|
|
|
|
Table,
|
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
|
|
|
import { isUserUsernameValid } from '../../helpers/custom-validators/users'
|
|
|
|
import { sendDeleteAccount } from '../../lib/activitypub/send'
|
2017-12-14 13:18:49 +03:00
|
|
|
import { ActorModel } from '../activitypub/actor'
|
2017-12-12 19:53:50 +03:00
|
|
|
import { ApplicationModel } from '../application/application'
|
|
|
|
import { ServerModel } from '../server/server'
|
|
|
|
import { throwIfNotValid } from '../utils'
|
|
|
|
import { VideoChannelModel } from '../video/video-channel'
|
|
|
|
import { UserModel } from './user'
|
|
|
|
|
|
|
|
@Table({
|
|
|
|
tableName: 'account',
|
|
|
|
indexes: [
|
2017-11-09 19:51:58 +03:00
|
|
|
{
|
2017-12-12 19:53:50 +03:00
|
|
|
fields: [ 'name' ]
|
2017-11-09 19:51:58 +03:00
|
|
|
},
|
|
|
|
{
|
2017-12-12 19:53:50 +03:00
|
|
|
fields: [ 'serverId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'userId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'applicationId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'name', 'serverId', 'applicationId' ],
|
|
|
|
unique: true
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 19:53:50 +03:00
|
|
|
})
|
2017-12-14 13:18:49 +03:00
|
|
|
export class AccountModel extends Model<AccountModel> {
|
2017-12-12 19:53:50 +03:00
|
|
|
|
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
2017-12-14 13:18:49 +03:00
|
|
|
@ForeignKey(() => ActorModel)
|
2017-12-12 19:53:50 +03:00
|
|
|
@Column
|
2017-12-14 13:18:49 +03:00
|
|
|
actorId: number
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-14 13:18:49 +03:00
|
|
|
@BelongsTo(() => ActorModel, {
|
2017-11-09 19:51:58 +03:00
|
|
|
foreignKey: {
|
2017-12-14 13:18:49 +03:00
|
|
|
allowNull: false
|
2017-11-09 19:51:58 +03:00
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-14 13:18:49 +03:00
|
|
|
Actor: ActorModel
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@ForeignKey(() => UserModel)
|
|
|
|
@Column
|
|
|
|
userId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => UserModel, {
|
2017-11-09 19:51:58 +03:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 19:53:50 +03:00
|
|
|
User: UserModel
|
|
|
|
|
|
|
|
@ForeignKey(() => ApplicationModel)
|
|
|
|
@Column
|
|
|
|
applicationId: number
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@BelongsTo(() => ApplicationModel, {
|
2017-11-09 19:51:58 +03:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 19:53:50 +03:00
|
|
|
Application: ApplicationModel
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@HasMany(() => VideoChannelModel, {
|
2017-11-09 19:51:58 +03:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade',
|
|
|
|
hooks: true
|
|
|
|
})
|
2017-12-12 19:53:50 +03:00
|
|
|
VideoChannels: VideoChannelModel[]
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
@AfterDestroy
|
|
|
|
static sendDeleteIfOwned (instance: AccountModel) {
|
|
|
|
if (instance.isOwned()) {
|
|
|
|
return sendDeleteAccount(instance, undefined)
|
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
return undefined
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
static loadApplication () {
|
|
|
|
return AccountModel.findOne({
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ApplicationModel,
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
static load (id: number) {
|
|
|
|
return AccountModel.findById(id)
|
|
|
|
}
|
2017-12-04 12:34:40 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
static loadByUUID (uuid: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
uuid
|
|
|
|
}
|
2017-12-04 12:34:40 +03:00
|
|
|
}
|
2017-11-15 13:00:25 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
return AccountModel.findOne(query)
|
2017-11-15 13:00:25 +03:00
|
|
|
}
|
2017-11-15 12:10:41 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
static loadLocalByName (name: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
name,
|
|
|
|
[ Sequelize.Op.or ]: [
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
[ Sequelize.Op.ne ]: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
applicationId: {
|
|
|
|
[ Sequelize.Op.ne ]: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2017-11-13 19:39:41 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
return AccountModel.findOne(query)
|
|
|
|
}
|
2017-11-13 19:39:41 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
static loadByNameAndHost (name: string, host: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
name
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ServerModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
2017-12-12 19:53:50 +03:00
|
|
|
|
|
|
|
return AccountModel.findOne(query)
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
|
|
|
|
const query = {
|
2017-12-14 13:18:49 +03:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ActorModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2017-12-12 19:53:50 +03:00
|
|
|
transaction
|
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
return AccountModel.findOne(query)
|
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
|
|
|
|
const query = {
|
2017-12-14 13:18:49 +03:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ActorModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
followersUrl: {
|
|
|
|
[ Sequelize.Op.in ]: followersUrls
|
|
|
|
}
|
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
2017-12-14 13:18:49 +03:00
|
|
|
],
|
2017-12-12 19:53:50 +03:00
|
|
|
transaction
|
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
return AccountModel.findAll(query)
|
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
toFormattedJSON () {
|
2017-12-14 13:18:49 +03:00
|
|
|
const actor = this.Actor.toFormattedJSON()
|
|
|
|
const account = {
|
2017-12-12 19:53:50 +03:00
|
|
|
id: this.id,
|
|
|
|
createdAt: this.createdAt,
|
2017-12-14 13:18:49 +03:00
|
|
|
updatedAt: this.updatedAt
|
2017-12-12 19:53:50 +03:00
|
|
|
}
|
2017-12-14 13:18:49 +03:00
|
|
|
|
|
|
|
return Object.assign(actor, account)
|
2017-12-12 19:53:50 +03:00
|
|
|
}
|
2017-11-09 19:51:58 +03:00
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
toActivityPubObject () {
|
2017-12-14 13:18:49 +03:00
|
|
|
return this.Actor.toActivityPubObject(this.name, this.uuid, 'Account')
|
2017-11-09 19:51:58 +03:00
|
|
|
}
|
|
|
|
|
2017-12-12 19:53:50 +03:00
|
|
|
isOwned () {
|
2017-12-14 13:18:49 +03:00
|
|
|
return this.Actor.isOwned()
|
2017-12-12 19:53:50 +03:00
|
|
|
}
|
2017-11-24 15:41:10 +03:00
|
|
|
}
|