chore(server): previews IoC 5 - listenForPreviewGenerationUpdatesFactory

This commit is contained in:
Kristaps Fabians Geikins
2024-09-13 16:45:21 +03:00
parent 3677304ee3
commit 1ed43e2c2f
2 changed files with 47 additions and 30 deletions
+7 -1
View File
@@ -10,7 +10,7 @@ const {
const { makeOgImage } = require('./ogImage')
const { moduleLogger } = require('@/logging/logging')
const {
listenForPreviewGenerationUpdates
listenForPreviewGenerationUpdatesFactory
} = require('@/modules/previews/services/resultListener')
const httpErrorImage = (httpErrorCode) =>
@@ -29,6 +29,8 @@ const {
createObjectPreviewFactory,
getPreviewImageFactory
} = require('@/modules/previews/repository/previews')
const { publish } = require('@/modules/shared/utils/subscriptions')
const { getObjectCommitsWithStreamIds } = require('@/modules/core/repositories/commits')
const noPreviewImage = require.resolve('#/assets/previews/images/no_preview.png')
@@ -159,6 +161,10 @@ exports.init = (app, isInitial) => {
})
if (isInitial) {
const listenForPreviewGenerationUpdates = listenForPreviewGenerationUpdatesFactory({
getObjectCommitsWithStreamIds,
publish
})
listenForPreviewGenerationUpdates()
}
}
@@ -1,39 +1,50 @@
import { getObjectCommitsWithStreamIds } from '@/modules/core/repositories/commits'
import { ProjectSubscriptions, publish } from '@/modules/shared/utils/subscriptions'
import {
ProjectSubscriptions,
PublishSubscription
} from '@/modules/shared/utils/subscriptions'
import { listenFor, MessageType } from '@/modules/core/utils/dbNotificationListener'
const payloadRegexp = /^([\w\d]+):([\w\d]+):([\w\d]+)$/i
async function messageProcessor(msg: MessageType) {
if (msg.channel !== 'preview_generation_update') return
const [, status, streamId, objectId] = payloadRegexp.exec(msg.payload) || [
null,
null,
null,
null
]
type MessageProcessorDeps = {
getObjectCommitsWithStreamIds: typeof getObjectCommitsWithStreamIds
publish: PublishSubscription
}
if (status !== 'finished' || !objectId || !streamId) return
const messageProcessorFactory =
(deps: MessageProcessorDeps) => async (msg: MessageType) => {
if (msg.channel !== 'preview_generation_update') return
const [, status, streamId, objectId] = payloadRegexp.exec(msg.payload) || [
null,
null,
null,
null
]
// Get all commits with that objectId
const commits = await getObjectCommitsWithStreamIds([objectId], {
streamIds: [streamId]
})
if (!commits.length) return
if (status !== 'finished' || !objectId || !streamId) return
await Promise.all(
commits.map((c) =>
publish(ProjectSubscriptions.ProjectVersionsPreviewGenerated, {
projectVersionsPreviewGenerated: {
versionId: c.id,
projectId: c.streamId,
objectId
}
})
// Get all commits with that objectId
const commits = await deps.getObjectCommitsWithStreamIds([objectId], {
streamIds: [streamId]
})
if (!commits.length) return
await Promise.all(
commits.map((c) =>
deps.publish(ProjectSubscriptions.ProjectVersionsPreviewGenerated, {
projectVersionsPreviewGenerated: {
versionId: c.id,
projectId: c.streamId,
objectId
}
})
)
)
)
}
}
export function listenForPreviewGenerationUpdates() {
listenFor('preview_generation_update', messageProcessor)
}
export const listenForPreviewGenerationUpdatesFactory =
(deps: MessageProcessorDeps) => () => {
const messageProcessor = messageProcessorFactory(deps)
listenFor('preview_generation_update', messageProcessor)
}