Skip to content

Commit

Permalink
UBERF-5476: Fix archive in inbox
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
  • Loading branch information
haiodo committed Feb 14, 2024
1 parent 9d7c858 commit ba7a2ee
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.
-->
<script lang="ts">
import { Class, Doc, getCurrentAccount, isOtherDay, Ref, Timestamp } from '@hcengineering/core'
import { Class, Doc, generateId, getCurrentAccount, isOtherDay, Ref, Timestamp } from '@hcengineering/core'
import { getClient } from '@hcengineering/presentation'
import activity, {
ActivityExtension,
Expand Down Expand Up @@ -234,8 +234,10 @@
return [message._id, ...(combined ?? [])]
})
.flat()
inboxClient.readMessages(allIds)
const ops = getClient().apply(generateId())
inboxClient.readMessages(ops, allIds).then(() => {
void ops.commit()
})
const notifyContext = get(inboxClient.docNotifyContextByDoc).get(objectId)
const lastTimestamp = messages[messages.length - 1].createdOn ?? 0
Expand Down
4 changes: 2 additions & 2 deletions plugins/gmail-resources/src/components/Chats.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
{ attachedTo: channelId },
(res) => {
plainMessages = res
inboxClient.readDoc(channelId)
inboxClient.readDoc(getClient(), channelId)
},
{ sort: { sendOn: SortingOrder.Descending } }
)
Expand All @@ -84,7 +84,7 @@
messages: convertMessages(object, channel, selectedMessages, $personAccountByIdStore, $employeeByIdStore)
}
)
await inboxClient.readDoc(channel._id)
await inboxClient.readDoc(getClient(), channel._id)
clear()
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/gmail-resources/src/components/Main.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
let integrations: Integration[] = []
let selectedIntegration: Integration | undefined = undefined
inboxClient.forceReadDoc(channel._id, channel._class)
inboxClient.forceReadDoc(getClient(), channel._id, channel._class)
const dispatch = createEventDispatcher()
Expand Down
2 changes: 1 addition & 1 deletion plugins/gmail-resources/src/components/NewMessage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
},
objectId
)
await inboxClient.forceReadDoc(channel._id, channel._class)
await inboxClient.forceReadDoc(getClient(), channel._id, channel._class)
objectId = generateId()
dispatch('close')
}
Expand Down
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 @@ -91,7 +91,7 @@
.map((m) => m.trim())
.filter((m) => m.length)
})
await inboxClient.forceReadDoc(channel._id, channel._class)
await inboxClient.forceReadDoc(getClient(), channel._id, channel._class)
for (const attachment of attachments) {
await client.addCollection(
attachmentP.class.Attachment,
Expand Down
22 changes: 15 additions & 7 deletions plugins/notification-resources/src/components/inbox/Inbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,21 @@
const contextNotifications = $notificationsByContextStore.get(selectedContext._id) ?? []
await inboxClient.readNotifications(
contextNotifications
.filter(({ _class, isViewed }) =>
isChunterChannel ? _class === notification.class.CommonInboxNotification : !isViewed
)
.map(({ _id }) => _id)
)
const doneOp = await getClient().measure('readNotifications')
const ops = getClient().apply(selectedContext._id)
try {
await inboxClient.readNotifications(
ops,
contextNotifications
.filter(({ _class, isViewed }) =>
isChunterChannel ? _class === notification.class.CommonInboxNotification : !isViewed
)
.map(({ _id }) => _id)
)
} finally {
await ops.commit()
await doneOp()
}
}
function filterNotifications (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
const context = $notifyContextsStore.find(({ _id }) => _id === displayData[listSelection]?.[0])
deleteContextNotifications(context)
void deleteContextNotifications(context)
}
if (key.code === 'Enter') {
key.preventDefault()
Expand Down
71 changes: 37 additions & 34 deletions plugins/notification-resources/src/inboxNotificationsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@
// limitations under the License.
//
import activity, { type ActivityMessage } from '@hcengineering/activity'
import { SortingOrder, getCurrentAccount, type Class, type Doc, type Ref, type WithLookup } from '@hcengineering/core'
import {
SortingOrder,
getCurrentAccount,
type Class,
type Doc,
type Ref,
type TxOperations,
type WithLookup
} from '@hcengineering/core'
import notification, {
type ActivityInboxNotification,
type Collaborators,
type DocNotifyContext,
type InboxNotification,
type InboxNotificationsClient
} from '@hcengineering/notification'
import { createQuery, getClient } from '@hcengineering/presentation'
import { createQuery } from '@hcengineering/presentation'
import { derived, get, writable } from 'svelte/store'

export const inboxMessagesStore = writable<ActivityMessage[]>([])
Expand Down Expand Up @@ -132,30 +140,28 @@ export class InboxNotificationsClientImpl implements InboxNotificationsClient {
return InboxNotificationsClientImpl._instance
}

async readDoc (_id: Ref<Doc>): Promise<void> {
const client = getClient()
async readDoc (client: TxOperations, _id: Ref<Doc>): Promise<void> {
const docNotifyContext = this._docNotifyContextByDoc.get(_id)

if (docNotifyContext === undefined) {
return
}

const inboxNotifications = get(this.inboxNotifications).filter(
const inboxNotifications = (get(this.inboxNotifications) ?? []).filter(
(notification) => notification.docNotifyContext === docNotifyContext._id && !notification.isViewed
)

await Promise.all([
...inboxNotifications.map(async (notification) => await client.update(notification, { isViewed: true })),
client.update(docNotifyContext, { lastViewedTimestamp: Date.now() })
])
for (const notification of inboxNotifications) {
await client.update(notification, { isViewed: true })
}
await client.update(docNotifyContext, { lastViewedTimestamp: Date.now() })
}

async forceReadDoc (_id: Ref<Doc>, _class: Ref<Class<Doc>>): Promise<void> {
const client = getClient()
async forceReadDoc (client: TxOperations, _id: Ref<Doc>, _class: Ref<Class<Doc>>): Promise<void> {
const context = this._docNotifyContextByDoc.get(_id)

if (context !== undefined) {
await this.readDoc(_id)
await this.readDoc(client, _id)
return
}

Expand Down Expand Up @@ -200,39 +206,36 @@ export class InboxNotificationsClientImpl implements InboxNotificationsClient {
})
}

async readMessages (ids: Array<Ref<ActivityMessage>>): Promise<void> {
const client = getClient()

async readMessages (client: TxOperations, ids: Array<Ref<ActivityMessage>>): Promise<void> {
const notificationsToRead = get(this.activityInboxNotifications).filter(
({ attachedTo, isViewed }) => ids.includes(attachedTo) && !isViewed
)

await Promise.all(
notificationsToRead.map(async (notification) => await client.update(notification, { isViewed: true }))
)
for (const notification of notificationsToRead) {
await client.update(notification, { isViewed: true })
}
}

async readNotifications (ids: Array<Ref<InboxNotification>>): Promise<void> {
const client = getClient()
const notificationsToRead = get(this.inboxNotifications).filter(({ _id }) => ids.includes(_id))
async readNotifications (client: TxOperations, ids: Array<Ref<InboxNotification>>): Promise<void> {
const notificationsToRead = (get(this.inboxNotifications) ?? []).filter(({ _id }) => ids.includes(_id))

await Promise.all(
notificationsToRead.map(async (notification) => await client.update(notification, { isViewed: true }))
)
for (const notification of notificationsToRead) {
await client.update(notification, { isViewed: true })
}
}

async unreadNotifications (ids: Array<Ref<InboxNotification>>): Promise<void> {
const client = getClient()
const notificationsToUnread = get(this.inboxNotifications).filter(({ _id }) => ids.includes(_id))
async unreadNotifications (client: TxOperations, ids: Array<Ref<InboxNotification>>): Promise<void> {
const notificationsToUnread = (get(this.inboxNotifications) ?? []).filter(({ _id }) => ids.includes(_id))

await Promise.all(
notificationsToUnread.map(async (notification) => await client.update(notification, { isViewed: false }))
)
for (const notification of notificationsToUnread) {
await client.update(notification, { isViewed: false })
}
}

async deleteNotifications (ids: Array<Ref<InboxNotification>>): Promise<void> {
const client = getClient()
const inboxNotifications = get(this.inboxNotifications).filter(({ _id }) => ids.includes(_id))
await Promise.all(inboxNotifications.map(async (notification) => await client.remove(notification)))
async deleteNotifications (client: TxOperations, ids: Array<Ref<InboxNotification>>): Promise<void> {
const inboxNotifications = (get(this.inboxNotifications) ?? []).filter(({ _id }) => ids.includes(_id))
for (const notification of inboxNotifications) {
await client.remove(notification)
}
}
}
Loading

0 comments on commit ba7a2ee

Please sign in to comment.