~mrooney/etherpad/ubuntu

« back to all changes in this revision

Viewing changes to trunk/trunk/etherpad/src/etherpad/pro/pro_pad_editors.js

  • Committer: Aaron Iba
  • Date: 2009-12-18 07:40:23 UTC
  • Revision ID: hg-v1:a9f8774a2e00cc15b35857471fecea17f649e3c9
initial code push

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Copyright 2009 Google Inc.
 
3
 * 
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 * 
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 * 
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS-IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
import("execution");
 
18
import("jsutils.*");
 
19
import("cache_utils.syncedWithCache");
 
20
 
 
21
import("etherpad.pad.padutils");
 
22
import("etherpad.pro.pro_padmeta");
 
23
import("etherpad.log");
 
24
 
 
25
var _DOMAIN_EDIT_WRITE_INTERVAL = 2000; // 2 seconds
 
26
 
 
27
function _withCache(name, fn) {
 
28
  return syncedWithCache('pro-padmeta.'+name, fn);
 
29
}
 
30
 
 
31
function _withDomainCache(domainId, name, fn) {
 
32
  return _withCache(name+"."+domainId, fn);
 
33
}
 
34
 
 
35
 
 
36
function onStartup() {
 
37
  execution.initTaskThreadPool("pro-padmeta-edits", 1);
 
38
}
 
39
 
 
40
function onShutdown() {
 
41
  var success = execution.shutdownAndWaitOnTaskThreadPool("pro-padmeta-edits", 4000);
 
42
  if (!success) {
 
43
    log.warn("Warning: pro.padmeta failed to flush pad edits on shutdown.");
 
44
  }
 
45
}
 
46
 
 
47
function notifyEdit(domainId, localPadId, editorId, editTime) {
 
48
  if (!editorId) {
 
49
    // guest editors
 
50
    return;
 
51
  }
 
52
  _withDomainCache(domainId, "edits", function(c) {
 
53
    if (!c[localPadId]) {
 
54
      c[localPadId] = {
 
55
        lastEditorId: editorId,
 
56
        lastEditTime: editTime,
 
57
        recentEditors: []
 
58
      };
 
59
    }
 
60
    var info = c[localPadId];
 
61
    if (info.recentEditors.indexOf(editorId) < 0) {
 
62
      info.recentEditors.push(editorId);
 
63
    }
 
64
  });
 
65
  _flushPadEditsEventually(domainId);
 
66
}
 
67
 
 
68
 
 
69
function _flushPadEditsEventually(domainId) {
 
70
  // Make sure there is a recurring edit-writer for this domain
 
71
  _withDomainCache(domainId, "recurring-edit-writers", function(c) {
 
72
    if (!c[domainId]) {
 
73
      flushEditsNow(domainId);
 
74
      c[domainId] = true;
 
75
    }
 
76
  });
 
77
}
 
78
 
 
79
function flushEditsNow(domainId) {
 
80
  if (!appjet.cache.shutdownHandlerIsRunning) {
 
81
    execution.scheduleTask("pro-padmeta-edits", "proPadmetaFlushEdits",
 
82
                            _DOMAIN_EDIT_WRITE_INTERVAL, [domainId]);
 
83
  }
 
84
 
 
85
  _withDomainCache(domainId, "edits", function(edits) {
 
86
    var padIdList = keys(edits);
 
87
    padIdList.forEach(function(localPadId) {
 
88
      _writePadEditsToDbNow(domainId, localPadId, edits[localPadId]);
 
89
      delete edits[localPadId];
 
90
    });
 
91
  });
 
92
}
 
93
 
 
94
function _writePadEditsToDbNow(domainId, localPadId, editInfo) {
 
95
  var globalPadId = padutils.makeGlobalId(domainId, localPadId);
 
96
  pro_padmeta.accessProPad(globalPadId, function(propad) {
 
97
    propad.setLastEditedDate(editInfo.lastEditTime);
 
98
    propad.setLastEditor(editInfo.lastEditorId);
 
99
    editInfo.recentEditors.forEach(function(eid) {
 
100
      propad.addEditor(eid);
 
101
    });
 
102
  });
 
103
}
 
104