Files
huly-platform/plugins/telegram-resources/src/components/Chat.svelte
T

290 lines
8.4 KiB
Svelte

<!--
// 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.
-->
<script lang="ts">
import attachment from '@anticrm/attachment'
import { AttachmentRefInput } from '@anticrm/attachment-resources'
import contact, { Channel, Contact, EmployeeAccount, formatName } from '@anticrm/contact'
import { generateId, getCurrentAccount, Ref, SortingOrder, Space } from '@anticrm/core'
import { NotificationClientImpl } from '@anticrm/notification-resources'
import { createQuery, getClient } from '@anticrm/presentation'
import setting, { Integration } from '@anticrm/setting'
import type { NewTelegramMessage, SharedTelegramMessage, TelegramMessage } from '@anticrm/telegram'
import { Button, eventToHTMLElement, IconShare, Tooltip, Scroller, showPopup } from '@anticrm/ui'
import telegram from '../plugin'
import Connect from './Connect.svelte'
import TelegramIcon from './icons/Telegram.svelte'
import Messages from './Messages.svelte'
import Reconnect from './Reconnect.svelte'
export let object: Contact
let channel: Channel | undefined = undefined
let objectId: Ref<NewTelegramMessage> = generateId()
const client = getClient()
const notificationClient = NotificationClientImpl.getClient()
client
.findOne(contact.class.Channel, {
attachedTo: object._id,
provider: contact.channelProvider.Telegram
})
.then((res) => {
channel = res
})
let messages: TelegramMessage[] = []
let accounts: EmployeeAccount[] = []
let integration: Integration | undefined
let selected: Set<Ref<SharedTelegramMessage>> = new Set<Ref<SharedTelegramMessage>>()
let selectable = false
const messagesQuery = createQuery()
const accauntsQuery = createQuery()
const settingsQuery = createQuery()
const accountId = getCurrentAccount()._id
function updateMessagesQuery (channelId: Ref<Channel>): void {
messagesQuery.query(
telegram.class.Message,
{ attachedTo: channelId },
(res) => {
messages = res.reverse()
if (channel !== undefined) {
notificationClient.updateLastView(channel._id, channel._class, undefined, true)
}
const accountsIds = new Set(messages.map((p) => p.modifiedBy as Ref<EmployeeAccount>))
updateAccountsQuery(accountsIds)
},
{
sort: { modifiedOn: SortingOrder.Descending },
limit: 500,
lookup: {
_id: { attachments: attachment.class.Attachment }
}
}
)
}
$: channel && updateMessagesQuery(channel._id)
function updateAccountsQuery (accountsIds: Set<Ref<EmployeeAccount>>): void {
accauntsQuery.query(contact.class.EmployeeAccount, { _id: { $in: Array.from(accountsIds) } }, (result) => {
accounts = result
})
}
settingsQuery.query(
setting.class.Integration,
{ type: telegram.integrationType.Telegram, space: accountId as string as Ref<Space> },
(res) => {
integration = res[0]
}
)
async function onMessage (event: CustomEvent) {
if (channel === undefined) return
const { message, attachments } = event.detail
await client.createDoc(
telegram.class.NewMessage,
telegram.space.Telegram,
{
content: message,
status: 'new',
attachments,
attachedTo: channel._id,
attachedToClass: channel._class,
collection: 'newMessages'
},
objectId
)
objectId = generateId()
}
function getName (message: TelegramMessage, accounts: EmployeeAccount[]): string {
return message.incoming ? object.name : accounts.find((p) => p._id === message.modifiedBy)?.name ?? ''
}
async function share (): Promise<void> {
const selectedMessages = messages.filter((m) => selected.has(m._id as unknown as Ref<SharedTelegramMessage>))
await client.addCollection(
telegram.class.SharedMessages,
object.space,
object._id,
object._class,
'sharedTelegramMessages',
{
messages: convertMessages(selectedMessages, accounts)
}
)
if (channel !== undefined) {
await notificationClient.updateLastView(channel._id, channel._class, channel.modifiedOn, true)
}
clear()
}
function clear (): void {
selectable = false
selected.clear()
selected = selected
}
function convertMessages (messages: TelegramMessage[], accounts: EmployeeAccount[]): SharedTelegramMessage[] {
return messages.map((m) => {
return {
...m,
_id: m._id as unknown as Ref<SharedTelegramMessage>,
sender: getName(m, accounts)
}
})
}
async function onConnectClose (res: any): Promise<void> {
if (res?.value) {
await client.createDoc(setting.class.Integration, accountId as string as Ref<Space>, {
type: telegram.integrationType.Telegram,
value: res.value,
disabled: false
})
}
}
async function onReconnect (res: any): Promise<void> {
if (res?.value && integration !== undefined) {
await client.update(integration, {
disabled: false
})
}
}
</script>
<div class="telegram-header">
<div class="ac-header__wrap-title">
<div class="flex-center icon"><TelegramIcon size={'small'} /></div>
<div class="ac-header__wrap-description">
<span class="ac-header__title">Telegram</span>
<span class="ac-header__description">You and {formatName(object.name)}</span>
</div>
</div>
<Tooltip label={telegram.string.Share}>
<Button
icon={IconShare}
kind={'transparent'}
size={'medium'}
on:click={async () => {
selectable = !selectable
}}
/>
</Tooltip>
</div>
<div class="telegram-content" class:selectable>
<Scroller bottomStart autoscroll>
{#if messages && accounts}
<Messages messages={convertMessages(messages, accounts)} {selectable} bind:selected />
{/if}
</Scroller>
</div>
<div class="ref-input" class:selectable>
{#if selectable}
<div class="flex-between">
<span>{selected.size} messages selected</span>
<div class="flex">
<div>
<Button label={telegram.string.Cancel} size={'medium'} on:click={clear} />
</div>
<div class="ml-3">
<Button
label={telegram.string.PublishSelected}
size={'medium'}
kind={'primary'}
disabled={!selected.size}
on:click={share}
/>
</div>
</div>
</div>
{:else if integration === undefined}
<div class="flex-center">
<Button
label={telegram.string.Connect}
kind={'primary'}
on:click={(e) => {
showPopup(Connect, {}, eventToHTMLElement(e), onConnectClose)
}}
/>
</div>
{:else if integration.disabled}
<div class="flex-center">
<Button
label={setting.string.Reconnect}
kind={'primary'}
on:click={(e) => {
showPopup(Reconnect, {}, eventToHTMLElement(e), onReconnect)
}}
/>
</div>
{:else}
<AttachmentRefInput
space={telegram.space.Telegram}
_class={telegram.class.NewMessage}
{objectId}
on:message={onMessage}
/>
{/if}
</div>
<style lang="scss">
.telegram-header {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: nowrap;
margin: 0 2.5rem;
padding: 0;
height: 4rem;
min-height: 4rem;
border-bottom: 1px solid var(--divider-color);
}
.icon {
margin-right: 1rem;
width: 2.25rem;
height: 2.25rem;
color: var(--white-color);
background-color: var(--primary-bg-color);
border-radius: 50%;
}
.ref-input {
padding: 0 2.5rem 1.5rem;
&.selectable {
padding: 1rem 2.5rem;
color: var(--theme-caption-color);
background-color: var(--accent-bg-color);
border-top: 1px solid var(--theme-card-divider);
}
}
.telegram-content {
padding-bottom: 1.5rem;
min-height: 0;
height: 100%;
&.selectable { padding-bottom: 0; }
}
</style>