Skip to content

Instantly share code, notes, and snippets.

@patrickml
Last active August 29, 2015 14:22
Show Gist options
  • Save patrickml/68f64d927fba774a742d to your computer and use it in GitHub Desktop.
Save patrickml/68f64d927fba774a742d to your computer and use it in GitHub Desktop.
Simple Activity Log by Collection for MeteorJS
/**
* Example Document
{
_id : 'xDvcFlsQws',
activity : 'Project Created',
createdAt : '1231213412312'
collection : 'projects',
documentId : 'xJ4iTyxDw2',
userId : 'pQ4iTyxDw7'
}
* @type {Mongo.Collection}
*/
Activity = new Meteor.Collection('activity');
Activity.allow({
'insert': function(userId, doc) {
return true;
},
'update': function(userId, docs, fields, modifier) {
return true;
},
'remove': function(userId, docs) {
return true;
}
});
Activity.before.insert(function (userId, doc) {
//Check if the document has the required entries
if(!doc.activity || !doc.collection || !doc.documentId)
return false;
//Only insert if ran on the server otherwise the server will deny the insert
if(Meteor.isServer) {
//Format the document
doc.createdAt = Date.now();
doc.userId = userId;
} else {
return false;
}
});
Activity.helpers({
/**
* Gets the related document
* @returns {any}
*/
'getDocument': function () {
return window[this.collection].findOne({ _id: this.documentId });
},
/**
* Gets the user who performed the activity
* @returns {any}
*/
'getUser': function () {
return window[this.collection].findOne({ _id: this.userId });
}
});
Projects.after.insert(function (userId, doc) {
if(Meteor.isServer) {
//Log the fact that a Project was created
Activity.insert({
'activity' : 'Project Created',
'collection' : 'Projects',
'documentId' : doc._id
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment