import { values } from 'lodash'
import { extname } from 'path'
+import { literal, Op, Transaction } from 'sequelize'
import {
AllowNull,
BelongsTo,
Table,
UpdatedAt
} from 'sequelize-typescript'
+import { ModelCache } from '@server/models/model-cache'
import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
import { Avatar } from '../../../shared/models/avatars/avatar.model'
import { activityPubContextify } from '../../helpers/activitypub'
} from '../../helpers/custom-validators/activitypub/actor'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
-import { AccountModel } from '../account/account'
-import { AvatarModel } from '../avatar/avatar'
-import { ServerModel } from '../server/server'
-import { isOutdated, throwIfNotValid } from '../utils'
-import { VideoChannelModel } from '../video/video-channel'
-import { ActorFollowModel } from './actor-follow'
-import { VideoModel } from '../video/video'
import {
MActor,
MActorAccountChannelId,
MActorFull,
MActorHost,
MActorServer,
- MActorSummaryFormattable, MActorUrl,
+ MActorSummaryFormattable,
+ MActorUrl,
MActorWithInboxes
-} from '../../typings/models'
-import * as Bluebird from 'bluebird'
-import { Op, Transaction, literal } from 'sequelize'
+} from '../../types/models'
+import { AccountModel } from '../account/account'
+import { AvatarModel } from '../avatar/avatar'
+import { ServerModel } from '../server/server'
+import { isOutdated, throwIfNotValid } from '../utils'
+import { VideoModel } from '../video/video'
+import { VideoChannelModel } from '../video/video-channel'
+import { ActorFollowModel } from './actor-follow'
enum ScopeNames {
FULL = 'FULL'
'sharedInboxUrl',
'followersUrl',
'followingUrl',
- 'url',
'createdAt',
'updatedAt'
]
}
}
},
- // {
- // fields: [ 'preferredUsername' ],
- // unique: true,
- // where: {
- // serverId: null
- // }
- // },
+ {
+ fields: [ 'preferredUsername' ],
+ unique: true,
+ where: {
+ serverId: null
+ }
+ },
{
fields: [ 'inboxUrl', 'sharedInboxUrl' ]
},
}
]
})
-export class ActorModel extends Model<ActorModel> {
+export class ActorModel extends Model {
@AllowNull(false)
@Column(DataType.ENUM(...values(ACTIVITY_PUB_ACTOR_TYPES)))
})
VideoChannel: VideoChannelModel
- private static localNameCache: { [ id: string ]: any } = {}
- private static localUrlCache: { [ id: string ]: any } = {}
-
- static load (id: number): Bluebird<MActor> {
+ static load (id: number): Promise<MActor> {
return ActorModel.unscoped().findByPk(id)
}
- static loadFull (id: number): Bluebird<MActorFull> {
+ static loadFull (id: number): Promise<MActorFull> {
return ActorModel.scope(ScopeNames.FULL).findByPk(id)
}
- static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Bluebird<MActor> {
+ static loadFromAccountByVideoId (videoId: number, transaction: Transaction): Promise<MActor> {
const query = {
include: [
{
.then(a => !!a)
}
- static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Bluebird<MActorFull[]> {
+ static listByFollowersUrls (followersUrls: string[], transaction?: Transaction): Promise<MActorFull[]> {
const query = {
where: {
followersUrl: {
return ActorModel.scope(ScopeNames.FULL).findAll(query)
}
- static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
- // The server actor never change, so we can easily cache it
- if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localNameCache[preferredUsername]) {
- return Bluebird.resolve(ActorModel.localNameCache[preferredUsername])
- }
+ static loadLocalByName (preferredUsername: string, transaction?: Transaction): Promise<MActorFull> {
+ const fun = () => {
+ const query = {
+ where: {
+ preferredUsername,
+ serverId: null
+ },
+ transaction
+ }
- const query = {
- where: {
- preferredUsername,
- serverId: null
- },
- transaction
+ return ActorModel.scope(ScopeNames.FULL)
+ .findOne(query)
}
- return ActorModel.scope(ScopeNames.FULL)
- .findOne(query)
- .then(actor => {
- if (preferredUsername === SERVER_ACTOR_NAME) {
- ActorModel.localNameCache[preferredUsername] = actor
- }
-
- return actor
- })
+ return ModelCache.Instance.doCache({
+ cacheType: 'local-actor-name',
+ key: preferredUsername,
+ // The server actor never change, so we can easily cache it
+ whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
+ fun
+ })
}
- static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
- // The server actor never change, so we can easily cache it
- if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localUrlCache[preferredUsername]) {
- return Bluebird.resolve(ActorModel.localUrlCache[preferredUsername])
- }
+ static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Promise<MActorUrl> {
+ const fun = () => {
+ const query = {
+ attributes: [ 'url' ],
+ where: {
+ preferredUsername,
+ serverId: null
+ },
+ transaction
+ }
- const query = {
- attributes: [ 'url' ],
- where: {
- preferredUsername,
- serverId: null
- },
- transaction
+ return ActorModel.unscoped()
+ .findOne(query)
}
- return ActorModel.unscoped()
- .findOne(query)
- .then(actor => {
- if (preferredUsername === SERVER_ACTOR_NAME) {
- ActorModel.localUrlCache[preferredUsername] = actor
- }
-
- return actor
- })
+ return ModelCache.Instance.doCache({
+ cacheType: 'local-actor-name',
+ key: preferredUsername,
+ // The server actor never change, so we can easily cache it
+ whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
+ fun
+ })
}
- static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {
+ static loadByNameAndHost (preferredUsername: string, host: string): Promise<MActorFull> {
const query = {
where: {
preferredUsername
return ActorModel.scope(ScopeNames.FULL).findOne(query)
}
- static loadByUrl (url: string, transaction?: Transaction): Bluebird<MActorAccountChannelId> {
+ static loadByUrl (url: string, transaction?: Transaction): Promise<MActorAccountChannelId> {
const query = {
where: {
url
return ActorModel.unscoped().findOne(query)
}
- static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Bluebird<MActorFull> {
+ static loadByUrlAndPopulateAccountAndChannel (url: string, transaction?: Transaction): Promise<MActorFull> {
const query = {
where: {
url
}, { where, transaction })
}
+ static loadAccountActorByVideoId (videoId: number): Promise<MActor> {
+ const query = {
+ include: [
+ {
+ attributes: [ 'id' ],
+ model: AccountModel.unscoped(),
+ required: true,
+ include: [
+ {
+ attributes: [ 'id', 'accountId' ],
+ model: VideoChannelModel.unscoped(),
+ required: true,
+ include: [
+ {
+ attributes: [ 'id', 'channelId' ],
+ model: VideoModel.unscoped(),
+ where: {
+ id: videoId
+ }
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+
+ return ActorModel.unscoped().findOne(query)
+ }
+
getSharedInbox (this: MActorWithInboxes) {
return this.sharedInboxUrl || this.inboxUrl
}