PeerTube/server/models/account/account.ts

447 lines
10 KiB
TypeScript
Raw Normal View History

2017-12-12 17:53:50 +01:00
import {
AllowNull,
BeforeDestroy,
BelongsTo,
Column,
2019-08-15 11:53:26 +02:00
CreatedAt,
DataType,
Default,
DefaultScope,
ForeignKey,
HasMany,
Is,
2019-03-05 11:30:43 +01:00
Model,
Scopes,
Table,
2017-12-12 17:53:50 +01:00
UpdatedAt
} from 'sequelize-typescript'
2019-02-26 10:55:40 +01:00
import { Account, AccountSummary } from '../../../shared/models/actors'
import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
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'
import { ServerModel } from '../server/server'
import { getSort, throwIfNotValid } from '../utils'
2017-12-12 17:53:50 +01:00
import { VideoChannelModel } from '../video/video-channel'
import { VideoCommentModel } from '../video/video-comment'
2017-12-12 17:53:50 +01:00
import { UserModel } from './user'
2019-02-26 10:55:40 +01:00
import { AvatarModel } from '../avatar/avatar'
import { VideoPlaylistModel } from '../video/video-playlist'
2019-12-27 13:33:16 +01:00
import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
2019-07-31 15:57:32 +02:00
import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
import { AccountBlocklistModel } from './account-blocklist'
import { ServerBlocklistModel } from '../server/server-blocklist'
2019-08-02 09:46:48 +02:00
import { ActorFollowModel } from '../activitypub/actor-follow'
2020-06-18 10:45:25 +02:00
import { MAccountActor, MAccountAP, MAccountDefault, MAccountFormattable, MAccountSummaryFormattable, MAccount } from '../../types/models'
2019-08-15 11:53:26 +02:00
import * as Bluebird from 'bluebird'
2020-02-04 11:26:51 +01:00
import { ModelCache } from '@server/models/model-cache'
import { VideoModel } from '../video/video'
2019-02-26 10:55:40 +01:00
export enum ScopeNames {
SUMMARY = 'SUMMARY'
}
2017-12-12 17:53:50 +01:00
2019-07-31 15:57:32 +02:00
export type SummaryOptions = {
2020-07-07 14:34:16 +02:00
actorRequired?: boolean // Default: true
2019-07-31 15:57:32 +02:00
whereActor?: WhereOptions
withAccountBlockerIds?: number[]
}
2019-04-23 09:50:57 +02:00
@DefaultScope(() => ({
2017-12-14 17:38:41 +01:00
include: [
2017-12-12 17:53:50 +01:00
{
2019-04-23 09:50:57 +02:00
model: ActorModel, // Default scope includes avatar and server
2018-08-23 17:58:39 +02:00
required: true
2017-11-09 17:51:58 +01:00
}
]
2019-04-23 09:50:57 +02:00
}))
@Scopes(() => ({
2020-01-31 16:56:52 +01:00
[ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
2019-07-31 15:57:32 +02:00
const whereActor = options.whereActor || undefined
const serverInclude: IncludeOptions = {
attributes: [ 'host' ],
model: ServerModel.unscoped(),
required: false
}
const query: FindOptions = {
2020-07-07 14:34:16 +02:00
attributes: [ 'id', 'name', 'actorId' ],
2019-02-26 10:55:40 +01:00
include: [
{
2019-05-31 14:02:26 +02:00
attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
2019-02-26 10:55:40 +01:00
model: ActorModel.unscoped(),
2020-07-07 14:34:16 +02:00
required: options.actorRequired ?? true,
2019-02-26 10:55:40 +01:00
where: whereActor,
include: [
2019-07-31 15:57:32 +02:00
serverInclude,
2019-02-26 10:55:40 +01:00
{
model: AvatarModel.unscoped(),
required: false
}
]
}
]
}
2019-07-31 15:57:32 +02:00
if (options.withAccountBlockerIds) {
query.include.push({
attributes: [ 'id' ],
model: AccountBlocklistModel.unscoped(),
as: 'BlockedAccounts',
required: false,
where: {
accountId: {
[Op.in]: options.withAccountBlockerIds
}
}
})
serverInclude.include = [
{
attributes: [ 'id' ],
model: ServerBlocklistModel.unscoped(),
required: false,
where: {
accountId: {
[Op.in]: options.withAccountBlockerIds
}
}
}
]
}
return query
2019-02-26 10:55:40 +01:00
}
2019-04-23 09:50:57 +02: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
@AllowNull(true)
@Default(null)
2019-04-18 11:28:17 +02:00
@Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true))
2019-05-13 14:37:30 +02:00
@Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max))
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'
})
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
2019-02-26 10:55:40 +01:00
@HasMany(() => VideoPlaylistModel, {
foreignKey: {
allowNull: false
},
onDelete: 'cascade',
hooks: true
})
VideoPlaylists: VideoPlaylistModel[]
@HasMany(() => VideoCommentModel, {
foreignKey: {
allowNull: true
},
onDelete: 'cascade',
hooks: true
})
VideoComments: VideoCommentModel[]
2019-07-31 15:57:32 +02:00
@HasMany(() => AccountBlocklistModel, {
foreignKey: {
name: 'targetAccountId',
allowNull: false
},
as: 'BlockedAccounts',
onDelete: 'CASCADE'
})
BlockedAccounts: AccountBlocklistModel[]
@BeforeDestroy
static async sendDeleteIfOwned (instance: AccountModel, options) {
if (!instance.Actor) {
instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
}
2019-08-02 09:46:48 +02:00
await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
if (instance.isOwned()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
return undefined
2017-11-09 17:51:58 +01:00
}
2019-08-15 11:53:26 +02:00
static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
2019-02-21 14:28:06 +01:00
return AccountModel.findByPk(id, { transaction })
2017-12-12 17:53:50 +01:00
}
2017-12-04 10:34:40 +01:00
2019-08-15 11:53:26 +02:00
static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
2019-02-21 14:06:10 +01:00
const [ accountName, host ] = nameWithHost.split('@')
2019-04-11 11:33:44 +02:00
if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
2019-02-21 14:06:10 +01:00
return AccountModel.loadByNameAndHost(accountName, host)
}
2019-08-15 11:53:26 +02:00
static loadLocalByName (name: string): Bluebird<MAccountDefault> {
2020-02-04 11:26:51 +01:00
const fun = () => {
const query = {
where: {
[Op.or]: [
{
userId: {
[Op.ne]: null
}
},
{
applicationId: {
[Op.ne]: null
}
2017-12-12 17:53:50 +01:00
}
2020-02-04 11:26:51 +01:00
]
},
include: [
2017-12-12 17:53:50 +01:00
{
2020-02-04 11:26:51 +01:00
model: ActorModel,
required: true,
where: {
preferredUsername: name
2017-12-12 17:53:50 +01:00
}
}
]
2020-02-04 11:26:51 +01:00
}
2018-02-21 16:44:18 +01:00
2020-02-04 11:26:51 +01:00
return AccountModel.findOne(query)
}
2019-12-27 13:33:16 +01:00
2020-02-04 11:26:51 +01:00
return ModelCache.Instance.doCache({
cacheType: 'local-account-name',
key: name,
fun,
// The server actor never change, so we can easily cache it
whitelist: () => name === SERVER_ACTOR_NAME
})
2018-02-21 16:44:18 +01:00
}
2019-08-15 11:53:26 +02:00
static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
2018-02-21 16:44:18 +01:00
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
2019-08-15 11:53:26 +02:00
static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
2017-12-12 17:53:50 +01:00
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)
.then(({ rows, count }) => {
return {
data: rows,
total: count
}
})
2018-01-03 16:38:50 +01:00
}
static loadAccountIdFromVideo (videoId: number): Bluebird<MAccount> {
const query = {
include: [
{
attributes: [ 'id', 'accountId' ],
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
attributes: [ 'id', 'channelId' ],
model: VideoModel.unscoped(),
where: {
id: videoId
}
}
]
}
]
}
return AccountModel.findOne(query)
}
2019-08-15 11:53:26 +02:00
static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
2018-12-05 17:27:24 +01:00
const query = {
attributes: [ ],
offset: 0,
order: getSort(sort),
include: [
{
attributes: [ 'preferredUsername', 'serverId' ],
model: ActorModel.unscoped(),
where: {
serverId: null
}
}
]
}
return AccountModel
.unscoped()
.findAll(query)
}
2020-07-01 16:05:30 +02:00
getClientUrl () {
return WEBSERVER.URL + '/accounts/' + this.Actor.getIdentifier()
}
2019-08-20 19:05:31 +02:00
toFormattedJSON (this: MAccountFormattable): 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,
displayName: this.getDisplayName(),
description: this.description,
2017-12-12 17:53:50 +01:00
createdAt: this.createdAt,
updatedAt: this.updatedAt,
userId: this.userId ? this.userId : undefined
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
2019-08-20 19:05:31 +02:00
toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
const actor = this.Actor.toFormattedSummaryJSON()
2019-02-26 10:55:40 +01:00
return {
id: this.id,
name: actor.name,
displayName: this.getDisplayName(),
url: actor.url,
host: actor.host,
avatar: actor.avatar
}
}
2019-08-21 14:31:57 +02:00
toActivityPubObject (this: MAccountAP) {
const obj = this.Actor.toActivityPubObject(this.name)
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
}
2019-01-14 11:30:15 +01:00
isOutdated () {
return this.Actor.isOutdated()
}
getDisplayName () {
return this.name
}
2019-07-31 15:57:32 +02:00
isBlocked () {
return this.BlockedAccounts && this.BlockedAccounts.length !== 0
}
}