Minor gmail fixes (#6431)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2024-08-29 14:46:58 +05:00 committed by GitHub
parent 4ff8a4559f
commit 63aa456011
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 63 additions and 32 deletions

View File

@ -15,13 +15,14 @@
--> -->
<script lang="ts"> <script lang="ts">
import { Channel, Contact } from '@hcengineering/contact' import { Channel, Contact } from '@hcengineering/contact'
import { personAccountByIdStore, employeeByIdStore } from '@hcengineering/contact-resources' import { employeeByIdStore, personAccountByIdStore } from '@hcengineering/contact-resources'
import { Ref, SortingOrder } from '@hcengineering/core' import { Ref, SortingOrder } from '@hcengineering/core'
import { Message, SharedMessage } from '@hcengineering/gmail' import { Message, SharedMessage } from '@hcengineering/gmail'
import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources'
import { createQuery, getClient } from '@hcengineering/presentation' import { createQuery, getClient } from '@hcengineering/presentation'
import plugin, { Button, Icon, IconShare, Label, Scroller } from '@hcengineering/ui' import plugin, { Button, Icon, IconShare, Label, Scroller } from '@hcengineering/ui'
import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources'
import { Integration } from '@hcengineering/setting'
import gmail from '../plugin' import gmail from '../plugin'
import { convertMessages } from '../utils' import { convertMessages } from '../utils'
import Messages from './Messages.svelte' import Messages from './Messages.svelte'
@ -31,6 +32,7 @@
export let channel: Channel export let channel: Channel
export let newMessage: boolean export let newMessage: boolean
export let enabled: boolean export let enabled: boolean
export let allIntegrations: Integration[]
let plainMessages: Message[] = [] let plainMessages: Message[] = []
let newMessages: Message[] = [] let newMessages: Message[] = []
@ -81,7 +83,14 @@
object._class, object._class,
'gmailSharedMessages', 'gmailSharedMessages',
{ {
messages: convertMessages(object, channel, selectedMessages, $personAccountByIdStore, $employeeByIdStore) messages: convertMessages(
object,
channel,
selectedMessages,
allIntegrations,
$personAccountByIdStore,
$employeeByIdStore
)
} }
) )
await inboxClient.readDoc(getClient(), channel._id) await inboxClient.readDoc(getClient(), channel._id)
@ -129,7 +138,14 @@
<div class="antiVSpacer x2" /> <div class="antiVSpacer x2" />
<Scroller padding={'.5rem 1rem'}> <Scroller padding={'.5rem 1rem'}>
<Messages <Messages
messages={convertMessages(object, channel, messages, $personAccountByIdStore, $employeeByIdStore)} messages={convertMessages(
object,
channel,
messages,
allIntegrations,
$personAccountByIdStore,
$employeeByIdStore
)}
{selectable} {selectable}
bind:selected bind:selected
on:select on:select

View File

@ -15,7 +15,7 @@
--> -->
<script lang="ts"> <script lang="ts">
import contact, { Channel, Contact, getName } from '@hcengineering/contact' import contact, { Channel, Contact, getName } from '@hcengineering/contact'
import { employeeByIdStore } from '@hcengineering/contact-resources' import { employeeByIdStore, personAccountByIdStore } from '@hcengineering/contact-resources'
import { getCurrentAccount, Ref } from '@hcengineering/core' import { getCurrentAccount, Ref } from '@hcengineering/core'
import { Message, SharedMessage } from '@hcengineering/gmail' import { Message, SharedMessage } from '@hcengineering/gmail'
import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources' import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources'
@ -23,7 +23,7 @@
import { createQuery, getClient } from '@hcengineering/presentation' import { createQuery, getClient } from '@hcengineering/presentation'
import setting, { Integration } from '@hcengineering/setting' import setting, { Integration } from '@hcengineering/setting'
import templates, { TemplateDataProvider } from '@hcengineering/templates' import templates, { TemplateDataProvider } from '@hcengineering/templates'
import { Button, Icon, Label, Dialog, eventToHTMLElement, showPopup } from '@hcengineering/ui' import { Button, Dialog, eventToHTMLElement, Icon, Label, showPopup } from '@hcengineering/ui'
import { createEventDispatcher, onDestroy } from 'svelte' import { createEventDispatcher, onDestroy } from 'svelte'
import gmail from '../plugin' import gmail from '../plugin'
import { convertMessage } from '../utils' import { convertMessage } from '../utils'
@ -48,6 +48,7 @@
let currentMessage: SharedMessage | undefined = undefined let currentMessage: SharedMessage | undefined = undefined
let newMessage: boolean = false let newMessage: boolean = false
let allIntegrations: Integration[] = []
let integrations: Integration[] = [] let integrations: Integration[] = []
let selectedIntegration: Integration | undefined = undefined let selectedIntegration: Integration | undefined = undefined
@ -95,15 +96,18 @@
$: templateProvider && selectedIntegration && templateProvider.set(setting.class.Integration, selectedIntegration) $: templateProvider && selectedIntegration && templateProvider.set(setting.class.Integration, selectedIntegration)
settingsQuery.query(setting.class.Integration, { type: gmail.integrationType.Gmail, disabled: false }, (res) => { settingsQuery.query(setting.class.Integration, { type: gmail.integrationType.Gmail }, (res) => {
integrations = res.filter((p) => p.createdBy === me || p.shared?.includes(me)) allIntegrations = res.filter((p) => !p.disabled && p.value !== '')
integrations = allIntegrations.filter((p) => p.createdBy === me || p.shared?.includes(me))
selectedIntegration = integrations.find((p) => p.createdBy === me) ?? integrations[0] selectedIntegration = integrations.find((p) => p.createdBy === me) ?? integrations[0]
}) })
$: gmailMessage && $: gmailMessage &&
channel && channel &&
object && object &&
convertMessage(object, channel, gmailMessage, $employeeByIdStore).then((p) => (currentMessage = p)) convertMessage(object, channel, gmailMessage, allIntegrations, $personAccountByIdStore, $employeeByIdStore).then(
(p) => (currentMessage = p)
)
</script> </script>
{#if channel && object} {#if channel && object}
@ -148,7 +152,14 @@
{:else if currentMessage} {:else if currentMessage}
<FullMessage {currentMessage} bind:newMessage on:close={back} /> <FullMessage {currentMessage} bind:newMessage on:close={back} />
{:else} {:else}
<Chats {object} {channel} bind:newMessage enabled={integrations.length > 0} on:select={selectHandler} /> <Chats
{object}
{channel}
bind:newMessage
{allIntegrations}
enabled={integrations.length > 0}
on:select={selectHandler}
/>
{/if} {/if}
</Dialog> </Dialog>
{/if} {/if}

View File

@ -217,7 +217,7 @@
$: templateProvider && !Array.isArray(value) && templateProvider.set(contact.class.Contact, value) $: templateProvider && !Array.isArray(value) && templateProvider.set(contact.class.Contact, value)
settingsQuery.query(setting.class.Integration, { type: plugin.integrationType.Gmail, disabled: false }, (res) => { settingsQuery.query(setting.class.Integration, { type: plugin.integrationType.Gmail, disabled: false }, (res) => {
integrations = res.filter((p) => p.createdBy === me || p.shared?.includes(me)) integrations = res.filter((p) => p.createdBy === me || (p.shared?.includes(me) && p.value !== ''))
selectedIntegration = integrations.find((p) => p.createdBy === me) ?? integrations[0] selectedIntegration = integrations.find((p) => p.createdBy === me) ?? integrations[0]
}) })

View File

@ -1,13 +1,14 @@
import contact, { import contact, {
type Channel, type Channel,
type Employee,
type Contact, type Contact,
type Employee,
type PersonAccount, type PersonAccount,
getName as getContactName getName as getContactName
} from '@hcengineering/contact' } from '@hcengineering/contact'
import { type Client, type Doc, type IdMap, type Ref, toIdMap } from '@hcengineering/core' import { type Client, type Doc, type IdMap, type Ref } from '@hcengineering/core'
import { type Message, type SharedMessage } from '@hcengineering/gmail' import { type Message, type SharedMessage } from '@hcengineering/gmail'
import { getClient } from '@hcengineering/presentation' import { getClient } from '@hcengineering/presentation'
import { type Integration } from '@hcengineering/setting'
import gmail from './plugin' import gmail from './plugin'
export function getTime (time: number): string { export function getTime (time: number): string {
@ -72,6 +73,7 @@ export function convertMessages (
object: Contact, object: Contact,
channel: Channel, channel: Channel,
messages: Message[], messages: Message[],
integrations: Integration[],
accounts: IdMap<PersonAccount>, accounts: IdMap<PersonAccount>,
employees: IdMap<Employee> employees: IdMap<Employee>
): SharedMessage[] { ): SharedMessage[] {
@ -79,8 +81,8 @@ export function convertMessages (
return { return {
...m, ...m,
_id: m._id as string as Ref<SharedMessage>, _id: m._id as string as Ref<SharedMessage>,
sender: getName(object, channel, m, accounts, employees, true), sender: getName(object, channel, m, integrations, accounts, employees, true),
receiver: getName(object, channel, m, accounts, employees, false) receiver: getName(object, channel, m, integrations, accounts, employees, false)
} }
}) })
} }
@ -89,15 +91,15 @@ export async function convertMessage (
object: Contact, object: Contact,
channel: Channel, channel: Channel,
message: Message, message: Message,
integrations: Integration[],
accounts: IdMap<PersonAccount>,
employees: IdMap<Employee> employees: IdMap<Employee>
): Promise<SharedMessage> { ): Promise<SharedMessage> {
const client = getClient()
const accounts = toIdMap(await client.findAll(contact.class.PersonAccount, {}))
return { return {
...message, ...message,
_id: message._id as string as Ref<SharedMessage>, _id: message._id as string as Ref<SharedMessage>,
sender: getName(object, channel, message, accounts, employees, true), sender: getName(object, channel, message, integrations, accounts, employees, true),
receiver: getName(object, channel, message, accounts, employees, false) receiver: getName(object, channel, message, integrations, accounts, employees, false)
} }
} }
@ -105,6 +107,7 @@ export function getName (
object: Contact, object: Contact,
channel: Channel, channel: Channel,
message: Message, message: Message,
integrations: Integration[],
accounts: IdMap<PersonAccount>, accounts: IdMap<PersonAccount>,
employees: IdMap<Employee>, employees: IdMap<Employee>,
sender: boolean sender: boolean
@ -112,11 +115,12 @@ export function getName (
const h = getClient().getHierarchy() const h = getClient().getHierarchy()
if (message._class === gmail.class.NewMessage) { if (message._class === gmail.class.NewMessage) {
if (!sender) return `${getContactName(h, object)} (${channel.value})` if (!sender) return `${getContactName(h, object)} (${channel.value})`
const account = accounts.get(message.modifiedBy as Ref<PersonAccount>) const from = (message.from ?? message.createdBy ?? message.modifiedBy) as Ref<PersonAccount>
const account = accounts.get(from)
const emp = account != null ? employees.get(account?.person as Ref<Employee>) : undefined const emp = account != null ? employees.get(account?.person as Ref<Employee>) : undefined
const email = account?.email const integration = integrations.find((p) => p.createdBy === from)
const from = accounts.get(message.from as Ref<PersonAccount>)?.email ?? message.from const email = integration?.value ?? integrations[0]?.value
return emp != null ? `${getContactName(h, emp)} (${email})` : from return emp != null ? `${getContactName(h, emp)} (${email})` : email
} }
if (message.incoming === sender) { if (message.incoming === sender) {
return `${getContactName(h, object)} (${channel.value})` return `${getContactName(h, object)} (${channel.value})`

View File

@ -271,7 +271,7 @@ export class GmailClient {
status: 'error', status: 'error',
error: JSON.stringify(err) error: JSON.stringify(err)
}) })
console.log('Create message error', this.user.workspace, this.user.userId, JSON.stringify(err)) console.log('Create message error', this.user.workspace, this.user.userId, err.message)
if (err?.response?.data?.error === 'invalid_grant') { if (err?.response?.data?.error === 'invalid_grant') {
await this.refreshToken() await this.refreshToken()
} }
@ -389,7 +389,7 @@ export class GmailClient {
const controller = GmailController.getGmailController() const controller = GmailController.getGmailController()
controller.addClient(me, this) controller.addClient(me, this)
} catch (err) { } catch (err) {
console.log('Add client error', this.user.workspace, this.user.userId, JSON.stringify(err)) console.log('Add client error', this.user.workspace, this.user.userId, (err as any).message)
} }
} }
@ -398,7 +398,7 @@ export class GmailClient {
this.oAuth2Client.setCredentials(token) this.oAuth2Client.setCredentials(token)
await this.getMe() await this.getMe()
} catch (err: any) { } catch (err: any) {
console.log('Set token error', this.user.workspace, this.user.userId, JSON.stringify(err)) console.log('Set token error', this.user.workspace, this.user.userId, err.message)
if (this.checkError(err)) { if (this.checkError(err)) {
await this.signout(true) await this.signout(true)
} }
@ -420,7 +420,7 @@ export class GmailClient {
}) })
} }
} catch (err) { } catch (err) {
console.log('update token error', this.user.workspace, this.user.userId, JSON.stringify(err)) console.log('update token error', this.user.workspace, this.user.userId, (err as any).message)
} }
} }
@ -640,7 +640,7 @@ export class GmailClient {
data data
) )
} catch (err: any) { } catch (err: any) {
console.log('Add attachment error', this.user.workspace, this.user.userId, JSON.stringify(err)) console.log('Add attachment error', this.user.workspace, this.user.userId, err.message)
} }
} }
@ -746,7 +746,7 @@ export class GmailClient {
30 * 60 * 1000 30 * 60 * 1000
) )
} catch (err: any) { } catch (err: any) {
console.log("Couldn't refresh token, error:", JSON.stringify(err)) console.log("Couldn't refresh token, error:", err.message)
if (err?.response?.data?.error === 'invalid_grant') { if (err?.response?.data?.error === 'invalid_grant') {
await this.workspace.signoutByUserId(this.user.userId, true) await this.workspace.signoutByUserId(this.user.userId, true)
} else { } else {
@ -771,7 +771,7 @@ export class GmailClient {
} }
}) })
} catch (err) { } catch (err) {
console.log('Watch error', JSON.stringify(err)) console.log('Watch error', (err as any).message)
} }
} }
@ -838,7 +838,7 @@ export class GmailClient {
userId: 'me' userId: 'me'
}) })
} catch (err) { } catch (err) {
console.log('close error', JSON.stringify(err)) console.log('close error', (err as any).message)
} }
} }
} }

View File

@ -68,7 +68,7 @@ export const main = async (): Promise<void> => {
const url = gmail.getAutUrl(redirectURL) const url = gmail.getAutUrl(redirectURL)
res.send(url) res.send(url)
} catch (err) { } catch (err) {
console.log('signin error', JSON.stringify(err)) console.log('signin error', (err as any).message)
res.status(500).send() res.status(500).send()
} }
} }