Merge branch 'tom/socket-refactor'

This commit is contained in:
Tom Moor
2022-08-27 11:51:38 +02:00
43 changed files with 624 additions and 719 deletions

View File

@@ -10,11 +10,16 @@ import {
GroupUser,
Pin,
Star,
Team,
} from "@server/models";
import {
presentCollection,
presentDocument,
presentFileOperation,
presentGroup,
presentPin,
presentStar,
presentTeam,
} from "@server/presenters";
import { Event } from "../../types";
@@ -23,7 +28,6 @@ export default class WebsocketsProcessor {
switch (event.name) {
case "documents.publish":
case "documents.restore":
case "documents.archive":
case "documents.unarchive": {
const document = await Document.findByPk(event.documentId, {
paranoid: false,
@@ -51,52 +55,16 @@ export default class WebsocketsProcessor {
});
}
case "documents.delete": {
const document = await Document.findByPk(event.documentId, {
paranoid: false,
});
if (!document) {
return;
}
if (!document.publishedAt) {
return socketio.to(`user-${document.createdById}`).emit("entities", {
event: event.name,
documentIds: [
{
id: document.id,
updatedAt: document.updatedAt,
},
],
});
}
return socketio
.to(`collection-${document.collectionId}`)
.emit("entities", {
event: event.name,
documentIds: [
{
id: document.id,
updatedAt: document.updatedAt,
},
],
collectionIds: [
{
id: document.collectionId,
},
],
});
}
case "documents.permanent_delete": {
return socketio
.to(`collection-${event.collectionId}`)
.emit(event.name, {
documentId: event.documentId,
modelId: event.documentId,
});
}
case "documents.archive":
case "documents.delete":
case "documents.update": {
const document = await Document.findByPk(event.documentId, {
paranoid: false,
@@ -107,15 +75,9 @@ export default class WebsocketsProcessor {
const channel = document.publishedAt
? `collection-${document.collectionId}`
: `user-${event.actorId}`;
return socketio.to(channel).emit("entities", {
event: event.name,
documentIds: [
{
id: document.id,
updatedAt: document.updatedAt,
},
],
});
const data = await presentDocument(document);
return socketio.to(channel).emit(event.name, data);
}
case "documents.create": {
@@ -139,13 +101,6 @@ export default class WebsocketsProcessor {
});
}
case "documents.star":
case "documents.unstar": {
return socketio.to(`user-${event.actorId}`).emit(event.name, {
documentId: event.documentId,
});
}
case "documents.move": {
const documents = await Document.findAll({
where: {
@@ -188,22 +143,15 @@ export default class WebsocketsProcessor {
.to(
collection.permission
? `team-${collection.teamId}`
: `collection-${collection.id}`
: `user-${collection.createdById}`
)
.emit("entities", {
event: event.name,
collectionIds: [
{
id: collection.id,
updatedAt: collection.updatedAt,
},
],
});
.emit(event.name, presentCollection(collection));
return socketio
.to(
collection.permission
? `team-${collection.teamId}`
: `collection-${collection.id}`
: `user-${collection.createdById}`
)
.emit("join", {
event: event.name,
@@ -211,8 +159,7 @@ export default class WebsocketsProcessor {
});
}
case "collections.update":
case "collections.delete": {
case "collections.update": {
const collection = await Collection.findByPk(event.collectionId, {
paranoid: false,
});
@@ -230,6 +177,14 @@ export default class WebsocketsProcessor {
});
}
case "collections.delete": {
return socketio
.to(`collection-${event.collectionId}`)
.emit(event.name, {
modelId: event.collectionId,
});
}
case "collections.move": {
return socketio
.to(`collection-${event.collectionId}`)
@@ -366,8 +321,9 @@ export default class WebsocketsProcessor {
if (!fileOperation) {
return;
}
const data = await presentFileOperation(fileOperation);
return socketio.to(`user-${event.actorId}`).emit(event.name, data);
return socketio
.to(`user-${event.actorId}`)
.emit(event.name, presentFileOperation(fileOperation));
}
case "pins.create":
@@ -422,15 +378,9 @@ export default class WebsocketsProcessor {
if (!group) {
return;
}
return socketio.to(`team-${group.teamId}`).emit("entities", {
event: event.name,
groupIds: [
{
id: group.id,
updatedAt: group.updatedAt,
},
],
});
return socketio
.to(`team-${group.teamId}`)
.emit(event.name, presentGroup(group));
}
case "groups.add_user": {
@@ -518,25 +468,14 @@ export default class WebsocketsProcessor {
}
case "groups.delete": {
const group = await Group.findByPk(event.modelId, {
paranoid: false,
socketio.to(`team-${event.teamId}`).emit(event.name, {
modelId: event.modelId,
});
if (!group) {
return;
}
socketio.to(`team-${group.teamId}`).emit("entities", {
event: event.name,
groupIds: [
{
id: group.id,
updatedAt: group.updatedAt,
},
],
});
// we the users and collection relations that were just severed as a result of the group deletion
// since there are cascading deletes, we approximate this by looking for the recently deleted
// items in the GroupUser and CollectionGroup tables
// we get users and collection relations that were just severed as a
// result of the group deletion since there are cascading deletes, we
// approximate this by looking for the recently deleted items in the
// GroupUser and CollectionGroup tables
const groupUsers = await GroupUser.findAll({
paranoid: false,
where: {
@@ -594,14 +533,13 @@ export default class WebsocketsProcessor {
}
case "teams.update": {
return socketio.to(`team-${event.teamId}`).emit("entities", {
event: event.name,
teamIds: [
{
id: event.teamId,
},
],
});
const team = await Team.scope("withDomains").findByPk(event.teamId);
if (!team) {
return;
}
return socketio
.to(`team-${event.teamId}`)
.emit(event.name, presentTeam(team));
}
default: