Telegram attachments (#1127)

Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com>
This commit is contained in:
Denis Bykhov
2022-03-11 16:05:44 +07:00
committed by GitHub
parent 173c062369
commit 98bdcfce68
12 changed files with 176 additions and 91 deletions
+19 -5
View File
@@ -199,9 +199,25 @@ program
})
program
.command('clear-telegram-history')
.command('clear-telegram-history <workspace>')
.description('clear telegram history')
.option('-w, --workspace <workspace>', 'target workspace')
.action(async (workspace: string, 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)
}
console.log(`clearing ${workspace} history:`)
await clearTelegramHistory(mongodbUri, workspace, telegramDB, minio)
})
})
program
.command('clear-telegram-all-history')
.description('clear telegram history')
.action(async (cmd) => {
return await withDatabase(mongodbUri, async (db) => {
const telegramDB = process.env.TELEGRAM_DATABASE
@@ -211,12 +227,10 @@ program
}
const workspaces = await listWorkspaces(db)
const targetWorkspaces =
cmd.workspace !== undefined ? workspaces.filter((x) => x.workspace === cmd.workspace) : workspaces
for (const w of targetWorkspaces) {
for (const w of workspaces) {
console.log(`clearing ${w.workspace} history:`)
await clearTelegramHistory(mongodbUri, w.workspace, telegramDB)
await clearTelegramHistory(mongodbUri, w.workspace, telegramDB, minio)
}
})
})
+56 -6
View File
@@ -14,24 +14,60 @@
// limitations under the License.
//
import { MongoClient } from 'mongodb'
import { DOMAIN_TX } from '@anticrm/core'
import { DOMAIN_TX, Ref } from '@anticrm/core'
import { DOMAIN_ATTACHMENT } from '@anticrm/model-attachment'
import contact, { DOMAIN_CHANNEL } from '@anticrm/model-contact'
import { DOMAIN_TELEGRAM } from '@anticrm/model-telegram'
import telegram from '@anticrm/telegram'
import telegram, { SharedTelegramMessage, SharedTelegramMessages } from '@anticrm/telegram'
import { Client } from 'minio'
import { MongoClient } from 'mongodb'
const LastMessages = 'last-msgs'
/**
* @public
*/
export async function clearTelegramHistory (mongoUrl: string, workspace: string, tgDb: string): Promise<void> {
export async function clearTelegramHistory (
mongoUrl: string,
workspace: string,
tgDb: string,
minio: Client
): Promise<void> {
const client = new MongoClient(mongoUrl)
try {
await client.connect()
const workspaceDB = client.db(workspace)
const telegramDB = client.db(tgDb)
const sharedMessages = await workspaceDB
.collection(DOMAIN_TELEGRAM)
.find<SharedTelegramMessages>({
_class: telegram.class.SharedMessages
})
.toArray()
const sharedIds: Ref<SharedTelegramMessage>[] = []
for (const sharedMessage of sharedMessages) {
for (const message of sharedMessage.messages) {
sharedIds.push(message._id)
}
}
const files = await workspaceDB
.collection(DOMAIN_ATTACHMENT)
.find(
{
attachedToClass: telegram.class.Message,
attachedTo: { $nin: sharedIds }
},
{
projection: {
file: 1
}
}
)
.toArray()
const attachments = files.map((file) => file.file)
console.log('clearing txes and messages...')
await Promise.all([
workspaceDB.collection(DOMAIN_TX).deleteMany({
@@ -39,7 +75,21 @@ export async function clearTelegramHistory (mongoUrl: string, workspace: string,
}),
workspaceDB.collection(DOMAIN_TELEGRAM).deleteMany({
_class: telegram.class.Message
})
}),
workspaceDB.collection(DOMAIN_CHANNEL).updateMany(
{
provider: contact.channelProvider.Telegram
},
{
$set: {
items: 0
}
}
),
workspaceDB.collection(DOMAIN_ATTACHMENT).deleteMany({
attachedToClass: telegram.class.Message
}),
minio.removeObjects(workspace, Array.from(attachments))
])
console.log('clearing telegram service data...')