Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor gmail fixes #6431

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions plugins/gmail-resources/src/components/Chats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
-->
<script lang="ts">
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 { Message, SharedMessage } from '@hcengineering/gmail'
import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources'
import { createQuery, getClient } from '@hcengineering/presentation'
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 { convertMessages } from '../utils'
import Messages from './Messages.svelte'
Expand All @@ -31,6 +32,7 @@
export let channel: Channel
export let newMessage: boolean
export let enabled: boolean
export let allIntegrations: Integration[]

let plainMessages: Message[] = []
let newMessages: Message[] = []
Expand Down Expand Up @@ -81,7 +83,14 @@
object._class,
'gmailSharedMessages',
{
messages: convertMessages(object, channel, selectedMessages, $personAccountByIdStore, $employeeByIdStore)
messages: convertMessages(
object,
channel,
selectedMessages,
allIntegrations,
$personAccountByIdStore,
$employeeByIdStore
)
}
)
await inboxClient.readDoc(getClient(), channel._id)
Expand Down Expand Up @@ -129,7 +138,14 @@
<div class="antiVSpacer x2" />
<Scroller padding={'.5rem 1rem'}>
<Messages
messages={convertMessages(object, channel, messages, $personAccountByIdStore, $employeeByIdStore)}
messages={convertMessages(
object,
channel,
messages,
allIntegrations,
$personAccountByIdStore,
$employeeByIdStore
)}
{selectable}
bind:selected
on:select
Expand Down
23 changes: 17 additions & 6 deletions plugins/gmail-resources/src/components/Main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
-->
<script lang="ts">
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 { Message, SharedMessage } from '@hcengineering/gmail'
import { InboxNotificationsClientImpl } from '@hcengineering/notification-resources'
import { getResource } from '@hcengineering/platform'
import { createQuery, getClient } from '@hcengineering/presentation'
import setting, { Integration } from '@hcengineering/setting'
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 gmail from '../plugin'
import { convertMessage } from '../utils'
Expand All @@ -48,6 +48,7 @@
let currentMessage: SharedMessage | undefined = undefined

let newMessage: boolean = false
let allIntegrations: Integration[] = []
let integrations: Integration[] = []
let selectedIntegration: Integration | undefined = undefined

Expand Down Expand Up @@ -95,15 +96,18 @@

$: templateProvider && selectedIntegration && templateProvider.set(setting.class.Integration, selectedIntegration)

settingsQuery.query(setting.class.Integration, { type: gmail.integrationType.Gmail, disabled: false }, (res) => {
integrations = res.filter((p) => p.createdBy === me || p.shared?.includes(me))
settingsQuery.query(setting.class.Integration, { type: gmail.integrationType.Gmail }, (res) => {
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]
})

$: gmailMessage &&
channel &&
object &&
convertMessage(object, channel, gmailMessage, $employeeByIdStore).then((p) => (currentMessage = p))
convertMessage(object, channel, gmailMessage, allIntegrations, $personAccountByIdStore, $employeeByIdStore).then(
(p) => (currentMessage = p)
)
</script>

{#if channel && object}
Expand Down Expand Up @@ -148,7 +152,14 @@
{:else if currentMessage}
<FullMessage {currentMessage} bind:newMessage on:close={back} />
{: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}
</Dialog>
{/if}
2 changes: 1 addition & 1 deletion plugins/gmail-resources/src/components/NewMessages.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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]
})

Expand Down
28 changes: 16 additions & 12 deletions plugins/gmail-resources/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -72,15 +73,16 @@ export function convertMessages (
object: Contact,
channel: Channel,
messages: Message[],
integrations: Integration[],
accounts: IdMap<PersonAccount>,
employees: IdMap<Employee>
): SharedMessage[] {
return messages.map((m) => {
return {
...m,
_id: m._id as string as Ref<SharedMessage>,
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)
}
})
}
Expand All @@ -89,34 +91,36 @@ export async function convertMessage (
object: Contact,
channel: Channel,
message: Message,
integrations: Integration[],
accounts: IdMap<PersonAccount>,
employees: IdMap<Employee>
): Promise<SharedMessage> {
const client = getClient()
const accounts = toIdMap(await client.findAll(contact.class.PersonAccount, {}))
return {
...message,
_id: message._id as string as Ref<SharedMessage>,
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)
}
}

export function getName (
object: Contact,
channel: Channel,
message: Message,
integrations: Integration[],
accounts: IdMap<PersonAccount>,
employees: IdMap<Employee>,
sender: boolean
): string {
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<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 email = account?.email
const from = accounts.get(message.from as Ref<PersonAccount>)?.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})`
Expand Down
16 changes: 8 additions & 8 deletions services/gmail/pod-gmail/src/gmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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)
}
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -771,7 +771,7 @@ export class GmailClient {
}
})
} catch (err) {
console.log('Watch error', JSON.stringify(err))
console.log('Watch error', (err as any).message)
}
}

Expand Down Expand Up @@ -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)
}
}
}
2 changes: 1 addition & 1 deletion services/gmail/pod-gmail/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const main = async (): Promise<void> => {
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()
}
}
Expand Down