2017-03-06 4 views
1

私は現在、ユーザーがお友達としてお互いを追加できるユーザーシステムを持っています。これは私のルートです:MongoDB + NodeJS + Express - ユーザーは既に友達であるユーザーにフレンドリクエストを送ることができます

app.post("/addFriend", function(req, res) { 
var conditions = { 
    $or: [ 
     {$and: [ 
      {username: req.body.globalUserName}, 
      {$or: [ 
       {'pendingFriends._id': {$ne: req.user._id}}, 
       {'friends._id': {$ne: req.user._id}} 
      ]} 
     ]}, 
     {$and: [ 
      {username: req.user.username}, 
      {$or: [ 
       {'pendingFriends._id': {$ne: req.body.globalUserId}}, 
       {'friends._id': {$ne: req.body.globalUserId}} 
      ]} 
     ]} 
    ] 
} 
var update = { 
    $addToSet: {pendingFriends: { _id: req.user._id, username: req.user.username, language: req.user.language, profilePicture: req.user.profilePicture}} 
} 

User.findOneAndUpdate(conditions, update, function(error, doc) { 
     if(error) { 
      console.log(currentTime + " - FRIEND_REQUEST_SEND_ERROR: '" + req.user.username + "' TRIED TO SEND A FRIEND REQUEST TO '" + req.body.globalUserName + "'"); 
     } 
     else { 
      console.log(currentTime + " - FRIEND_REQUEST_SENT: '" + req.user.username + "' SENT A FRIEND REQUEST TO '" + req.body.globalUserName + "'"); 
     } 
     res.redirect("/talk"); 
    }); 
}); 

これはどのように動作するのですか。

U1はU2にフレンドリクエストを送信します。

U2の保留中の友人にU1が追加されます。

U2が受け入れると、U1はU2の友人に行き、U2はU1の友達に行きます。

しかし、今私は知っている2つのバグがあります。

U1がU2の友人要求を送信し、U1がU2の保留中の友人にいる場合、U2は友人要求をU1に送信できます。

また、U1がU2にフレンドリクエストを送信した場合、U2が受け入れた後、U1とU2の両方がお互いにフレンドリクエストを送信できます。

これを修正するにはどうすればよいですか?ちなみに、req.userはアクションを実行しているユーザー(フォームの送信など)です。 req.body.globalUserIdは、アクションを実行しているユーザーが追加しようとしているユーザーのIDです。

EDIT(ユーザーの要求によってユーザー・スキーマ):

UserSchema = new mongoose.Schema({ 
    username: String, 
    email: String, 
    password: String, 
    language: { type: String, default: "English" }, 
    profilePicture: { type: String, default: "/images/talk/blank-profile-picture.png" }, 
    status: String, 
    pendingFriends: [this], 
    friends: [this] 
}) 
+0

私はそれが操作のためだと思います。 – Remario

答えて

0

U1 =ユーザ名をユーザ=> req.body.globalUserName

U2 =ユーザーログイン

クエリが発見:

U1 IF U2 is not a pending friend OR U2 is not a friend 

または

U2 IF U1 is not a pending friend OR U1 is not a friend 

Updateは一つだけ、それらのユーザーのを見つけ、そしてU2はU1にリクエストを送信し、U1がシステムに存在しない、U2が自分pendingfriends

にその人自身を追加する場合、例えばそのpendingFriends にU1が追加されます

実際にはU2を探したくありません.U1がU2の友人/未婚の友人でないことを確認したいだけです。

正しいクエリは、最後にそれを固定

let U2PendingFriendIds = array of all U2's pending friends ids 
let U2FriendIds = array of all U2's friends ids 

var conditions = { 
    $or: [ 
     {$and: [ 
      {_id: {$nin: U2PendingFriendIds}, // not a pending friend of U2 
      {_id: {$nin: U2FriendIds},  // not a friend of U2 
      {username: req.body.globalUserName}, 
      {'pendingFriends._id': {$ne: req.user._id}}, // U2 is not a pending friend 
      {'friends._id': {$ne: req.user._id}}   // U2 is not a friend 
     ]} 
    ] 
} 
0

の線に沿ってだろう!ここに私のコードです:

app.post("/addFriend", function(req, res) { 
    var pendingIds, friendIds; 
    if (req.user.pendingFriends.length > 0) { 
     pendingIds = new Array(req.user.pendingFriends.length - 1); 
     req.user.pendingFriends.forEach(function (pendingFriend) { 
      pendingIds.push(pendingFriend._id); 
      console.log("Pending friend id: " + pendingFriend._id); 
     }) 
    } 
    if (req.user.friends.length > 0) { 
     friendIds = new Array(req.user.friends.length - 1); 
     req.user.friends.forEach(function (friend) { 
      friendIds.push(friend._id); 
      console.log("Friend id: " + friend._id); 
     }) 
    } 
    var conditions = { 
     $or: [ 
      {$and: [ 
       {_id: {$nin: pendingIds}}, // not a pending friend of U2 
       {_id: {$nin: friendIds}},  // not a friend of U2 
       {username: req.body.globalUserName}, 
       {'pendingFriends._id.toString()': {$ne: req.user._id.toString()}}, // U2 is not a pending friend 
       {'friends._id.toString()': {$ne: req.user._id.toString()}}   // U2 is not a friend 
      ]} 
     ] 
    } 
    var update = { 
     $addToSet: {pendingFriends: { _id: req.user._id.toString(), username: req.user.username, language: req.user.language, profilePicture: req.user.profilePicture}} 
    } 

    User.findOneAndUpdate(conditions, update, function(error, doc) { 
     if(error) { 
      console.log(currentTime + " - FRIEND_REQUEST_SEND_ERROR: '" + req.user.username + "' TRIED TO SEND A FRIEND REQUEST TO '" + req.body.globalUserName + "'"); 
     } 
     else { 
      console.log(currentTime + " - FRIEND_REQUEST_SENT: '" + req.user.username + "' SENT A FRIEND REQUEST TO '" + req.body.globalUserName + "'"); 
     } 
     res.redirect("/talk"); 
    }); 
}); 
+0

こんにちはRussell、可能であれば、あなたのUserスキーマを助けてください。私は同じ機能を開発しているので、ユーザーのスキーマがどのように見えるかを見たいと思っています。それは大きな助けになるでしょう。 –

+1

@HemadriDasariよろしく!私はオリジナルの投稿を編集し、自分のユーザースキーマを編集しました。 –

0

最近私が見つけた1つのリソースはすべての友情機能を促進するnpmモジュールです。 https://www.npmjs.com/package/mongoose-friends

友人を追加するためのコード設定が既にあることがわかりますが、このモジュールは、コードを扱いにくく読みやすくするための攻撃の代替方法かもしれません。それが興味のある場合に限ります。

関連する問題