Create feed about all collection's changes (update, insert, remove and others) - need ideas!

Hello. I’m working on editable by users dictionary. And there are several things happens with articles, I wont to display them on main page like news feed in social networks.
Actions for Articles are:

  • Added by user,
  • Directly updated by admin,
  • Added correction,
  • Correction approved,
  • Correction ejected,
  • Removed,
  • etc…

I use packages: SimpleSchema, AutoForm, Collection-hooks.

What I have tried: I created new collection Events. Made publish, subscribe…
General idea is to insert to Events object from Articles, witch stores all recent actions with them.
With Insert all works fine. Information about creation of new article appears immediately in format: User created Article

Articles.after.insert(function (userId, doc) {

    let lastEvent = {
        type: "added",
        user1id: doc.createdByUserId, 
        user1name: doc.createdByUserName, 
        articleId: doc._id, 
        articleTitle: articleTitle(doc.words), 
        happenedAt:doc.createdAt
    };

    Meteor.call('events.insert', lastEvent);

    FlowRouter.go('articles',{ id: doc._id});
});

Problem is in ‘artecles.update’ method, because inside it two different types of events happens: Directly update by admin or Added correction by ordinary user. I still can’t catch this change of type. I added to Article schema “lastEvent” object, for store type of last event.

  'articles.update'(doc) {
    check(doc, Object);
//    Articles.update({_id: doc._id}, doc.modifier );
    const userId = Meteor.userId()||'anonymous';
    let correction = doc.modifier.$set;
    correction._id = doc._id

    //if user is Admin, we apply he's correction directly to db, 
    if (userId == "ghZegnrrKqnNFaFxb") {
        doc.modifier.$set.lastEvent.type = "Directly changed"
        Articles.update({_id: doc._id}, 
                        doc.modifier, 
                        {upsert: true});
    }
    //if he's not - just save it to history, to accept or reject in the future by admin
    else {
        doc.modifier.$set.lastEvent.type = "Correction added"
        Articles.update(
            {_id: doc._id },
            { $pull: { corrections : { editedByUserId: userId } } },
            );
        
        Articles.update(
            {_id: doc._id},
            { $push: { corrections : correction } },
            );
    }

    FlowRouter.go('articles',{ id: doc._id});
  },

But this code fires an error:

doc.modifier.$set.lastEvent.type = "Directly changed"

Exception while simulating the effect of invoking ‘articles.update’ TypeError: Cannot set property ‘type’ of undefined

I tried to change my schema by adding defaultValue,

    lastEvent: {
        type: Object, 
        optional: true, 
        blackbox: true,
        autoform: {
            type: "hidden",
            label: false,
        },
        defaultValue() {
            return {type:""}
        },
    }

But when I added defaultValue , submit Button doesn’t work in Article add page… And I can’t even create article with existing “lastEvent” field, for trying change it in the future…

I stuck. I hope is temporary) Please, people, share ideas… Even general thoughts after reading topic title can be useful.

I’m pretty sure you’re seeing an error because lastEvent is undefined, so you can’t set the property type.

I haven’t really used hooks to set nested properties, but I’m guessing this might work?

doc.modifier.$set['lastEvent.type'] = "Directly changed"

If you can’t set it directly, then two steps:

doc.modifier.$set.lastEvent = {};
doc.modifier.$set.lastEvent.type = 'Directly changed';
1 Like

Thanks a lot. Looks working )
Except this, there was some strange thing… I can’t save document more than once. After first saving button “submit” doesn’t work. But I fixed it after deleted this code from AutoForm :

{{> afQuickField name='lastEvent'}}

I almost did what I want , but code is ugly, I don’t like this logic (
I will be think about it later… And I’m still hope to read more ideas about concept in general…