mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-23 10:05:01 +02:00
129 lines
3.7 KiB
TypeScript
129 lines
3.7 KiB
TypeScript
//
|
|
// Copyright © 2022 Hardcore Engineering Inc.
|
|
//
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License. You may
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
//
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
import { type DocUpdateMessage } from '@hcengineering/activity'
|
|
import core, { type Class, type Doc, type Domain, type Ref } from '@hcengineering/core'
|
|
import {
|
|
type MigrateOperation,
|
|
type MigrateUpdate,
|
|
type MigrationClient,
|
|
type MigrationDocumentQuery,
|
|
type MigrationIterator,
|
|
type MigrationUpgradeClient,
|
|
tryMigrate
|
|
} from '@hcengineering/model'
|
|
import { htmlToMarkup } from '@hcengineering/text'
|
|
|
|
import activity from './plugin'
|
|
import { activityId, DOMAIN_ACTIVITY } from './index'
|
|
|
|
const DOMAIN_CHUNTER = 'chunter' as Domain
|
|
|
|
async function migrateReactions (client: MigrationClient): Promise<void> {
|
|
await client.update(
|
|
DOMAIN_CHUNTER,
|
|
{ _class: 'chunter:class:Reaction' as Ref<Class<Doc>> },
|
|
{ _class: activity.class.Reaction }
|
|
)
|
|
await client.move(DOMAIN_CHUNTER, { _class: activity.class.Reaction }, DOMAIN_ACTIVITY)
|
|
}
|
|
|
|
async function migrateMarkup (client: MigrationClient): Promise<void> {
|
|
const iterator = await client.traverse<DocUpdateMessage>(
|
|
DOMAIN_ACTIVITY,
|
|
{
|
|
_class: activity.class.DocUpdateMessage,
|
|
'attributeUpdates.attrClass': {
|
|
$in: [core.class.TypeMarkup, core.class.TypeCollaborativeMarkup]
|
|
}
|
|
},
|
|
{
|
|
projection: {
|
|
_id: 1,
|
|
attributeUpdates: 1
|
|
}
|
|
}
|
|
)
|
|
|
|
try {
|
|
await processMigrateMarkupFor(client, iterator)
|
|
} finally {
|
|
await iterator.close()
|
|
}
|
|
}
|
|
|
|
async function processMigrateMarkupFor (
|
|
client: MigrationClient,
|
|
iterator: MigrationIterator<DocUpdateMessage>
|
|
): Promise<void> {
|
|
let processed = 0
|
|
while (true) {
|
|
const docs = await iterator.next(1000)
|
|
if (docs === null || docs.length === 0) {
|
|
break
|
|
}
|
|
|
|
const ops: { filter: MigrationDocumentQuery<DocUpdateMessage>, update: MigrateUpdate<DocUpdateMessage> }[] = []
|
|
|
|
for (const doc of docs) {
|
|
if (doc.attributeUpdates == null) continue
|
|
if (doc.attributeUpdates.set == null) continue
|
|
if (doc.attributeUpdates.set.length === 0) continue
|
|
|
|
const update: MigrateUpdate<DocUpdateMessage> = {}
|
|
|
|
const attributeUpdatesSet = [...doc.attributeUpdates.set]
|
|
for (let i = 0; i < attributeUpdatesSet.length; i++) {
|
|
const value = attributeUpdatesSet[i]
|
|
if (value != null && typeof value === 'string') {
|
|
attributeUpdatesSet[i] = htmlToMarkup(value)
|
|
}
|
|
|
|
update['attributeUpdates.set'] = attributeUpdatesSet
|
|
}
|
|
|
|
const prevValue = doc.attributeUpdates.prevValue
|
|
if (prevValue != null && typeof prevValue === 'string') {
|
|
update['attributeUpdates.prevValue'] = htmlToMarkup(prevValue)
|
|
}
|
|
|
|
ops.push({ filter: { _id: doc._id }, update })
|
|
}
|
|
|
|
if (ops.length > 0) {
|
|
await client.bulk(DOMAIN_ACTIVITY, ops)
|
|
}
|
|
|
|
processed += docs.length
|
|
console.log('...processed', processed)
|
|
}
|
|
}
|
|
|
|
export const activityOperation: MigrateOperation = {
|
|
async migrate (client: MigrationClient): Promise<void> {
|
|
await tryMigrate(client, activityId, [
|
|
{
|
|
state: 'reactions',
|
|
func: migrateReactions
|
|
},
|
|
{
|
|
state: 'markup',
|
|
func: migrateMarkup
|
|
}
|
|
])
|
|
},
|
|
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
|
|
}
|