From 7d8880a0cd8b48d37ab5eb92aacc751ab3f73c77 Mon Sep 17 00:00:00 2001 From: Ilya Sumbatyants Date: Thu, 2 Dec 2021 21:21:41 +0700 Subject: [PATCH] Add clear-telegram-history command (#497) Signed-off-by: Ilya Sumbatyants --- dev/tool/package.json | 6 +++-- dev/tool/src/index.ts | 35 +++++++++++++++++++++++++- dev/tool/src/telegram.ts | 52 +++++++++++++++++++++++++++++++++++++++ dev/tool/src/workspace.ts | 23 +++++++++++------ 4 files changed, 105 insertions(+), 11 deletions(-) create mode 100644 dev/tool/src/telegram.ts diff --git a/dev/tool/package.json b/dev/tool/package.json index 865cbf1739..012817b5b4 100644 --- a/dev/tool/package.json +++ b/dev/tool/package.json @@ -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" } } diff --git a/dev/tool/src/index.ts b/dev/tool/src/index.ts index 48851ecb7e..de5428b86e 100644 --- a/dev/tool/src/index.ts +++ b/dev/tool/src/index.ts @@ -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 ', '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) diff --git a/dev/tool/src/telegram.ts b/dev/tool/src/telegram.ts new file mode 100644 index 0000000000..4ae7f8c04f --- /dev/null +++ b/dev/tool/src/telegram.ts @@ -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 { + 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() + } +} diff --git a/dev/tool/src/workspace.ts b/dev/tool/src/workspace.ts index bd56e311f9..f528a63e62 100644 --- a/dev/tool/src/workspace.ts +++ b/dev/tool/src/workspace.ts @@ -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 { +export async function upgradeWorkspace ( + mongoUrl: string, + dbName: string, + clientUrl: string, + minio: Client +): Promise { 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 { 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', () => {