summaryrefslogtreecommitdiff
path: root/common/src/leap/soledad/common/ddocs/docs/updates/resolve_doc.js
blob: 7ba66cf8ce95fe9a07e159c60ef3ac0851016753 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
function(doc, req){
    /* we expect to receive the following in `req.body`:
     * {
     *     'couch_rev': '<couch_rev>',
     *     'conflicts': '<base64 encoded conflicts>',
     * }
     */
    var body = JSON.parse(req.body);

    // fail if no document was given
    if (!doc) {
        return [null, 'document does not exist']
    } 

    // fail if couch revisions do not match
    if (body['couch_rev'] != null
        && doc['_rev'] != body['couch_rev']) {
        return [null, 'revision conflict']
    }

    // fail if conflicts were not sent
    if (body['conflicts'] == null)
        return [null, 'missing conflicts']

    // save conflicts as attachment if they were sent
    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
        }
    }
    // or delete attachment if there are no conflicts
    else if (doc._attachments && doc._attachments.u1db_conflicts)
        delete doc._attachments.u1db_conflicts;

    return [doc, 'ok'];
}