aboutsummaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/pro/pro_padmeta.js
blob: 6f911b23f0648e4a39ec7b3edace27e7c0cf56a6 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("stringutils");
import("cache_utils.syncedWithCache");
import("sync");

import("etherpad.pad.padutils");
import("etherpad.pro.pro_pad_db");

function _doWithProPadLock(domainId, localPadId, func) {
  var lockName = ["pro-pad", domainId, localPadId].join("/");
  return sync.doWithStringLock(lockName, func);
}

function accessProPad(globalPadId, fn) {
  // retrieve pad from cache
  var domainId = padutils.getDomainId(globalPadId);
  if (!domainId) {
    throw Error("not a pro pad: "+globalPadId);
  }
  var localPadId = padutils.globalToLocalId(globalPadId);
  var padRecord = pro_pad_db.getSingleRecord(domainId, localPadId);

  return _doWithProPadLock(domainId, localPadId, function() {
    var isDirty = false;

    var proPad = {
      exists: function() { return !!padRecord; },
      getDomainId: function() { return domainId; },
      getLocalPadId: function() { return localPadId; },
      getGlobalId: function() { return globalPadId; },
      getDisplayTitle: function() { return padutils.getProDisplayTitle(localPadId, padRecord.title); },
      setTitle: function(newTitle) {
        padRecord.title = newTitle;
        isDirty = true;
      },
      isDeleted: function() { return padRecord.isDeleted; },
      markDeleted: function() {
        padRecord.isDeleted = true;
        isDirty = true;
      },
      getPassword: function() { return padRecord.password; },
      setPassword: function(newPass) {
        if (newPass == "") {
          newPass = null;
        }
        padRecord.password = newPass;
        isDirty = true;
      },
      isArchived: function() { return padRecord.isArchived; },
      markArchived: function() {
        padRecord.isArchived = true;
        isDirty = true;
      },
      unmarkArchived: function() {
        padRecord.isArchived = false;
        isDirty = true;
      },
      setLastEditedDate: function(d) {
        padRecord.lastEditedDate = d;
        isDirty = true;
      },
      addEditor: function(editorId) {
        var es = String(editorId);
        if (es && es.length > 0 && stringutils.isNumeric(editorId)) {
          if (padRecord.proAttrs.editors.indexOf(editorId) < 0) {
            padRecord.proAttrs.editors.push(editorId);
            padRecord.proAttrs.editors.sort();
          }
          isDirty = true;
        }
      },
      setLastEditor: function(editorId) {
        var es = String(editorId);
        if (es && es.length > 0 && stringutils.isNumeric(editorId)) {
          padRecord.lastEditorId = editorId;
          this.addEditor(editorId);
          isDirty = true;
        }
      }
    };

    var ret = fn(proPad);

    if (isDirty) {
      pro_pad_db.update(padRecord);
    }

    return ret;
  });
}

function accessProPadLocal(localPadId, fn) {
   var globalPadId = padutils.getGlobalPadId(localPadId);
   return accessProPad(globalPadId, fn);
}