您可以创建另一个节点friendRequests,在此节点内您可能有带有键 userUIDs 和数据senderUID1 : datelong, senderUID2 : datelong 的子节点,只需在您的应用程序 dbref.child('friendRequests/' + userUID).addChildEventListener 中侦听它,您将获得每个已经存在的好友请求 + 监听新请求。
让具有 parentID 的用户能够将 senderUUID 的值写入 true 或“已接受”,w/e 适合您。
然后创建一个云函数(触发器)以在friendRequests/{userUID}/{senderUID} 上监听 onUpdate,如果值为 true,则将每个用户的 id 添加到两个朋友列表中。如果为假,请删除此参考。
由于根据您的数据结构编写所有这些代码将花费大量时间,因此我将为您提供已经存在的代码,但您必须重写它:
这些是规则:
"friendList": {
"$uid":{
".read" : "$uid === auth.uid",
"$friendUID": {
".write": "$uid === auth.uid && data.exists() && newData.val() == false"
}
}
},
"friendRequest": {
"$uid": {
".read" : "$uid === auth.uid",
"$senderUID": {
".write" : "$senderUID === auth.uid && $senderUID != $uid && !data.exists() && root.child('userInfo').child($uid).exists() && !root.child('blockList').child($uid).child($senderUID).exists() && !root.child('blockList').child($senderUID).child($uid).exists() && !root.child('friendList').child($uid).child($senderUID).exists() && !root.child('friendList').child($senderUID).child($uid).exists() && !root.child('friendRequest').child($senderUID).child($uid).exists()",
".validate": "newData.hasChildren(['message', 'date'])",
"message": {
".validate": "newData.isString() && newData.val().length < 51 && newData.val().length > 0"
},
"date": {
".validate": "newData.val() == now" // ServerValue.TIMESTAMP
},
"accepted": {
".write" : "$uid === auth.uid && $uid != $senderUID && (newData.val() == true || newData.val() == false) && !data.exists() && root.child('friendRequest').child($uid).child($senderUID).exists()"
},
"$other": {
".validate": false
}
}
}
},
"blockList":{
"$uid":{
".read": "$uid === auth.uid",
"$blockUID": {
".write" : "$uid === auth.uid && $uid != $blockUID && ( ((!data.exists() && newData.val() == true) || (data.exists() && !newData.exists())) ) && root.child('userInfo').child($blockUID).exists()"
}
}
},
如您所见,这些规则不是很好,但您可以重写它们以适应您的情况,并在我所见的范围内删除一些无用的代码。
现在云功能:
exports.dbWriteOnFriendRequestChoice = functions.database.ref('/friendRequest/{receiverid}/{senderid}/accepted').onWrite((change, context) => {
if (!change.after.exists()) {
return null;
}
const writtenContent = change.after.val(); // true || false
const receiverid = context.params.receiverid;
const senderid = context.params.senderid;
if(writtenContent) {
const promiseAccepted1 = admin.database().ref('/friendList/' + senderid + '/' + receiverid).set(true);
const promiseAccepted2 = admin.database().ref('/friendList/' + receiverid + '/' + senderid).set(true);
const promiseRefused = admin.database().ref('/friendRequest/' + receiverid + '/' + senderid).remove();
return Promise.all([promiseAccepted1, promiseAccepted2, promiseRefused]);
} else {
const promiseRefused = admin.database().ref('/friendRequest/' + receiverid + '/' + senderid).remove();
return Promise.all([promiseRefused]);
}
});
exports.dbWriteOnBlock = functions.database.ref('/blockList/{useruid}/{blockuid}').onWrite((change, context) => {
if (!change.after.exists()) {
return null;
}
//const writtenContent = change.after.val(); // true || false
const useruid = context.params.useruid;
const blockuid = context.params.blockuid;
const promiseFriendList1 = admin.database().ref('/friendList/' + useruid + '/' + blockuid).remove();
const promiseFriendList2 = admin.database().ref('/friendList/' + blockuid + '/' + useruid).remove();
return Promise.all([promiseFriendList1, promiseFriendList2]);
/*const promiseUnblock = admin.database().ref('/blockList/' + useruid + '/' + blockuid).remove();
if(writtenContent) {
return Promise.all([promiseFriendList1, promiseFriendList2]);
} else {
return Promise.all([promiseUnblock]);
}*/
});
exports.dbWriteOnUnfriend = functions.database.ref('/friendList/{useruid}/{frienduid}').onWrite((change, context) => {
if (!change.after.exists()) {
return null;
}
const writtenContent = change.after.val(); // true || false
const useruid = context.params.useruid;
const frienduid = context.params.frienduid;
if(!writtenContent) {
const promiseReove1 = admin.database().ref('/friendList/' + useruid + '/' + frienduid).remove();
const promiseReove2 = admin.database().ref('/friendList/' + frienduid + '/' + useruid).remove();
return Promise.all([promiseReove1, promiseReove2]);
} else {
return null;
}
});
您可以删除阻止并执行您的操作。这适用于 100%,因为我已经在这个应用程序中对其进行了测试:https://www.youtube.com/watch?v=vGCNsD_eeSA