Add clear-telegram-history command (#497)

Signed-off-by: Ilya Sumbatyants <ilya.sumb@gmail.com>
This commit is contained in:
Ilya Sumbatyants 2021-12-02 21:21:41 +07:00 committed by GitHub
parent f7cfe46a2e
commit 7d8880a0cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 105 additions and 11 deletions

View File

@ -12,7 +12,7 @@
"bundle": "esbuild src/index.ts --bundle --minify --platform=node > bundle.js",
"docker:build": "docker build -t anticrm/tool .",
"docker:push": "docker push anticrm/tool",
"run-local": "MINIO_ACCESS_KEY=minioadmin MINIO_SECRET_KEY=minioadmin MONGO_URL=mongodb://localhost:27017 TRANSACTOR_URL=ws:/localhost:3333 MINIO_ENDPOINT=localhost ts-node ./src/index.ts",
"run-local": "MINIO_ACCESS_KEY=minioadmin MINIO_SECRET_KEY=minioadmin MONGO_URL=mongodb://localhost:27017 TRANSACTOR_URL=ws:/localhost:3333 MINIO_ENDPOINT=localhost TELEGRAM_DATABASE=telegram-service ts-node ./src/index.ts",
"lint": "eslint src",
"format": "prettier --write src && eslint --fix src"
},
@ -44,6 +44,8 @@
"@anticrm/contact": "~0.6.2",
"@anticrm/workspace": "~0.6.0",
"minio": "^7.0.19",
"@anticrm/model-all": "~0.6.0"
"@anticrm/model-all": "~0.6.0",
"@anticrm/model-telegram": "~0.6.0",
"@anticrm/telegram": "~0.6.0"
}
}

View File

@ -16,7 +16,16 @@
import { program } from 'commander'
import { MongoClient, Db } from 'mongodb'
import { getAccount, createAccount, assignWorkspace, createWorkspace, ACCOUNT_DB, dropWorkspace, dropAccount, listWorkspaces } from '@anticrm/account'
import {
getAccount,
createAccount,
assignWorkspace,
createWorkspace,
ACCOUNT_DB,
dropWorkspace,
dropAccount,
listWorkspaces
} from '@anticrm/account'
import { createContributingClient } from '@anticrm/contrib'
import core, { TxOperations } from '@anticrm/core'
import { encode } from 'jwt-simple'
@ -24,6 +33,7 @@ import { Client } from 'minio'
import { initWorkspace, upgradeWorkspace, dumpWorkspace } from './workspace'
import contact, { combineName } from '@anticrm/contact'
import { clearTelegramHistory } from './telegram'
const mongodbUri = process.env.MONGO_URL
if (mongodbUri === undefined) {
@ -190,4 +200,27 @@ program
return await dumpWorkspace(mongodbUri, workspace, fileName, minio)
})
program
.command('clear-telegram-history')
.description('clear telegram history')
.option('-w, --workspace <workspace>', 'target workspace')
.action(async (cmd) => {
return await withDatabase(mongodbUri, async (db) => {
const telegramDB = process.env.TELEGRAM_DATABASE
if (telegramDB === undefined) {
console.error('please provide TELEGRAM_DATABASE.')
process.exit(1)
}
const workspaces = await listWorkspaces(db)
const targetWorkspaces =
cmd.workspace !== undefined ? workspaces.filter((x) => x.workspace === cmd.workspace) : workspaces
for (const w of targetWorkspaces) {
console.log(`clearing ${w.workspace} history:`)
await clearTelegramHistory(mongodbUri, w.workspace, telegramDB)
}
})
})
program.parse(process.argv)

52
dev/tool/src/telegram.ts Normal file
View File

@ -0,0 +1,52 @@
//
// Copyright © 2020, 2021 Anticrm Platform Contributors.
// Copyright © 2021 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 { MongoClient } from 'mongodb'
import { DOMAIN_TX } from '@anticrm/core'
import { DOMAIN_TELEGRAM } from '@anticrm/model-telegram'
import telegram from '@anticrm/telegram'
const LastMessages = 'last-msgs'
/**
* @public
*/
export async function clearTelegramHistory (mongoUrl: string, workspace: string, tgDb: string): Promise<void> {
const client = new MongoClient(mongoUrl)
try {
await client.connect()
const workspaceDB = client.db(workspace)
const telegramDB = client.db(tgDb)
console.log('clearing txes and messages...')
await Promise.all([
workspaceDB.collection(DOMAIN_TX).deleteMany({
objectClass: telegram.class.Message
}),
workspaceDB.collection(DOMAIN_TELEGRAM).deleteMany({
_class: telegram.class.Message
})
])
console.log('clearing telegram service data...')
await telegramDB.collection(LastMessages).deleteMany({
workspace
})
} finally {
await client.close()
}
}

View File

@ -41,12 +41,12 @@ export async function initWorkspace (mongoUrl: string, dbName: string, clientUrl
await db.dropDatabase()
console.log('creating model...')
const model = txes.filter(tx => tx.objectSpace === core.space.Model)
const model = txes.filter((tx) => tx.objectSpace === core.space.Model)
const result = await db.collection(DOMAIN_TX).insertMany(model as Document[])
console.log(`${result.insertedCount} model transactions inserted.`)
console.log('creating data...')
const data = txes.filter(tx => tx.objectSpace !== core.space.Model)
const data = txes.filter((tx) => tx.objectSpace !== core.space.Model)
const token = encode({ email: 'anticrm@hc.engineering', workspace: dbName }, 'secret')
const url = new URL(`/${token}`, clientUrl)
const contrib = await createContributingClient(url.href)
@ -56,7 +56,9 @@ export async function initWorkspace (mongoUrl: string, dbName: string, clientUrl
contrib.close()
console.log('create minio bucket')
if (!await minio.bucketExists(dbName)) { await minio.makeBucket(dbName, 'k8s') }
if (!(await minio.bucketExists(dbName))) {
await minio.makeBucket(dbName, 'k8s')
}
} finally {
await client.close()
}
@ -65,7 +67,12 @@ export async function initWorkspace (mongoUrl: string, dbName: string, clientUrl
/**
* @public
*/
export async function upgradeWorkspace (mongoUrl: string, dbName: string, clientUrl: string, minio: Client): Promise<void> {
export async function upgradeWorkspace (
mongoUrl: string,
dbName: string,
clientUrl: string,
minio: Client
): Promise<void> {
const client = new MongoClient(mongoUrl)
try {
await client.connect()
@ -81,7 +88,7 @@ export async function upgradeWorkspace (mongoUrl: string, dbName: string, client
console.log(`${result.deletedCount} transactions deleted.`)
console.log('creating model...')
const model = txes.filter(tx => tx.objectSpace === core.space.Model)
const model = txes.filter((tx) => tx.objectSpace === core.space.Model)
const insert = await db.collection(DOMAIN_TX).insertMany(model as Document[])
console.log(`${insert.insertedCount} model transactions inserted.`)
} finally {
@ -90,8 +97,8 @@ export async function upgradeWorkspace (mongoUrl: string, dbName: string, client
}
/**
* @public
*/
* @public
*/
export async function dumpWorkspace (mongoUrl: string, dbName: string, fileName: string, minio: Client): Promise<void> {
const client = new MongoClient(mongoUrl)
try {
@ -108,7 +115,7 @@ export async function dumpWorkspace (mongoUrl: string, dbName: string, fileName:
const minioData: BucketItem[] = []
const list = await minio.listObjects(dbName, undefined, true)
await new Promise((resolve) => {
list.on('data', data => {
list.on('data', (data) => {
minioData.push(data)
})
list.on('end', () => {