如何发布帖子取决于隐私?

时间:2016-12-14 12:00:11

标签: mongodb meteor queue

我需要根据隐私从集合帖子发布队列。但我不知道如何得到这个。我已经尝试实现了主要概念的思维导图:

enter image description here

输入:

var currentUser = "Andrey"; 

Events.find();

输出:

[
{
    //it's going to output, becouse I've created this post
    "_id" : "1",
    "createdBy" : "Andrey",
    "private" : true,
    "title" : "",
    "text": "",
    "members" : [ 
        "Sheldon", "Mike"
    ]
},
{
    //it's going to output, becouse this post not private
    "_id" : "2",
    "createdBy" : "Sheldon",
    "private" : false,
    "title" : "",
    "members" : []
},
{
    //it's going to output, becouse I'm one of members'
    "_id" : "3",
    "createdBy" : "Mike",
    "private" : true,
    "title" : "",
    "text": "",
    "members" : [ 
        "Andrey"
    ]
},
{
    //it's NOT going to output, becouse it's private post, I'm not the member or author
    "_id" : "4",
    "createdBy" : "Ana",
    "private" : true,
    "title" : "",
    "text": "",
    "members" : [ 
         "Sheldon"
    ]
},
]

预期结果:

[
{
    "_id" : "1",
    "createdBy" : "Andrey",
    "private" : true,
    "title" : "",
    "text": "",
    "members" : [ 
        "Sheldon", "Mike"
    ]
},
{
    "_id" : "2",
    "createdBy" : "Sheldon",
    "private" : false,
    "title" : "",
    "text": "",
    "members" : []
},
{
    "_id" : "3",
    "createdBy" : "Mike",
    "private" : true,
    "title" : "",
    "text": "",
    "members" : [ 
        "Andrey"
    ]
}
]

但这个想法可能是错的,也许你有另一种方式?

2 个答案:

答案 0 :(得分:2)

我不确定这是否是正确的做法,但如果我面对这个问题,我可能会尝试这样的事情。 (顺便说一下,我使用userId而不是名字。如果你使用名字来解释你的目标,请忽略这个评论)

只要我记得,您可以返回多个查询结果

return [
        Result1,
        Result2,
       ]

对于公共帖子,我猜不用担心。只需返回private: false即可。

对于私有的,我会使用userId作为参数并尝试这个复合出版物:

Meteor.publish('posts', function postPublication(userId) {

  return [
    Posts.find({
    $and: [{
      $or: [
        {_id: userId},
        {memberId: {$in: userId}}
      ]},
      {private: {$eq: true}}
     ]}), // => should return private posts created or commented by member
    Posts.find({private: {$eq: false}}) // => should return public ones
  ];
}

然后使用userId进行订阅

Meteor.subscribe('posts', userId);

答案 1 :(得分:0)

这段代码我用$或$和解决了这个任务。但不确定,如果它是最好的解决方案。但我得到了预期的结果。

Meteor.publish('posts', function(currentUser) {
selector = { 
                $or : [
                    { $and : [ {'private': true, 'members': currentUser} ] },
                    { $and : [ {'private': true, 'author': currentUser} ] },
                    { $and : [ {'private': false, 'author': currentUser} ] },
                    { $and : [ {'private': undefined} ] },
                    { $and : [ {'private': false} ] },

                ]
            }
 return Posts.find(selector);
});