From 4edbdc16412975a88e4b47c5ce0a34772383f094 Mon Sep 17 00:00:00 2001 From: Vyacheslav Tumanov Date: Fri, 2 Jun 2023 19:22:38 +0500 Subject: [PATCH] UBER-276: New messages and Has messages option for filter (#3326) * UBER-276: New messages and Has messages option for filter Signed-off-by: Vyacheslav Tumanov * UBER-263: made it as mode Signed-off-by: Vyacheslav Tumanov --------- Signed-off-by: Vyacheslav Tumanov --- models/contact/src/index.ts | 20 +++++++++++ plugins/contact-assets/lang/en.json | 4 ++- plugins/contact-assets/lang/ru.json | 4 ++- .../src/components/ChannelFilter.svelte | 7 +++- plugins/contact-resources/src/index.ts | 4 +++ plugins/contact-resources/src/plugin.ts | 8 +++-- plugins/contact-resources/src/utils.ts | 36 +++++++++++++++++-- plugins/contact/src/index.ts | 4 ++- 8 files changed, 78 insertions(+), 9 deletions(-) diff --git a/models/contact/src/index.ts b/models/contact/src/index.ts index 9381593fa3..7520db0f0c 100644 --- a/models/contact/src/index.ts +++ b/models/contact/src/index.ts @@ -815,6 +815,26 @@ export function createModel (builder: Builder): void { contact.filter.FilterChannelNin ) + builder.createDoc( + view.class.FilterMode, + core.space.Model, + { + label: contact.string.HasMessagesIn, + result: contact.function.FilterChannelHasMessagesResult + }, + contact.filter.FilterChannelHasMessages + ) + + builder.createDoc( + view.class.FilterMode, + core.space.Model, + { + label: contact.string.HasNewMessagesIn, + result: contact.function.FilterChannelHasNewMessagesResult + }, + contact.filter.FilterChannelHasNewMessages + ) + builder.createDoc( templates.class.TemplateFieldCategory, core.space.Model, diff --git a/plugins/contact-assets/lang/en.json b/plugins/contact-assets/lang/en.json index baa9de059f..c5f646de8a 100644 --- a/plugins/contact-assets/lang/en.json +++ b/plugins/contact-assets/lang/en.json @@ -96,6 +96,8 @@ "NumberMembers": "{count, plural, =0 {no members} =1 {1 member} other {# members}}", "Position": "Position", "ConfigLabel": "Contacts", - "ConfigDescription": "Extension to hold information about all Employees and other Person/Organization contacts." + "ConfigDescription": "Extension to hold information about all Employees and other Person/Organization contacts.", + "HasMessagesIn": "has messages in", + "HasNewMessagesIn": "has new messages in" } } diff --git a/plugins/contact-assets/lang/ru.json b/plugins/contact-assets/lang/ru.json index 81cedcf284..c20f9be0ec 100644 --- a/plugins/contact-assets/lang/ru.json +++ b/plugins/contact-assets/lang/ru.json @@ -96,6 +96,8 @@ "CategoryOther": "Прочие", "Position": "Должность", "ConfigLabel": "Контакты", - "ConfigDescription": "Расширение по работе с сотрудниками и другими контактами." + "ConfigDescription": "Расширение по работе с сотрудниками и другими контактами.", + "HasMessagesIn": "имеет сообщения в", + "HasNewMessagesIn": "имеет новые сообщения в" } } diff --git a/plugins/contact-resources/src/components/ChannelFilter.svelte b/plugins/contact-resources/src/components/ChannelFilter.svelte index 3333ad8b66..4e4c51f6f1 100644 --- a/plugins/contact-resources/src/components/ChannelFilter.svelte +++ b/plugins/contact-resources/src/components/ChannelFilter.svelte @@ -32,7 +32,12 @@ let filterUpdateTimeout: number | undefined - filter.modes = [contact.filter.FilterChannelIn, contact.filter.FilterChannelNin] + filter.modes = [ + contact.filter.FilterChannelIn, + contact.filter.FilterChannelNin, + contact.filter.FilterChannelHasMessages, + contact.filter.FilterChannelHasNewMessages + ] filter.mode = filter.mode === undefined ? filter.modes[0] : filter.mode const isSelected = (element: ChannelProvider, selected: Ref[]): boolean => { diff --git a/plugins/contact-resources/src/index.ts b/plugins/contact-resources/src/index.ts index 578a1263b8..96c79788bb 100644 --- a/plugins/contact-resources/src/index.ts +++ b/plugins/contact-resources/src/index.ts @@ -84,6 +84,8 @@ import contact from './plugin' import { contactTitleProvider, employeeSort, + filterChannelHasMessagesResult, + filterChannelHasNewMessagesResult, filterChannelInResult, filterChannelNinResult, getContactFirstName, @@ -324,6 +326,8 @@ export default async (): Promise => ({ EmployeeSort: employeeSort, FilterChannelInResult: filterChannelInResult, FilterChannelNinResult: filterChannelNinResult, + FilterChannelHasMessagesResult: filterChannelHasMessagesResult, + FilterChannelHasNewMessagesResult: filterChannelHasNewMessagesResult, GetCurrentEmployeeName: getCurrentEmployeeName, GetCurrentEmployeeEmail: getCurrentEmployeeEmail, GetCurrentEmployeePosition: getCurrentEmployeePosition, diff --git a/plugins/contact-resources/src/plugin.ts b/plugins/contact-resources/src/plugin.ts index 48f1485bc7..27b5d2ff53 100644 --- a/plugins/contact-resources/src/plugin.ts +++ b/plugins/contact-resources/src/plugin.ts @@ -76,12 +76,16 @@ export default mergeIds(contactId, contact, { CategoryPreviousAssigned: '' as IntlString, CategoryComponentLead: '' as IntlString, CategoryOther: '' as IntlString, - DeleteEmployee: '' as IntlString + DeleteEmployee: '' as IntlString, + HasMessagesIn: '' as IntlString, + HasNewMessagesIn: '' as IntlString }, function: { GetContactLink: '' as Resource<(doc: Doc, props: Record) => Promise>, EmployeeSort: '' as SortFunc, FilterChannelInResult: '' as FilterFunction, - FilterChannelNinResult: '' as FilterFunction + FilterChannelNinResult: '' as FilterFunction, + FilterChannelHasMessagesResult: '' as FilterFunction, + FilterChannelHasNewMessagesResult: '' as FilterFunction } }) diff --git a/plugins/contact-resources/src/utils.ts b/plugins/contact-resources/src/utils.ts index 235df172c6..c4b7e475e1 100644 --- a/plugins/contact-resources/src/utils.ts +++ b/plugins/contact-resources/src/utils.ts @@ -35,6 +35,8 @@ import view, { Filter } from '@hcengineering/view' import { FilterQuery } from '@hcengineering/view-resources' import { get, writable } from 'svelte/store' import contact from './plugin' +import notification, { DocUpdates, DocUpdateTx } from '@hcengineering/notification' +import { getResource } from '@hcengineering/platform' export function formatDate (dueDateMs: Timestamp): string { return new Date(dueDateMs).toLocaleString('default', { @@ -71,6 +73,20 @@ export async function employeeSort (value: Array>): Promise void): Promise> { + const result = await getRefs(filter, onUpdate, true) + return { $in: result } +} + +export async function filterChannelHasNewMessagesResult ( + filter: Filter, + onUpdate: () => void +): Promise> { + const docUpdates = await get((await getResource(notification.function.GetNotificationClient))().docUpdatesStore) + const result = await getRefs(filter, onUpdate, undefined, docUpdates) + return { $in: result } +} + export async function filterChannelInResult (filter: Filter, onUpdate: () => void): Promise> { const result = await getRefs(filter, onUpdate) return { $in: result } @@ -81,19 +97,33 @@ export async function filterChannelNinResult (filter: Filter, onUpdate: () => vo return { $nin: result } } -export async function getRefs (filter: Filter, onUpdate: () => void): Promise>> { +export async function getRefs ( + filter: Filter, + onUpdate: () => void, + hasMessages?: boolean, + docUpdates?: Map, DocUpdates> +): Promise>> { const lq = FilterQuery.getLiveQuery(filter.index) const client = getClient() const mode = await client.findOne(view.class.FilterMode, { _id: filter.mode }) if (mode === undefined) return [] const promise = new Promise>>((resolve, reject) => { + const hasMessagesQuery = hasMessages === true ? { items: { $gt: 0 } } : {} const refresh = lq.query( contact.class.Channel, { - provider: { $in: filter.value } + provider: { $in: filter.value }, + ...hasMessagesQuery }, (refs) => { - const result = Array.from(new Set(refs.map((p) => p.attachedTo))) + const filteredRefs = + docUpdates !== undefined + ? refs.filter((channel) => { + const docUpdate = docUpdates.get(channel._id) + return docUpdate != null ? docUpdate.txes.some((p: DocUpdateTx) => p.isNew) : (channel.items ?? 0) > 0 + }) + : refs + const result = Array.from(new Set(filteredRefs.map((p) => p.attachedTo))) FilterQuery.results.set(filter.index, result) resolve(result) onUpdate() diff --git a/plugins/contact/src/index.ts b/plugins/contact/src/index.ts index 7295b74b4d..3740537265 100644 --- a/plugins/contact/src/index.ts +++ b/plugins/contact/src/index.ts @@ -274,7 +274,9 @@ export const contactPlugin = plugin(contactId, { }, filter: { FilterChannelIn: '' as Ref, - FilterChannelNin: '' as Ref + FilterChannelNin: '' as Ref, + FilterChannelHasMessages: '' as Ref, + FilterChannelHasNewMessages: '' as Ref }, resolver: { Location: '' as Resource<(loc: Location) => Promise>