PeerTube/server/models/account/account.ts

364 lines
7.4 KiB
TypeScript
Raw Normal View History

2017-11-09 10:51:58 -06:00
import * as Sequelize from 'sequelize'
2017-12-12 10:53:50 -06:00
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
Default,
DefaultScope,
ForeignKey,
HasMany,
Is,
2019-03-05 04:30:43 -06:00
Model,
Scopes,
Table,
2017-12-12 10:53:50 -06:00
UpdatedAt
} from 'sequelize-typescript'
2019-02-26 03:55:40 -06:00
import { Account, AccountSummary } from '../../../shared/models/actors'
import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
import { sendDeleteActor } from '../../lib/activitypub/send'
2017-12-14 04:18:49 -06:00
import { ActorModel } from '../activitypub/actor'
2017-12-12 10:53:50 -06:00
import { ApplicationModel } from '../application/application'
import { ServerModel } from '../server/server'
import { getSort, throwIfNotValid } from '../utils'
2017-12-12 10:53:50 -06:00
import { VideoChannelModel } from '../video/video-channel'
import { VideoCommentModel } from '../video/video-comment'
2017-12-12 10:53:50 -06:00
import { UserModel } from './user'
2019-02-26 03:55:40 -06:00
import { AvatarModel } from '../avatar/avatar'
import { VideoPlaylistModel } from '../video/video-playlist'
2019-04-11 04:33:44 -05:00
import { WEBSERVER } from '../../initializers/constants'
2019-02-26 03:55:40 -06:00
export enum ScopeNames {
SUMMARY = 'SUMMARY'
}
2017-12-12 10:53:50 -06:00
2017-12-14 10:38:41 -06:00
@DefaultScope({
include: [
2017-12-12 10:53:50 -06:00
{
2018-08-23 10:58:39 -05:00
model: () => ActorModel, // Default scope includes avatar and server
required: true
2017-11-09 10:51:58 -06:00
}
]
2017-12-12 10:53:50 -06:00
})
2019-02-26 03:55:40 -06:00
@Scopes({
2019-03-05 04:30:43 -06:00
[ ScopeNames.SUMMARY ]: (whereActor?: Sequelize.WhereOptions<ActorModel>) => {
2019-02-26 03:55:40 -06:00
return {
attributes: [ 'id', 'name' ],
include: [
{
attributes: [ 'id', 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
model: ActorModel.unscoped(),
required: true,
where: whereActor,
include: [
{
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
},
{
model: AvatarModel.unscoped(),
required: false
}
]
}
]
}
}
})
2017-12-14 10:38:41 -06:00
@Table({
2018-07-23 13:13:30 -05:00
tableName: 'account',
indexes: [
{
fields: [ 'actorId' ],
unique: true
},
{
fields: [ 'applicationId' ]
},
{
fields: [ 'userId' ]
}
]
2017-12-14 10:38:41 -06:00
})
2017-12-14 04:18:49 -06:00
export class AccountModel extends Model<AccountModel> {
2017-12-12 10:53:50 -06:00
2017-12-14 10:38:41 -06:00
@AllowNull(false)
@Column
name: string
@AllowNull(true)
@Default(null)
@Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
@Column
description: string
2017-12-12 10:53:50 -06:00
@CreatedAt
createdAt: Date
@UpdatedAt
updatedAt: Date
2017-12-14 04:18:49 -06:00
@ForeignKey(() => ActorModel)
2017-12-12 10:53:50 -06:00
@Column
2017-12-14 04:18:49 -06:00
actorId: number
2017-11-09 10:51:58 -06:00
2017-12-14 04:18:49 -06:00
@BelongsTo(() => ActorModel, {
2017-11-09 10:51:58 -06:00
foreignKey: {
2017-12-14 04:18:49 -06:00
allowNull: false
2017-11-09 10:51:58 -06:00
},
onDelete: 'cascade'
})
2017-12-14 04:18:49 -06:00
Actor: ActorModel
2017-11-09 10:51:58 -06:00
2017-12-12 10:53:50 -06:00
@ForeignKey(() => UserModel)
@Column
userId: number
@BelongsTo(() => UserModel, {
2017-11-09 10:51:58 -06:00
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
2017-12-12 10:53:50 -06:00
User: UserModel
@ForeignKey(() => ApplicationModel)
@Column
applicationId: number
2017-11-09 10:51:58 -06:00
2017-12-12 10:53:50 -06:00
@BelongsTo(() => ApplicationModel, {
2017-11-09 10:51:58 -06:00
foreignKey: {
allowNull: true
},
onDelete: 'cascade'
})
Application: ApplicationModel
2017-11-09 10:51:58 -06:00
2017-12-12 10:53:50 -06:00
@HasMany(() => VideoChannelModel, {
2017-11-09 10:51:58 -06:00
foreignKey: {
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
2017-12-12 10:53:50 -06:00
VideoChannels: VideoChannelModel[]
2017-11-09 10:51:58 -06:00
2019-02-26 03:55:40 -06:00
@HasMany(() => VideoPlaylistModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
VideoPlaylists: VideoPlaylistModel[]
@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
}
if (instance.isOwned()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
return undefined
2017-11-09 10:51:58 -06:00
}
static load (id: number, transaction?: Sequelize.Transaction) {
2019-02-21 07:28:06 -06:00
return AccountModel.findByPk(id, { transaction })
2017-12-12 10:53:50 -06:00
}
2017-12-04 03:34:40 -06:00
2017-12-12 10:53:50 -06:00
static loadByUUID (uuid: string) {
const query = {
2017-12-14 10:38:41 -06:00
include: [
{
model: ActorModel,
required: true,
where: {
uuid
}
}
]
2017-12-04 03:34:40 -06:00
}
2017-11-15 04:00:25 -06:00
2017-12-12 10:53:50 -06:00
return AccountModel.findOne(query)
2017-11-15 04:00:25 -06:00
}
2019-02-21 07:06:10 -06:00
static loadByNameWithHost (nameWithHost: string) {
const [ accountName, host ] = nameWithHost.split('@')
2019-04-11 04:33:44 -05:00
if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
2019-02-21 07:06:10 -06:00
return AccountModel.loadByNameAndHost(accountName, host)
}
2017-12-12 10:53:50 -06:00
static loadLocalByName (name: string) {
const query = {
where: {
[ Sequelize.Op.or ]: [
{
userId: {
[ Sequelize.Op.ne ]: null
}
},
{
applicationId: {
[ Sequelize.Op.ne ]: null
}
}
]
2018-02-21 09:44:18 -06:00
},
include: [
{
model: ActorModel,
required: true,
where: {
preferredUsername: name
}
}
]
}
return AccountModel.findOne(query)
}
2018-08-17 08:45:42 -05:00
static loadByNameAndHost (name: string, host: string) {
2018-02-21 09:44:18 -06:00
const query = {
include: [
{
model: ActorModel,
required: true,
where: {
preferredUsername: name
},
include: [
{
model: ServerModel,
required: true,
where: {
host
}
}
]
}
]
2017-12-12 10:53:50 -06:00
}
2017-11-13 10:39:41 -06:00
2017-12-12 10:53:50 -06:00
return AccountModel.findOne(query)
}
2017-11-13 10:39:41 -06:00
2017-12-12 10:53:50 -06:00
static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
const query = {
2017-12-14 04:18:49 -06:00
include: [
{
model: ActorModel,
required: true,
where: {
url
}
}
],
2017-12-12 10:53:50 -06:00
transaction
}
2017-11-09 10:51:58 -06:00
2017-12-12 10:53:50 -06:00
return AccountModel.findOne(query)
}
2017-11-09 10:51:58 -06:00
2018-01-03 09:38:50 -06:00
static listForApi (start: number, count: number, sort: string) {
const query = {
offset: start,
limit: count,
2018-02-19 04:31:50 -06:00
order: getSort(sort)
2018-01-03 09:38:50 -06:00
}
return AccountModel.findAndCountAll(query)
.then(({ rows, count }) => {
return {
data: rows,
total: count
}
})
2018-01-03 09:38:50 -06:00
}
2018-12-05 10:27:24 -06:00
static listLocalsForSitemap (sort: string) {
const query = {
attributes: [ ],
offset: 0,
order: getSort(sort),
include: [
{
attributes: [ 'preferredUsername', 'serverId' ],
model: ActorModel.unscoped(),
where: {
serverId: null
}
}
]
}
return AccountModel
.unscoped()
.findAll(query)
}
2017-12-29 12:10:13 -06:00
toFormattedJSON (): Account {
2017-12-14 04:18:49 -06:00
const actor = this.Actor.toFormattedJSON()
const account = {
2017-12-12 10:53:50 -06:00
id: this.id,
displayName: this.getDisplayName(),
description: this.description,
2017-12-12 10:53:50 -06:00
createdAt: this.createdAt,
updatedAt: this.updatedAt,
userId: this.userId ? this.userId : undefined
2017-12-12 10:53:50 -06:00
}
2017-12-14 04:18:49 -06:00
return Object.assign(actor, account)
2017-12-12 10:53:50 -06:00
}
2017-11-09 10:51:58 -06:00
2019-02-26 03:55:40 -06:00
toFormattedSummaryJSON (): AccountSummary {
const actor = this.Actor.toFormattedJSON()
return {
id: this.id,
uuid: actor.uuid,
name: actor.name,
displayName: this.getDisplayName(),
url: actor.url,
host: actor.host,
avatar: actor.avatar
}
}
2017-12-12 10:53:50 -06:00
toActivityPubObject () {
const obj = this.Actor.toActivityPubObject(this.name, 'Account')
return Object.assign(obj, {
summary: this.description
})
2017-11-09 10:51:58 -06:00
}
2017-12-12 10:53:50 -06:00
isOwned () {
2017-12-14 04:18:49 -06:00
return this.Actor.isOwned()
2017-12-12 10:53:50 -06:00
}
2019-01-14 04:30:15 -06:00
isOutdated () {
return this.Actor.isOutdated()
}
getDisplayName () {
return this.name
}
}