Add integration disconnect handler (#460)

Signed-off-by: Ilya Sumbatyants <ilya.sumb@gmail.com>
This commit is contained in:
Ilya Sumbatyants 2021-12-01 18:04:52 +07:00 committed by GitHub
parent 2f1733cd47
commit c44f384644
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 40 additions and 10 deletions

View File

@ -17,7 +17,7 @@ import { Builder, Model } from '@anticrm/model'
import { Ref, Domain, DOMAIN_MODEL } from '@anticrm/core'
import core, { TDoc } from '@anticrm/model-core'
import setting from '@anticrm/setting'
import type { Integration, IntegrationType } from '@anticrm/setting'
import type { Integration, IntegrationType, Handler } from '@anticrm/setting'
import type { IntlString } from '@anticrm/platform'
import workbench from '@anticrm/model-workbench'
@ -37,6 +37,7 @@ export class TIntegrationType extends TDoc implements IntegrationType {
description!: IntlString
icon!: AnyComponent
createComponent!: AnyComponent
onDisconnect!: Handler
}
export function createModel (builder: Builder): void {
@ -87,6 +88,7 @@ export function createModel (builder: Builder): void {
label: 'Email',
description: 'Use email integration' as IntlString,
icon: setting.component.IconGmail,
createComponent: setting.component.ConnectEmail
createComponent: setting.component.ConnectEmail,
onDisconnect: setting.handler.EmailDisconnectHandler
})
}

View File

@ -53,7 +53,8 @@ export function createModel (builder: Builder): void {
label: 'Telegram',
description: 'Use telegram integration' as IntlString,
icon: telegram.component.IconTelegram,
createComponent: telegram.component.Connect
createComponent: telegram.component.Connect,
onDisconnect: telegram.handler.DisconnectHandler
}, telegram.integrationType.Telegram)
builder.createDoc(core.class.Space, core.space.Model, {

View File

@ -14,7 +14,7 @@
-->
<script lang="ts">
import { Button, Component, Label, Link } from '@anticrm/ui'
import { getMetadata } from '@anticrm/platform'
import { getMetadata, getResource } from '@anticrm/platform'
import { showPopup } from '@anticrm/ui'
import type { Integration, IntegrationType } from '@anticrm/setting'
import setting from '@anticrm/setting'
@ -26,6 +26,7 @@
export let integration: Integration | undefined
const accountId = getMetadata(login.metadata.LoginEmail)
const client = getClient()
const onDisconnectP = getResource(integrationType.onDisconnect)
async function close(res: any): Promise<void> {
if (res.value) {
@ -38,9 +39,7 @@
async function disconnect(): Promise<void> {
if (integration !== undefined) {
// TODO: Need to call proper endpoint, so likely should not be here but in specific integration plugins
// await client.removeDoc(setting.class.Integration, accountId as Ref<Space>, integration._id)
await (await onDisconnectP)()
}
}
</script>

View File

@ -22,5 +22,8 @@ export default async () => ({
Integrations,
ConnectEmail,
IconGmail
},
handler: {
EmailDisconnectHandler: async () => {}
}
})

View File

@ -13,11 +13,16 @@
// limitations under the License.
//
import { Asset, IntlString, plugin } from '@anticrm/platform'
import { Asset, IntlString, plugin, Resource } from '@anticrm/platform'
import type { Plugin } from '@anticrm/platform'
import { AnyComponent } from '@anticrm/ui'
import type { Class, Doc, Ref } from '@anticrm/core'
/**
* @public
*/
export type Handler = Resource<() => Promise<void>>
/**
* @public
*/
@ -26,6 +31,7 @@ export interface IntegrationType extends Doc {
description: IntlString
icon: AnyComponent
createComponent: AnyComponent
onDisconnect: Handler
}
/**
@ -58,6 +64,9 @@ export default plugin(settingId, {
ConnectEmail: '' as AnyComponent,
IconGmail: '' as AnyComponent
},
handler: {
EmailDisconnectHandler: '' as Handler
},
string: {
Setting: '' as IntlString,
Integrations: '' as IntlString,

View File

@ -14,7 +14,8 @@
// limitations under the License.
//
import { Resources } from '@anticrm/platform'
import { getMetadata, Resources } from '@anticrm/platform'
import login from '@anticrm/login'
import Chat from './components/Chat.svelte'
import Connect from './components/Connect.svelte'
import IconTelegram from './components/icons/TelegramColor.svelte'
@ -24,5 +25,17 @@ export default async (): Promise<Resources> => ({
Chat,
Connect,
IconTelegram
},
handler: {
DisconnectHandler: async () => {
const url = getMetadata(login.metadata.TelegramUrl) ?? ''
await fetch(url + '/signout', {
method: 'POST',
headers: {
Authorization: 'Bearer ' + (getMetadata(login.metadata.LoginToken) ?? ''),
'Content-Type': 'application/json'
}
})
}
}
})

View File

@ -17,7 +17,7 @@ import { plugin } from '@anticrm/platform'
import type { Plugin } from '@anticrm/platform'
import type { Doc, Ref, Class, Space } from '@anticrm/core'
import type { AnyComponent } from '@anticrm/ui'
import type { IntegrationType } from '@anticrm/setting'
import type { IntegrationType, Handler } from '@anticrm/setting'
/**
* @public
@ -43,6 +43,9 @@ export default plugin(telegramId, {
integrationType: {
Telegram: '' as Ref<IntegrationType>
},
handler: {
DisconnectHandler: '' as Handler
},
class: {
Message: '' as Ref<Class<TelegramMessage>>
},