mirror of
https://github.com/hcengineering/platform.git
synced 2024-12-22 19:11:33 +03:00
Minor gmail fixes (#6431)
Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
parent
4ff8a4559f
commit
63aa456011
@ -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'
|
||||
@ -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[] = []
|
||||
@ -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)
|
||||
@ -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
|
||||
|
@ -15,7 +15,7 @@
|
||||
-->
|
||||
<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'
|
||||
@ -23,7 +23,7 @@
|
||||
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'
|
||||
@ -48,6 +48,7 @@
|
||||
let currentMessage: SharedMessage | undefined = undefined
|
||||
|
||||
let newMessage: boolean = false
|
||||
let allIntegrations: Integration[] = []
|
||||
let integrations: Integration[] = []
|
||||
let selectedIntegration: Integration | undefined = undefined
|
||||
|
||||
@ -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}
|
||||
@ -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}
|
||||
|
@ -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]
|
||||
})
|
||||
|
||||
|
@ -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<PersonAccount>,
|
||||
employees: IdMap<Employee>
|
||||
): SharedMessage[] {
|
||||
@ -79,8 +81,8 @@ export function convertMessages (
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -89,15 +91,15 @@ 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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,6 +107,7 @@ export function getName (
|
||||
object: Contact,
|
||||
channel: Channel,
|
||||
message: Message,
|
||||
integrations: Integration[],
|
||||
accounts: IdMap<PersonAccount>,
|
||||
employees: IdMap<Employee>,
|
||||
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<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})`
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user