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