diff --git a/plugins/gmail-resources/src/components/Chats.svelte b/plugins/gmail-resources/src/components/Chats.svelte index d9f2d66ca6..97307df18a 100644 --- a/plugins/gmail-resources/src/components/Chats.svelte +++ b/plugins/gmail-resources/src/components/Chats.svelte @@ -15,13 +15,14 @@ --> {#if channel && object} @@ -148,7 +152,14 @@ {:else if currentMessage} {:else} - 0} on:select={selectHandler} /> + 0} + on:select={selectHandler} + /> {/if} {/if} diff --git a/plugins/gmail-resources/src/components/NewMessages.svelte b/plugins/gmail-resources/src/components/NewMessages.svelte index 745a12d13d..791318d362 100644 --- a/plugins/gmail-resources/src/components/NewMessages.svelte +++ b/plugins/gmail-resources/src/components/NewMessages.svelte @@ -217,7 +217,7 @@ $: templateProvider && !Array.isArray(value) && templateProvider.set(contact.class.Contact, value) 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] }) diff --git a/plugins/gmail-resources/src/utils.ts b/plugins/gmail-resources/src/utils.ts index f53651dcb0..1c4c059dfb 100644 --- a/plugins/gmail-resources/src/utils.ts +++ b/plugins/gmail-resources/src/utils.ts @@ -1,13 +1,14 @@ import contact, { type Channel, - type Employee, type Contact, + type Employee, type PersonAccount, getName as getContactName } 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 { getClient } from '@hcengineering/presentation' +import { type Integration } from '@hcengineering/setting' import gmail from './plugin' export function getTime (time: number): string { @@ -72,6 +73,7 @@ export function convertMessages ( object: Contact, channel: Channel, messages: Message[], + integrations: Integration[], accounts: IdMap, employees: IdMap ): SharedMessage[] { @@ -79,8 +81,8 @@ export function convertMessages ( return { ...m, _id: m._id as string as Ref, - sender: getName(object, channel, m, accounts, employees, true), - receiver: getName(object, channel, m, accounts, employees, false) + sender: getName(object, channel, m, integrations, accounts, employees, true), + receiver: getName(object, channel, m, integrations, accounts, employees, false) } }) } @@ -89,15 +91,15 @@ export async function convertMessage ( object: Contact, channel: Channel, message: Message, + integrations: Integration[], + accounts: IdMap, employees: IdMap ): Promise { - const client = getClient() - const accounts = toIdMap(await client.findAll(contact.class.PersonAccount, {})) return { ...message, _id: message._id as string as Ref, - sender: getName(object, channel, message, accounts, employees, true), - receiver: getName(object, channel, message, accounts, employees, false) + sender: getName(object, channel, message, integrations, accounts, employees, true), + receiver: getName(object, channel, message, integrations, accounts, employees, false) } } @@ -105,6 +107,7 @@ export function getName ( object: Contact, channel: Channel, message: Message, + integrations: Integration[], accounts: IdMap, employees: IdMap, sender: boolean @@ -112,11 +115,12 @@ export function getName ( const h = getClient().getHierarchy() if (message._class === gmail.class.NewMessage) { if (!sender) return `${getContactName(h, object)} (${channel.value})` - const account = accounts.get(message.modifiedBy as Ref) + const from = (message.from ?? message.createdBy ?? message.modifiedBy) as Ref + const account = accounts.get(from) const emp = account != null ? employees.get(account?.person as Ref) : undefined - const email = account?.email - const from = accounts.get(message.from as Ref)?.email ?? message.from - return emp != null ? `${getContactName(h, emp)} (${email})` : from + const integration = integrations.find((p) => p.createdBy === from) + const email = integration?.value ?? integrations[0]?.value + return emp != null ? `${getContactName(h, emp)} (${email})` : email } if (message.incoming === sender) { return `${getContactName(h, object)} (${channel.value})` diff --git a/services/gmail/pod-gmail/src/gmail.ts b/services/gmail/pod-gmail/src/gmail.ts index 61cecdc53e..f8d84a1789 100644 --- a/services/gmail/pod-gmail/src/gmail.ts +++ b/services/gmail/pod-gmail/src/gmail.ts @@ -271,7 +271,7 @@ export class GmailClient { status: 'error', 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') { await this.refreshToken() } @@ -389,7 +389,7 @@ export class GmailClient { const controller = GmailController.getGmailController() controller.addClient(me, this) } 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) await this.getMe() } 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)) { await this.signout(true) } @@ -420,7 +420,7 @@ export class GmailClient { }) } } 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 ) } 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 ) } 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') { await this.workspace.signoutByUserId(this.user.userId, true) } else { @@ -771,7 +771,7 @@ export class GmailClient { } }) } 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' }) } catch (err) { - console.log('close error', JSON.stringify(err)) + console.log('close error', (err as any).message) } } } diff --git a/services/gmail/pod-gmail/src/main.ts b/services/gmail/pod-gmail/src/main.ts index 1004da41f0..1bf475adb7 100644 --- a/services/gmail/pod-gmail/src/main.ts +++ b/services/gmail/pod-gmail/src/main.ts @@ -68,7 +68,7 @@ export const main = async (): Promise => { const url = gmail.getAutUrl(redirectURL) res.send(url) } catch (err) { - console.log('signin error', JSON.stringify(err)) + console.log('signin error', (err as any).message) res.status(500).send() } }