I have a question concerning the sync function. In my use case, I would like to create a channel(doc.id) and grant access to the user. Furthermore, I have a list doc.shareWithUsers where different users can be listed. Each listed user shall get access to the channel by access(doc.shareWithUsers[i].userID, doc.id). In this way, I can share a list with defined users.
My issue arises with the usage of requireAccess(doc.id).
Here is my complete function:
function (doc, oldDoc, meta) {
if (doc.owner) {
channel(doc.id);
access(doc.owner, doc.id);
}
if (doc.shareWithUsers) {
for (var i = 0; i < doc.shareWithUsers.length; i++) {
access(doc.shareWithUsers[i].userID, doc.id);
}
}
if(oldDoc){requireAccess(oldDoc.id);}
}
The code works fine without if(oldDoc){requireAccess(oldDoc.id);}. However, with it, I encounter the following issue: If I remove one user from doc.shareWithUsers, the related data are not removed from the related client where the user is logged in. The data remains available, but the synchronization is not maintained.
My question is, why is the data not automatically removed in the described case?
Since requireAccess() applies write security, it sounds like your intended update to the document (to remove a user from doc.shareWithUsers) is failing because the user attempting to make the update doesn’t have access to channel oldDoc.id.
It seems that the client cannot remove the data set from itself anymore, as it no longer has permission to access the channel. Below is the log from the client that is supposed to remove the data set from the local database:
The fact that the client no longer has access to the channel seems to be correct behavior. However, how can I address this issue and remove the data set from the client in this scenario (automatically)?
When you say “remove the data set”, I assume you’re only talking about a single document (as your sync function only assigns documents to channel(doc.id)).
If I’m understanding your description properly, one of the users in the doc.shareWithUsers list is attempting to update the document to remove themselves from doc.shareWithUsers, and so revoke their own access to the document, and this is failing because they already have had their access revoked.
I agree this should work - it seems most likely that the document has already been modified on the server to remove their access. Have you tried reviewing the version of the document on the server to identify whether it’s current still granting the user access to the document?