diff options
author | Tomás Touceda <chiiph@leap.se> | 2013-12-19 11:38:06 -0300 |
---|---|---|
committer | Tomás Touceda <chiiph@leap.se> | 2013-12-19 11:38:06 -0300 |
commit | bf0cdbd1db302867bacf2646090db4673f922957 (patch) | |
tree | 47f3dbb49dbd6bed81b235541a1a933381ca6e57 /common/src/leap/soledad/common/ddocs/docs/updates/put.js | |
parent | 8789e1e662a8b6ef7635bd8eed4f8e3af41ba138 (diff) | |
parent | ebb113849f26e91803f5c2017a8576fbd2de7033 (diff) |
Merge remote-tracking branch 'refs/remotes/drebs/feature/consistent-metadata' into develop
Diffstat (limited to 'common/src/leap/soledad/common/ddocs/docs/updates/put.js')
-rw-r--r-- | common/src/leap/soledad/common/ddocs/docs/updates/put.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/common/src/leap/soledad/common/ddocs/docs/updates/put.js b/common/src/leap/soledad/common/ddocs/docs/updates/put.js new file mode 100644 index 00000000..5a4647de --- /dev/null +++ b/common/src/leap/soledad/common/ddocs/docs/updates/put.js @@ -0,0 +1,64 @@ +function(doc, req){ + /* we expect to receive the following in `req.body`: + * { + * 'couch_rev': '<couch_rev>', + * 'u1db_rev': '<u1db_rev>', + * 'content': '<base64 encoded content>', + * 'trans_id': '<reansaction_id>' + * 'conflicts': '<base64 encoded conflicts>', + * 'update_conflicts': <boolean> + * } + */ + var body = JSON.parse(req.body); + + // create a new document document + if (!doc) { + doc = {} + doc['_id'] = req['id']; + } + // or fail if couch revisions do not match + else if (doc['_rev'] != body['couch_rev']) { + // of fail if revisions do not match + return [null, 'revision conflict'] + } + + // store u1db rev + doc.u1db_rev = body['u1db_rev']; + + // save content as attachment + if (body['content'] != null) { + // save u1db content as attachment + if (!doc._attachments) + doc._attachments = {}; + doc._attachments.u1db_content = { + content_type: "application/octet-stream", + data: body['content'] // should be base64 encoded + }; + } + // or delete the attachment if document is tombstone + else if (doc._attachments && + doc._attachments.u1db_content) + delete doc._attachments.u1db_content; + + // store the transaction id + if (!doc.u1db_transactions) + doc.u1db_transactions = []; + var d = new Date(); + doc.u1db_transactions.push([d.getTime(), body['trans_id']]); + + // save conflicts as attachment if they were sent + if (body['update_conflicts']) + if (body['conflicts'] != null) { + if (!doc._attachments) + doc._attachments = {}; + doc._attachments.u1db_conflicts = { + content_type: "application/octet-stream", + data: body['conflicts'] // should be base64 encoded + } + } else { + if(doc._attachments && doc._attachments.u1db_conflicts) + delete doc._attachments.u1db_conflicts + } + + return [doc, 'ok']; +} |