BeforeDestroy,
BelongsTo,
Column,
- CreatedAt, DataType,
+ CreatedAt,
+ DataType,
Default,
DefaultScope,
ForeignKey,
import { UserModel } from './user'
import { AvatarModel } from '../avatar/avatar'
import { VideoPlaylistModel } from '../video/video-playlist'
-import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
+import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
import { AccountBlocklistModel } from './account-blocklist'
import { ServerBlocklistModel } from '../server/server-blocklist'
+import { ActorFollowModel } from '../activitypub/actor-follow'
+import { MAccountActor, MAccountDefault, MAccountSummaryFormattable, MAccountFormattable, MAccountAP } from '../../typings/models'
+import * as Bluebird from 'bluebird'
export enum ScopeNames {
SUMMARY = 'SUMMARY'
@HasMany(() => VideoCommentModel, {
foreignKey: {
- allowNull: false
+ allowNull: true
},
onDelete: 'cascade',
hooks: true
})
BlockedAccounts: AccountBlocklistModel[]
+ private static cache: { [ id: string ]: any } = {}
+
@BeforeDestroy
static async sendDeleteIfOwned (instance: AccountModel, options) {
if (!instance.Actor) {
- instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
+ instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
}
+ await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
if (instance.isOwned()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
return undefined
}
- static load (id: number, transaction?: Transaction) {
+ static load (id: number, transaction?: Transaction): Bluebird<MAccountDefault> {
return AccountModel.findByPk(id, { transaction })
}
- static loadByNameWithHost (nameWithHost: string) {
+ static loadByNameWithHost (nameWithHost: string): Bluebird<MAccountDefault> {
const [ accountName, host ] = nameWithHost.split('@')
if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName)
return AccountModel.loadByNameAndHost(accountName, host)
}
- static loadLocalByName (name: string) {
+ static loadLocalByName (name: string): Bluebird<MAccountDefault> {
+ // The server actor never change, so we can easily cache it
+ if (name === SERVER_ACTOR_NAME && AccountModel.cache[name]) {
+ return Bluebird.resolve(AccountModel.cache[name])
+ }
+
const query = {
where: {
[ Op.or ]: [
}
return AccountModel.findOne(query)
+ .then(account => {
+ if (name === SERVER_ACTOR_NAME) {
+ AccountModel.cache[name] = account
+ }
+
+ return account
+ })
}
- static loadByNameAndHost (name: string, host: string) {
+ static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {
const query = {
include: [
{
return AccountModel.findOne(query)
}
- static loadByUrl (url: string, transaction?: Transaction) {
+ static loadByUrl (url: string, transaction?: Transaction): Bluebird<MAccountDefault> {
const query = {
include: [
{
})
}
- static listLocalsForSitemap (sort: string) {
+ static listLocalsForSitemap (sort: string): Bluebird<MAccountActor[]> {
const query = {
attributes: [ ],
offset: 0,
.findAll(query)
}
- toFormattedJSON (): Account {
+ toFormattedJSON (this: MAccountFormattable): Account {
const actor = this.Actor.toFormattedJSON()
const account = {
id: this.id,
return Object.assign(actor, account)
}
- toFormattedSummaryJSON (): AccountSummary {
- const actor = this.Actor.toFormattedJSON()
+ toFormattedSummaryJSON (this: MAccountSummaryFormattable): AccountSummary {
+ const actor = this.Actor.toFormattedSummaryJSON()
return {
id: this.id,
}
}
- toActivityPubObject () {
- const obj = this.Actor.toActivityPubObject(this.name, 'Account')
+ toActivityPubObject (this: MAccountAP) {
+ const obj = this.Actor.toActivityPubObject(this.name)
return Object.assign(obj, {
summary: this.description