Skip to content
Jan Dvorak edited this page Mar 14, 2016 · 4 revisions

Following you will find publications that were originally included with this package. Since every app is different they were removed so that you can adjust them to your liking. For all of these to work properly you will need aldeed:simple-schema, tmeasday:publish-with-relations, meteorhacks:unblock and socialize:user-model packages to be included.

var publicationOptionsSchema = new SimpleSchema({
  limit:{
    type: Number,
    optional: true
  },
  skip:{
    type: Number,
    optional: true
  },
  sort:{
    type: Number,
    optional: true
  }
});

Meteor.publish("conversations", function (options) {
    this.unblock();
    var currentUser;

    if(!this.userId){
        return this.ready();
    }

    options = options || {};

    check(options, publicationOptionsSchema);

    options = _.pick(options, ["limit", "skip"]);

    options.sort = {date:-1};

    Meteor.publishWithRelations({
        handle: this,
        collection: Meteor.participants,
        options:options,
        filter: {userId:this.userId, deleted:{$exists:false}},
        mappings: [{
            key: 'conversationId',
            collection: Meteor.conversations,
            mappings:[{
                key: "conversationId",
                collection: Meteor.participants,
                filter: {userId:{$ne:this.userId}},
                reverse:true,
                mappings:[{
                    key:"userId",
                    collection:Meteor.users,
                    options:{fields:{username:true, avatarId:true}}
                }]
            },{
                reverse:true,
                key: "conversationId",
                collection: Meteor.messages,
                options:{limit:1, sort:{date:-1}}
            }]
        }]
    });

});

/**
 * Publish conversations that have not been read yet by the user
 */
Meteor.publish("unreadConversations", function(){

    if(!this.userId){
        return this.ready();
    }

    Meteor.publishWithRelations({
        handle: this,
        collection: Meteor.participants,
        filter: {userId:this.userId, deleted:{$exists:false}, read:false},
        mappings: [{
            key: 'conversationId',
            collection: Meteor.conversations,
            mappings:[{
                key: "conversationId",
                collection: Meteor.participants,
                reverse:true,
                options:{limit:1, sort:{date:-1}},
                mappings:[{
                    key:"userId",
                    collection:Meteor.users,
                    options:{fields:{username:true, avatarId:true}}
                }]
            },{
                key: "conversationId",
                collection: Meteor.messages,
                options:{limit:1, sort:{date:-1}}
            }]
        }]
    });
});


/**
 * Publish messages for a particular conversation
 * @param   {String}       conversationId The _id of the conversation to fetch messages for
 * @param   {Object}       options        Query options {limit:Number, skip:Number}
 * @returns {Mongo.Cursor} A cursor of messsages that belong to the current conversation
 */
Meteor.publish("messagesFor", function(conversationId, options){
    var self = this;
    var user = User.createEmpty(self.userId);

    check(conversationId, String);
    check(options, publicationOptionsSchema);

    self.unblock();

    if(!self.userId){
        return this.ready();
    }

    var conversation = Conversation.createEmpty(conversationId);

    if(user.isParticipatingIn(conversation)){
        return conversation.messages(options.limit, options.skip, "date", -1);
    }
});
Clone this wiki locally