Table,
UpdatedAt
} from 'sequelize-typescript'
-import { ActivityPubActorType } from '../../../shared/models/activitypub'
+import { ActivityIconObject, ActivityPubActorType } from '../../../shared/models/activitypub'
import { Avatar } from '../../../shared/models/avatars/avatar.model'
import { activityPubContextify } from '../../helpers/activitypub'
import {
MActorFull,
MActorHost,
MActorServer,
- MActorSummaryFormattable,
+ MActorSummaryFormattable, MActorUrl,
MActorWithInboxes
} from '../../typings/models'
import * as Bluebird from 'bluebird'
-import { Op, Transaction } from 'sequelize'
+import { Op, Transaction, literal } from 'sequelize'
+import { ModelCache } from '@server/models/model-cache'
enum ScopeNames {
FULL = 'FULL'
})
VideoChannel: VideoChannelModel
- private static cache: { [ id: string ]: any } = {}
-
static load (id: number): Bluebird<MActor> {
return ActorModel.unscoped().findByPk(id)
}
const query = {
where: {
followersUrl: {
- [ Op.in ]: followersUrls
+ [Op.in]: followersUrls
}
},
transaction
}
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.cache[preferredUsername]) {
- return Bluebird.resolve(ActorModel.cache[preferredUsername])
- }
+ 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.cache[ preferredUsername ] = 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> {
+ const fun = () => {
+ const query = {
+ attributes: [ 'url' ],
+ where: {
+ preferredUsername,
+ serverId: null
+ },
+ transaction
+ }
+
+ return ActorModel.unscoped()
+ .findOne(query)
+ }
- 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> {
return ActorModel.scope(ScopeNames.FULL).findOne(query)
}
- static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
- return ActorModel.increment(column, {
- by,
- where: {
- id
- }
- })
+ static rebuildFollowsCount (ofId: number, type: 'followers' | 'following', transaction?: Transaction) {
+ const sanitizedOfId = parseInt(ofId + '', 10)
+ const where = { id: sanitizedOfId }
+
+ let columnToUpdate: string
+ let columnOfCount: string
+
+ if (type === 'followers') {
+ columnToUpdate = 'followersCount'
+ columnOfCount = 'targetActorId'
+ } else {
+ columnToUpdate = 'followingCount'
+ columnOfCount = 'actorId'
+ }
+
+ return ActorModel.update({
+ [columnToUpdate]: literal(`(SELECT COUNT(*) FROM "actorFollow" WHERE "${columnOfCount}" = ${sanitizedOfId})`)
+ }, { where, transaction })
+ }
+
+ static loadAccountActorByVideoId (videoId: number): Bluebird<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) {
}
toActivityPubObject (this: MActorAP, name: string) {
- let icon = undefined
+ let icon: ActivityIconObject
+
if (this.avatarId) {
const extension = extname(this.Avatar.filename)
+
icon = {
type: 'Image',
mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',