PeerTube/server/models/account/account.ts

210 lines
3.9 KiB
TypeScript
Raw Normal View History

2017-11-09 16:51:58 +00:00
import * as Sequelize from 'sequelize'
2017-12-12 16:53:50 +00:00
import {
AfterDestroy,
AllowNull,
BelongsTo,
Column,
CreatedAt,
2017-12-14 16:38:41 +00:00
DefaultScope,
2017-12-12 16:53:50 +00:00
ForeignKey,
HasMany,
Is,
Model,
Table,
UpdatedAt
} from 'sequelize-typescript'
2017-12-29 18:10:13 +00:00
import { Account } from '../../../shared/models/actors'
2017-12-12 16:53:50 +00:00
import { isUserUsernameValid } from '../../helpers/custom-validators/users'
2017-12-14 16:38:41 +00:00
import { sendDeleteActor } from '../../lib/activitypub/send'
2017-12-14 10:18:49 +00:00
import { ActorModel } from '../activitypub/actor'
2017-12-12 16:53:50 +00:00
import { ApplicationModel } from '../application/application'
2018-01-03 15:38:50 +00:00
import { AvatarModel } from '../avatar/avatar'
2017-12-12 16:53:50 +00:00
import { ServerModel } from '../server/server'
2018-01-03 15:38:50 +00:00
import { getSort, throwIfNotValid } from '../utils'
2017-12-12 16:53:50 +00:00
import { VideoChannelModel } from '../video/video-channel'
import { UserModel } from './user'
2017-12-14 16:38:41 +00:00
@DefaultScope({
include: [
2017-12-12 16:53:50 +00:00
{
2017-12-14 16:38:41 +00:00
model: () => ActorModel,
required: true,
include: [
{
model: () => ServerModel,
required: false
2018-01-03 15:38:50 +00:00
},
{
model: () => AvatarModel,
required: false
2017-12-14 16:38:41 +00:00
}
]
2017-11-09 16:51:58 +00:00
}
]
2017-12-12 16:53:50 +00:00
})
2017-12-14 16:38:41 +00:00
@Table({
tableName: 'account'
})
2017-12-14 10:18:49 +00:00
export class AccountModel extends Model<AccountModel> {
2017-12-12 16:53:50 +00:00
2017-12-14 16:38:41 +00:00
@AllowNull(false)
@Column
name: string
2017-12-12 16:53:50 +00:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
2017-12-14 10:18:49 +00:00
@ForeignKey(() => ActorModel)
2017-12-12 16:53:50 +00:00
@Column
2017-12-14 10:18:49 +00:00
actorId: number
2017-11-09 16:51:58 +00:00
2017-12-14 10:18:49 +00:00
@BelongsTo(() => ActorModel, {
2017-11-09 16:51:58 +00:00
foreignKey: {
2017-12-14 10:18:49 +00:00
allowNull: false
2017-11-09 16:51:58 +00:00
},
onDelete: 'cascade'
})
2017-12-14 10:18:49 +00:00
Actor: ActorModel
2017-11-09 16:51:58 +00:00
2017-12-12 16:53:50 +00:00
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
2017-11-09 16:51:58 +00:00
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
2017-12-12 16:53:50 +00:00
User: UserModel
@ForeignKey(() => ApplicationModel)
@Column
applicationId: number
2017-11-09 16:51:58 +00:00
2017-12-12 16:53:50 +00:00
@BelongsTo(() => ApplicationModel, {
2017-11-09 16:51:58 +00:00
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
2017-12-14 16:38:41 +00:00
Account: ApplicationModel
2017-11-09 16:51:58 +00:00
2017-12-12 16:53:50 +00:00
@HasMany(() => VideoChannelModel, {
2017-11-09 16:51:58 +00:00
foreignKey: {
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
2017-12-12 16:53:50 +00:00
VideoChannels: VideoChannelModel[]
2017-11-09 16:51:58 +00:00
2017-12-12 16:53:50 +00:00
@AfterDestroy
static sendDeleteIfOwned (instance: AccountModel) {
if (instance.isOwned()) {
2017-12-14 16:38:41 +00:00
return sendDeleteActor(instance.Actor, undefined)
2017-12-12 16:53:50 +00:00
}
2017-11-09 16:51:58 +00:00
2017-12-12 16:53:50 +00:00
return undefined
2017-11-09 16:51:58 +00:00
}
2017-12-12 16:53:50 +00:00
static load (id: number) {
return AccountModel.findById(id)
}
2017-12-04 09:34:40 +00:00
2017-12-12 16:53:50 +00:00
static loadByUUID (uuid: string) {
const query = {
2017-12-14 16:38:41 +00:00
include: [
{
model: ActorModel,
required: true,
where: {
uuid
}
}
]
2017-12-04 09:34:40 +00:00
}
2017-11-15 10:00:25 +00:00
2017-12-12 16:53:50 +00:00
return AccountModel.findOne(query)
2017-11-15 10:00:25 +00:00
}
2017-12-12 16:53:50 +00: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 16:39:41 +00:00
2017-12-12 16:53:50 +00:00
return AccountModel.findOne(query)
}
2017-11-13 16:39:41 +00:00
2017-12-12 16:53:50 +00:00
static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
const query = {
2017-12-14 10:18:49 +00:00
include: [
{
model: ActorModel,
required: true,
where: {
url
}
}
],
2017-12-12 16:53:50 +00:00
transaction
}
2017-11-09 16:51:58 +00:00
2017-12-12 16:53:50 +00:00
return AccountModel.findOne(query)
}
2017-11-09 16:51:58 +00:00
2018-01-03 15:38:50 +00:00
static listForApi (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
order: [ getSort(sort) ]
}
return AccountModel.findAndCountAll(query)
.then(({ rows, count }) => {
return {
data: rows,
total: count
}
})
}
2017-12-29 18:10:13 +00:00
toFormattedJSON (): Account {
2017-12-14 10:18:49 +00:00
const actor = this.Actor.toFormattedJSON()
const account = {
2017-12-12 16:53:50 +00:00
id: this.id,
2017-12-29 18:10:13 +00:00
name: this.Actor.preferredUsername,
displayName: this.name,
2017-12-12 16:53:50 +00:00
createdAt: this.createdAt,
2017-12-14 10:18:49 +00:00
updatedAt: this.updatedAt
2017-12-12 16:53:50 +00:00
}
2017-12-14 10:18:49 +00:00
return Object.assign(actor, account)
2017-12-12 16:53:50 +00:00
}
2017-11-09 16:51:58 +00:00
2017-12-12 16:53:50 +00:00
toActivityPubObject () {
2017-12-14 16:38:41 +00:00
return this.Actor.toActivityPubObject(this.name, 'Account')
2017-11-09 16:51:58 +00:00
}
2017-12-12 16:53:50 +00:00
isOwned () {
2017-12-14 10:18:49 +00:00
return this.Actor.isOwned()
2017-12-12 16:53:50 +00:00
}
}