Add realtime events

This commit is contained in:
Tom Moor committed 2024-07-27 16:21:41 -04:00
1 parent 220e3c02cc
commit 6a29e91ddf
4 files changed
+89 -1

No files matched your search

+23
View File
@@ -10,6 +10,7 @@ import { FileOperationState, FileOperationType } from "@shared/types";
import RootStore from "~/stores/RootStore";
import Collection from "~/models/Collection";
import Comment from "~/models/Comment";
import DataAttribute from "~/models/DataAttribute";
import Document from "~/models/Document";
import FileOperation from "~/models/FileOperation";
import Group from "~/models/Group";
@@ -82,6 +83,7 @@ class WebsocketProvider extends React.Component<Props> {
this.socket.authenticated = false;
const {
auth,
dataAttributes,
documents,
collections,
groups,
@@ -290,6 +292,27 @@ class WebsocketProvider extends React.Component<Props> {
}
);
this.socket.on(
"dataAttributes.create",
(event: PartialWithId<DataAttribute>) => {
dataAttributes.add(event);
}
);
this.socket.on(
"dataAttributes.update",
(event: PartialWithId<DataAttribute>) => {
dataAttributes.add(event);
}
);
this.socket.on(
"dataAttributes.delete",
(event: WebsocketEntityDeletedEvent) => {
dataAttributes.remove(event.modelId);
}
);
this.socket.on("comments.create", (event: PartialWithId<Comment>) => {
comments.add(event);
});
@@ -16,10 +16,12 @@ import {
Notification,
UserMembership,
User,
DataAttribute,
} from "@server/models";
import {
presentComment,
presentCollection,
presentDataAttribute,
presentDocument,
presentFileOperation,
presentGroup,
@@ -371,6 +373,22 @@ export default class WebsocketsProcessor {
.emit(event.name, presentFileOperation(fileOperation));
}
case "dataAttributes.create":
case "dataAttributes.update": {
const dataAttribute = await DataAttribute.findByPk(event.modelId);
if (!dataAttribute) {
return;
}
return socketio
.to(`team-${dataAttribute.teamId}`)
.emit(event.name, presentDataAttribute(dataAttribute));
}
case "dataAttributes.delete": {
return socketio.to(`team-${event.teamId}`).emit(event.name, {
modelId: event.modelId,
});
}
case "pins.create":
case "pins.update": {
const pin = await Pin.findByPk(event.modelId);
@@ -3,7 +3,7 @@ import { UserRole } from "@shared/types";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import { DataAttribute } from "@server/models";
import { DataAttribute, Event } from "@server/models";
import { authorize } from "@server/policies";
import { presentDataAttribute, presentPolicies } from "@server/presenters";
import { APIContext } from "@server/types";
@@ -77,6 +77,18 @@ router.post(
{ transaction }
);
await Event.createFromContext(
ctx,
{
name: "dataAttributes.create",
modelId: dataAttribute.id,
data: {
name,
},
},
{ transaction }
);
ctx.body = {
data: presentDataAttribute(dataAttribute),
policies: presentPolicies(user, [dataAttribute]),
@@ -102,8 +114,20 @@ router.post(
authorize(user, "update", dataAttribute);
dataAttribute.set(input);
const changes = dataAttribute.changeset;
await dataAttribute.save({ transaction });
await Event.createFromContext(
ctx,
{
name: "dataAttributes.update",
modelId: dataAttribute.id,
changes,
},
{ transaction }
);
ctx.body = {
data: presentDataAttribute(dataAttribute),
policies: presentPolicies(user, [dataAttribute]),
@@ -130,6 +154,18 @@ router.post(
authorize(user, "delete", dataAttribute);
await dataAttribute.destroy({ transaction });
await Event.createFromContext(
ctx,
{
name: "dataAttributes.delete",
modelId: dataAttribute.id,
data: {
name: dataAttribute.name,
},
},
{ transaction }
);
ctx.body = {
success: true,
};
+11
View File
@@ -36,6 +36,7 @@ import type {
Notification,
Share,
GroupMembership,
DataAttribute,
} from "./models";
export enum AuthenticationType {
@@ -259,6 +260,15 @@ export type CollectionGroupEvent = BaseEvent<GroupMembership> & {
data: { name: string };
};
export type DataAttributeEvent = BaseEvent<DataAttribute> & {
name:
| "dataAttributes.create"
| "dataAttributes.update"
| "dataAttributes.delete";
modelId: string;
data: { name: string };
};
export type DocumentUserEvent = BaseEvent<UserMembership> & {
name: "documents.add_user" | "documents.remove_user";
userId: string;
@@ -439,6 +449,7 @@ export type Event =
| ApiKeyEvent
| AttachmentEvent
| AuthenticationProviderEvent
| DataAttributeEvent
| DocumentEvent
| DocumentUserEvent
| DocumentGroupEvent