Skip to content

Commit

Permalink
#28 - Added event triggers when modifying items
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikpyt committed Oct 16, 2023
1 parent 7517869 commit 3a96fe8
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/modules/item/blob/blob.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { UploadInput, ReadInput, EditInput, DeleteInput } from './blob.schema';
import BlobService from './blob.service';
import AccessService from '../sharing/access.service';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';

export default class BlobController {
private blobService: BlobService;
Expand Down Expand Up @@ -51,6 +52,8 @@ export default class BlobController {

const updatedBlob = await this.blobService.updateBlob(request.body);

triggerItemEvent(updatedBlob, ItemEventType.UPDATE);

return reply.code(200).send(updatedBlob);
} catch (e) {
if (e instanceof Error) {
Expand Down Expand Up @@ -94,13 +97,15 @@ export default class BlobController {
throw new Error('Unauthorized');
}

await this.blobService.createBlob({
const createdBlob = await this.blobService.createBlob({
name: blob.pathname,
mimeType: blob.contentType,
blobUrl: blob.url,
ownerId: tokenPayloadObject.ownerId,
parentId: tokenPayloadObject.parentId ?? null,
});

triggerItemEvent(createdBlob, ItemEventType.UPDATE);
} catch (e) {
request.log.error(e);
await this.blobService.deleteBlobByUrl(blob.url);
Expand Down Expand Up @@ -163,6 +168,8 @@ export default class BlobController {

await this.blobService.deleteBlobByItemId(blob.id);

triggerItemEvent(blob, ItemEventType.DELETE);

return reply.code(204).send();
} catch (e) {
if (e instanceof Error) {
Expand Down
8 changes: 8 additions & 0 deletions src/modules/item/docs/docs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { ReadInput, EditInput, AddInput, DeleteInput } from './docs.schema';
import DocsService from './docs.service';
import AccessService from '../sharing/access.service';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';

export default class DocsController {
private docsService: DocsService;
Expand Down Expand Up @@ -51,6 +52,8 @@ export default class DocsController {

const updatedDocs = await this.docsService.updateDocs(request.body);

triggerItemEvent(updatedDocs, ItemEventType.UPDATE);

return reply.code(200).send(updatedDocs);
} catch (e) {
if (e instanceof Error) {
Expand Down Expand Up @@ -84,6 +87,8 @@ export default class DocsController {
parentId: request.body.parentId ?? null,
});

triggerItemEvent(docs, ItemEventType.UPDATE);

return reply.code(200).send(docs);
} catch (e) {
/* istanbul ignore next */
Expand All @@ -105,6 +110,9 @@ export default class DocsController {
}

await this.docsService.deleteDocsByItemId(docs.id);

triggerItemEvent(docs, ItemEventType.DELETE);

return reply.code(204).send();
} catch (e) {
if (e instanceof Error) {
Expand Down
15 changes: 7 additions & 8 deletions src/modules/item/folder/folder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { ReadInput, EditInput, AddInput, DeleteInput } from './folder.schema';
import FolderService from './folder.service';
import AccessService from '../sharing/access.service';
import { pusher } from '../../../plugins/pusher';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';

export default class FolderController {
private folderService: FolderService;
Expand Down Expand Up @@ -60,6 +60,8 @@ export default class FolderController {

const updatedFolder = await this.folderService.updateFolder(request.body);

triggerItemEvent(updatedFolder, ItemEventType.UPDATE);

return reply.code(200).send(updatedFolder);
} catch (e) {
if (e instanceof Error) {
Expand Down Expand Up @@ -93,13 +95,7 @@ export default class FolderController {
parentId: request.body.parentId ?? null,
});

const channelName = request.body.parentId
? `browser-folder-${request.body.parentId}`
: `browser-root-${request.user.sub}`;

pusher.trigger(channelName, 'update', {
message: 'hello world',
});
triggerItemEvent(folder, ItemEventType.UPDATE);

return reply.code(200).send(folder);
} catch (e) {
Expand All @@ -122,6 +118,9 @@ export default class FolderController {
}

await this.folderService.deleteFolderByItemId(folder.id);

triggerItemEvent(folder, ItemEventType.DELETE);

return reply.code(204).send();
} catch (e) {
if (e instanceof Error) {
Expand Down
21 changes: 21 additions & 0 deletions src/modules/item/item.event_handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { pusher } from '../../plugins/pusher';
import { Item } from './item.schema';

export enum ItemEventType {
UPDATE = 'update',
DELETE = 'delete',
}

const getItemParentChannel = (item: Item): string => {
if (!item.parentId) {
return `browser-root-${item.ownerId}`;
}

return `browser-folder-${item.parentId}`;
};

export const triggerItemEvent = async (item: Item, type: ItemEventType): Promise<void> => {
const channelName = getItemParentChannel(item);

await pusher.trigger(channelName, type, item);
};
3 changes: 3 additions & 0 deletions src/modules/item/item.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export default class ItemService {
userId: userId,
},
},
ownerId: {
not: userId,
},
},
include: {
ItemBlob: true,
Expand Down
16 changes: 12 additions & 4 deletions src/modules/item/sharing/sharing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,25 @@ export default class SharingService {
}

public async syncSharingsByItemId(fromItemId: number, toItemId: number) {
const sharings = await prisma.itemSharing.findMany({
const fromItem = await prisma.item.findUniqueOrThrow({
where: {
itemId: fromItemId,
id: fromItemId,
},
include: {
ItemSharing: true,
},
});

const userIds = [fromItem.ownerId];
fromItem.ItemSharing.forEach((sharing) => {
userIds.push(sharing.userId);
});

await prisma.itemSharing.createMany({
data: sharings.map((sharing) => {
data: userIds.map((userId) => {
return {
itemId: toItemId,
userId: sharing.userId,
userId: userId,
};
}),
skipDuplicates: true,
Expand Down
8 changes: 8 additions & 0 deletions src/modules/item/shortcut/shortcut.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { ReadInput, EditInput, AddInput, DeleteInput } from './shortcut.schema';
import ShortcutService from './shortcut.service';
import AccessService from '../sharing/access.service';
import { ItemEventType, triggerItemEvent } from '../item.event_handler';

export default class ShortcutController {
private shortcutService: ShortcutService;
Expand Down Expand Up @@ -59,6 +60,8 @@ export default class ShortcutController {

const updatedShortcut = await this.shortcutService.updateShortcut(request.body);

triggerItemEvent(updatedShortcut, ItemEventType.UPDATE);

return reply.code(200).send(updatedShortcut);
} catch (e) {
if (e instanceof Error) {
Expand Down Expand Up @@ -92,6 +95,8 @@ export default class ShortcutController {
parentId: request.body.parentId ?? null,
});

triggerItemEvent(shortcut, ItemEventType.UPDATE);

return reply.code(200).send(shortcut);
} catch (e) {
/* istanbul ignore next */
Expand All @@ -113,6 +118,9 @@ export default class ShortcutController {
}

await this.shortcutService.deleteShortcutByItemId(shortcut.id);

triggerItemEvent(shortcut, ItemEventType.DELETE);

return reply.code(204).send();
} catch (e) {
if (e instanceof Error) {
Expand Down

0 comments on commit 3a96fe8

Please sign in to comment.