From a34150221b45951b56782652879594027c85c006 Mon Sep 17 00:00:00 2001 From: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> Date: Wed, 2 Feb 2022 15:07:19 +0600 Subject: [PATCH] Gmail public messages (#907) Signed-off-by: Denis Bykhov <80476319+BykhovDenis@users.noreply.github.com> --- .../src/components/Chats.svelte | 39 +++++++++++-------- .../src/components/Message.svelte | 7 ++-- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/plugins/gmail-resources/src/components/Chats.svelte b/plugins/gmail-resources/src/components/Chats.svelte index 6c73d59f49..f91dbdddf4 100644 --- a/plugins/gmail-resources/src/components/Chats.svelte +++ b/plugins/gmail-resources/src/components/Chats.svelte @@ -30,29 +30,31 @@ export let channel: Channel export let newMessage: boolean + const EMAIL_REGEX = /(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/ + let messages: Message[] = [] - let account: EmployeeAccount | undefined + let accounts: EmployeeAccount[] = [] let enabled: boolean let selected: Set> = new Set>() let selectable = false - let me = '' const messagesQuery = createQuery() - const accauntQuery = createQuery() + const accauntsQuery = createQuery() const settingsQuery = createQuery() const accountId = getCurrentAccount()._id $: messagesQuery.query( gmail.class.Message, - { modifiedBy: accountId, attachedTo: channel._id }, + { attachedTo: channel._id }, (res) => { messages = res }, { sort: { modifiedOn: SortingOrder.Descending } } ) - $: accauntQuery.query(contact.class.EmployeeAccount, { _id: accountId as Ref }, (result) => { - account = result[0] + $: accountsIds = messages.map((p) => p.modifiedBy as Ref) + $: accauntsQuery.query(contact.class.EmployeeAccount, { _id: { $in: accountsIds }}, (result) => { + accounts = result }) $: settingsQuery.query( @@ -60,7 +62,6 @@ { type: gmail.integrationType.Gmail, space: accountId as string as Ref }, (res) => { enabled = res.length > 0 - me = res[0].value } ) const client = getClient() @@ -68,7 +69,7 @@ async function share (): Promise { const selectedMessages = messages.filter((m) => selected.has(m._id as string as Ref)) await client.addCollection(gmail.class.SharedMessages, object.space, object._id, object._class, 'gmailSharedMessages', { - messages: convertMessages(selectedMessages) + messages: convertMessages(selectedMessages, accounts) }) clear() } @@ -79,21 +80,27 @@ selected = selected } - function convertMessages (messages: Message[]): SharedMessage[] { + function convertMessages (messages: Message[], accounts: EmployeeAccount[]): SharedMessage[] { return messages.map((m) => { return { ...m, _id: m._id as string as Ref, - sender: account ? getName(m, account, true) : '', - receiver: account ? getName(m, account, false) : '' + sender: getName(m, accounts, true), + receiver: getName(m, accounts, false) } }) } - function getName (message: Message, account: EmployeeAccount, sender: boolean): string { - return message.incoming === sender - ? `${formatName(object.name)} (${channel.value})` - : `${formatName(account.name)} (${me})` + + function getName (message: Message, accounts: EmployeeAccount[], sender: boolean): string { + if (message.incoming === sender) { + return `${formatName(object.name)} (${channel.value})` + } else { + const account = accounts.find((p) => p._id === message.modifiedBy) + const value = message.incoming ? message.to : message.from + const email = value.match(EMAIL_REGEX) + return account ? `${formatName(account.name)} (${email?.[0] ?? value})` : email?.[0] ?? value + } } @@ -157,7 +164,7 @@
{#if messages} - + {/if}
diff --git a/plugins/gmail-resources/src/components/Message.svelte b/plugins/gmail-resources/src/components/Message.svelte index 8e360292a2..b16e665d58 100644 --- a/plugins/gmail-resources/src/components/Message.svelte +++ b/plugins/gmail-resources/src/components/Message.svelte @@ -15,7 +15,6 @@ -->