feat: Events / audit log (#1008)

* feat: Record events in DB

* feat: events API

* First pass, hacky activity feed

* WIP

* Reset dashboard

* feat: audit log UI
feat: store ip address

* chore: Document events.list api

* fix: command specs

* await event create

* fix: backlinks service

* tidy

* fix: Hide audit log menu item if not admin
This commit is contained in:
Tom Moor
2019-08-05 20:38:31 -07:00
committed by GitHub
parent 75b03fdba2
commit fb4f6822a4
37 changed files with 911 additions and 148 deletions

View File

@@ -8,18 +8,18 @@ export default class Backlinks {
async on(event: DocumentEvent) {
switch (event.name) {
case 'documents.publish': {
const document = await Document.findByPk(event.modelId);
const document = await Document.findByPk(event.documentId);
const linkIds = parseDocumentIds(document.text);
await Promise.all(
linkIds.map(async linkId => {
const linkedDocument = await Document.findByPk(linkId);
if (linkedDocument.id === event.modelId) return;
if (linkedDocument.id === event.documentId) return;
await Backlink.findOrCreate({
where: {
documentId: linkedDocument.id,
reverseDocumentId: event.modelId,
reverseDocumentId: event.documentId,
},
defaults: {
userId: document.lastModifiedById,
@@ -32,14 +32,14 @@ export default class Backlinks {
}
case 'documents.update': {
// no-op for now
if (event.autosave) return;
if (event.data.autosave) return;
// no-op for drafts
const document = await Document.findByPk(event.modelId);
const document = await Document.findByPk(event.documentId);
if (!document.publishedAt) return;
const [currentRevision, previsionRevision] = await Revision.findAll({
where: { documentId: event.modelId },
where: { documentId: event.documentId },
order: [['createdAt', 'desc']],
limit: 2,
});
@@ -51,12 +51,12 @@ export default class Backlinks {
await Promise.all(
addedLinkIds.map(async linkId => {
const linkedDocument = await Document.findByPk(linkId);
if (linkedDocument.id === event.modelId) return;
if (linkedDocument.id === event.documentId) return;
await Backlink.findOrCreate({
where: {
documentId: linkedDocument.id,
reverseDocumentId: event.modelId,
reverseDocumentId: event.documentId,
},
defaults: {
userId: currentRevision.userId,
@@ -71,7 +71,7 @@ export default class Backlinks {
await Backlink.destroy({
where: {
documentId: document.id,
reverseDocumentId: event.modelId,
reverseDocumentId: event.documentId,
},
});
})
@@ -81,12 +81,12 @@ export default class Backlinks {
case 'documents.delete': {
await Backlink.destroy({
where: {
reverseDocumentId: event.modelId,
reverseDocumentId: event.documentId,
},
});
await Backlink.destroy({
where: {
documentId: event.modelId,
documentId: event.documentId,
},
});
break;

View File

@@ -18,12 +18,12 @@ export default class Notifications {
async documentUpdated(event: DocumentEvent) {
// lets not send a notification on every autosave update
if (event.autosave) return;
if (event.data && event.data.autosave) return;
// wait until the user has finished editing
if (!event.done) return;
if (event.data && !event.data.done) return;
const document = await Document.findByPk(event.modelId);
const document = await Document.findByPk(event.documentId);
if (!document) return;
const { collection } = document;
@@ -72,7 +72,7 @@ export default class Notifications {
}
async collectionCreated(event: CollectionEvent) {
const collection = await Collection.findByPk(event.modelId, {
const collection = await Collection.findByPk(event.collectionId, {
include: [
{
model: User,

View File

@@ -58,9 +58,12 @@ export default class Slack {
async documentUpdated(event: DocumentEvent) {
// lets not send a notification on every autosave update
if (event.autosave) return;
if (event.data && event.data.autosave) return;
const document = await Document.findByPk(event.modelId);
// lets not send a notification on every CMD+S update
if (event.data && !event.data.done) return;
const document = await Document.findByPk(event.documentId);
if (!document) return;
// never send information on draft documents

View File

@@ -17,7 +17,7 @@ export default class Websockets {
case 'documents.unpin':
case 'documents.update':
case 'documents.delete': {
const document = await Document.findByPk(event.modelId, {
const document = await Document.findByPk(event.documentId, {
paranoid: false,
});
const documents = [await presentDocument(document)];
@@ -32,7 +32,7 @@ export default class Websockets {
});
}
case 'documents.create': {
const document = await Document.findByPk(event.modelId);
const document = await Document.findByPk(event.documentId);
const documents = [await presentDocument(document)];
const collections = [await presentCollection(document.collection)];
@@ -45,19 +45,19 @@ export default class Websockets {
case 'documents.star':
case 'documents.unstar': {
return socketio.to(`user-${event.actorId}`).emit(event.name, {
documentId: event.modelId,
documentId: event.documentId,
});
}
case 'documents.move': {
const documents = await Document.findAll({
where: {
id: event.documentIds,
id: event.data.documentIds,
},
paranoid: false,
});
const collections = await Collection.findAll({
where: {
id: event.collectionIds,
id: event.data.collectionIds,
},
paranoid: false,
});
@@ -78,7 +78,7 @@ export default class Websockets {
return;
}
case 'collections.create': {
const collection = await Collection.findByPk(event.modelId, {
const collection = await Collection.findByPk(event.collectionId, {
paranoid: false,
});
const collections = [await presentCollection(collection)];
@@ -106,7 +106,7 @@ export default class Websockets {
}
case 'collections.update':
case 'collections.delete': {
const collection = await Collection.findByPk(event.modelId, {
const collection = await Collection.findByPk(event.collectionId, {
paranoid: false,
});
const collections = [await presentCollection(collection)];
@@ -117,12 +117,12 @@ export default class Websockets {
});
}
case 'collections.add_user':
return socketio.to(`user-${event.modelId}`).emit('join', {
return socketio.to(`user-${event.userId}`).emit('join', {
event: event.name,
roomId: event.collectionId,
});
case 'collections.remove_user':
return socketio.to(`user-${event.modelId}`).emit('leave', {
return socketio.to(`user-${event.userId}`).emit('leave', {
event: event.name,
roomId: event.collectionId,
});