Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Fix actor table associations

Chocobozzzaf29d2a8afde
diff --git a/packages/tests/src/api/check-params/channels.ts b/packages/tests/src/api/check-params/channels.ts
index e7c0713b28..30cec6449d 100644
--- a/packages/tests/src/api/check-params/channels.ts
+++ b/packages/tests/src/api/check-params/channels.ts
@@ -383,6 +383,15 @@ describe('Test video channels API validator', function () {
it('Should fail to delete the last user video channel', async function () {
await command.delete({ channelName: 'root_channel', expectedStatus: HttpStatusCode.CONFLICT_409 })
})
+
+ it('Should fail to create a channel with the same name as the deleted one', async function () {
+ await command.create({
+ attributes: {
+ name: 'super_channel'
+ },
+ expectedStatus: HttpStatusCode.CONFLICT_409
+ })
+ })
})
after(async function () {
diff --git a/packages/tests/src/api/users/users-multiple-servers.ts b/packages/tests/src/api/users/users-multiple-servers.ts
index 83c973ad44..5c3812e351 100644
--- a/packages/tests/src/api/users/users-multiple-servers.ts
+++ b/packages/tests/src/api/users/users-multiple-servers.ts
@@ -182,14 +182,13 @@ describe('Test users with multiple servers', function () {
}
})
- // FIXME: test doesn't work
- // it('Should not have actor files', async () => {
- // for (const server of servers) {
- // for (const userAvatarFilename of userAvatarFilenames) {
- // await checkActorFilesWereRemoved(userAvatarFilename, server)
- // }
- // }
- // })
+ it('Should not have actor files', async () => {
+ for (const server of servers) {
+ for (const userAvatarFilename of userAvatarFilenames) {
+ await checkActorFilesWereRemoved(userAvatarFilename, server)
+ }
+ }
+ })
it('Should not have video files', async () => {
for (const server of servers) {
diff --git a/packages/tests/src/api/videos/videos-common-filters.ts b/packages/tests/src/api/videos/videos-common-filters.ts
index 10fe4e10c2..fb59d59d5e 100644
--- a/packages/tests/src/api/videos/videos-common-filters.ts
+++ b/packages/tests/src/api/videos/videos-common-filters.ts
@@ -532,6 +532,7 @@ describe('Test videos filter', function () {
it('Should filter already watched videos by the user', async function () {
const { id } = await servers[0].videos.upload({ attributes: { name: 'video for history' } })
+ await waitJobs(servers)
for (const path of [ ...paths, userVideosPath ]) {
const videos = await listVideos({ server: servers[0], path, isLocal: true, excludeAlreadyWatched: true })
diff --git a/packages/tests/src/client/head-tags.ts b/packages/tests/src/client/head-tags.ts
index 089c3c2044..228e0e8b5a 100644
--- a/packages/tests/src/client/head-tags.ts
+++ b/packages/tests/src/client/head-tags.ts
@@ -346,7 +346,9 @@ describe('Test <head> HTML tags', function () {
const feedUrl = `${servers[0].url}/feeds/podcast/videos.xml?videoChannelId=${servers[0].store.channel.id}`
expect(text).to.contain(
- `<link rel="alternate" type="application/rss+xml" title="${servers[0].store.channel.displayName} podcast feed" href="${feedUrl}" />`
+ `<link rel="alternate" type="application/rss+xml" title="${
+ servers[0].store.channel.displayName
+ } - Podcast feed" href="${feedUrl}" />`
)
}
diff --git a/server/core/controllers/api/accounts.ts b/server/core/controllers/api/accounts.ts
index 69cc6e3b41..d40d3f854b 100644
--- a/server/core/controllers/api/accounts.ts
+++ b/server/core/controllers/api/accounts.ts
@@ -274,7 +274,7 @@ async function listAccountFollowers (req: express.Request, res: express.Response
const account = res.locals.account
const channels = await VideoChannelModel.listAllOwnedByAccount(account.id)
- const actorIds = [ account.actorId ].concat(channels.map(c => c.actorId))
+ const actorIds = [ account.Actor.id ].concat(channels.map(c => c.Actor.id))
const resultList = await ActorFollowModel.listFollowersForApi({
actorIds,
diff --git a/server/core/controllers/api/video-channels/index.ts b/server/core/controllers/api/video-channels/index.ts
index 3ecbbfd46d..1a5b15ab4a 100644
--- a/server/core/controllers/api/video-channels/index.ts
+++ b/server/core/controllers/api/video-channels/index.ts
@@ -79,7 +79,12 @@ videoChannelRouter.get(
asyncMiddleware(listVideoChannels)
)
-videoChannelRouter.post('/', authenticate, asyncMiddleware(videoChannelsAddValidator), asyncRetryTransactionMiddleware(createVideoChannel))
+videoChannelRouter.post(
+ '/',
+ authenticate,
+ asyncMiddleware(videoChannelsAddValidator),
+ asyncRetryTransactionMiddleware(createVideoChannel)
+)
videoChannelRouter.put(
'/:handle',
@@ -207,7 +212,7 @@ async function createVideoChannel (req: express.Request, res: express.Response)
await JobQueue.Instance.createJob({
type: 'actor-keys',
- payload: { actorId: videoChannelCreated.actorId }
+ payload: { actorId: videoChannelCreated.Actor.id }
})
auditLogger.create(getAuditIdFromRes(res), new VideoChannelAuditView(videoChannelCreated.toFormattedJSON()))
@@ -417,7 +422,7 @@ async function listVideoChannelFollowers (req: express.Request, res: express.Res
const channel = res.locals.videoChannel
const resultList = await ActorFollowModel.listFollowersForApi({
- actorIds: [ channel.actorId ],
+ actorIds: [ channel.Actor.id ],
start: req.query.start,
count: req.query.count,
sort: req.query.sort,
diff --git a/server/core/initializers/constants.ts b/server/core/initializers/constants.ts
index d1a81530d1..07ba1f15ea 100644
--- a/server/core/initializers/constants.ts
+++ b/server/core/initializers/constants.ts
@@ -58,7 +58,7 @@ import { CONFIG, registerConfigChangedHandler } from './config.js'
// ---------------------------------------------------------------------------
-export const LAST_MIGRATION_VERSION = 950
+export const LAST_MIGRATION_VERSION = 960
// ---------------------------------------------------------------------------
diff --git a/server/core/initializers/database.ts b/server/core/initializers/database.ts
index c82ca411ea..9efb01731b 100644
--- a/server/core/initializers/database.ts
+++ b/server/core/initializers/database.ts
@@ -1,5 +1,6 @@
import { isTestOrDevInstance } from '@peertube/peertube-node-utils'
import { ActorCustomPageModel } from '@server/models/account/actor-custom-page.js'
+import { ActorReservedModel } from '@server/models/actor/actor-reserved.js'
import { UploadImageModel } from '@server/models/application/upload-image.js'
import { AccountAutomaticTagPolicyModel } from '@server/models/automatic-tag/account-automatic-tag-policy.js'
import { AutomaticTagModel } from '@server/models/automatic-tag/automatic-tag.js'
@@ -195,7 +196,8 @@ export async function initDatabaseModels (silent: boolean) {
UploadImageModel,
VideoLiveScheduleModel,
PlayerSettingModel,
- VideoChannelCollaboratorModel
+ VideoChannelCollaboratorModel,
+ ActorReservedModel
])
// Check extensions exist in the database
diff --git a/server/core/initializers/migrations/0955-actor-associations.ts b/server/core/initializers/migrations/0955-actor-associations.ts
new file mode 100644
index 0000000000..aadeb18e10
--- /dev/null
+++ b/server/core/initializers/migrations/0955-actor-associations.ts
@@ -0,0 +1,55 @@
+import * as Sequelize from 'sequelize'
+
+async function up (utils: {
+ transaction: Sequelize.Transaction
+ queryInterface: Sequelize.QueryInterface
+ sequelize: Sequelize.Sequelize
+}): Promise<void> {
+ {
+ const query = `
+CREATE TABLE IF NOT EXISTS "actorReserved" (
+ "id" SERIAL,
+ "preferredUsername" VARCHAR(255) NOT NULL,
+ "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL,
+ "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL,
+ PRIMARY KEY ("id")
+)`
+
+ await utils.sequelize.query(query, { transaction: utils.transaction })
+ }
+
+ {
+ const query = `ALTER TABLE "actor" ADD COLUMN IF NOT EXISTS "accountId" INTEGER ` +
+ `REFERENCES "account" ("id") ON DELETE CASCADE ON UPDATE CASCADE`
+ await utils.sequelize.query(query, { transaction: utils.transaction })
+ }
+
+ {
+ const query = `ALTER TABLE "actor" ADD COLUMN IF NOT EXISTS "videoChannelId" INTEGER ` +
+ `REFERENCES "videoChannel" ("id") ON DELETE CASCADE ON UPDATE CASCADE`
+ await utils.sequelize.query(query, { transaction: utils.transaction })
+ }
+
+ // ---------------------------------------------------------------------------
+
+ {
+ await utils.sequelize.query(
+ `
+ INSERT INTO "actorReserved" ("preferredUsername", "createdAt", "updatedAt")
+ SELECT "preferredUsername", NOW(), NOW()
+ FROM "actor"
+ WHERE "accountId" IS NULL AND "videoChannelId" IS NULL AND "serverId" IS NULL
+ `,
+ { transaction: utils.transaction }
+ )
+ }
+}
+
+function down (options) {
+ throw new Error('Not implemented.')
+}
+
+export {
+ down,
+ up
+}
diff --git a/server/core/initializers/migrations/0960-actor-associations-data.ts b/server/core/initializers/migrations/0960-actor-associations-data.ts
new file mode 100644
index 0000000000..25e81d6e3c
--- /dev/null
+++ b/server/core/initializers/migrations/0960-actor-associations-data.ts
@@ -0,0 +1,70 @@
+import * as Sequelize from 'sequelize'
+import { QueryTypes } from 'sequelize'
+
+async function up (utils: {
+ transaction: Sequelize.Transaction
+ queryInterface: Sequelize.QueryInterface
+ sequelize: Sequelize.Sequelize
+}): Promise<void> {
+ const batchSize = 1000
+
+ // ---------------------------------------------------------------------------
+ // Do not use transaction on purpose, to speed up chunk updates
+ // ---------------------------------------------------------------------------
+
+ // Fill "actor"."accountId"
+ while (true) {
+ const query = `
+WITH batch AS (
+ SELECT a."id" AS actor_id, ac."id" AS account_id
+ FROM "actor" a INNER JOIN "account" ac ON ac."actorId" = a."id"
+ WHERE a."accountId" IS DISTINCT FROM ac."id" ORDER BY a."id"
+ LIMIT ${batchSize}
+) UPDATE "actor" SET "accountId" = batch.account_id FROM batch WHERE "actor"."id" = batch.actor_id`
+
+ const [ , rowsUpdated ] = await utils.sequelize.query(query, {
+ type: QueryTypes.UPDATE,
+ replacements: { limit: batchSize }
+ })
+
+ if (rowsUpdated < batchSize) {
+ break
+ }
+ }
+
+ // Fill "actor"."videoChannelId"
+ while (true) {
+ const query = `
+WITH batch AS (
+ SELECT a."id" AS actor_id, vc."id" AS channel_id
+ FROM "actor" a INNER JOIN "videoChannel" vc ON vc."actorId" = a."id"
+ WHERE a."videoChannelId" IS DISTINCT FROM vc."id" ORDER BY a."id"
+ LIMIT ${batchSize}
+) UPDATE "actor" SET "videoChannelId" = batch.channel_id FROM batch WHERE "actor"."id" = batch.actor_id`
+
+ const [ , rowsUpdated ] = await utils.sequelize.query(query, {
+ type: QueryTypes.UPDATE,
+ replacements: { limit: batchSize }
+ })
+
+ if (rowsUpdated < batchSize) {
+ break
+ }
+ }
+
+ // ---------------------------------------------------------------------------
+
+ {
+ await utils.sequelize.query(`ALTER TABLE "account" DROP COLUMN "actorId"`, { transaction: utils.transaction })
+ await utils.sequelize.query(`ALTER TABLE "videoChannel" DROP COLUMN "actorId"`, { transaction: utils.transaction })
+ }
+}
+
+function down (options) {
+ throw new Error('Not implemented.')
+}
+
+export {
+ down,
+ up
+}
diff --git a/server/core/lib/activitypub/actors/get.ts b/server/core/lib/activitypub/actors/get.ts
index 579b08b001..c38625cdf5 100644
--- a/server/core/lib/activitypub/actors/get.ts
+++ b/server/core/lib/activitypub/actors/get.ts
@@ -38,7 +38,7 @@ async function getOrCreateAPActor (
updateCollections = false
): Promise<MActorFullActor | MActorAccountChannelId> {
const actorUrl = getAPId(activityActor)
- let actor = await loadActorFromDB(actorUrl, fetchType)
+ let actor = await loadActorByUrl(actorUrl, fetchType)
let created = false
let accountPlaylistsUrl: string
@@ -117,25 +117,13 @@ async function findOwner (rootUrl: string, attributedTo: APObjectId[] | APObject
// ---------------------------------------------------------------------------
export {
- getOrCreateAPOwner,
+ findOwner,
getOrCreateAPActor,
- findOwner
+ getOrCreateAPOwner
}
// ---------------------------------------------------------------------------
-async function loadActorFromDB (actorUrl: string, fetchType: ActorLoadByUrlType) {
- let actor = await loadActorByUrl(actorUrl, fetchType)
-
- // Orphan actor (not associated to an account of channel) so recreate it
- if (actor && (!actor.Account && !actor.VideoChannel)) {
- await actor.destroy()
- actor = null
- }
-
- return actor
-}
-
async function scheduleOutboxFetchIfNeeded (actor: MActor, created: boolean, refreshed: boolean, updateCollections: boolean) {
if ((created === true || refreshed === true) && updateCollections === true) {
const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' }
diff --git a/server/core/lib/activitypub/actors/shared/creator.ts b/server/core/lib/activitypub/actors/shared/creator.ts
index 2799663665..3946becc93 100644
--- a/server/core/lib/activitypub/actors/shared/creator.ts
+++ b/server/core/lib/activitypub/actors/shared/creator.ts
@@ -1,61 +1,76 @@
import { ActivityPubActor, ActorImageType, ActorImageType_Type } from '@peertube/peertube-models'
import { isAccountActor, isChannelActor } from '@server/helpers/actors.js'
+import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
import { AccountModel } from '@server/models/account/account.js'
import { ActorModel } from '@server/models/actor/actor.js'
import { ServerModel } from '@server/models/server/server.js'
import { VideoChannelModel } from '@server/models/video/video-channel.js'
-import {
- MAccount,
- MAccountDefault,
- MActor,
- MActorFullActor,
- MActorId,
- MActorImages,
- MChannel,
- MServer
-} from '@server/types/models/index.js'
-import { Op, Transaction } from 'sequelize'
+import { MAccount, MActor, MActorFullActor, MActorImages, MChannel, MServer } from '@server/types/models/index.js'
+import { Transaction } from 'sequelize'
import { upsertAPPlayerSettings } from '../../player-settings.js'
import { updateActorImages } from '../image.js'
import { getActorAttributesFromObject, getActorDisplayNameFromObject, getImagesInfoFromObject } from './object-to-model-attributes.js'
import { fetchActorFollowsCount } from './url-to-object.js'
export class APActorCreator {
+ private readonly lTags: LoggerTagsFn
+
constructor (
private readonly actorObject: ActivityPubActor,
private readonly ownerActor?: MActorFullActor
) {
+ this.lTags = loggerTagsFactory('ap', 'actor', 'create', this.actorObject.id)
}
async create (): Promise<MActorFullActor> {
+ logger.debug('Creating remote actor from object', { actorObject: this.actorObject, ...this.lTags() })
+
const { followersCount, followingCount } = await fetchActorFollowsCount(this.actorObject)
- const actorInstance = new ActorModel(getActorAttributesFromObject(this.actorObject, followersCount, followingCount))
+ const actorInstance = new ActorModel(getActorAttributesFromObject(this.actorObject, followersCount, followingCount)) as MActorFullActor
const actor = await sequelizeTypescript.transaction(async t => {
const server = await this.setServer(actorInstance, t)
- const { actorCreated, created } = await this.saveActor(actorInstance, t)
-
- await this.setImageIfNeeded(actorCreated, ActorImageType.AVATAR, t)
- await this.setImageIfNeeded(actorCreated, ActorImageType.BANNER, t)
+ // Remove
+ const existingActor = await ActorModel.loadByUniqueKeys({
+ preferredUsername: actorInstance.preferredUsername,
+ url: actorInstance.url,
+ serverId: actorInstance.serverId,
+ transaction: t
+ })
- await this.tryToFixActorUrlIfNeeded(actorCreated, actorInstance, created, t)
+ if (existingActor) {
+ logger.debug(`Actor ${existingActor.id} already exists, removing existing one before creating a new one`, this.lTags())
- if (isAccountActor(actorCreated.type)) {
- actorCreated.Account = await this.saveAccount(actorCreated, t) as MAccountDefault
- actorCreated.Account.Actor = actorCreated
+ if (existingActor.videoChannelId) {
+ const channel = await VideoChannelModel.loadAndPopulateAccount(existingActor.videoChannelId, t)
+ await channel.destroy({ transaction: t })
+ } else if (existingActor.accountId) {
+ const account = await AccountModel.findByPk(existingActor.accountId, { transaction: t })
+ if (account) await account.destroy({ transaction: t })
+ }
}
- if (isChannelActor(actorCreated.type)) {
- const channel = await this.saveVideoChannel(actorCreated, t)
- actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: this.ownerActor.Account })
+ if (isAccountActor(this.actorObject.type)) {
+ const account = await this.saveAccount(actorInstance, t)
+ await actorInstance.save({ transaction: t })
+
+ actorInstance.Account = Object.assign(account, { Actor: actorInstance })
+ } else if (isChannelActor(this.actorObject.type)) {
+ const channel = await this.saveVideoChannel(actorInstance, t)
+ await actorInstance.save({ transaction: t })
+
+ actorInstance.VideoChannel = Object.assign(channel, { Actor: actorInstance, Account: this.ownerActor.Account })
}
- actorCreated.Server = server
+ await this.setImageIfNeeded(actorInstance, ActorImageType.AVATAR, t)
+ await this.setImageIfNeeded(actorInstance, ActorImageType.BANNER, t)
- return actorCreated
+ actorInstance.Server = server
+
+ return actorInstance
})
if (isChannelActor(actor.type) && typeof this.actorObject.playerSettings === 'string') {
@@ -97,72 +112,26 @@ export class APActorCreator {
return updateActorImages(actor as MActorImages, type, imagesInfo, t)
}
- private async saveActor (actor: MActor, t: Transaction) {
- // Force the actor creation using findOrCreate() instead of save()
- // Sometimes Sequelize skips the save() when it thinks the instance already exists
- // (which could be false in a retried query)
- const [ actorCreated, created ] = await ActorModel.findOrCreate<MActorFullActor>({
- defaults: actor.toJSON(),
- where: {
- [Op.or]: [
- {
- url: actor.url
- },
- {
- serverId: actor.serverId,
- preferredUsername: actor.preferredUsername
- }
- ]
- },
- transaction: t
- })
-
- return { actorCreated, created }
- }
+ private async saveAccount (actor: MActor, t: Transaction) {
+ const accountCreated = await AccountModel.create({
+ name: getActorDisplayNameFromObject(this.actorObject),
+ description: this.actorObject.summary
+ }, { transaction: t })
- private async tryToFixActorUrlIfNeeded (actorCreated: MActor, newActor: MActor, created: boolean, t: Transaction) {
- // Try to fix non HTTPS accounts of remote instances that fixed their URL afterwards
- if (created !== true && actorCreated.url !== newActor.url) {
- // Only fix http://example.com/account/djidane to https://example.com/account/djidane
- if (actorCreated.url.replace(/^http:\/\//, '') !== newActor.url.replace(/^https:\/\//, '')) {
- throw new Error(`Actor from DB with URL ${actorCreated.url} does not correspond to actor ${newActor.url}`)
- }
-
- actorCreated.url = newActor.url
- await actorCreated.save({ transaction: t })
- }
- }
-
- private async saveAccount (actor: MActorId, t: Transaction) {
- const [ accountCreated ] = await AccountModel.findOrCreate({
- defaults: {
- name: getActorDisplayNameFromObject(this.actorObject),
- description: this.actorObject.summary,
- actorId: actor.id
- },
- where: {
- actorId: actor.id
- },
- transaction: t
- })
+ actor.accountId = accountCreated.id
return accountCreated as MAccount
}
- private async saveVideoChannel (actor: MActorId, t: Transaction) {
- const [ videoChannelCreated ] = await VideoChannelModel.findOrCreate({
- defaults: {
- name: getActorDisplayNameFromObject(this.actorObject),
- description: this.actorObject.summary,
- support: this.actorObject.support,
- actorId: actor.id,
- accountId: this.ownerActor.Account.id
- },
- where: {
- actorId: actor.id
- },
- transaction: t
- })
+ private async saveVideoChannel (actor: MActor, t: Transaction) {
+ const videoChannelCreated = await VideoChannelModel.create({
+ name: getActorDisplayNameFromObject(this.actorObject),
+ description: this.actorObject.summary,
+ support: this.actorObject.support,
+ accountId: this.ownerActor.Account.id
+ }, { transaction: t })
+
+ actor.videoChannelId = videoChannelCreated.id
return videoChannelCreated as MChannel
}
diff --git a/server/core/lib/activitypub/actors/updater.ts b/server/core/lib/activitypub/actors/updater.ts
index f62690f729..97524c5446 100644
--- a/server/core/lib/activitypub/actors/updater.ts
+++ b/server/core/lib/activitypub/actors/updater.ts
@@ -1,6 +1,7 @@
import { ActivityPubActor, ActorImageType } from '@peertube/peertube-models'
import { resetSequelizeInstance, runInReadCommittedTransaction } from '@server/helpers/database-utils.js'
import { logger } from '@server/helpers/logger.js'
+import { AccountModel } from '@server/models/account/account.js'
import { VideoChannelModel } from '@server/models/video/video-channel.js'
import { MAccount, MActor, MActorFull, MChannel } from '@server/types/models/index.js'
import { upsertAPPlayerSettings } from '../player-settings.js'
@@ -56,8 +57,17 @@ export class APActorUpdater {
})
await runInReadCommittedTransaction(async t => {
- await this.actor.save({ transaction: t })
await this.accountOrChannel.save({ transaction: t })
+
+ if (accountOrChannel instanceof VideoChannelModel) {
+ this.actor.videoChannelId = accountOrChannel.id
+ this.actor.accountId = null
+ } else if (accountOrChannel instanceof AccountModel) {
+ this.actor.accountId = accountOrChannel.id
+ this.actor.videoChannelId = null
+ }
+
+ await this.actor.save({ transaction: t })
})
// Update the following line to template string
diff --git a/server/core/lib/activitypub/send/http.ts b/server/core/lib/activitypub/send/http.ts
index 7ebb5a46bf..d47cced87d 100644
--- a/server/core/lib/activitypub/send/http.ts
+++ b/server/core/lib/activitypub/send/http.ts
@@ -1,23 +1,22 @@
import { ContextType } from '@peertube/peertube-models'
+import { signAndContextify } from '@server/helpers/activity-pub-utils.js'
+import { logger } from '@server/helpers/logger.js'
import { ACTIVITY_PUB, HTTP_SIGNATURE } from '@server/initializers/constants.js'
+import { buildDigestFromWorker, signJsonLDObjectFromWorker } from '@server/lib/worker/parent-process.js'
+import { ActorReservedModel } from '@server/models/actor/actor-reserved.js'
import { ActorModel } from '@server/models/actor/actor.js'
import { getServerActor } from '@server/models/application/application.js'
-import { MActor } from '@server/types/models/index.js'
import { getContextFilter } from '../context.js'
-import { buildDigestFromWorker, signJsonLDObjectFromWorker } from '@server/lib/worker/parent-process.js'
-import { signAndContextify } from '@server/helpers/activity-pub-utils.js'
-import { logger } from '@server/helpers/logger.js'
-type Payload <T> = { body: T, contextType: ContextType, signatureActorId?: number }
+type Payload<T> = { body: T, contextType: ContextType, signatureActorId?: number }
-export async function computeBody <T> (
+export async function computeBody<T> (
payload: Payload<T>
): Promise<T | T & { type: 'RsaSignature2017', creator: string, created: string }> {
let body = payload.body
if (payload.signatureActorId) {
- const actorSignature = await ActorModel.load(payload.signatureActorId)
- if (!actorSignature) throw new Error('Unknown signature actor id.')
+ const actorSignature = await loadActorForSignature(payload.signatureActorId)
try {
body = await signAndContextify({
@@ -47,23 +46,24 @@ export async function buildSignedRequestOptions (options: {
signatureActorId?: number
hasPayload: boolean
}) {
- let actor: MActor | null
-
- if (options.signatureActorId) {
- actor = await ActorModel.load(options.signatureActorId)
- if (!actor) throw new Error('Unknown signature actor id.')
- } else {
- // We need to sign the request, so use the server
- actor = await getServerActor()
- }
+ const actor = options.signatureActorId
+ ? await loadActorForSignature(options.signatureActorId)
+ : await getServerActor() // We need to sign the request, so use the server
return {
algorithm: HTTP_SIGNATURE.ALGORITHM,
authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
- keyId: actor.getPublicKeyUrl(),
+ keyId: ActorModel.getPublicKeyUrl(actor.url),
key: actor.privateKey,
headers: options.hasPayload
? HTTP_SIGNATURE.HEADERS_TO_SIGN_WITH_PAYLOAD
: HTTP_SIGNATURE.HEADERS_TO_SIGN_WITHOUT_PAYLOAD
}
}
+
+async function loadActorForSignature (actorId: number) {
+ const actor = await ActorModel.load(actorId) ?? await ActorReservedModel.loadByActorId(actorId)
+ if (!actor) throw new Error('Unknown signature actor id.')
+
+ return actor
+}
diff --git a/server/core/lib/activitypub/send/send-delete.ts b/server/core/lib/activitypub/send/send-delete.ts
index 8f05e1af61..f7202dd0bc 100644
--- a/server/core/lib/activitypub/send/send-delete.ts
+++ b/server/core/lib/activitypub/send/send-delete.ts
@@ -33,7 +33,7 @@ async function sendDeleteActor (byActor: ActorModel, transaction: Transaction) {
const url = getDeleteActivityPubUrl(byActor.url)
const activity = buildDeleteActivity(url, byActor.url, byActor)
- const actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, transaction)
+ const actorsInvolved = await VideoShareModel.listActorsWhoSharedVideosOf({ actorOwnerId: byActor.id, transaction })
// In case the actor did not have any videos
const serverActor = await getServerActor()
diff --git a/server/core/lib/activitypub/send/send-update.ts b/server/core/lib/activitypub/send/send-update.ts
index ebad8544c0..fa439bbfb9 100644
--- a/server/core/lib/activitypub/send/send-update.ts
+++ b/server/core/lib/activitypub/send/send-update.ts
@@ -193,10 +193,10 @@ async function getToFollowersOfForActor (accountOrChannel: MChannelDefault | MAc
let actorsInvolved: MActor[]
if (accountOrChannel instanceof AccountModel) {
// Actors that shared my videos are involved too
- actorsInvolved = await VideoShareModel.loadActorsWhoSharedVideosOf(accountOrChannel.Actor.id, transaction)
+ actorsInvolved = await VideoShareModel.listActorsWhoSharedVideosOf({ actorOwnerId: accountOrChannel.Actor.id, transaction })
} else {
// Actors that shared videos of my channel are involved too
- actorsInvolved = await VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, transaction)
+ actorsInvolved = await VideoShareModel.listActorsByVideoChannel({ channelId: accountOrChannel.id, transaction })
}
actorsInvolved.push(accountOrChannel.Actor)
diff --git a/server/core/lib/activitypub/share.ts b/server/core/lib/activitypub/share.ts
index 5edc2c7086..5a820c4471 100644
--- a/server/core/lib/activitypub/share.ts
+++ b/server/core/lib/activitypub/share.ts
@@ -1,3 +1,4 @@
+import { HttpStatusCode } from '@peertube/peertube-models'
import { getServerActor } from '@server/models/application/application.js'
import Bluebird from 'bluebird'
import { Transaction } from 'sequelize'
@@ -9,7 +10,6 @@ import { fetchAP, getAPId } from './activity.js'
import { getOrCreateAPActor } from './actors/index.js'
import { sendUndoAnnounce, sendVideoAnnounce } from './send/index.js'
import { checkUrlsSameHost, getLocalVideoAnnounceActivityPubUrl } from './url.js'
-import { HttpStatusCode } from '@peertube/peertube-models'
const lTags = loggerTagsFactory('share')
@@ -19,7 +19,10 @@ export async function changeVideoChannelShare (
t: Transaction
) {
logger.info(
- 'Updating video channel of video %s: %s -> %s.', video.uuid, oldVideoChannel.name, video.VideoChannel.name,
+ 'Updating video channel of video %s: %s -> %s.',
+ video.uuid,
+ oldVideoChannel.name,
+ video.VideoChannel.name,
lTags(video.uuid)
)
@@ -66,7 +69,7 @@ export async function shareByVideoChannel (video: MVideoAccountLight, t: Transac
const videoChannelShareUrl = getLocalVideoAnnounceActivityPubUrl(video.VideoChannel.Actor, video)
const [ videoChannelShare ] = await VideoShareModel.findOrCreate({
defaults: {
- actorId: video.VideoChannel.actorId,
+ actorId: video.VideoChannel.Actor.id,
videoId: video.id,
url: videoChannelShareUrl
},
@@ -105,8 +108,8 @@ async function addVideoShare (shareUrl: string, video: MVideoId) {
async function undoShareByVideoChannel (video: MVideo, oldVideoChannel: MChannelActorLight, t: Transaction) {
// Load old share
- const oldShare = await VideoShareModel.load(oldVideoChannel.actorId, video.id, t)
- if (!oldShare) return new Error('Cannot find old video channel share ' + oldVideoChannel.actorId + ' for video ' + video.id)
+ const oldShare = await VideoShareModel.load(oldVideoChannel.Actor.id, video.id, t)
+ if (!oldShare) return new Error(`Cannot find old video channel share ${oldVideoChannel.Actor.id} for video ${video.id}`)
await sendUndoAnnounce(oldVideoChannel.Actor, oldShare, video, t)
await oldShare.destroy({ transaction: t })
diff --git a/server/core/lib/job-queue/handlers/video-transcoding.ts b/server/core/lib/job-queue/handlers/video-transcoding.ts
index 449cd35529..8833e1d409 100644
--- a/server/core/lib/job-queue/handlers/video-transcoding.ts
+++ b/server/core/lib/job-queue/handlers/video-transcoding.ts
@@ -41,7 +41,7 @@ async function processVideoTranscoding (job: Job) {
return undefined
}
- const user = await UserModel.loadByChannelActorId(video.VideoChannel.actorId)
+ const user = await UserModel.loadByChannelActorId(video.VideoChannel.Actor.id)
const handler = handlers[payload.type]
diff --git a/server/core/lib/local-actor.ts b/server/core/lib/local-actor.ts
index c48127a2c7..9cad140399 100644
--- a/server/core/lib/local-actor.ts
+++ b/server/core/lib/local-actor.ts
@@ -1,9 +1,10 @@
+import { ActivityPubActorType, ActorImageType, ActorImageType_Type } from '@peertube/peertube-models'
+import { buildUUID, getLowercaseExtension } from '@peertube/peertube-node-utils'
+import { ActorReservedModel } from '@server/models/actor/actor-reserved.js'
+import { ActorModel } from '@server/models/actor/actor.js'
import { remove } from 'fs-extra/esm'
import { join } from 'path'
import { Transaction } from 'sequelize'
-import { ActivityPubActorType, ActorImageType, ActorImageType_Type } from '@peertube/peertube-models'
-import { ActorModel } from '@server/models/actor/actor.js'
-import { buildUUID, getLowercaseExtension } from '@peertube/peertube-node-utils'
import { retryTransactionWrapper } from '../helpers/database-utils.js'
import { CONFIG } from '../initializers/config.js'
import { ACTOR_IMAGES_SIZE, WEBSERVER } from '../initializers/constants.js'
@@ -54,26 +55,28 @@ export async function updateLocalActorImageFiles (options: {
const processedImages = await Promise.all(ACTOR_IMAGES_SIZE[type].map(processImageSize))
await remove(imagePhysicalFile.path)
- return retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => {
- const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({
- name: imageName,
- fileUrl: null,
- height: imageSize.height,
- width: imageSize.width,
- onDisk: true
- }))
-
- const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t)
- await updatedActor.save({ transaction: t })
+ return retryTransactionWrapper(() =>
+ sequelizeTypescript.transaction(async t => {
+ const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({
+ name: imageName,
+ fileUrl: null,
+ height: imageSize.height,
+ width: imageSize.width,
+ onDisk: true
+ }))
+
+ const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t)
+ await updatedActor.save({ transaction: t })
- if (sendActorUpdate) {
- await sendUpdateActor(accountOrChannel, t)
- }
+ if (sendActorUpdate) {
+ await sendUpdateActor(accountOrChannel, t)
+ }
- return type === ActorImageType.AVATAR
- ? updatedActor.Avatars
- : updatedActor.Banners
- }))
+ return type === ActorImageType.AVATAR
+ ? updatedActor.Avatars
+ : updatedActor.Banners
+ })
+ )
}
export async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType_Type) {
@@ -91,16 +94,20 @@ export async function deleteLocalActorImageFile (accountOrChannel: MAccountDefau
// ---------------------------------------------------------------------------
-export async function findAvailableLocalActorName (baseActorName: string, transaction?: Transaction) {
- let actor = await ActorModel.loadLocalByName(baseActorName, transaction)
- if (!actor) return baseActorName
+export async function findAvailableLocalActorName (baseName: string, transaction?: Transaction) {
+ let actor = await loadReservedActorName(baseName, transaction)
+ if (!actor) return baseName
for (let i = 1; i < 30; i++) {
- const name = `${baseActorName}-${i}`
+ const name = `${baseName}-${i}`
- actor = await ActorModel.loadLocalByName(name, transaction)
+ actor = await loadReservedActorName(name, transaction)
if (!actor) return name
}
throw new Error('Cannot find available actor local name (too much iterations).')
}
+
+export async function loadReservedActorName (baseName: string, transaction?: Transaction) {
+ return await ActorModel.loadLocalByName(baseName, transaction) ?? await ActorReservedModel.loadByName(baseName, transaction)
+}
diff --git a/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts b/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
index bad552c916..3e601e8667 100644
--- a/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
+++ b/server/core/lib/notifier/shared/abuse/abuse-state-change-for-reporter.ts
@@ -15,7 +15,7 @@ export class AbuseStateChangeForReporter extends AbstractNotification<MAbuseFull
const reporter = this.abuse.ReporterAccount
if (reporter.isLocal() !== true) return
- this.user = await UserModel.loadByAccountActorId(this.abuse.ReporterAccount.actorId)
+ this.user = await UserModel.loadByAccountActorId(this.abuse.ReporterAccount.Actor.id)
}
log () {
diff --git a/server/core/lib/notifier/shared/abuse/new-abuse-message-for-reporter.ts b/server/core/lib/notifier/shared/abuse/new-abuse-message-for-reporter.ts
index 7c598718bb..810eda9161 100644
--- a/server/core/lib/notifier/shared/abuse/new-abuse-message-for-reporter.ts
+++ b/server/core/lib/notifier/shared/abuse/new-abuse-message-for-reporter.ts
@@ -13,7 +13,7 @@ export class NewAbuseMessageForReporter extends AbstractNewAbuseMessage {
await this.loadMessageAccount()
- const reporter = await UserModel.loadByAccountActorId(this.abuse.ReporterAccount.actorId)
+ const reporter = await UserModel.loadByAccountActorId(this.abuse.ReporterAccount.Actor.id)
// Don't notify my own message
if (reporter.Account.id === this.message.accountId) return
diff --git a/server/core/lib/notifier/shared/video-publication/new-video-or-live-for-subscribers.ts b/server/core/lib/notifier/shared/video-publication/new-video-or-live-for-subscribers.ts
index db49787680..74a99c76a0 100644
--- a/server/core/lib/notifier/shared/video-publication/new-video-or-live-for-subscribers.ts
+++ b/server/core/lib/notifier/shared/video-publication/new-video-or-live-for-subscribers.ts
@@ -12,7 +12,7 @@ export class NewVideoOrLiveForSubscribers extends AbstractNotification<MVideoAcc
async prepare () {
// List all followers that are users
- this.users = await UserModel.listUserSubscribersOf(this.payload.VideoChannel.actorId)
+ this.users = await UserModel.listUserSubscribersOf(this.payload.VideoChannel.Actor.id)
}
log () {
diff --git a/server/core/lib/sync-channel.ts b/server/core/lib/sync-channel.ts
index fdd2963a30..6b6ba7da60 100644
--- a/server/core/lib/sync-channel.ts
+++ b/server/core/lib/sync-channel.ts
@@ -27,7 +27,7 @@ export async function synchronizeChannel (options: {
}
try {
- const user = await UserModel.loadByChannelActorId(channel.actorId)
+ const user = await UserModel.loadByChannelActorId(channel.Actor.id)
const youtubeDL = new YoutubeDLWrapper(
externalChannelUrl,
ServerConfigManager.Instance.getEnabledResolutions('vod'),
diff --git a/server/core/lib/user-import-export/exporters/followers-exporter.ts b/server/core/lib/user-import-export/exporters/followers-exporter.ts
index 02147266ad..9ff199086d 100644
--- a/server/core/lib/user-import-export/exporters/followers-exporter.ts
+++ b/server/core/lib/user-import-export/exporters/followers-exporter.ts
@@ -6,7 +6,7 @@ import { VideoChannelModel } from '@server/models/video/video-channel.js'
export class FollowersExporter extends AbstractUserExporter<FollowersExportJSON> {
async export () {
let followersJSON = this.formatFollowersJSON(
- await ActorFollowModel.listAcceptedFollowersForExport(this.user.Account.actorId),
+ await ActorFollowModel.listAcceptedFollowersForExport(this.user.Account.Actor.id),
this.user.Account.Actor.getFullIdentifier()
)
diff --git a/server/core/lib/user-import-export/exporters/following-exporter.ts b/server/core/lib/user-import-export/exporters/following-exporter.ts
index 0a04e2f741..871d617ba6 100644
--- a/server/core/lib/user-import-export/exporters/following-exporter.ts
+++ b/server/core/lib/user-import-export/exporters/following-exporter.ts
@@ -1,14 +1,13 @@
-import { AbstractUserExporter } from './abstract-user-exporter.js'
import { ActivityPubOrderedCollection, FollowingExportJSON } from '@peertube/peertube-models'
-import { ActorFollowModel } from '@server/models/actor/actor-follow.js'
-import { activityPubCollection } from '@server/lib/activitypub/collection.js'
import { activityPubContextify } from '@server/helpers/activity-pub-utils.js'
+import { activityPubCollection } from '@server/lib/activitypub/collection.js'
import { getContextFilter } from '@server/lib/activitypub/context.js'
+import { ActorFollowModel } from '@server/models/actor/actor-follow.js'
+import { AbstractUserExporter } from './abstract-user-exporter.js'
-export class FollowingExporter extends AbstractUserExporter <FollowingExportJSON> {
-
+export class FollowingExporter extends AbstractUserExporter<FollowingExportJSON> {
async export () {
- const following = await ActorFollowModel.listAcceptedFollowingForExport(this.user.Account.actorId)
+ const following = await ActorFollowModel.listAcceptedFollowingForExport(this.user.Account.Actor.id)
const followingJSON = this.formatFollowingJSON(following, this.user.Account.Actor.getFullIdentifier())
return {
@@ -45,5 +44,4 @@ export class FollowingExporter extends AbstractUserExporter <FollowingExportJSON
getContextFilter()
)
}
-
}
diff --git a/server/core/lib/user-import-export/importers/channels-importer.ts b/server/core/lib/user-import-export/importers/channels-importer.ts
index 8db2a528f6..98b016f408 100644
--- a/server/core/lib/user-import-export/importers/channels-importer.ts
+++ b/server/core/lib/user-import-export/importers/channels-importer.ts
@@ -57,7 +57,7 @@ export class ChannelsImporter extends AbstractUserImporter<ChannelExportJSON, Ch
await this.importPlayerSettings(videoChannelCreated, channelImportData)
- await JobQueue.Instance.createJob({ type: 'actor-keys', payload: { actorId: videoChannelCreated.actorId } })
+ await JobQueue.Instance.createJob({ type: 'actor-keys', payload: { actorId: videoChannelCreated.Actor.id } })
for (const type of [ ActorImageType.AVATAR, ActorImageType.BANNER ]) {
const relativePath = type === ActorImageType.AVATAR
diff --git a/server/core/lib/user.ts b/server/core/lib/user.ts
index 2cca429d23..09f533fa86 100644
--- a/server/core/lib/user.ts
+++ b/server/core/lib/user.ts
@@ -10,7 +10,6 @@ import {
import { logger } from '@server/helpers/logger.js'
import { CONFIG } from '@server/initializers/config.js'
import { UserModel } from '@server/models/user/user.js'
-import { MActorDefault } from '@server/types/models/actor/index.js'
import { Transaction } from 'sequelize'
import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants.js'
import { sequelizeTypescript } from '../initializers/database.js'
@@ -135,22 +134,19 @@ export async function createLocalAccountWithoutKeys (parameters: {
type?: ActivityPubActorType
}) {
const { name, displayName, userId, applicationId, t, type = 'Person' } = parameters
- const url = getLocalAccountActivityPubUrl(name)
-
- const actorInstance = buildActorInstance(type, url, name)
- const actorInstanceCreated: MActorDefault = await actorInstance.save({ transaction: t })
- const accountInstance = new AccountModel({
+ const account = await AccountModel.create({
name: displayName || name,
userId,
- applicationId,
- actorId: actorInstanceCreated.id
- })
+ applicationId
+ }, { transaction: t })
- const accountInstanceCreated: MAccountDefault = await accountInstance.save({ transaction: t })
- accountInstanceCreated.Actor = actorInstanceCreated
+ const url = getLocalAccountActivityPubUrl(name)
+ const actor = buildActorInstance(type, url, name)
+ actor.accountId = account.id
+ await actor.save({ transaction: t })
- return accountInstanceCreated
+ return Object.assign(account, { Actor: actor })
}
export async function createApplicationActor (applicationId: number) {
diff --git a/server/core/lib/video-channel.ts b/server/core/lib/video-channel.ts
index 33cc70c738..76ebc50e5a 100644
--- a/server/core/lib/video-channel.ts
+++ b/server/core/lib/video-channel.ts
@@ -8,28 +8,20 @@ import { federateVideoIfNeeded } from './activitypub/videos/index.js'
import { buildActorInstance } from './local-actor.js'
export async function createLocalVideoChannelWithoutKeys (body: VideoChannelCreate, account: MAccountId, t: Sequelize.Transaction) {
- const url = getLocalVideoChannelActivityPubUrl(body.name)
- const actorInstance = buildActorInstance('Group', url, body.name)
-
- const actorInstanceCreated = await actorInstance.save({ transaction: t })
-
- const videoChannelData = {
+ const channel = await VideoChannelModel.create({
name: body.displayName,
description: body.description,
support: body.support,
- accountId: account.id,
- actorId: actorInstanceCreated.id
- }
+ accountId: account.id
+ }, { transaction: t })
- const videoChannel = new VideoChannelModel(videoChannelData)
-
- const options = { transaction: t }
- const videoChannelCreated = await videoChannel.save(options)
-
- videoChannelCreated.Actor = actorInstanceCreated
+ const url = getLocalVideoChannelActivityPubUrl(body.name)
+ const actor = buildActorInstance('Group', url, body.name)
+ actor.videoChannelId = channel.id
+ await actor.save({ transaction: t })
// No need to send this empty video channel to followers
- return videoChannelCreated
+ return Object.assign(channel, { Actor: actor })
}
export async function federateAllVideosOfChannel (videoChannel: MChannelId) {
diff --git a/server/core/middlewares/validators/feeds.ts b/server/core/middlewares/validators/feeds.ts
index bff1e21faf..e55e62b97a 100644
--- a/server/core/middlewares/validators/feeds.ts
+++ b/server/core/middlewares/validators/feeds.ts
@@ -140,7 +140,7 @@ export const videoSubscriptionFeedsValidator = [
if (areValidationErrors(req, res)) return
if (!await doesAccountIdExist({ id: req.query.accountId, req, res, checkIsLocal: true, checkCanManage: false })) return
- if (!await doesUserFeedTokenCorrespond(res.locals.account.userId, req.query.token, res)) return
+ if (!await doesUserFeedTokenCorrespond({ id: res.locals.account.userId, token: req.query.token, req, res })) return
return next()
}
diff --git a/server/core/middlewares/validators/shared/users.ts b/server/core/middlewares/validators/shared/users.ts
index 02f1cbdccb..66827b51f7 100644
--- a/server/core/middlewares/validators/shared/users.ts
+++ b/server/core/middlewares/validators/shared/users.ts
@@ -1,7 +1,7 @@
import { forceNumber } from '@peertube/peertube-core-utils'
import { HttpStatusCode, UserRightType } from '@peertube/peertube-models'
+import { loadReservedActorName } from '@server/lib/local-actor.js'
import { getByEmailPermissive } from '@server/lib/user.js'
-import { ActorModel } from '@server/models/actor/actor.js'
import { UserModel } from '@server/models/user/user.js'
import { MAccountId, MUserAccountId, MUserDefault } from '@server/types/models/index.js'
import express from 'express'
@@ -35,23 +35,30 @@ export function checkUserPendingEmailExistPermissive (email: string, res: expres
)
}
-export async function checkUsernameOrEmailDoNotAlreadyExist (username: string, email: string, res: express.Response) {
+export async function checkUsernameOrEmailDoNotAlreadyExist (options: {
+ username: string
+ email: string
+ req: express.Request
+ res: express.Response
+}) {
+ const { username, email, req, res } = options
+
const existingUser = await UserModel.loadByUsernameOrEmailCaseInsensitive(username)
const existingEmail = await UserModel.loadByUsernameOrEmailCaseInsensitive(email)
if (existingUser.length > 0 || existingEmail.length > 0) {
res.fail({
status: HttpStatusCode.CONFLICT_409,
- message: 'User with this username or email already exists.'
+ message: req.t('User with this username or email already exists.')
})
return false
}
- const actor = await ActorModel.loadLocalByName(username)
+ const actor = await loadReservedActorName(username)
if (actor) {
res.fail({
status: HttpStatusCode.CONFLICT_409,
- message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
+ message: req.t('Another actor (account/channel) with this name on this instance already exists or has already existed.')
})
return false
}
@@ -59,13 +66,18 @@ export async function checkUsernameOrEmailDoNotAlreadyExist (username: string, e
return true
}
-export async function checkEmailDoesNotAlreadyExist (email: string, res: express.Response) {
+export async function checkEmailDoesNotAlreadyExist (options: {
+ email: string
+ req: express.Request
+ res: express.Response
+}) {
+ const { email, req, res } = options
const user = await UserModel.loadByEmailCaseInsensitive(email)
if (user.length !== 0) {
res.fail({
status: HttpStatusCode.CONFLICT_409,
- message: 'User with this email already exists.'
+ message: req.t('User with this email already exists.')
})
return false
}
@@ -78,10 +90,7 @@ export async function checkUserExist (finder: () => Promise<MUserDefault>, res:
if (!user) {
if (abortResponse === true) {
- res.fail({
- status: HttpStatusCode.NOT_FOUND_404,
- message: 'User not found'
- })
+ res.sendStatus(HttpStatusCode.NOT_FOUND_404)
}
return false
@@ -119,13 +128,19 @@ export function checkCanManageAccount (options: {
return false
}
-export async function doesUserFeedTokenCorrespond (id: number, token: string, res: express.Response) {
+export async function doesUserFeedTokenCorrespond (options: {
+ id: number
+ token: string
+ req: express.Request
+ res: express.Response
+}) {
+ const { id, token, req, res } = options
const user = await UserModel.loadByIdWithChannels(forceNumber(id))
if (token !== user.feedToken) {
res.fail({
status: HttpStatusCode.FORBIDDEN_403,
- message: 'User and token mismatch'
+ message: req.t('User and token mismatch')
})
return false
}
diff --git a/server/core/middlewares/validators/users/user-registrations.ts b/server/core/middlewares/validators/users/user-registrations.ts
index 530dfa19c2..61d1c12662 100644
--- a/server/core/middlewares/validators/users/user-registrations.ts
+++ b/server/core/middlewares/validators/users/user-registrations.ts
@@ -1,14 +1,14 @@
-import express from 'express'
-import { body, param, query, ValidationChain } from 'express-validator'
+import { HttpStatusCode, UserRegister, UserRegistrationRequest, UserRegistrationState } from '@peertube/peertube-models'
import { exists, isBooleanValid, isIdValid, toBooleanOrNull } from '@server/helpers/custom-validators/misc.js'
import { isRegistrationModerationResponseValid, isRegistrationReasonValid } from '@server/helpers/custom-validators/user-registration.js'
import { CONFIG } from '@server/initializers/config.js'
+import { loadReservedActorName } from '@server/lib/local-actor.js'
import { Hooks } from '@server/lib/plugins/hooks.js'
-import { HttpStatusCode, UserRegister, UserRegistrationRequest, UserRegistrationState } from '@peertube/peertube-models'
+import express from 'express'
+import { body, param, query, ValidationChain } from 'express-validator'
import { isUserDisplayNameValid, isUserPasswordValid, isUserUsernameValid } from '../../../helpers/custom-validators/users.js'
import { isVideoChannelDisplayNameValid, isVideoChannelUsernameValid } from '../../../helpers/custom-validators/video-channels.js'
import { isSignupAllowed, isSignupAllowedForCurrentIP, SignupMode } from '../../../lib/signup.js'
-import { ActorModel } from '../../../models/actor/actor.js'
import { areValidationErrors, checkUsernameOrEmailDoNotAlreadyExist } from '../shared/index.js'
import { checkRegistrationHandlesDoNotAlreadyExist, checkRegistrationIdExist } from './shared/user-registrations.js'
@@ -26,7 +26,7 @@ const usersRequestRegistrationValidator = [
if (CONFIG.SIGNUP.REQUIRES_APPROVAL !== true) {
return res.fail({
status: HttpStatusCode.BAD_REQUEST_400,
- message: 'Signup approval is not enabled on this instance'
+ message: req.t('Signup approval is not enabled on this instance')
})
}
@@ -50,7 +50,6 @@ function ensureUserRegistrationAllowedFactory (signupMode: SignupMode) {
const allowedResult = await Hooks.wrapPromiseFun(
isSignupAllowed,
allowedParams,
-
signupMode === 'direct-registration'
? 'filter:api.user.signup.allowed.result'
: 'filter:api.user.request-signup.allowed.result'
@@ -59,7 +58,7 @@ function ensureUserRegistrationAllowedFactory (signupMode: SignupMode) {
if (allowedResult.allowed === false) {
return res.fail({
status: HttpStatusCode.FORBIDDEN_403,
- message: allowedResult.errorMessage || 'User registration is not allowed'
+ message: allowedResult.errorMessage || req.t('User registration is not allowed')
})
}
@@ -74,7 +73,7 @@ const ensureUserRegistrationAllowedForIP = [
if (allowed === false) {
return res.fail({
status: HttpStatusCode.FORBIDDEN_403,
- message: 'You are not on a network authorized for registration.'
+ message: req.t('You are not on a network authorized for registration.')
})
}
@@ -103,7 +102,7 @@ const acceptOrRejectRegistrationValidator = [
if (res.locals.userRegistration.state !== UserRegistrationState.PENDING) {
return res.fail({
status: HttpStatusCode.CONFLICT_409,
- message: 'This registration is already accepted or rejected.'
+ message: req.t('This registration is already accepted or rejected.')
})
}
@@ -142,16 +141,13 @@ const listRegistrationsValidator = [
// ---------------------------------------------------------------------------
export {
- usersDirectRegistrationValidator,
- usersRequestRegistrationValidator,
-
+ acceptOrRejectRegistrationValidator,
ensureUserRegistrationAllowedFactory,
ensureUserRegistrationAllowedForIP,
-
getRegistrationValidator,
listRegistrationsValidator,
-
- acceptOrRejectRegistrationValidator
+ usersDirectRegistrationValidator,
+ usersRequestRegistrationValidator
}
// ---------------------------------------------------------------------------
@@ -182,22 +178,24 @@ function usersCommonRegistrationValidatorFactory (additionalValidationChain: Val
const body: UserRegister | UserRegistrationRequest = req.body
- if (!await checkUsernameOrEmailDoNotAlreadyExist(body.username, body.email, res)) return
+ if (!await checkUsernameOrEmailDoNotAlreadyExist({ username: body.username, email: body.email, req, res })) return
if (body.channel) {
if (!body.channel.name || !body.channel.displayName) {
- return res.fail({ message: 'Channel is optional but if you specify it, channel.name and channel.displayName are required.' })
+ return res.fail({
+ message: req.t('Channel is optional but if you specify it, channel.name and channel.displayName are required.')
+ })
}
if (body.channel.name === body.username) {
- return res.fail({ message: 'Channel name cannot be the same as user username.' })
+ return res.fail({ message: req.t('Channel name cannot be the same as user username.') })
}
- const existing = await ActorModel.loadLocalByName(body.channel.name)
+ const existing = await loadReservedActorName(body.channel.name)
if (existing) {
return res.fail({
status: HttpStatusCode.CONFLICT_409,
- message: `Channel with name ${body.channel.name} already exists.`
+ message: req.t(`Channel with name {name} already exists.`, { name: body.channel.name })
})
}
}
diff --git a/server/core/middlewares/validators/users/users.ts b/server/core/middlewares/validators/users/users.ts
index 2a5575673a..91be9ffb2b 100644
--- a/server/core/middlewares/validators/users/users.ts
+++ b/server/core/middlewares/validators/users/users.ts
@@ -2,6 +2,7 @@ import { arrayify, forceNumber } from '@peertube/peertube-core-utils'
import { HttpStatusCode, ServerErrorCode, UserRole, UserUpdateMe } from '@peertube/peertube-models'
import { isStringArray } from '@server/helpers/custom-validators/search.js'
import { isNSFWFlagsValid } from '@server/helpers/custom-validators/videos.js'
+import { loadReservedActorName } from '@server/lib/local-actor.js'
import { Hooks } from '@server/lib/plugins/hooks.js'
import { MUser } from '@server/types/models/user/user.js'
import express from 'express'
@@ -33,7 +34,6 @@ import { isVideoChannelUsernameValid } from '../../../helpers/custom-validators/
import { logger } from '../../../helpers/logger.js'
import { isThemeRegistered } from '../../../lib/plugins/theme-utils.js'
import { Redis } from '../../../lib/redis.js'
-import { ActorModel } from '../../../models/actor/actor.js'
import {
areValidationErrors,
checkEmailDoesNotAlreadyExist,
@@ -89,7 +89,7 @@ export const usersAddValidator = [
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res, { omitBodyLog: true })) return
- if (!await checkUsernameOrEmailDoNotAlreadyExist(req.body.username, req.body.email, res)) return
+ if (!await checkUsernameOrEmailDoNotAlreadyExist({ username: req.body.username, email: req.body.email, req, res })) return
const authUser = res.locals.oauth.token.User
if (authUser.role !== UserRole.ADMINISTRATOR && req.body.role !== UserRole.USER) {
@@ -104,7 +104,7 @@ export const usersAddValidator = [
return res.fail({ message: 'Channel name cannot be the same as user username.' })
}
- const existing = await ActorModel.loadLocalByName(req.body.channelName)
+ const existing = await loadReservedActorName(req.body.channelName)
if (existing) {
return res.fail({
status: HttpStatusCode.CONFLICT_409,
@@ -209,7 +209,11 @@ export const usersUpdateValidator = [
if (!checkCanModerate(user, res)) return
- if (req.body.email && req.body.email !== user.email && !await checkEmailDoesNotAlreadyExist(req.body.email, res)) return
+ if (
+ req.body.email &&
+ req.body.email !== user.email &&
+ !await checkEmailDoesNotAlreadyExist({ email: req.body.email, req, res })
+ ) return
return next()
}
@@ -323,7 +327,11 @@ export const usersUpdateMeValidator = [
if (areValidationErrors(req, res, { omitBodyLog: true })) return
- if (body.email && body.email !== user.email && !await checkEmailDoesNotAlreadyExist(body.email, res)) return
+ if (
+ body.email &&
+ body.email !== user.email &&
+ !await checkEmailDoesNotAlreadyExist({ email: body.email, req, res })
+ ) return
return next()
}
diff --git a/server/core/middlewares/validators/videos/video-channels.ts b/server/core/middlewares/validators/videos/video-channels.ts
index 787de8023a..7c7425cbe0 100644
--- a/server/core/middlewares/validators/videos/video-channels.ts
+++ b/server/core/middlewares/validators/videos/video-channels.ts
@@ -1,6 +1,7 @@
import { HttpStatusCode, VideosImportInChannelCreate } from '@peertube/peertube-models'
import { isUrlValid } from '@server/helpers/custom-validators/activitypub/misc.js'
import { CONFIG } from '@server/initializers/config.js'
+import { loadReservedActorName } from '@server/lib/local-actor.js'
import { MChannelAccountDefault } from '@server/types/models/index.js'
import express from 'express'
import { body, param, query } from 'express-validator'
@@ -11,7 +12,6 @@ import {
isVideoChannelSupportValid,
isVideoChannelUsernameValid
} from '../../../helpers/custom-validators/video-channels.js'
-import { ActorModel } from '../../../models/actor/actor.js'
import { VideoChannelModel } from '../../../models/video/video-channel.js'
import { areValidationErrors, checkUserQuota, doesChannelHandleExist } from '../shared/index.js'
import { doesVideoChannelSyncIdExist } from '../shared/video-channel-syncs.js'
@@ -31,7 +31,7 @@ export const videoChannelsAddValidator = [
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
- const actor = await ActorModel.loadLocalByName(req.body.name)
+ const actor = await loadReservedActorName(req.body.name)
if (actor) {
res.fail({
status: HttpStatusCode.CONFLICT_409,
diff --git a/server/core/middlewares/validators/videos/videos.ts b/server/core/middlewares/validators/videos/videos.ts
index 059b363585..c4ee185b89 100644
--- a/server/core/middlewares/validators/videos/videos.ts
+++ b/server/core/middlewares/validators/videos/videos.ts
@@ -11,7 +11,6 @@ import { isHostValid } from '@server/helpers/custom-validators/servers.js'
import { VideoLoadType } from '@server/lib/model-loaders/video.js'
import { Redis } from '@server/lib/redis.js'
import { buildUploadXFile, safeUploadXCleanup } from '@server/lib/uploadx.js'
-import { getServerActor } from '@server/models/application/application.js'
import { ExpressPromiseHandler } from '@server/types/express-handler.js'
import { MUserAccountId, MVideoFullLight } from '@server/types/models/index.js'
import express from 'express'
@@ -285,8 +284,7 @@ export async function checkVideoFollowConstraints (req: express.Request, res: ex
if (CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true) return next()
// Check our instance follows an actor that shared this video
- const serverActor = await getServerActor()
- if (await VideoModel.checkVideoHasInstanceFollow(video.id, serverActor.id) === true) return next()
+ if (await VideoModel.checkVideoHasInstanceFollow(video.id) === true) return next()
return res.fail({
status: HttpStatusCode.FORBIDDEN_403,
diff --git a/server/core/models/account/account-blocklist.ts b/server/core/models/account/account-blocklist.ts
index 88a0e81b4c..d268d29e21 100644
--- a/server/core/models/account/account-blocklist.ts
+++ b/server/core/models/account/account-blocklist.ts
@@ -220,7 +220,7 @@ export class AccountBlocklistModel extends SequelizeModel<AccountBlocklistModel>
const rawQuery = `SELECT "accountBlocklist"."accountId", "actor"."preferredUsername" AS "name", "server"."host" ` +
`FROM "accountBlocklist" ` +
`INNER JOIN "account" ON "account"."id" = "accountBlocklist"."targetAccountId" ` +
- `INNER JOIN "actor" ON "actor"."id" = "account"."actorId" ` +
+ `INNER JOIN "actor" ON "actor"."accountId" = "account"."id" ` +
`LEFT JOIN "server" ON "server"."id" = "actor"."serverId" ` +
`WHERE "accountBlocklist"."accountId" IN (${createSafeIn(AccountBlocklistModel.sequelize, byAccountIds)}) ` +
`AND (${handlesWhere.join(' OR ')})`
diff --git a/server/core/models/account/account-video-rate.ts b/server/core/models/account/account-video-rate.ts
index 9cccd2dcc8..527cc78722 100644
--- a/server/core/models/account/account-video-rate.ts
+++ b/server/core/models/account/account-video-rate.ts
@@ -211,7 +211,7 @@ export class AccountVideoRateModel extends SequelizeModel<AccountVideoRateModel>
static listRemoteRateUrlsOfLocalVideos () {
const query = `SELECT "accountVideoRate".url FROM "accountVideoRate" ` +
`INNER JOIN account ON account.id = "accountVideoRate"."accountId" ` +
- `INNER JOIN actor ON actor.id = account."actorId" AND actor."serverId" IS NOT NULL ` +
+ `INNER JOIN actor ON "actor"."accountId" = account."id" AND actor."serverId" IS NOT NULL ` +
`INNER JOIN video ON video.id = "accountVideoRate"."videoId" AND video.remote IS FALSE`
return AccountVideoRateModel.sequelize.query<{ url: string }>(query, {
@@ -231,12 +231,12 @@ export class AccountVideoRateModel extends SequelizeModel<AccountVideoRateModel>
transaction: t,
include: [
{
- attributes: [ 'actorId' ],
+ attributes: [ 'id' ],
model: AccountModel.unscoped(),
required: true,
include: [
{
- attributes: [ 'url' ],
+ attributes: [ 'url', 'accountId' ],
model: ActorModel.unscoped(),
required: true
}
diff --git a/server/core/models/account/account.ts b/server/core/models/account/account.ts
index cec288545c..b1f7b05cb2 100644
--- a/server/core/models/account/account.ts
+++ b/server/core/models/account/account.ts
@@ -3,7 +3,6 @@ import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { ModelCache } from '@server/models/shared/model-cache.js'
import { FindOptions, IncludeOptions, Includeable, Op, Transaction, WhereOptions, literal } from 'sequelize'
import {
- AfterDestroy,
AllowNull,
BeforeDestroy,
BelongsTo,
@@ -14,6 +13,7 @@ import {
DefaultScope,
ForeignKey,
HasMany,
+ HasOne,
Is,
Scopes,
Table,
@@ -32,7 +32,6 @@ import {
MAccountSummaryFormattable,
MChannelIdHost
} from '../../types/models/index.js'
-import { ActorFollowModel } from '../actor/actor-follow.js'
import { ActorImageModel } from '../actor/actor-image.js'
import { ActorModel, actorSummaryAttributes } from '../actor/actor.js'
import { ApplicationModel } from '../application/application.js'
@@ -54,7 +53,7 @@ export enum ScopeNames {
SUMMARY = 'SUMMARY'
}
-const accountSummaryAttributes = [ 'id', 'name', 'actorId' ] as const satisfies (keyof AttributesOnly<AccountModel>)[]
+const accountSummaryAttributes = [ 'id', 'name' ] as const satisfies (keyof AttributesOnly<AccountModel>)[]
export type SummaryOptions = {
actorRequired?: boolean // Default: true
@@ -140,10 +139,6 @@ export type SummaryOptions = {
@Table({
tableName: 'account',
indexes: [
- {
- fields: [ 'actorId' ],
- unique: true
- },
{
fields: [ 'applicationId' ]
},
@@ -169,18 +164,6 @@ export class AccountModel extends SequelizeModel<AccountModel> {
@UpdatedAt
declare updatedAt: Date
- @ForeignKey(() => ActorModel)
- @Column
- declare actorId: number
-
- @BelongsTo(() => ActorModel, {
- foreignKey: {
- allowNull: false
- },
- onDelete: 'cascade'
- })
- declare Actor: Awaited<ActorModel>
-
@ForeignKey(() => UserModel)
@Column
declare userId: number
@@ -269,14 +252,21 @@ export class AccountModel extends SequelizeModel<AccountModel> {
})
declare VideoChannelCollaborators: Awaited<VideoChannelCollaboratorModel>[]
+ @HasOne(() => ActorModel, {
+ foreignKey: {
+ allowNull: true
+ },
+ hooks: true,
+ onDelete: 'cascade'
+ })
+ declare Actor: Awaited<ActorModel>
+
@BeforeDestroy
static async sendDeleteIfOwned (instance: AccountModel, options) {
if (!instance.Actor) {
instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
}
- await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
-
if (instance.isLocal()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
@@ -284,18 +274,6 @@ export class AccountModel extends SequelizeModel<AccountModel> {
return undefined
}
- @AfterDestroy
- static async deleteActorIfRemote (instance: AccountModel, options) {
- if (!instance.Actor) {
- instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
- }
-
- // Remote actor, delete it
- if (instance.Actor.serverId) {
- await instance.Actor.destroy({ transaction: options.transaction })
- }
- }
-
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
@@ -407,18 +385,7 @@ export class AccountModel extends SequelizeModel<AccountModel> {
return AccountModel.findOne(query)
}
- static listForApi (start: number, count: number, sort: string) {
- const query = {
- offset: start,
- limit: count,
- order: getSort(sort)
- }
-
- return Promise.all([
- AccountModel.count(),
- AccountModel.findAll(query)
- ]).then(([ total, data ]) => ({ total, data }))
- }
+ // ---------------------------------------------------------------------------
static loadAccountIdFromVideo (videoId: number): Promise<MAccount> {
const query = {
@@ -443,6 +410,21 @@ export class AccountModel extends SequelizeModel<AccountModel> {
return AccountModel.findOne(query)
}
+ // ---------------------------------------------------------------------------
+
+ static listForApi (start: number, count: number, sort: string) {
+ const query = {
+ offset: start,
+ limit: count,
+ order: getSort(sort)
+ }
+
+ return Promise.all([
+ AccountModel.count(),
+ AccountModel.findAll(query)
+ ]).then(([ total, data ]) => ({ total, data }))
+ }
+
static listLocalsForSitemap (sort: string): Promise<MAccountHost[]> {
return AccountModel.unscoped().findAll({
attributes: [],
@@ -470,6 +452,8 @@ export class AccountModel extends SequelizeModel<AccountModel> {
})
}
+ // ---------------------------------------------------------------------------
+
toFormattedJSON (this: MAccountFormattable): Account {
return {
...this.Actor.toFormattedJSON(false),
diff --git a/server/core/models/actor/actor-follow.ts b/server/core/models/actor/actor-follow.ts
index 9922a01577..5a623ca54d 100644
--- a/server/core/models/actor/actor-follow.ts
+++ b/server/core/models/actor/actor-follow.ts
@@ -215,24 +215,6 @@ export class ActorFollowModel extends SequelizeModel<ActorFollowModel> {
return [ actorFollow, created ]
}
- static removeFollowsOf (actorId: number, t?: Transaction) {
- const query = {
- where: {
- [Op.or]: [
- {
- actorId
- },
- {
- targetActorId: actorId
- }
- ]
- },
- transaction: t
- }
-
- return ActorFollowModel.destroy(query)
- }
-
// Remove actor follows with a score of 0 (too many requests where they were unreachable)
static async removeBadActorFollows () {
const actorFollows = await ActorFollowModel.listBadActorFollows()
@@ -659,7 +641,7 @@ export class ActorFollowModel extends SequelizeModel<ActorFollowModel> {
'WHERE id IN (' +
'SELECT "actorFollow"."id" FROM "actorFollow" ' +
'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."targetActorId" ' +
- `WHERE "actorFollow"."actorId" = ${me.Account.actorId} ` + // I'm the follower
+ `WHERE "actorFollow"."actorId" = ${me.Account.Actor.id} ` + // I'm the follower
`AND "actor"."serverId" IN (${serverIdsString})` + // Criteria on followings
')'
diff --git a/server/core/models/actor/actor-image.ts b/server/core/models/actor/actor-image.ts
index 49d87070a5..e1c0cb70a6 100644
--- a/server/core/models/actor/actor-image.ts
+++ b/server/core/models/actor/actor-image.ts
@@ -3,7 +3,7 @@ import { getLowercaseExtension } from '@peertube/peertube-node-utils'
import { MActorId, MActorImage, MActorImageFormattable, MActorImagePath } from '@server/types/models/index.js'
import { remove } from 'fs-extra/esm'
import { join } from 'path'
-import { Op } from 'sequelize'
+import { Op, Transaction } from 'sequelize'
import { AfterDestroy, AllowNull, BelongsTo, Column, CreatedAt, Default, ForeignKey, Table, UpdatedAt } from 'sequelize-typescript'
import { logger } from '../../helpers/logger.js'
import { CONFIG } from '../../initializers/config.js'
@@ -100,19 +100,20 @@ export class ActorImageModel extends SequelizeModel<ActorImageModel> {
return ActorImageModel.findOne(query)
}
- static listByActor (actor: MActorId, type: ActorImageType_Type) {
+ static listByActor (actor: MActorId, type: ActorImageType_Type, transaction?: Transaction) {
const query = {
where: {
actorId: actor.id,
type
- }
+ },
+ transaction
}
return ActorImageModel.findAll(query)
}
- static async listActorImages (actor: MActorId) {
- const promises = [ ActorImageType.AVATAR, ActorImageType.BANNER ].map(type => ActorImageModel.listByActor(actor, type))
+ static async listActorImages (actor: MActorId, transaction?: Transaction) {
+ const promises = [ ActorImageType.AVATAR, ActorImageType.BANNER ].map(type => ActorImageModel.listByActor(actor, type, transaction))
const [ avatars, banners ] = await Promise.all(promises)
diff --git a/server/core/models/actor/actor-reserved.ts b/server/core/models/actor/actor-reserved.ts
new file mode 100644
index 0000000000..87ea72463f
--- /dev/null
+++ b/server/core/models/actor/actor-reserved.ts
@@ -0,0 +1,67 @@
+import { CONSTRAINTS_FIELDS } from '@server/initializers/constants.js'
+import { col, fn, Transaction, where } from 'sequelize'
+import { AllowNull, Column, DataType, Table, UpdatedAt } from 'sequelize-typescript'
+import { SequelizeModel } from '../shared/index.js'
+
+// ---------------------------------------------------------------------------
+// For example deleted actors
+// Usernames we want to reserve to prevent reuse that would break federation
+// ---------------------------------------------------------------------------
+
+@Table({
+ tableName: 'actorReserved',
+ indexes: [
+ {
+ fields: [ fn('lower', col('preferredUsername')) ],
+ name: 'actor_reserved_preferred_username_lower',
+ unique: true
+ }
+ ]
+})
+export class ActorReservedModel extends SequelizeModel<ActorReservedModel> {
+ @AllowNull(false)
+ @Column
+ declare preferredUsername: string
+
+ @AllowNull(false)
+ @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
+ declare publicKey: string
+
+ @AllowNull(false)
+ @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
+ declare privateKey: string
+
+ @AllowNull(false)
+ @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
+ declare url: string
+
+ @AllowNull(false)
+ @Column
+ actorId: number
+
+ @AllowNull(true)
+ @Column
+ accountId: number
+
+ @AllowNull(true)
+ @Column
+ videoChannelId: number
+
+ @UpdatedAt
+ declare updatedAt: Date
+
+ static loadByActorId (actorId: number) {
+ return this.findOne({
+ where: { actorId }
+ })
+ }
+
+ static loadByName (preferredUsername: string, transaction?: Transaction) {
+ return this.findOne({
+ where: [
+ where(fn('lower', col('preferredUsername')), preferredUsername.toLowerCase())
+ ],
+ transaction
+ })
+ }
+}
diff --git a/server/core/models/actor/actor.ts b/server/core/models/actor/actor.ts
index 8726c52613..172a6c0fd6 100644
--- a/server/core/models/actor/actor.ts
+++ b/server/core/models/actor/actor.ts
@@ -7,6 +7,7 @@ import { ModelCache } from '@server/models/shared/model-cache.js'
import { Op, QueryTypes, Transaction, col, fn, literal, where } from 'sequelize'
import {
AllowNull,
+ BeforeDestroy,
BelongsTo,
Column,
CreatedAt,
@@ -14,7 +15,6 @@ import {
DefaultScope,
ForeignKey,
HasMany,
- HasOne,
Is,
Scopes,
Table,
@@ -54,12 +54,15 @@ import { VideoChannelModel } from '../video/video-channel.js'
import { VideoModel } from '../video/video.js'
import { ActorFollowModel } from './actor-follow.js'
import { ActorImageModel } from './actor-image.js'
+import { ActorReservedModel } from './actor-reserved.js'
export const actorSummaryAttributes = [
'id',
'preferredUsername',
'url',
- 'serverId'
+ 'serverId',
+ 'accountId',
+ 'videoChannelId'
] as const satisfies (keyof AttributesOnly<ActorModel>)[]
enum ScopeNames {
@@ -159,6 +162,14 @@ export const unusedActorAttributesForAPI: (keyof AttributesOnly<ActorModel>)[] =
},
{
fields: [ 'followersUrl' ]
+ },
+ {
+ fields: [ 'accountId' ],
+ unique: true
+ },
+ {
+ fields: [ 'videoChannelId' ],
+ unique: true
}
]
})
@@ -300,23 +311,48 @@ export class ActorModel extends SequelizeModel<ActorModel> {
})
declare Server: Awaited<ServerModel>
- @HasOne(() => AccountModel, {
+ @ForeignKey(() => VideoChannelModel)
+ @Column
+ declare videoChannelId: number
+
+ @BelongsTo(() => VideoChannelModel, {
foreignKey: {
allowNull: true
},
- onDelete: 'cascade',
- hooks: true
+ onDelete: 'cascade'
})
- declare Account: Awaited<AccountModel>
+ declare VideoChannel: Awaited<VideoChannelModel>
+
+ @ForeignKey(() => AccountModel)
+ @Column
+ declare accountId: number
- @HasOne(() => VideoChannelModel, {
+ @BelongsTo(() => AccountModel, {
foreignKey: {
allowNull: true
},
- onDelete: 'cascade',
- hooks: true
+ onDelete: 'cascade'
})
- declare VideoChannel: Awaited<VideoChannelModel>
+ declare Account: Awaited<AccountModel>
+
+ // ---------------------------------------------------------------------------
+
+ @BeforeDestroy
+ static async reserveActor (instance: ActorModel, options) {
+ if (instance.isLocal()) {
+ await ActorReservedModel.create({
+ preferredUsername: instance.preferredUsername,
+ url: instance.url,
+ publicKey: instance.publicKey,
+ privateKey: instance.privateKey,
+ actorId: instance.id,
+ accountId: instance.accountId,
+ videoChannelId: instance.videoChannelId
+ }, { transaction: options.transaction })
+ }
+
+ return undefined
+ }
// ---------------------------------------------------------------------------
@@ -362,14 +398,14 @@ export class ActorModel extends SequelizeModel<ActorModel> {
return ActorModel.unscoped().findByPk(id, { transaction })
}
- static loadFull (id: number): Promise<MActorFull> {
- return ActorModel.scope(ScopeNames.FULL).findByPk(id)
+ static loadFull (id: number, transaction?: Transaction): Promise<MActorFull> {
+ return ActorModel.scope(ScopeNames.FULL).findByPk(id, { transaction })
}
static loadAccountActorFollowerUrlByVideoId (videoId: number, transaction: Transaction) {
const query = `SELECT "actor"."id" AS "id", "actor"."followersUrl" AS "followersUrl" ` +
`FROM "actor" ` +
- `INNER JOIN "account" ON "actor"."id" = "account"."actorId" ` +
+ `INNER JOIN "account" ON "actor"."accountId" = "account"."id" ` +
`INNER JOIN "videoChannel" ON "videoChannel"."accountId" = "account"."id" ` +
`INNER JOIN "video" ON "video"."channelId" = "videoChannel"."id" AND "video"."id" = :videoId`
@@ -505,6 +541,30 @@ export class ActorModel extends SequelizeModel<ActorModel> {
return ActorModel.scope(ScopeNames.FULL).findOne(query)
}
+ static loadByUniqueKeys (options: {
+ preferredUsername: string
+ serverId: number
+ url: string
+ transaction: Transaction
+ }) {
+ const { preferredUsername, serverId, url, transaction } = options
+
+ return ActorModel.findOne({
+ where: {
+ [Op.or]: [
+ {
+ url
+ },
+ {
+ serverId,
+ preferredUsername
+ }
+ ]
+ },
+ transaction
+ })
+ }
+
// ---------------------------------------------------------------------------
static async recalculateFollowsCount (options: {
@@ -581,6 +641,14 @@ export class ActorModel extends SequelizeModel<ActorModel> {
return ActorModel.unscoped().findOne(query)
}
+ // ---------------------------------------------------------------------------
+
+ static getPublicKeyUrl (url: string) {
+ return url + '#main-key'
+ }
+
+ // ---------------------------------------------------------------------------
+
getSharedInbox (this: MActorWithInboxes) {
return this.sharedInboxUrl || this.inboxUrl
}
@@ -642,7 +710,7 @@ export class ActorModel extends SequelizeModel<ActorModel> {
sharedInbox: this.sharedInboxUrl
},
publicKey: {
- id: this.getPublicKeyUrl(),
+ id: ActorModel.getPublicKeyUrl(this.url),
owner: this.url,
publicKeyPem: this.publicKey
},
@@ -690,10 +758,6 @@ export class ActorModel extends SequelizeModel<ActorModel> {
return this.url + '/playlists'
}
- getPublicKeyUrl () {
- return this.url + '#main-key'
- }
-
isLocal () {
return this.serverId === null
}
diff --git a/server/core/models/application/application.ts b/server/core/models/application/application.ts
index 722de70646..2778664bfd 100644
--- a/server/core/models/application/application.ts
+++ b/server/core/models/application/application.ts
@@ -17,7 +17,7 @@ export const getServerActor = memoizee(async function () {
actor.Avatars = avatars
actor.Banners = banners
- const uploadImages = await UploadImageModel.listByActor(actor)
+ const uploadImages = await UploadImageModel.listByActor(actor, undefined)
actor.UploadImages = uploadImages
return actor
diff --git a/server/core/models/application/upload-image.ts b/server/core/models/application/upload-image.ts
index 241eeb8a9c..78897ebd69 100644
--- a/server/core/models/application/upload-image.ts
+++ b/server/core/models/application/upload-image.ts
@@ -75,11 +75,12 @@ export class UploadImageModel extends SequelizeModel<UploadImageModel> {
.catch(err => logger.error('Cannot remove upload image file %s.', instance.filename, { err }))
}
- static listByActor (actor: MActorId) {
+ static listByActor (actor: MActorId, transaction: Transaction) {
const query = {
where: {
actorId: actor.id
- }
+ },
+ transaction
}
return UploadImageModel.findAll(query)
diff --git a/server/core/models/shared/sql/actor-helpers.ts b/server/core/models/shared/sql/actor-helpers.ts
index 2b9449e7db..600bb53c3b 100644
--- a/server/core/models/shared/sql/actor-helpers.ts
+++ b/server/core/models/shared/sql/actor-helpers.ts
@@ -4,9 +4,10 @@ export function getActorJoin (options: {
base?: string
on: string
required: boolean
+ type: 'account' | 'channel'
includeAvatars?: boolean // default false
}) {
- const { base = '', on, includeAvatars = false, required } = options
+ const { base = '', on, includeAvatars = false, type, required } = options
const avatarsJoin = includeAvatars
? getAvatarsJoin({ base: `${base}Actor`, on: `"${base}Actor"."id"` })
@@ -16,7 +17,11 @@ export function getActorJoin (options: {
? 'INNER JOIN'
: 'LEFT JOIN'
- return ` ${join} "actor" "${base}Actor" ON "${base}Actor"."id" = ${on} ` +
+ const column = type === 'account'
+ ? 'accountId'
+ : 'videoChannelId'
+
+ return ` ${join} "actor" "${base}Actor" ON "${base}Actor"."${column}" = ${on} ` +
`LEFT JOIN "server" "${base}Actor->Server" ` +
` ON "${base}Actor->Server"."id" = "${base}Actor"."serverId" ` +
avatarsJoin
@@ -43,7 +48,13 @@ export function getChannelJoin (options: {
: ''
const actorJoin = includeActors
- ? getActorJoin({ base: `${base}VideoChannel->`, on: `"${base}VideoChannel"."actorId"`, includeAvatars, required })
+ ? getActorJoin({
+ base: `${base}VideoChannel->`,
+ on: `"${base}VideoChannel"."id"`,
+ type: 'channel',
+ includeAvatars,
+ required
+ })
: ''
const join = required
@@ -65,7 +76,13 @@ export function getAccountJoin (options: {
const { base = '', on, includeAvatars, includeActor, required } = options
const actorJoin = includeActor
- ? getActorJoin({ base: `${base}Account->`, on: `"${base}Account"."actorId"`, includeAvatars, required })
+ ? getActorJoin({
+ base: `${base}Account->`,
+ on: `"${base}Account"."id"`,
+ type: 'account',
+ includeAvatars,
+ required
+ })
: ''
return ` LEFT JOIN "account" "${base}Account" ON "${base}Account"."id" = ${on} ` +
diff --git a/server/core/models/shared/sql/common-helpers.ts b/server/core/models/shared/sql/common-helpers.ts
index 8b3ce52e65..a7e05c50d8 100644
--- a/server/core/models/shared/sql/common-helpers.ts
+++ b/server/core/models/shared/sql/common-helpers.ts
@@ -6,7 +6,7 @@ import { Literal } from 'sequelize/types/utils'
// FIXME: have to specify the result type to not break peertube typings generation
export function buildLocalAccountIdsIn (): Literal {
return literal(
- '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
+ '(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."accountId" = "account"."id" AND "actor"."serverId" IS NULL)'
)
}
@@ -22,7 +22,7 @@ export function buildBlockedAccountSQL (blockerIds: number[]) {
return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
' UNION ' +
- 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
+ 'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."id" = actor."accountId" ' +
'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
}
diff --git a/server/core/models/user/sql/user-notification/user-notification-list-query-builder.ts b/server/core/models/user/sql/user-notification/user-notification-list-query-builder.ts
index e2c0d1ce9f..b0a169f7a5 100644
--- a/server/core/models/user/sql/user-notification/user-notification-list-query-builder.ts
+++ b/server/core/models/user/sql/user-notification/user-notification-list-query-builder.ts
@@ -173,7 +173,7 @@ export class UserNotificationListQueryBuilder extends AbstractListQuery {
ON "Abuse->VideoCommentAbuse->VideoComment"."videoId" = "Abuse->VideoCommentAbuse->VideoComment->Video"."id"
LEFT JOIN (
"account" AS "Abuse->FlaggedAccount"
- ${this.getActorJoin('Abuse->FlaggedAccount')}
+ ${this.getActorJoin('Abuse->FlaggedAccount', 'accountId')}
) ON "Abuse"."flaggedAccountId" = "Abuse->FlaggedAccount"."id"
LEFT JOIN (
@@ -192,21 +192,21 @@ export class UserNotificationListQueryBuilder extends AbstractListQuery {
"actorFollow" AS "ActorFollow"
INNER JOIN "actor" AS "ActorFollow->ActorFollower" ON "ActorFollow"."actorId" = "ActorFollow->ActorFollower"."id"
INNER JOIN "account" AS "ActorFollow->ActorFollower->Account"
- ON "ActorFollow->ActorFollower"."id" = "ActorFollow->ActorFollower->Account"."actorId"
+ ON "ActorFollow->ActorFollower"."accountId" = "ActorFollow->ActorFollower->Account"."id"
${this.getActorImageJoin('ActorFollow->ActorFollower')}
${this.getActorServerJoin('ActorFollow->ActorFollower')}
INNER JOIN "actor" AS "ActorFollow->ActorFollowing" ON "ActorFollow"."targetActorId" = "ActorFollow->ActorFollowing"."id"
LEFT JOIN "videoChannel" AS "ActorFollow->ActorFollowing->VideoChannel"
- ON "ActorFollow->ActorFollowing"."id" = "ActorFollow->ActorFollowing->VideoChannel"."actorId"
+ ON "ActorFollow->ActorFollowing"."videoChannelId" = "ActorFollow->ActorFollowing->VideoChannel"."id"
LEFT JOIN "account" AS "ActorFollow->ActorFollowing->Account"
- ON "ActorFollow->ActorFollowing"."id" = "ActorFollow->ActorFollowing->Account"."actorId"
+ ON "ActorFollow->ActorFollowing"."accountId" = "ActorFollow->ActorFollowing->Account"."id"
${this.getActorServerJoin('ActorFollow->ActorFollowing')}
) ON "UserNotificationModel"."actorFollowId" = "ActorFollow"."id"
LEFT JOIN (
"account" AS "Account"
- ${this.getActorJoin('Account')}
+ ${this.getActorJoin('Account', 'accountId')}
) ON "UserNotificationModel"."accountId" = "Account"."id"
LEFT JOIN "userRegistration" as "UserRegistration" ON "UserNotificationModel"."userRegistrationId" = "UserRegistration"."id"
@@ -243,17 +243,17 @@ export class UserNotificationListQueryBuilder extends AbstractListQuery {
private getAccountJoin (tableName: string, columnJoin: string) {
return `INNER JOIN "account" AS "${tableName}->Account" ON "${tableName}"."${columnJoin}" = "${tableName}->Account"."id" ` +
- this.getActorJoin(`${tableName}->Account`)
+ this.getActorJoin(`${tableName}->Account`, 'accountId')
}
private getChannelJoin (tableName: string, columnJoin: string, aliasTableName = 'VideoChannel') {
// eslint-disable-next-line max-len
return `INNER JOIN "videoChannel" AS "${tableName}->${aliasTableName}" ON "${tableName}"."${columnJoin}" = "${tableName}->${aliasTableName}".id ` +
- this.getActorJoin(`${tableName}->${aliasTableName}`)
+ this.getActorJoin(`${tableName}->${aliasTableName}`, 'videoChannelId')
}
- private getActorJoin (tableName: string) {
- return `INNER JOIN "actor" AS "${tableName}->Actor" ON "${tableName}"."actorId" = "${tableName}->Actor"."id" ` +
+ private getActorJoin (tableName: string, column: string) {
+ return `INNER JOIN "actor" AS "${tableName}->Actor" ON "${tableName}"."id" = "${tableName}->Actor"."${column}" ` +
this.getActorImageJoin(`${tableName}->Actor`) +
this.getActorServerJoin(`${tableName}->Actor`)
}
diff --git a/server/core/models/user/sql/user/user-list-query-builder.ts b/server/core/models/user/sql/user/user-list-query-builder.ts
index 371e4ee46b..7abe4e2ce4 100644
--- a/server/core/models/user/sql/user/user-list-query-builder.ts
+++ b/server/core/models/user/sql/user/user-list-query-builder.ts
@@ -41,7 +41,7 @@ export class UserListQueryBuilder extends AbstractListQuery {
if (this.builtAccountJoin) return
this.join += ' INNER JOIN "account" "Account" ON "Account"."userId" = "UserModel"."id" ' +
- 'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."id" = "Account"."actorId" ' +
+ 'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."accountId" = "Account"."id" ' +
'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" ' +
'LEFT JOIN "actorImage" "Account->Actor->Avatars" ON "Account->Actor->Avatars"."actorId" = "Account->Actor"."id" ' +
` AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
@@ -53,7 +53,8 @@ export class UserListQueryBuilder extends AbstractListQuery {
if (this.builtChannelsJoin) return
this.join += ' LEFT JOIN "videoChannel" "Account->VideoChannels" ON "Account"."id" = "Account->VideoChannels"."accountId" ' +
- 'LEFT JOIN "actor" "Account->VideoChannels->Actor" ON "Account->VideoChannels->Actor"."id" = "Account->VideoChannels"."actorId" ' +
+ 'LEFT JOIN "actor" "Account->VideoChannels->Actor" ' +
+ ' ON "Account->VideoChannels->Actor"."videoChannelId" = "Account->VideoChannels"."id" ' +
'LEFT JOIN "server" "Account->VideoChannels->Actor->Server" ' +
' ON "Account->VideoChannels->Actor->Server"."id" = "Account->VideoChannels->Actor"."serverId" ' +
'LEFT JOIN "actorImage" "Account->VideoChannels->Actor->Avatars" ' +
@@ -74,7 +75,7 @@ export class UserListQueryBuilder extends AbstractListQuery {
'LEFT JOIN (' +
' "videoChannel" "Account->Collabs->Channel" ' +
' INNER JOIN "actor" AS "Account->Collabs->Channel->Actor" ' +
- ' ON "Account->Collabs->Channel"."actorId" = "Account->Collabs->Channel->Actor"."id"' +
+ ' ON "Account->Collabs->Channel"."id" = "Account->Collabs->Channel->Actor"."videoChannelId" ' +
' LEFT JOIN "server" AS "Account->Collabs->Channel->Actor->Server" ' +
' ON "Account->Collabs->Channel->Actor"."serverId" = "Account->Collabs->Channel->Actor->Server"."id"' +
' LEFT JOIN "actorImage" AS "Account->Collabs->Channel->Actor->Avatars" ' +
diff --git a/server/core/models/user/user-notification.ts b/server/core/models/user/user-notification.ts
index 1264fe7fad..1ae00f8b51 100644
--- a/server/core/models/user/user-notification.ts
+++ b/server/core/models/user/user-notification.ts
@@ -380,7 +380,7 @@ export class UserNotificationModel extends SequelizeModel<UserNotificationModel>
buildAccountWhereQuery(
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "account" ON "userNotification"."accountId" = "account"."id" ` +
- `INNER JOIN actor ON "actor"."id" = "account"."actorId" `
+ `INNER JOIN actor ON "actor"."accountId" = "account"."id" `
),
// Remove notifications from muted accounts that followed ours
@@ -388,7 +388,7 @@ export class UserNotificationModel extends SequelizeModel<UserNotificationModel>
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
`INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
- `INNER JOIN account ON account."actorId" = actor.id `
+ `INNER JOIN account ON account."id" = actor."accountId" `
),
// Remove notifications from muted accounts that commented something
@@ -396,7 +396,7 @@ export class UserNotificationModel extends SequelizeModel<UserNotificationModel>
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
`INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
- `INNER JOIN account ON account."actorId" = actor.id `
+ `INNER JOIN account ON account."id" = actor."accountId" `
),
// Remove notifications of comments from muted accounts
@@ -404,7 +404,7 @@ export class UserNotificationModel extends SequelizeModel<UserNotificationModel>
`SELECT "userNotification"."id" FROM "userNotification" ` +
`INNER JOIN "videoComment" ON "videoComment".id = "userNotification"."commentId" ` +
`INNER JOIN account ON account.id = "videoComment"."accountId" ` +
- `INNER JOIN actor ON "actor"."id" = "account"."actorId" `
+ `INNER JOIN actor ON "actor"."accountId" = "account"."id" `
),
// Remove notifications from muted accounts that invited us to collaborate to a channel
@@ -413,7 +413,7 @@ export class UserNotificationModel extends SequelizeModel<UserNotificationModel>
`INNER JOIN "videoChannelCollaborator" ON "videoChannelCollaborator".id = "userNotification"."channelCollaboratorId" ` +
`INNER JOIN "videoChannel" ON "videoChannel".id = "videoChannelCollaborator"."channelId" ` +
`INNER JOIN "account" ON "videoChannel"."accountId" = "account"."id" ` +
- `INNER JOIN actor ON "actor"."id" = "account"."actorId" `
+ `INNER JOIN actor ON "actor"."accountId" = "account"."id" `
)
]
diff --git a/server/core/models/user/user.ts b/server/core/models/user/user.ts
index ad23315bf2..26e1176d36 100644
--- a/server/core/models/user/user.ts
+++ b/server/core/models/user/user.ts
@@ -715,12 +715,12 @@ export class UserModel extends SequelizeModel<UserModel> {
include: [
{
required: true,
- attributes: [ 'id' ],
+ attributes: [],
model: VideoChannelModel.unscoped(),
include: [
{
required: true,
- attributes: [ 'id' ],
+ attributes: [],
model: VideoModel.unscoped(),
where: {
id: videoId
@@ -754,7 +754,7 @@ export class UserModel extends SequelizeModel<UserModel> {
}
static loadByChannelActorId (videoChannelActorId: number): Promise<MUserDefault> {
- const query = {
+ return UserModel.findOne({
include: [
{
required: true,
@@ -763,18 +763,23 @@ export class UserModel extends SequelizeModel<UserModel> {
include: [
{
required: true,
- attributes: [ 'id' ],
+ attributes: [],
model: VideoChannelModel.unscoped(),
- where: {
- actorId: videoChannelActorId
- }
+ include: [
+ {
+ model: ActorModel.unscoped(),
+ required: true,
+ attributes: [],
+ where: {
+ id: videoChannelActorId
+ }
+ }
+ ]
}
]
}
]
- }
-
- return UserModel.findOne(query)
+ })
}
static loadByAccountId (accountId: number): Promise<MUserDefault> {
@@ -794,20 +799,25 @@ export class UserModel extends SequelizeModel<UserModel> {
}
static loadByAccountActorId (accountActorId: number): Promise<MUserDefault> {
- const query = {
+ return UserModel.findOne({
include: [
{
required: true,
attributes: [ 'id' ],
model: AccountModel.unscoped(),
- where: {
- actorId: accountActorId
- }
+ include: [
+ {
+ model: ActorModel.unscoped(),
+ required: true,
+ attributes: [ 'id' ],
+ where: {
+ id: accountActorId
+ }
+ }
+ ]
}
]
- }
-
- return UserModel.findOne(query)
+ })
}
static loadByLiveId (liveId: number): Promise<MUser> {
@@ -819,12 +829,12 @@ export class UserModel extends SequelizeModel<UserModel> {
required: true,
include: [
{
- attributes: [ 'id' ],
+ attributes: [],
model: VideoChannelModel.unscoped(),
required: true,
include: [
{
- attributes: [ 'id' ],
+ attributes: [],
model: VideoModel.unscoped(),
required: true,
include: [
diff --git a/server/core/models/video/sql/channel/video-channel-list-query-builder.ts b/server/core/models/video/sql/channel/video-channel-list-query-builder.ts
index e53c8feea7..53b022a5bc 100644
--- a/server/core/models/video/sql/channel/video-channel-list-query-builder.ts
+++ b/server/core/models/video/sql/channel/video-channel-list-query-builder.ts
@@ -137,7 +137,7 @@ export class VideoChannelListQueryBuilder extends AbstractListQuery {
private buildActorJoin () {
if (this.builtActorJoin) return
- this.subQueryJoin += ' INNER JOIN "actor" "Actor" ON "Actor"."id" = "VideoChannelModel"."actorId" ' +
+ this.subQueryJoin += ' INNER JOIN "actor" "Actor" ON "Actor"."videoChannelId" = "VideoChannelModel"."id" ' +
'LEFT JOIN "server" "Actor->Server" ON "Actor"."serverId" = "Actor->Server"."id" '
this.builtActorJoin = true
@@ -147,7 +147,7 @@ export class VideoChannelListQueryBuilder extends AbstractListQuery {
if (this.builtAccountJoin) return
this.subQueryJoin += ' INNER JOIN "account" "Account" ON "Account"."id" = "VideoChannelModel"."accountId" ' +
- 'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."id" = "Account"."actorId" ' +
+ 'INNER JOIN "actor" "Account->Actor" ON "Account->Actor"."accountId" = "Account"."id" ' +
'LEFT JOIN "server" "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id" '
this.builtAccountJoin = true
@@ -184,7 +184,7 @@ export class VideoChannelListQueryBuilder extends AbstractListQuery {
if (this.builtChannelAvatarJoin) return
this.join += `LEFT JOIN "actorImage" "Actor->Avatars" ` +
- `ON "Actor->Avatars"."actorId" = "VideoChannelModel"."actorId" ` +
+ `ON "Actor->Avatars"."actorId" = "VideoChannelModel"."Actor.id" ` +
`AND "Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
this.builtChannelAvatarJoin = true
@@ -194,7 +194,7 @@ export class VideoChannelListQueryBuilder extends AbstractListQuery {
if (this.builtChannelBannerJoin) return
this.join += `LEFT JOIN "actorImage" "Actor->Banners" ` +
- `ON "Actor->Banners"."actorId" = "VideoChannelModel"."actorId" ` +
+ `ON "Actor->Banners"."actorId" = "VideoChannelModel"."Actor.id" ` +
`AND "Actor->Banners"."type" = ${ActorImageType.BANNER} `
this.builtChannelBannerJoin = true
diff --git a/server/core/models/video/sql/comment/video-comment-list-query-builder.ts b/server/core/models/video/sql/comment/video-comment-list-query-builder.ts
index 604b475fa1..1d43dc40b4 100644
--- a/server/core/models/video/sql/comment/video-comment-list-query-builder.ts
+++ b/server/core/models/video/sql/comment/video-comment-list-query-builder.ts
@@ -239,7 +239,8 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
this.subQueryJoin += getActorJoin({
base: 'Account->',
- on: `"Account"."actorId"`,
+ on: `"Account"."id"`,
+ type: 'account',
includeAvatars: false,
required: false
})
@@ -277,7 +278,8 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
this.subQueryJoin += getActorJoin({
base: 'Video->VideoChannel->',
- on: '"Video->VideoChannel"."actorId"',
+ on: '"Video->VideoChannel"."id"',
+ type: 'channel',
includeAvatars: false,
required: true
})
@@ -496,7 +498,7 @@ export class VideoCommentListQueryBuilder extends AbstractListQuery {
where.push(
`NOT EXISTS (` +
`SELECT 1 FROM "account" ` +
- `INNER JOIN "actor" ON account."actorId" = actor.id ` +
+ `INNER JOIN "actor" ON account."id" = actor."accountId" ` +
`INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ` +
`WHERE "account"."id" = "${commentTableName}"."accountId" ` +
`AND "serverBlocklist"."accountId" IN (${blockerIdsString})` +
diff --git a/server/core/models/video/sql/playlist/video-playlist-list-query-builder.ts b/server/core/models/video/sql/playlist/video-playlist-list-query-builder.ts
index ad8f45df90..ef82207000 100644
--- a/server/core/models/video/sql/playlist/video-playlist-list-query-builder.ts
+++ b/server/core/models/video/sql/playlist/video-playlist-list-query-builder.ts
@@ -162,7 +162,7 @@ export class VideoPlaylistListQueryBuilder extends AbstractListQuery {
if (this.builtAccountJoin) return
this.subQueryJoin += ' INNER JOIN "account" "OwnerAccount" ON "OwnerAccount"."id" = "VideoPlaylistModel"."ownerAccountId" ' +
- 'INNER JOIN "actor" "OwnerAccount->Actor" ON "OwnerAccount->Actor"."id" = "OwnerAccount"."actorId" ' +
+ 'INNER JOIN "actor" "OwnerAccount->Actor" ON "OwnerAccount->Actor"."accountId" = "OwnerAccount"."id" ' +
'LEFT JOIN "server" "OwnerAccount->Actor->Server" ON "OwnerAccount->Actor"."serverId" = "OwnerAccount->Actor->Server"."id" '
this.builtAccountJoin = true
@@ -172,7 +172,7 @@ export class VideoPlaylistListQueryBuilder extends AbstractListQuery {
if (this.builtVideoChannelJoin) return
this.subQueryJoin += ' LEFT JOIN "videoChannel" "VideoChannel" ON "VideoPlaylistModel"."videoChannelId" = "VideoChannel"."id" ' +
- 'LEFT JOIN "actor" "VideoChannel->Actor" ON "VideoChannel->Actor"."id" = "VideoChannel"."actorId" ' +
+ 'LEFT JOIN "actor" "VideoChannel->Actor" ON "VideoChannel->Actor"."videoChannelId" = "VideoChannel"."id" ' +
'LEFT JOIN "server" "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id" '
this.builtVideoChannelJoin = true
@@ -209,7 +209,7 @@ export class VideoPlaylistListQueryBuilder extends AbstractListQuery {
if (this.builtChannelAvatarJoin) return
this.join += `LEFT JOIN "actorImage" "VideoChannel->Actor->Avatars" ` +
- `ON "VideoChannel->Actor->Avatars"."actorId" = "VideoPlaylistModel"."VideoChannel.actorId" ` +
+ `ON "VideoChannel->Actor->Avatars"."actorId" = "VideoPlaylistModel"."VideoChannel.Actor.id" ` +
`AND "VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR} `
this.builtChannelAvatarJoin = true
diff --git a/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts b/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts
index 6d8fcbbde2..c4a10082ac 100644
--- a/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts
+++ b/server/core/models/video/sql/video/shared/abstract-video-query-builder.ts
@@ -38,7 +38,7 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
protected includeChannels () {
this.addJoin('INNER JOIN "videoChannel" AS "VideoChannel" ON "video"."channelId" = "VideoChannel"."id"')
- this.addJoin('INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."actorId" = "VideoChannel->Actor"."id"')
+ this.addJoin('INNER JOIN "actor" AS "VideoChannel->Actor" ON "VideoChannel"."id" = "VideoChannel->Actor"."videoChannelId"')
this.addJoin(
'LEFT OUTER JOIN "server" AS "VideoChannel->Actor->Server" ON "VideoChannel->Actor"."serverId" = "VideoChannel->Actor->Server"."id"'
@@ -63,7 +63,7 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
protected includeAccounts () {
this.addJoin('INNER JOIN "account" AS "VideoChannel->Account" ON "VideoChannel"."accountId" = "VideoChannel->Account"."id"')
this.addJoin(
- 'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."actorId" = "VideoChannel->Account->Actor"."id"'
+ 'INNER JOIN "actor" AS "VideoChannel->Account->Actor" ON "VideoChannel->Account"."id" = "VideoChannel->Account->Actor"."accountId"'
)
this.addJoin(
@@ -73,7 +73,7 @@ export class AbstractVideoQueryBuilder extends AbstractRunQuery {
this.addJoin(
'LEFT OUTER JOIN "actorImage" AS "VideoChannel->Account->Actor->Avatars" ' +
- 'ON "VideoChannel->Account"."actorId"= "VideoChannel->Account->Actor->Avatars"."actorId" ' +
+ 'ON "VideoChannel->Account->Actor"."id" = "VideoChannel->Account->Actor->Avatars"."actorId" ' +
`AND "VideoChannel->Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}`
)
diff --git a/server/core/models/video/sql/video/shared/video-table-attributes.ts b/server/core/models/video/sql/video/shared/video-table-attributes.ts
index 55fcea2a38..34a76cc332 100644
--- a/server/core/models/video/sql/video/shared/video-table-attributes.ts
+++ b/server/core/models/video/sql/video/shared/video-table-attributes.ts
@@ -14,8 +14,7 @@ export class VideoTableAttributes {
'id',
'name',
'description',
- 'accountId',
- 'actorId'
+ 'accountId'
]
if (this.mode === 'get') {
@@ -34,7 +33,7 @@ export class VideoTableAttributes {
}
getAccountAttributes () {
- let attributeKeys = [ 'id', 'name', 'actorId' ]
+ let attributeKeys = [ 'id', 'name' ]
if (this.mode === 'get') {
attributeKeys = attributeKeys.concat([
@@ -221,7 +220,9 @@ export class VideoTableAttributes {
'id',
'preferredUsername',
'url',
- 'serverId'
+ 'serverId',
+ 'accountId',
+ 'videoChannelId'
]
if (this.mode === 'get') {
diff --git a/server/core/models/video/sql/video/videos-id-list-query-builder.ts b/server/core/models/video/sql/video/videos-id-list-query-builder.ts
index 8327426c0d..6fa576ce09 100644
--- a/server/core/models/video/sql/video/videos-id-list-query-builder.ts
+++ b/server/core/models/video/sql/video/videos-id-list-query-builder.ts
@@ -118,6 +118,8 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
private limit = ''
private offset = ''
+ private builtChannelJoin = false
+
constructor (protected readonly sequelize: Sequelize) {
super(sequelize)
}
@@ -154,7 +156,7 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
this.joins = this.joins.concat([
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId"',
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId"',
- 'INNER JOIN "actor" "accountActor" ON "account"."actorId" = "accountActor"."id"'
+ 'INNER JOIN "actor" "accountActor" ON "account"."id" = "accountActor"."accountId"'
])
if (!(options.include & VideoInclude.BLACKLISTED)) {
@@ -367,6 +369,13 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
)
}
+ private joinChannel () {
+ if (this.builtChannelJoin) return
+ this.builtChannelJoin = true
+
+ this.joins.push('INNER JOIN "actor" "channelActor" ON "videoChannel"."id" = "channelActor"."videoChannelId"')
+ }
+
private whereStateAvailable (options: {
includeScheduledLive: boolean
}) {
@@ -442,23 +451,26 @@ export class VideosIdListQueryBuilder extends AbstractRunQuery {
}
private whereChannelOneOf (channelOneOf: string[]) {
- this.joins.push('INNER JOIN "actor" "channelActor" ON "videoChannel"."actorId" = "channelActor"."id"')
+ this.joinChannel()
+
this.and.push('"channelActor"."preferredUsername" IN (:channelOneOf)')
this.replacements.channelOneOf = channelOneOf
}
private whereFollowerActorId (options: { actorId: number, orLocalVideos: boolean }) {
+ this.joinChannel()
+
let query = '(' +
- ' EXISTS (' + // Videos shared by actors we follow
+ ' EXISTS (' + // Videos shared by actors (instances, channels) we follow
' SELECT 1 FROM "videoShare" ' +
' INNER JOIN "actorFollow" "actorFollowShare" ON "actorFollowShare"."targetActorId" = "videoShare"."actorId" ' +
' AND "actorFollowShare"."actorId" = :followerActorId AND "actorFollowShare"."state" = \'accepted\' ' +
' WHERE "videoShare"."videoId" = "video"."id"' +
' )' +
' OR' +
- ' EXISTS (' + // Videos published by channels or accounts we follow
+ ' EXISTS (' + // Videos published by accounts we follow
' SELECT 1 from "actorFollow" ' +
- ' WHERE ("actorFollow"."targetActorId" = "account"."actorId" OR "actorFollow"."targetActorId" = "videoChannel"."actorId") ' +
+ ' WHERE ("actorFollow"."targetActorId" = "accountActor"."id" OR "actorFollow"."targetActorId" = "channelActor"."id") ' +
' AND "actorFollow"."actorId" = :followerActorId ' +
' AND "actorFollow"."state" = \'accepted\'' +
' )'
diff --git a/server/core/models/video/video-channel.ts b/server/core/models/video/video-channel.ts
index 2959409c8c..d60c3d13fb 100644
--- a/server/core/models/video/video-channel.ts
+++ b/server/core/models/video/video-channel.ts
@@ -19,6 +19,7 @@ import {
DefaultScope,
ForeignKey,
HasMany,
+ HasOne,
Is,
Scopes,
Table,
@@ -42,7 +43,6 @@ import {
type MChannel
} from '../../types/models/video/index.js'
import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account.js'
-import { ActorFollowModel } from '../actor/actor-follow.js'
import { ActorImageModel } from '../actor/actor-image.js'
import { ActorModel, actorSummaryAttributes } from '../actor/actor.js'
import { ServerModel, serverSummaryAttributes } from '../server/server.js'
@@ -52,7 +52,7 @@ import { VideoChannelCollaboratorModel } from './video-channel-collaborator.js'
import { VideoPlaylistModel } from './video-playlist.js'
import { VideoModel } from './video.js'
-const channelSummaryAttributes = [ 'id', 'name', 'description', 'actorId' ] as const satisfies (keyof AttributesOnly<AccountModel>)[]
+const channelSummaryAttributes = [ 'id', 'name', 'description' ] as const satisfies (keyof AttributesOnly<AccountModel>)[]
export enum ScopeNames {
SUMMARY = 'SUMMARY',
@@ -155,9 +155,6 @@ export type SummaryOptions = {
{
fields: [ 'accountId' ]
- },
- {
- fields: [ 'actorId' ]
}
]
})
@@ -185,15 +182,12 @@ export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {
@UpdatedAt
declare updatedAt: Date
- @ForeignKey(() => ActorModel)
- @Column
- declare actorId: number
-
- @BelongsTo(() => ActorModel, {
+ @HasOne(() => ActorModel, {
foreignKey: {
- allowNull: false
+ allowNull: true
},
- onDelete: 'cascade'
+ onDelete: 'cascade',
+ hooks: true
})
declare Actor: Awaited<ActorModel>
@@ -254,8 +248,6 @@ export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {
instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
}
- await ActorFollowModel.removeFollowsOf(instance.Actor.id, options.transaction)
-
if (instance.Actor.isLocal()) {
return sendDeleteActor(instance.Actor, options.transaction)
}
@@ -263,18 +255,6 @@ export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {
return undefined
}
- @AfterDestroy
- static async deleteActorIfRemote (instance: VideoChannelModel, options) {
- if (!instance.Actor) {
- instance.Actor = await instance.$get('Actor', { transaction: options.transaction })
- }
-
- // Remote actor, delete it
- if (instance.Actor.serverId) {
- await instance.Actor.destroy({ transaction: options.transaction })
- }
- }
-
// ---------------------------------------------------------------------------
static getSQLAttributes (tableName: string, aliasPrefix = '') {
@@ -323,7 +303,7 @@ export class VideoChannelModel extends SequelizeModel<VideoChannelModel> {
FROM "videoChannel" AS "VideoChannelModel"
${videoJoin}
INNER JOIN "account" AS "Account" ON "VideoChannelModel"."accountId" = "Account"."id"
- INNER JOIN "actor" AS "Account->Actor" ON "Account"."actorId" = "Account->Actor"."id"
+ INNER JOIN "actor" AS "Account->Actor" ON "Account"."id" = "Account->Actor"."accountId"
AND "Account->Actor"."serverId" IS NULL`
return VideoChannelModel.sequelize.query<{ count: string }>(query, options)
diff --git a/server/core/models/video/video-comment.ts b/server/core/models/video/video-comment.ts
index b1c47af5dc..dfaad7976d 100644
--- a/server/core/models/video/video-comment.ts
+++ b/server/core/models/video/video-comment.ts
@@ -655,7 +655,7 @@ export class VideoCommentModel extends SequelizeModel<VideoCommentModel> {
static listRemoteCommentUrlsOfLocalVideos () {
const query = `SELECT "videoComment".url FROM "videoComment" ` +
`INNER JOIN account ON account.id = "videoComment"."accountId" ` +
- `INNER JOIN actor ON actor.id = "account"."actorId" AND actor."serverId" IS NOT NULL ` +
+ `INNER JOIN actor ON actor."accountId" = "account"."id" AND actor."serverId" IS NOT NULL ` +
`INNER JOIN video ON video.id = "videoComment"."videoId" AND video.remote IS FALSE`
return VideoCommentModel.sequelize.query<{ url: string }>(query, {
diff --git a/server/core/models/video/video-share.ts b/server/core/models/video/video-share.ts
index 029f308fe9..2f3b33e26c 100644
--- a/server/core/models/video/video-share.ts
+++ b/server/core/models/video/video-share.ts
@@ -1,6 +1,6 @@
+import { forceNumber } from '@peertube/peertube-core-utils'
import { literal, Op, QueryTypes, Transaction } from 'sequelize'
import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
-import { forceNumber } from '@peertube/peertube-core-utils'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc.js'
import { CONSTRAINTS_FIELDS } from '../../initializers/constants.js'
import { MActorDefault, MActorFollowersUrl, MActorId } from '../../types/models/index.js'
@@ -121,8 +121,11 @@ export class VideoShareModel extends SequelizeModel<VideoShareModel> {
return VideoShareModel.sequelize.query<MActorId & MActorFollowersUrl>(query, options)
}
- static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Promise<MActorDefault[]> {
- const safeOwnerId = forceNumber(actorOwnerId)
+ static listActorsWhoSharedVideosOf (options: {
+ actorOwnerId: number
+ transaction: Transaction
+ }): Promise<MActorDefault[]> {
+ const safeOwnerId = forceNumber(options.actorOwnerId)
// /!\ On actor model
const query = {
@@ -134,20 +137,24 @@ export class VideoShareModel extends SequelizeModel<VideoShareModel> {
` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
` INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
- ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "account"."actorId" = ${safeOwnerId} ` +
+ ` INNER JOIN "actor" ON "actor"."accountId" = "account"."id" ` +
+ ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "actor"."id" = ${safeOwnerId} ` +
` LIMIT 1` +
`)`
)
]
},
- transaction: t
+ transaction: options.transaction
}
return ActorModel.findAll(query)
}
- static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Promise<MActorDefault[]> {
- const safeChannelId = forceNumber(videoChannelId)
+ static listActorsByVideoChannel (options: {
+ channelId: number
+ transaction: Transaction
+ }): Promise<MActorDefault[]> {
+ const safeChannelId = forceNumber(options.channelId)
// /!\ On actor model
const query = {
@@ -163,7 +170,7 @@ export class VideoShareModel extends SequelizeModel<VideoShareModel> {
)
]
},
- transaction: t
+ transaction: options.transaction
}
return ActorModel.findAll(query)
diff --git a/server/core/models/video/video.ts b/server/core/models/video/video.ts
index 90b558d6bf..248e68d734 100644
--- a/server/core/models/video/video.ts
+++ b/server/core/models/video/video.ts
@@ -831,7 +831,7 @@ export class VideoModel extends SequelizeModel<VideoModel> {
if (!instance.isLocal()) return undefined
// Lazy load channels
- if (!instance.VideoChannel) {
+ if (!instance.VideoChannel?.Account?.Actor) {
instance.VideoChannel = await instance.$get('VideoChannel', {
include: [
ActorModel,
@@ -938,7 +938,8 @@ export class VideoModel extends SequelizeModel<VideoModel> {
const queryVideo = 'SELECT ' + select + ' FROM "video" AS "Video" ' +
'INNER JOIN "videoChannel" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
'INNER JOIN "account" AS "Account" ON "Account"."id" = "VideoChannel"."accountId" ' +
- 'WHERE "Account"."actorId" = ' + actorId
+ 'INNER JOIN "actor" AS "Actor" ON "Actor"."accountId" = "Account"."id" ' +
+ 'WHERE "Actor"."id" = ' + actorId
const queryVideoShare = 'SELECT ' + select + ' FROM "videoShare" AS "VideoShare" ' +
'INNER JOIN "video" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
'WHERE "VideoShare"."actorId" = ' + actorId
@@ -1529,22 +1530,24 @@ export class VideoModel extends SequelizeModel<VideoModel> {
})
}
- static checkVideoHasInstanceFollow (videoId: number, followerActorId: number) {
- // Instances only share videos
- const query = 'SELECT 1 FROM "videoShare" ' +
+ static async checkVideoHasInstanceFollow (videoId: number) {
+ const serverActor = await getServerActor()
+
+ const query = 'SELECT 1 FROM "videoShare" ' + // Instances/channels we follow that shared the video
'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
- 'WHERE "actorFollow"."actorId" = $followerActorId AND "actorFollow"."state" = \'accepted\' AND "videoShare"."videoId" = $videoId ' +
+ 'WHERE "actorFollow"."actorId" = $serverActorId AND "actorFollow"."state" = \'accepted\' AND "videoShare"."videoId" = $videoId ' +
'UNION ' +
- 'SELECT 1 FROM "video" ' +
+ 'SELECT 1 FROM "video" ' + // Accounts we follow that published the video
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
- 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "account"."actorId" ' +
- 'WHERE "actorFollow"."actorId" = $followerActorId AND "actorFollow"."state" = \'accepted\' AND "video"."id" = $videoId ' +
+ 'INNER JOIN "actor" ON "actor"."accountId" = "account"."id" ' +
+ 'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
+ 'WHERE "actorFollow"."actorId" = $serverActorId AND "actorFollow"."state" = \'accepted\' AND "video"."id" = $videoId ' +
'LIMIT 1'
const options = {
type: QueryTypes.SELECT as QueryTypes.SELECT,
- bind: { followerActorId, videoId },
+ bind: { serverActorId: serverActor.id, videoId },
raw: true
}
diff --git a/server/core/types/models/actor/actor.ts b/server/core/types/models/actor/actor.ts
index 83e56cc9d6..a68535c4f0 100644
--- a/server/core/types/models/actor/actor.ts
+++ b/server/core/types/models/actor/actor.ts
@@ -138,7 +138,7 @@ export type MActorFullActor =
export type MActorSummary =
& FunctionProperties<MActor>
- & Pick<MActor, 'id' | 'preferredUsername' | 'url' | 'serverId'>
+ & Pick<MActor, 'id' | 'preferredUsername' | 'url' | 'serverId' | 'accountId' | 'videoChannelId'>
& Use<'Server', MServerHost>
& Use<'Avatars', MActorImage[]>
@@ -166,7 +166,7 @@ export type MActorAPI = Omit<
export type MActorSummaryFormattable =
& FunctionProperties<MActor>
- & Pick<MActor, 'url' | 'preferredUsername' | 'serverId'>
+ & Pick<MActor, 'url' | 'preferredUsername' | 'serverId' | 'accountId' | 'videoChannelId'>
& Use<'Server', MServerHost>
& Use<'Avatars', MActorImageFormattable[]>
diff --git a/server/core/types/models/video/video-channel.ts b/server/core/types/models/video/video-channel.ts
index cd5bdcce5f..8cc85603f1 100644
--- a/server/core/types/models/video/video-channel.ts
+++ b/server/core/types/models/video/video-channel.ts
@@ -133,7 +133,7 @@ export type MChannelVideos =
export type MChannelSummary =
& FunctionProperties<MChannel>
- & Pick<MChannel, 'id' | 'name' | 'description' | 'actorId'>
+ & Pick<MChannel, 'id' | 'name' | 'description'>
& Use<'Actor', MActorSummary>
export type MChannelSummaryAccount =
diff --git a/server/scripts/migrations/peertube-8.0.ts b/server/scripts/migrations/peertube-8.0.ts
new file mode 100644
index 0000000000..aa748d30b3
--- /dev/null
+++ b/server/scripts/migrations/peertube-8.0.ts
@@ -0,0 +1,38 @@
+import { initDatabaseModels, sequelizeTypescript } from '@server/initializers/database.js'
+import { QueryTypes } from 'sequelize'
+
+run()
+ .then(() => process.exit(0))
+ .catch(err => {
+ console.error(err)
+ process.exit(-1)
+ })
+
+async function run () {
+ await initDatabaseModels(true)
+
+ const chunkSize = 500
+ let i = 1
+
+ console.log(`Running SQL request to delete unused actors, in chunks of maximum ${chunkSize} rows...`)
+
+ while (true) {
+ console.log('Processing chunk ' + i)
+
+ const query = `WITH cte AS (SELECT id FROM "actor" WHERE "accountId" IS NULL AND "videoChannelId" IS NULL LIMIT ${chunkSize}) ` +
+ `DELETE FROM "actor" WHERE id IN (SELECT "id" FROM cte) RETURNING id`
+
+ const deleted = await sequelizeTypescript.query(query, {
+ type: QueryTypes.BULKDELETE,
+ replacements: { limit: chunkSize }
+ })
+
+ if (deleted < chunkSize) {
+ break
+ }
+
+ i++
+ }
+
+ console.log('Done!')
+}