diff --git a/models/gmail/src/index.ts b/models/gmail/src/index.ts index bd80a6b4c8..a0288f8714 100644 --- a/models/gmail/src/index.ts +++ b/models/gmail/src/index.ts @@ -179,6 +179,7 @@ export function createModel (builder: Builder): void { }, label: gmail.string.WriteEmail, icon: contact.icon.Email, + visibilityTester: gmail.function.HasEmail, keyBinding: [], input: 'any', category: contact.category.Contact, diff --git a/models/gmail/src/plugin.ts b/models/gmail/src/plugin.ts index d34a0a116b..f912af75c8 100644 --- a/models/gmail/src/plugin.ts +++ b/models/gmail/src/plugin.ts @@ -14,8 +14,8 @@ // limitations under the License. // -import { Ref } from '@hcengineering/core' -import { IntlString, mergeIds } from '@hcengineering/platform' +import { Doc, Ref } from '@hcengineering/core' +import { IntlString, mergeIds, Resource } from '@hcengineering/platform' import { gmailId } from '@hcengineering/gmail' import gmail from '@hcengineering/gmail-resources/src/plugin' import type { AnyComponent } from '@hcengineering/ui' @@ -45,5 +45,8 @@ export default mergeIds(gmailId, gmail, { }, activity: { TxSharedCreate: '' as AnyComponent + }, + function: { + HasEmail: '' as Resource<(doc?: Doc | Doc[] | undefined) => Promise> } }) diff --git a/plugins/gmail-resources/src/index.ts b/plugins/gmail-resources/src/index.ts index 0fe184f901..b8e6ff2461 100644 --- a/plugins/gmail-resources/src/index.ts +++ b/plugins/gmail-resources/src/index.ts @@ -24,6 +24,7 @@ import IconGmail from './components/icons/GmailColor.svelte' import Main from './components/Main.svelte' import NewMessages from './components/NewMessages.svelte' import gmail from '@hcengineering/gmail' +import { checkHasEmail } from './utils' export default async (): Promise => ({ component: { @@ -36,13 +37,18 @@ export default async (): Promise => ({ activity: { TxSharedCreate }, + function: { + HasEmail: checkHasEmail + }, handler: { DisconnectHandler: async () => { - const url = getMetadata(gmail.metadata.GmailURL) ?? '' + const url = getMetadata(gmail.metadata.GmailURL) + const token = getMetadata(presentation.metadata.Token) + if (url === undefined || token === undefined) return await fetch(concatLink(url, '/signout'), { method: 'GET', headers: { - Authorization: 'Bearer ' + (getMetadata(presentation.metadata.Token) ?? ''), + Authorization: 'Bearer ' + token, 'Content-Type': 'application/json' } }) diff --git a/plugins/gmail-resources/src/utils.ts b/plugins/gmail-resources/src/utils.ts index 187e6a1246..0e57cf7bd1 100644 --- a/plugins/gmail-resources/src/utils.ts +++ b/plugins/gmail-resources/src/utils.ts @@ -1,3 +1,7 @@ +import contact from '@hcengineering/contact' +import { Doc } from '@hcengineering/core' +import { getClient } from '@hcengineering/presentation' + export function getTime (time: number): string { let options: Intl.DateTimeFormatOptions = { hour: 'numeric', minute: 'numeric' } if (!isCurrentYear(time)) { @@ -33,3 +37,14 @@ export function isCurrentYear (time: number): boolean { const target = new Date(time) return current.getFullYear() === target.getFullYear() } + +export async function checkHasEmail (doc: Doc | Doc[] | undefined): Promise { + if (doc === undefined) return false + const client = getClient() + const arr = Array.isArray(doc) ? doc.map((p) => p._id) : [doc._id] + const res = await client.findAll(contact.class.Channel, { + provider: contact.channelProvider.Email, + attachedTo: { $in: arr } + }) + return res.length === arr.length +}