Skip to content

Commit

Permalink
Speedup isDerived and fix auto join channels doc state
Browse files Browse the repository at this point in the history
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
  • Loading branch information
haiodo committed Aug 17, 2024
1 parent 67e2f8e commit c0e54fb
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"ACCOUNT_PORT": "3000",
"FRONT_URL": "http://localhost:8080",
"outputCapture": "std",
"SES_URL": "http://localhost:8091",
"SES_URL": "",
"MINIO_ACCESS_KEY": "minioadmin",
"MINIO_SECRET_KEY": "minioadmin",
"MINIO_ENDPOINT": "localhost"
Expand Down
4 changes: 2 additions & 2 deletions dev/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ services:
- LAST_NAME_FIRST=true
- ACCOUNTS_URL=http://localhost:3000
- BRANDING_PATH=/var/cfg/branding.json
- INIT_SCRIPT_URL=https://raw.githubusercontent.com/hcengineering/init/main/script.yaml
- INIT_WORKSPACE=onboarding
# - INIT_SCRIPT_URL=https://raw.githubusercontent.com/hcengineering/init/main/script.yaml
# - INIT_WORKSPACE=onboarding
restart: unless-stopped
collaborator:
image: hardcoreeng/collaborator
Expand Down
28 changes: 11 additions & 17 deletions packages/core/src/hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Hierarchy {
private readonly attributes = new Map<Ref<Classifier>, Map<string, AnyAttribute>>()
private readonly attributesById = new Map<Ref<AnyAttribute>, AnyAttribute>()
private readonly descendants = new Map<Ref<Classifier>, Ref<Classifier>[]>()
private readonly ancestors = new Map<Ref<Classifier>, Ref<Classifier>[]>()
private readonly ancestors = new Map<Ref<Classifier>, { ordered: Ref<Classifier>[], set: Set<Ref<Classifier>> }>()
private readonly proxies = new Map<Ref<Mixin<Doc>>, ProxyHandler<Doc>>()

private readonly classifierProperties = new Map<Ref<Classifier>, Record<string, any>>()
Expand Down Expand Up @@ -166,7 +166,7 @@ export class Hierarchy {
if (result === undefined) {
throw new Error('ancestors not found: ' + _class)
}
return result
return result.ordered
}

getClass<T extends Obj = Obj>(_class: Ref<Class<T>>): Class<T> {
Expand Down Expand Up @@ -301,17 +301,7 @@ export class Hierarchy {
* It will iterate over parents.
*/
isDerived<T extends Obj>(_class: Ref<Class<T>>, from: Ref<Class<T>>): boolean {
let cl: Ref<Class<T>> | undefined = _class
while (cl !== undefined) {
if (cl === from) return true

const cll = this.classifiers.get(cl)
if (cll === undefined || this.isInterface(cll)) {
return false
}
cl = (cll as Class<T>).extends
}
return false
return this.ancestors.get(_class)?.set?.has(from) ?? false
}

/**
Expand Down Expand Up @@ -398,15 +388,19 @@ export class Hierarchy {
const list = this.ancestors.get(_class)
if (list === undefined) {
if (add) {
this.ancestors.set(_class, [classifier])
this.ancestors.set(_class, { ordered: [classifier], set: new Set([classifier]) })
}
} else {
if (add) {
addIf(list, classifier)
if (!list.set.has(classifier)) {
list.ordered.push(classifier)
list.set.add(classifier)
}
} else {
const pos = list.indexOf(classifier)
const pos = list.ordered.indexOf(classifier)
if (pos !== -1) {
list.splice(pos, 1)
list.ordered.splice(pos, 1)
list.set.delete(classifier)
}
}
}
Expand Down
26 changes: 20 additions & 6 deletions server-plugins/contact-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export async function OnSpaceTypeMembers (tx: Tx, control: TriggerControl): Prom
export async function OnEmployeeCreate (tx: Tx, control: TriggerControl): Promise<Tx[]> {
const mixinTx = tx as TxMixin<Person, Employee>
if (mixinTx.attributes.active !== true) return []

// It could be no account yet.
const acc = control.modelDb.getAccountByPersonId(mixinTx.objectId)
if (acc.length === 0) return []
const spaces = await control.findAll(core.class.Space, { autoJoin: true })
Expand Down Expand Up @@ -157,12 +159,24 @@ export async function OnPersonAccountCreate (tx: Tx, control: TriggerControl): P

for (const space of spaces) {
if (space.members.includes(acc._id)) continue
const pushTx = control.txFactory.createTxUpdateDoc(space._class, space.space, space._id, {
$push: {
members: acc._id
}
})
result.push(pushTx)
result.push(
control.txFactory.createTxUpdateDoc(space._class, space.space, space._id, {
$push: {
members: acc._id
}
})
)

// User should see auto join channels
result.push(
control.txFactory.createTxCreateDoc(notification.class.DocNotifyContext, space._id, {
user: acc._id,
objectId: space._id,
objectClass: space._class,
objectSpace: core.space.Space,
isPinned: false
})
)
}
return result
}
Expand Down
3 changes: 2 additions & 1 deletion server/account/src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1983,7 +1983,7 @@ async function createPersonAccount (
client ??
(await connect(getEndpoint(ctx, workspaceInfo, EndpointKind.Internal), getWorkspaceId(workspace, productId)))
try {
const ops = new TxOperations(connection, core.account.System)
const ops = new TxOperations(connection, core.account.System).apply('create-person' + generateId())

const name = combineName(account.first, account.last)
// Check if PersonAccount is not exists
Expand Down Expand Up @@ -2033,6 +2033,7 @@ async function createPersonAccount (
}
}
}
await ops.commit()
} finally {
if (client === undefined) {
await connection.close()
Expand Down

0 comments on commit c0e54fb

Please sign in to comment.