I’m trying to get the import filters and sync function to do what I need. So I’m sending test documents back and forth. However, I have a question as to why certain documents (type: Image
) are replicated to mobile?
Obviously, I’m missing something in my import filter and/or sync. formula. I can see that as soon as I start the replication (push-pull as a logged in user) I recieve Image
documents…
This is my import filter:
function(doc) {
// Some document types not allowed on mobile
if (doc.type == 'EnvLake' || doc.type == 'EnvMeasurement' || doc.type == 'ActivityLog' || doc.type == 'Image') {
return false;
}
if ((doc.type == 'FishingTrip' || doc.type == 'Catch') && typeof doc.clubonlykey !== 'undefined' && doc.clubonlykey != '') {
return false;
}
return true;
}
And this is my sync function (still work in progress):
function (doc, oldDoc) {
function _log(t) {
console.log('SG: ' + t);
}
function _getUserKey(d) {
var key = null;
if (d) {
if (d.type == 'User') {
key = d.key;
} else if (d.type == 'FishingTrip' || d.type == 'Catch' || d.type == 'Photo' || d.type == 'Private' || d.type == 'Image' || d.type == 'Feedback') {
// TODO: Not sure about Feedback here....??
key = d.userkey;
}
}
return key;
}
//if (doc && doc._deleted && oldDoc) {
if (doc && doc._deleted) {
// Doc. deleted -> if public then require
/*
var userkey = _getUserKey(oldDoc);
if (userkey != null) {
requireUser(userkey);
} else {
channel('!');
requireAdmin();
}
*/
_log('doc deleted, id: ' + (doc._id || 'no id!') + ', ' + (oldDoc ? ('old key=' + oldDoc.key + ', userkey=' + oldDoc.userkey) : 'no oldDoc'));
return;
}
_log('doc id: ' + (doc._id || 'no id!') + ', ' + (oldDoc ? ('old key=' + oldDoc.key + ', userkey=' + oldDoc.userkey) : 'no oldDoc'));
// Document type is mandatory
if (typeof doc.type === 'undefined') {
throw ({ forbidden: "Document type is required. id=" + JSON.stringify(doc) });
}
// --> Moved to import filter!
/*
// Some document types not allowed to sync to mobile
if (doc.type == 'EnvLake' || doc.type == 'EnvMeasurement' || doc.type == 'ActivityLog' || doc.type == 'Image') {
throw ({ forbidden: "Document type " + doc.type + " not allowed to sync to mobile..." });
}
// Some document types not allowed on mobile
if(oldDoc != null) {
if (doc.type == 'EnvLake' || doc.type == 'EnvMeasurement' || doc.type == 'ActivityLog' || doc.type == 'Image') {
throw ({ forbidden: "Document type " + doc.type + " not allowed to sync to mobile..." });
}
}
*/
// Document key is mandatory
if (typeof doc.key === 'undefined') {
throw ({ forbidden: "Document key is required. id=" + doc._id });
}
// Allow anyone to create a Feedback on the server
if (oldDoc == null && doc.type == 'Feedback') {
_log('Created feedback: ' + (doc._id || 'no id!') + ', key: ' + doc.key + ', user: ' + doc.userkey);
return;
}
// Allow anyone to create a new Image or delete an existing on the server
if (doc.type == 'Image') {
_log((oldDoc == null ? 'Created' : 'Deleted') + ' image: ' + (doc._id || 'no id!') + ', key: ' + doc.key + ', user: ' + doc.userkey);
return;
}
// All public docs are available in the app
if (doc.ispublic) {
_log('public, id: ' + (doc._id || 'no id!'));
channel('!');
}
// All non-club fishing trips and catches are available (for stats)
if ((doc.type == 'FishingTrip' || doc.type == 'Catch') && doc.clubonlykey === 'undefined') {
_log('non-club trips, id: ' + (doc._id || 'no id!'));
channel('!');
}
// All non-specific user info is available (for stats)
if (doc.type == 'User') {
_log('User doc, id: ' + (doc._id || 'no id!'));
channel('!');
}
// Only non-public docs "owned" by user can be replicated
var userkey = _getUserKey(doc);
if (userkey != null) {
if (oldDoc != null) {
// Update
if (oldDoc.type != doc.type) {
_log('Update: doc.type=' + doc.type + ', oldDoc.type=' + oldDoc.type);
throw ({ forbidden: "Can't change doc type" });
}
if (oldDoc.key != doc.key) {
_log('Update: doc.key=' + doc.key + ', oldDoc.key=' + oldDoc.key);
throw ({ forbidden: "Can't change doc key" });
}
if (oldDoc.userkey && oldDoc.userkey != doc.userkey) {
_log('Update: doc.userkey=' + doc.userkey + ', oldDoc.userkey=' + oldDoc.userkey);
throw ({ forbidden: "Can't change user key" });
}
}
_log('User owned, id: ' + (doc._id || 'no id!') + ', type: ' + doc.type + ', user: ' + doc.userkey);
if(doc.ispublic){
requireAdmin();
}
requireUser(userkey);
channel('channel.' + userkey);
access(userkey, 'channel.' + userkey);
}
}
The thing is that I do want to send an Image
from mobile to the server as I use replication to upload the photos/images (and purge them locally afterwards).
Following another question about sync. I could “guess” that I may need to add these images to a “fictive” channel that no user is using… (e.g. named “server-only”). But if this is so then I’m a little confused about the role of the import filters?
I also want to be able to delete an image on the server (considering creating a new Image
in the seldom event and delete it afterwards to send the deletion to the server - or if that won’t work then I need some “flag” to disable replication to the server after the first replication (so I can purge the base64 fields with the photo in them…)