2017-11-09 17:51:58 +01:00
|
|
|
import * as Sequelize from 'sequelize'
|
2017-12-12 17:53:50 +01:00
|
|
|
import {
|
2018-02-15 14:46:26 +01:00
|
|
|
AllowNull,
|
|
|
|
BeforeDestroy,
|
|
|
|
BelongsTo,
|
|
|
|
Column,
|
|
|
|
CreatedAt,
|
|
|
|
Default,
|
|
|
|
DefaultScope,
|
|
|
|
ForeignKey,
|
|
|
|
HasMany,
|
|
|
|
Is,
|
|
|
|
Model,
|
|
|
|
Table,
|
2017-12-12 17:53:50 +01:00
|
|
|
UpdatedAt
|
|
|
|
} from 'sequelize-typescript'
|
2017-12-29 19:10:13 +01:00
|
|
|
import { Account } from '../../../shared/models/actors'
|
2018-02-15 14:46:26 +01:00
|
|
|
import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
|
2018-07-30 13:39:20 +02:00
|
|
|
import { sendDeleteActor } from '../../lib/activitypub/send'
|
2017-12-14 11:18:49 +01:00
|
|
|
import { ActorModel } from '../activitypub/actor'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { ApplicationModel } from '../application/application'
|
2018-01-03 16:38:50 +01:00
|
|
|
import { AvatarModel } from '../avatar/avatar'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { ServerModel } from '../server/server'
|
2018-02-15 14:46:26 +01:00
|
|
|
import { getSort, throwIfNotValid } from '../utils'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoChannelModel } from '../video/video-channel'
|
2018-01-18 10:53:54 +01:00
|
|
|
import { VideoCommentModel } from '../video/video-comment'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { UserModel } from './user'
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
@DefaultScope({
|
|
|
|
include: [
|
2017-12-12 17:53:50 +01:00
|
|
|
{
|
2017-12-14 17:38:41 +01:00
|
|
|
model: () => ActorModel,
|
|
|
|
required: true,
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: () => ServerModel,
|
|
|
|
required: false
|
2018-01-03 16:38:50 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
model: () => AvatarModel,
|
|
|
|
required: false
|
2017-12-14 17:38:41 +01:00
|
|
|
}
|
|
|
|
]
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
]
|
2017-12-12 17:53:50 +01:00
|
|
|
})
|
2017-12-14 17:38:41 +01:00
|
|
|
@Table({
|
2018-07-23 20:13:30 +02:00
|
|
|
tableName: 'account',
|
|
|
|
indexes: [
|
|
|
|
{
|
|
|
|
fields: [ 'actorId' ],
|
|
|
|
unique: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'applicationId' ]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fields: [ 'userId' ]
|
|
|
|
}
|
|
|
|
]
|
2017-12-14 17:38:41 +01:00
|
|
|
})
|
2017-12-14 11:18:49 +01:00
|
|
|
export class AccountModel extends Model<AccountModel> {
|
2017-12-12 17:53:50 +01:00
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
@AllowNull(false)
|
|
|
|
@Column
|
|
|
|
name: string
|
|
|
|
|
2018-02-15 14:46:26 +01:00
|
|
|
@AllowNull(true)
|
|
|
|
@Default(null)
|
|
|
|
@Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
|
|
|
|
@Column
|
|
|
|
description: string
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@CreatedAt
|
|
|
|
createdAt: Date
|
|
|
|
|
|
|
|
@UpdatedAt
|
|
|
|
updatedAt: Date
|
|
|
|
|
2017-12-14 11:18:49 +01:00
|
|
|
@ForeignKey(() => ActorModel)
|
2017-12-12 17:53:50 +01:00
|
|
|
@Column
|
2017-12-14 11:18:49 +01:00
|
|
|
actorId: number
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-14 11:18:49 +01:00
|
|
|
@BelongsTo(() => ActorModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
2017-12-14 11:18:49 +01:00
|
|
|
allowNull: false
|
2017-11-09 17:51:58 +01:00
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-14 11:18:49 +01:00
|
|
|
Actor: ActorModel
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@ForeignKey(() => UserModel)
|
|
|
|
@Column
|
|
|
|
userId: number
|
|
|
|
|
|
|
|
@BelongsTo(() => UserModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
User: UserModel
|
|
|
|
|
|
|
|
@ForeignKey(() => ApplicationModel)
|
|
|
|
@Column
|
|
|
|
applicationId: number
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@BelongsTo(() => ApplicationModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: true
|
|
|
|
},
|
|
|
|
onDelete: 'cascade'
|
|
|
|
})
|
2018-01-18 10:53:54 +01:00
|
|
|
Application: ApplicationModel
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
@HasMany(() => VideoChannelModel, {
|
2017-11-09 17:51:58 +01:00
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade',
|
|
|
|
hooks: true
|
|
|
|
})
|
2017-12-12 17:53:50 +01:00
|
|
|
VideoChannels: VideoChannelModel[]
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2018-01-18 10:53:54 +01:00
|
|
|
@HasMany(() => VideoCommentModel, {
|
|
|
|
foreignKey: {
|
|
|
|
allowNull: false
|
|
|
|
},
|
|
|
|
onDelete: 'cascade',
|
|
|
|
hooks: true
|
|
|
|
})
|
|
|
|
VideoComments: VideoCommentModel[]
|
|
|
|
|
|
|
|
@BeforeDestroy
|
|
|
|
static async sendDeleteIfOwned (instance: AccountModel, options) {
|
|
|
|
if (!instance.Actor) {
|
|
|
|
instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
|
|
|
|
}
|
|
|
|
|
2018-07-30 13:39:20 +02:00
|
|
|
if (instance.isOwned()) {
|
|
|
|
return sendDeleteActor(instance.Actor, options.transaction)
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static load (id: number) {
|
|
|
|
return AccountModel.findById(id)
|
|
|
|
}
|
2017-12-04 10:34:40 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadByUUID (uuid: string) {
|
|
|
|
const query = {
|
2017-12-14 17:38:41 +01:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ActorModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
uuid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-12-04 10:34:40 +01:00
|
|
|
}
|
2017-11-15 11:00:25 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findOne(query)
|
2017-11-15 11:00:25 +01:00
|
|
|
}
|
2017-11-15 10:10:41 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadLocalByName (name: string) {
|
|
|
|
const query = {
|
|
|
|
where: {
|
|
|
|
[ Sequelize.Op.or ]: [
|
|
|
|
{
|
|
|
|
userId: {
|
|
|
|
[ Sequelize.Op.ne ]: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
applicationId: {
|
|
|
|
[ Sequelize.Op.ne ]: null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2018-02-21 16:44:18 +01:00
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ActorModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
preferredUsername: name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
return AccountModel.findOne(query)
|
|
|
|
}
|
|
|
|
|
|
|
|
static loadLocalByNameAndHost (name: string, host: string) {
|
|
|
|
const query = {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ActorModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
preferredUsername: name
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ServerModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
host
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2017-12-12 17:53:50 +01:00
|
|
|
}
|
2017-11-13 17:39:41 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findOne(query)
|
|
|
|
}
|
2017-11-13 17:39:41 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
|
|
|
|
const query = {
|
2017-12-14 11:18:49 +01:00
|
|
|
include: [
|
|
|
|
{
|
|
|
|
model: ActorModel,
|
|
|
|
required: true,
|
|
|
|
where: {
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
2017-12-12 17:53:50 +01:00
|
|
|
transaction
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return AccountModel.findOne(query)
|
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2018-01-03 16:38:50 +01:00
|
|
|
static listForApi (start: number, count: number, sort: string) {
|
|
|
|
const query = {
|
|
|
|
offset: start,
|
|
|
|
limit: count,
|
2018-02-19 11:31:50 +01:00
|
|
|
order: getSort(sort)
|
2018-01-03 16:38:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return AccountModel.findAndCountAll(query)
|
2018-07-30 13:39:20 +02:00
|
|
|
.then(({ rows, count }) => {
|
|
|
|
return {
|
|
|
|
data: rows,
|
|
|
|
total: count
|
|
|
|
}
|
|
|
|
})
|
2018-01-03 16:38:50 +01:00
|
|
|
}
|
|
|
|
|
2017-12-29 19:10:13 +01:00
|
|
|
toFormattedJSON (): Account {
|
2017-12-14 11:18:49 +01:00
|
|
|
const actor = this.Actor.toFormattedJSON()
|
|
|
|
const account = {
|
2017-12-12 17:53:50 +01:00
|
|
|
id: this.id,
|
2018-04-17 00:49:04 +02:00
|
|
|
displayName: this.getDisplayName(),
|
2018-02-15 14:46:26 +01:00
|
|
|
description: this.description,
|
2017-12-12 17:53:50 +01:00
|
|
|
createdAt: this.createdAt,
|
2017-12-14 11:18:49 +01:00
|
|
|
updatedAt: this.updatedAt
|
2017-12-12 17:53:50 +01:00
|
|
|
}
|
2017-12-14 11:18:49 +01:00
|
|
|
|
|
|
|
return Object.assign(actor, account)
|
2017-12-12 17:53:50 +01:00
|
|
|
}
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
toActivityPubObject () {
|
2018-02-15 14:46:26 +01:00
|
|
|
const obj = this.Actor.toActivityPubObject(this.name, 'Account')
|
|
|
|
|
|
|
|
return Object.assign(obj, {
|
|
|
|
summary: this.description
|
|
|
|
})
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
isOwned () {
|
2017-12-14 11:18:49 +01:00
|
|
|
return this.Actor.isOwned()
|
2017-12-12 17:53:50 +01:00
|
|
|
}
|
2018-04-17 00:49:04 +02:00
|
|
|
|
|
|
|
getDisplayName () {
|
|
|
|
return this.name
|
|
|
|
}
|
2017-11-24 13:41:10 +01:00
|
|
|
}
|