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

UBERF-6508: add user to doc collaborators on mention #5340

Merged
merged 1 commit into from
Apr 12, 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
3 changes: 2 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"ACCOUNTS_URL": "http://localhost:3000",
// "SERVER_PROVIDER":"uweb"
"SERVER_PROVIDER":"ws",
"MODEL_VERSION": ""
"MODEL_VERSION": "",
"ELASTIC_INDEX_NAME": "local_storage_index"

// "RETRANSLATE_URL": "http://127.0.0.1:4500",
//"RETRANSLATE_URL": "https://208.167.249.201",
Expand Down
38 changes: 27 additions & 11 deletions server-plugins/activity-resources/src/references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,11 @@ export async function getPersonNotificationTxes (
}

const res: Tx[] = []
const collaboratorsTx = await getCollaboratorsTxes(reference, control, receiver)
const doc = (await control.findAll(reference.srcDocClass, { _id: reference.srcDocId }))[0]

if (collaboratorsTx !== undefined) {
res.push(collaboratorsTx)
}
const collaboratorsTx = await getCollaboratorsTxes(reference, control, receiver, doc)

const doc = (await control.findAll(reference.srcDocClass, { _id: reference.srcDocId }))[0]
res.push(...collaboratorsTx)

if (doc === undefined) {
return res
Expand Down Expand Up @@ -157,16 +155,27 @@ async function isSpaceAvailable (user: PersonAccount, spaceId: Ref<Space>, contr
async function getCollaboratorsTxes (
reference: Data<ActivityReference>,
control: TriggerControl,
receiver: Account
): Promise<TxMixin<Doc, Doc> | undefined> {
receiver: Account,
object?: Doc
): Promise<TxMixin<Doc, Doc>[]> {
const { hierarchy } = control
const res: TxMixin<Doc, Doc>[] = []

if (object !== undefined) {
// Add user to collaborators of object where user is mentioned
const objectTx = getPushCollaboratorTx(control, receiver._id, object)

if (objectTx !== undefined) {
res.push(objectTx)
}
}

if (reference.attachedDocClass === undefined || reference.attachedDocId === undefined) {
return undefined
return res
}

if (!hierarchy.isDerived(reference.attachedDocClass, activity.class.ActivityMessage)) {
return undefined
return res
}

const message = (
Expand All @@ -180,10 +189,17 @@ async function getCollaboratorsTxes (
)[0]

if (message === undefined) {
return undefined
return res
}

return getPushCollaboratorTx(control, receiver._id, message)
// Add user to collaborators of message where user is mentioned
const messageTx = getPushCollaboratorTx(control, receiver._id, message)

if (messageTx !== undefined) {
res.push(messageTx)
}

return res
}

async function isReferenceAlreadyNotified (
Expand Down
44 changes: 37 additions & 7 deletions server-plugins/chunter-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ import core, {
TxCollectionCUD,
TxCreateDoc,
TxCUD,
TxMixin,
TxProcessor,
TxRemoveDoc,
TxUpdateDoc
} from '@hcengineering/core'
import notification, { NotificationContent } from '@hcengineering/notification'
import notification, { Collaborators, NotificationContent } from '@hcengineering/notification'
import { getMetadata, IntlString } from '@hcengineering/platform'
import serverCore, { TriggerControl } from '@hcengineering/server-core'
import {
Expand Down Expand Up @@ -197,16 +198,24 @@ async function OnChatMessageCreated (tx: TxCUD<Doc>, control: TriggerControl): P
}

if (isChannel && !(targetDoc as Channel).members.includes(chatMessage.modifiedBy)) {
res.push(
control.txFactory.createTxUpdateDoc(targetDoc._class, targetDoc.space, targetDoc._id, {
$push: { members: chatMessage.modifiedBy }
})
)
res.push(...joinChannel(control, targetDoc as Channel, chatMessage.modifiedBy))
}

return res
}

function joinChannel (control: TriggerControl, channel: Channel, user: Ref<Account>): Tx[] {
if (channel.members.includes(user)) {
return []
}

return [
control.txFactory.createTxUpdateDoc(channel._class, channel.space, channel._id, {
$push: { members: user }
})
]
}

async function OnThreadMessageDeleted (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const hierarchy = control.hierarchy
const removeTx = TxProcessor.extractTx(tx) as TxRemoveDoc<ThreadMessage>
Expand Down Expand Up @@ -252,7 +261,8 @@ export async function ChunterTrigger (tx: Tx, control: TriggerControl): Promise<
const res = await Promise.all([
OnThreadMessageCreated(tx, control),
OnThreadMessageDeleted(tx, control),
OnChatMessageCreated(tx as TxCUD<Doc>, control)
OnChatMessageCreated(tx as TxCUD<Doc>, control),
OnCollaboratorsChanged(tx as TxMixin<Doc, Collaborators>, control)
])
return res.flat()
}
Expand Down Expand Up @@ -462,6 +472,26 @@ async function OnChannelMembersChanged (tx: TxUpdateDoc<Channel>, control: Trigg
return res
}

async function OnCollaboratorsChanged (tx: TxMixin<Doc, Collaborators>, control: TriggerControl): Promise<Tx[]> {
if (tx._class !== core.class.TxMixin || tx.mixin !== notification.mixin.Collaborators) return []

if (!control.hierarchy.isDerived(tx.objectClass, chunter.class.Channel)) return []

const doc = (await control.findAll(tx.objectClass, { _id: tx.objectId }))[0] as Channel | undefined

if (doc === undefined) return []
if (doc.private) return []

const added = combineAttributes([tx.attributes], 'collaborators', '$push', '$each')
const res: Tx[] = []

for (const addedMember of added) {
res.push(...joinChannel(control, doc, addedMember))
}

return res
}

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export default async () => ({
trigger: {
Expand Down