2016-12-14 17 views
0

我需要根據隱私從收集帖子發佈隊列。但我不知道如何得到這個。我有主要概念的畫思維導圖什麼,我努力實現:如何發佈帖子取決於隱私?

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

我不確定這是否正確,但如果我面對這個問題,我可能會嘗試類似的方法。 (順便說一句我會使用用戶id而不是名稱。如果你使用,以名稱來解釋你的目標,請忽略此評論)

只要我還記得,你可以返回多個查詢結果一樣,

return [ 
     Result1, 
     Result2, 
     ] 

對於公衆帖子,我不需要擔心。只需返回private: false就足夠了。

對於私立學校,我會使用用戶ID作爲參數,並嘗試這種複合出版:

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 
    ]; 
} 

然後用戶id

Meteor.subscribe('posts', userId); 
+0

Thanx,很多!我認爲這個問題只有一個決定,就是處理$和$或者語句,就像你的例子。我花了整整一天的時間進行實驗,現在我得到了預期的結果。而關於用戶ID,我也使用了IDS,這是例如。謝謝! –

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); 
});