Skip to content

Open-source and local-first Slack and Notion alternative that puts you in control of your data

Publish event on useer sync

Hakan Shehu710243ec1b56
diff --git a/apps/desktop/src/main/services/user-service.ts b/apps/desktop/src/main/services/user-service.ts
index c149a19b..72dd8d58 100644
--- a/apps/desktop/src/main/services/user-service.ts
+++ b/apps/desktop/src/main/services/user-service.ts
@@ -1,14 +1,17 @@
import { SyncUserData } from '@colanode/core';
+import { mapUser } from '@/main/utils';
import { databaseService } from '@/main/data/database-service';
+import { eventBus } from '@/shared/lib/event-bus';
class UserService {
public async syncServerUser(userId: string, user: SyncUserData) {
const workspaceDatabase =
await databaseService.getWorkspaceDatabase(userId);
- await workspaceDatabase
+ const createdUser = await workspaceDatabase
.insertInto('users')
+ .returningAll()
.values({
id: user.id,
email: user.email,
@@ -36,7 +39,7 @@ class UserService {
})
.where('version', '<', BigInt(user.version))
)
- .execute();
+ .executeTakeFirst();
await workspaceDatabase
.deleteFrom('texts')
@@ -47,6 +50,14 @@ class UserService {
.insertInto('texts')
.values({ id: user.id, name: user.name, text: null })
.execute();
+
+ if (createdUser) {
+ eventBus.publish({
+ type: 'user_created',
+ userId: userId,
+ user: mapUser(createdUser),
+ });
+ }
}
}