From 98bdcfce68b6a19dcbfa42b78838bb0e9d248f4c Mon Sep 17 00:00:00 2001 From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> Date: Fri, 11 Mar 2022 15:05:44 +0600 Subject: [PATCH] Telegram attachments (#1127) Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> --- dev/tool/package.json | 1 + dev/tool/src/index.ts | 24 ++++- dev/tool/src/telegram.ts | 62 +++++++++-- models/telegram/package.json | 1 + models/telegram/src/index.ts | 28 ++++- models/telegram/src/plugin.ts | 3 +- plugins/telegram-assets/lang/en.json | 3 +- plugins/telegram-assets/lang/ru.json | 3 +- plugins/telegram-resources/package.json | 4 +- .../src/components/Chat.svelte | 100 +++++++----------- .../src/components/Message.svelte | 18 ++-- plugins/telegram/src/index.ts | 20 +++- 12 files changed, 176 insertions(+), 91 deletions(-) diff --git a/dev/tool/package.json b/dev/tool/package.json index e110f3a966..e4a8b48737 100644 --- a/dev/tool/package.json +++ b/dev/tool/package.json @@ -61,6 +61,7 @@ "@anticrm/server-core": "~0.6.1", "@anticrm/server-token": "~0.6.0", "@anticrm/model-attachment": "~0.6.0", + "@anticrm/model-contact": "~0.6.0", "@anticrm/mongo": "~0.6.0", "@anticrm/dev-storage": "~0.6.0", "@anticrm/server-attachment": "~0.6.0", diff --git a/dev/tool/src/index.ts b/dev/tool/src/index.ts index 03cc68d574..334b47e8d4 100644 --- a/dev/tool/src/index.ts +++ b/dev/tool/src/index.ts @@ -199,9 +199,25 @@ program }) program - .command('clear-telegram-history') + .command('clear-telegram-history ') .description('clear telegram history') .option('-w, --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) } }) }) diff --git a/dev/tool/src/telegram.ts b/dev/tool/src/telegram.ts index 4ae7f8c04f..c7491da144 100644 --- a/dev/tool/src/telegram.ts +++ b/dev/tool/src/telegram.ts @@ -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 { +export async function clearTelegramHistory ( + mongoUrl: string, + workspace: string, + tgDb: string, + minio: Client +): Promise { 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({ + _class: telegram.class.SharedMessages + }) + .toArray() + const sharedIds: Ref[] = [] + 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...') diff --git a/models/telegram/package.json b/models/telegram/package.json index a38a45ad2c..3524e4fb29 100644 --- a/models/telegram/package.json +++ b/models/telegram/package.json @@ -30,6 +30,7 @@ "@anticrm/core": "~0.6.0", "@anticrm/platform": "~0.6.5", "@anticrm/model-core": "~0.6.0", + "@anticrm/model-attachment": "~0.6.0", "@anticrm/model-contact": "~0.6.1", "@anticrm/telegram": "~0.6.0", "@anticrm/telegram-resources": "~0.6.0", diff --git a/models/telegram/src/index.ts b/models/telegram/src/index.ts index 15fd040e70..db2413c18e 100644 --- a/models/telegram/src/index.ts +++ b/models/telegram/src/index.ts @@ -14,14 +14,20 @@ // limitations under the License. // -import { Builder, Model, TypeString, TypeBoolean, Prop, ArrOf, Index } from '@anticrm/model' +import { Builder, Model, TypeString, TypeBoolean, Prop, ArrOf, Index, Collection } from '@anticrm/model' import core, { TAttachedDoc } from '@anticrm/model-core' import contact from '@anticrm/model-contact' import telegram from './plugin' -import type { TelegramMessage, SharedTelegramMessage, SharedTelegramMessages } from '@anticrm/telegram' +import type { + TelegramMessage, + NewTelegramMessage, + SharedTelegramMessage, + SharedTelegramMessages +} from '@anticrm/telegram' import { Domain, IndexKind, Type } from '@anticrm/core' import setting from '@anticrm/setting' import activity from '@anticrm/activity' +import attachment from '@anticrm/model-attachment' export const DOMAIN_TELEGRAM = 'telegram' as Domain @@ -37,6 +43,22 @@ export class TTelegramMessage extends TAttachedDoc implements TelegramMessage { @Prop(TypeBoolean(), telegram.string.Incoming) incoming!: boolean + + @Prop(Collection(attachment.class.Attachment), attachment.string.Attachments) + attachments?: number +} + +@Model(telegram.class.NewMessage, core.class.AttachedDoc, DOMAIN_TELEGRAM) +export class TNewTelegramMessage extends TAttachedDoc implements NewTelegramMessage { + @Prop(TypeString(), telegram.string.Content) + @Index(IndexKind.FullText) + content!: string + + @Prop(TypeString(), telegram.string.Status) + status!: 'new' | 'sent' + + @Prop(Collection(attachment.class.Attachment), attachment.string.Attachments) + attachments?: number } @Model(telegram.class.SharedMessages, core.class.AttachedDoc, DOMAIN_TELEGRAM) @@ -46,7 +68,7 @@ export class TSharedTelegramMessages extends TAttachedDoc implements SharedTeleg } export function createModel (builder: Builder): void { - builder.createModel(TTelegramMessage, TSharedTelegramMessages) + builder.createModel(TTelegramMessage, TSharedTelegramMessages, TNewTelegramMessage) builder.createDoc( contact.class.ChannelProvider, diff --git a/models/telegram/src/plugin.ts b/models/telegram/src/plugin.ts index e459c3efac..bfb218b8f2 100644 --- a/models/telegram/src/plugin.ts +++ b/models/telegram/src/plugin.ts @@ -29,7 +29,8 @@ export default mergeIds(telegramId, telegram, { Incoming: '' as IntlString, Messages: '' as IntlString, Telegram: '' as IntlString, - TelegramIntegrationDesc: '' as IntlString + TelegramIntegrationDesc: '' as IntlString, + Status: '' as IntlString }, ids: { TxSharedCreate: '' as Ref diff --git a/plugins/telegram-assets/lang/en.json b/plugins/telegram-assets/lang/en.json index a70b20bc90..bdaa28a3ab 100644 --- a/plugins/telegram-assets/lang/en.json +++ b/plugins/telegram-assets/lang/en.json @@ -20,6 +20,7 @@ "Incoming": "Incoming", "Messages": "Messages", "Telegram": "Telegram", - "TelegramIntegrationDesc": "Use telegram integration" + "TelegramIntegrationDesc": "Use telegram integration", + "Status": "Status" } } \ No newline at end of file diff --git a/plugins/telegram-assets/lang/ru.json b/plugins/telegram-assets/lang/ru.json index 462ce67f97..5ac27ef965 100644 --- a/plugins/telegram-assets/lang/ru.json +++ b/plugins/telegram-assets/lang/ru.json @@ -20,6 +20,7 @@ "Incoming": "Входящее", "Messages": "Сообщения", "Telegram": "Telegram", - "TelegramIntegrationDesc": "Подключить Telegram" + "TelegramIntegrationDesc": "Подключить Telegram", + "Status": "Статус" } } \ No newline at end of file diff --git a/plugins/telegram-resources/package.json b/plugins/telegram-resources/package.json index c642d86057..542edccf27 100644 --- a/plugins/telegram-resources/package.json +++ b/plugins/telegram-resources/package.json @@ -41,6 +41,8 @@ "@anticrm/chunter": "~0.6.0", "@anticrm/login": "~0.6.1", "@anticrm/core": "~0.6.11", - "@anticrm/notification-resources": "~0.6.0" + "@anticrm/notification-resources": "~0.6.0", + "@anticrm/attachment": "~0.6.0", + "@anticrm/attachment-resources": "~0.6.0" } } diff --git a/plugins/telegram-resources/src/components/Chat.svelte b/plugins/telegram-resources/src/components/Chat.svelte index 80c0166d5a..3f9484dae6 100644 --- a/plugins/telegram-resources/src/components/Chat.svelte +++ b/plugins/telegram-resources/src/components/Chat.svelte @@ -14,23 +14,24 @@ // limitations under the License. --> @@ -32,7 +35,7 @@
{ + on:click={() => { dispatch('select', message) }} > @@ -41,6 +44,9 @@ {#if showName}
{formatName(message.sender)}
{/if} + {#if attachments} + + {/if}
diff --git a/plugins/telegram/src/index.ts b/plugins/telegram/src/index.ts index 2b0303d336..e6bee05d3f 100644 --- a/plugins/telegram/src/index.ts +++ b/plugins/telegram/src/index.ts @@ -22,16 +22,29 @@ import type { IntegrationType, Handler } from '@anticrm/setting' /** * @public */ -export interface TelegramMessage extends AttachedDoc { +export interface BaseTelegramMessage extends Doc { content: string + attachments?: number +} + +/** + * @public + */ +export interface TelegramMessage extends BaseTelegramMessage, AttachedDoc { incoming: boolean } /** * @public */ -export interface SharedTelegramMessage extends Doc { - content: string +export interface NewTelegramMessage extends BaseTelegramMessage, AttachedDoc { + status: 'new' | 'sent' +} + +/** + * @public + */ +export interface SharedTelegramMessage extends BaseTelegramMessage { incoming: boolean sender: string } @@ -62,6 +75,7 @@ export default plugin(telegramId, { }, class: { Message: '' as Ref>, + NewMessage: '' as Ref>, SharedMessage: '' as Ref>, SharedMessages: '' as Ref> },