2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2015-12-31 13:19:23 +01:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01:00
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* 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
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const chat = require('./chat').chat;
|
|
|
|
const hooks = require('./pluginfw/hooks');
|
2012-01-16 05:16:11 +01:00
|
|
|
|
2012-01-17 09:43:11 +01:00
|
|
|
// Dependency fill on init. This exists for `pad.socket` only.
|
|
|
|
// TODO: bind directly to the socket.
|
2020-11-23 19:24:19 +01:00
|
|
|
let pad = undefined;
|
2012-01-17 09:43:11 +01:00
|
|
|
function getSocket() {
|
|
|
|
return pad && pad.socket;
|
|
|
|
}
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/** Call this when the document is ready, and a new Ace2Editor() has been created and inited.
|
|
|
|
ACE's ready callback does not need to have fired yet.
|
|
|
|
"serverVars" are from calling doc.getCollabClientVars() on the server. */
|
2020-11-21 19:37:57 +01:00
|
|
|
function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad) {
|
2020-11-23 19:24:19 +01:00
|
|
|
const editor = ace2editor;
|
2012-01-17 09:43:11 +01:00
|
|
|
pad = _pad; // Inject pad to avoid a circular dependency.
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let rev = serverVars.rev;
|
|
|
|
const padId = serverVars.padId;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let state = 'IDLE';
|
|
|
|
let stateMessage;
|
|
|
|
let channelState = 'CONNECTING';
|
|
|
|
let appLevelDisconnectReason = null;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let lastCommitTime = 0;
|
|
|
|
let initialStartConnectTime = 0;
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const userId = initialUserInfo.userId;
|
|
|
|
// var socket;
|
|
|
|
const userSet = {}; // userId -> userInfo
|
2011-03-26 14:10:41 +01:00
|
|
|
userSet[userId] = initialUserInfo;
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const caughtErrors = [];
|
|
|
|
const caughtErrorCatchers = [];
|
|
|
|
const caughtErrorTimes = [];
|
|
|
|
const debugMessages = [];
|
|
|
|
const msgQueue = [];
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let isPendingRevision = false;
|
2018-02-10 18:00:22 +01:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
tellAceAboutHistoricalAuthors(serverVars.historicalAuthorData);
|
|
|
|
tellAceActiveAuthorInfo(initialUserInfo);
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const callbacks = {
|
|
|
|
onUserJoin() {},
|
|
|
|
onUserLeave() {},
|
|
|
|
onUpdateUserInfo() {},
|
|
|
|
onChannelStateChange() {},
|
|
|
|
onClientMessage() {},
|
|
|
|
onInternalAction() {},
|
|
|
|
onConnectionTrouble() {},
|
2011-03-26 14:10:41 +01:00
|
|
|
};
|
2020-11-23 19:24:19 +01:00
|
|
|
if (browser.firefox) {
|
2011-03-26 14:10:41 +01:00
|
|
|
// Prevent "escape" from taking effect and canceling a comet connection;
|
|
|
|
// doesn't work if focus is on an iframe.
|
2020-11-23 19:24:19 +01:00
|
|
|
$(window).bind('keydown', (evt) => {
|
|
|
|
if (evt.which == 27) {
|
|
|
|
evt.preventDefault();
|
2011-07-07 19:59:34 +02:00
|
|
|
}
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
editor.setProperty('userAuthor', userId);
|
2011-03-26 14:10:41 +01:00
|
|
|
editor.setBaseAttributedText(serverVars.initialAttributedText, serverVars.apool);
|
2020-11-23 19:24:19 +01:00
|
|
|
editor.setUserChangeNotificationCallback(wrapRecordingErrors('handleUserChanges', handleUserChanges));
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function dmesg(str) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (typeof window.ajlog === 'string') window.ajlog += `${str}\n`;
|
2011-03-26 14:10:41 +01:00
|
|
|
debugMessages.push(str);
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function handleUserChanges() {
|
2012-10-11 16:39:01 +02:00
|
|
|
if (editor.getInInternationalComposition()) return;
|
2020-11-23 19:24:19 +01:00
|
|
|
if ((!getSocket()) || channelState == 'CONNECTING') {
|
|
|
|
if (channelState == 'CONNECTING' && (((+new Date()) - initialStartConnectTime) > 20000)) {
|
|
|
|
setChannelState('DISCONNECTED', 'initsocketfail');
|
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
// check again in a bit
|
2020-11-23 19:24:19 +01:00
|
|
|
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), 1000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const t = (+new Date());
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (state != 'IDLE') {
|
|
|
|
if (state == 'COMMITTING' && msgQueue.length == 0 && (t - lastCommitTime) > 20000) {
|
2011-03-26 14:10:41 +01:00
|
|
|
// a commit is taking too long
|
2020-11-23 19:24:19 +01:00
|
|
|
setChannelState('DISCONNECTED', 'slowcommit');
|
|
|
|
} else if (state == 'COMMITTING' && msgQueue.length == 0 && (t - lastCommitTime) > 5000) {
|
|
|
|
callbacks.onConnectionTrouble('SLOW');
|
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
// run again in a few seconds, to detect a disconnect
|
2020-11-23 19:24:19 +01:00
|
|
|
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), 3000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const earliestCommit = lastCommitTime + 500;
|
|
|
|
if (t < earliestCommit) {
|
|
|
|
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), earliestCommit - t);
|
2011-03-26 14:10:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-13 16:19:53 +02:00
|
|
|
// apply msgQueue changeset.
|
|
|
|
if (msgQueue.length != 0) {
|
2020-11-23 19:24:19 +01:00
|
|
|
let msg;
|
2014-12-16 17:00:35 +01:00
|
|
|
while (msg = msgQueue.shift()) {
|
2020-11-23 19:24:19 +01:00
|
|
|
const newRev = msg.newRev;
|
|
|
|
rev = newRev;
|
|
|
|
if (msg.type == 'ACCEPT_COMMIT') {
|
2012-09-13 16:19:53 +02:00
|
|
|
editor.applyPreparedChangesetToBase();
|
|
|
|
setStateIdle();
|
2020-11-23 19:24:19 +01:00
|
|
|
callCatchingErrors('onInternalAction', () => {
|
|
|
|
callbacks.onInternalAction('commitAcceptedByServer');
|
2012-09-13 16:19:53 +02:00
|
|
|
});
|
2020-11-23 19:24:19 +01:00
|
|
|
callCatchingErrors('onConnectionTrouble', () => {
|
|
|
|
callbacks.onConnectionTrouble('OK');
|
2012-09-13 16:19:53 +02:00
|
|
|
});
|
|
|
|
handleUserChanges();
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'NEW_CHANGES') {
|
|
|
|
const changeset = msg.changeset;
|
|
|
|
const author = (msg.author || '');
|
|
|
|
const apool = msg.apool;
|
2012-09-13 16:19:53 +02:00
|
|
|
|
|
|
|
editor.applyChangesToBase(changeset, author, apool);
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 15:21:14 +02:00
|
|
|
if (isPendingRevision) {
|
|
|
|
setIsPendingRevision(false);
|
|
|
|
}
|
2012-09-13 16:19:53 +02:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let sentMessage = false;
|
2018-04-03 15:21:14 +02:00
|
|
|
// Check if there are any pending revisions to be received from server.
|
|
|
|
// Allow only if there are no pending revisions to be received from server
|
2020-11-23 19:24:19 +01:00
|
|
|
if (!isPendingRevision) {
|
|
|
|
const userChangesData = editor.prepareUserChangeset();
|
|
|
|
if (userChangesData.changeset) {
|
|
|
|
lastCommitTime = t;
|
|
|
|
state = 'COMMITTING';
|
|
|
|
stateMessage = {
|
|
|
|
type: 'USER_CHANGES',
|
|
|
|
baseRev: rev,
|
|
|
|
changeset: userChangesData.changeset,
|
|
|
|
apool: userChangesData.apool,
|
|
|
|
};
|
|
|
|
sendMessage(stateMessage);
|
|
|
|
sentMessage = true;
|
|
|
|
callbacks.onInternalAction('commitPerformed');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// run again in a few seconds, to check if there was a reconnection attempt
|
|
|
|
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), 3000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (sentMessage) {
|
2011-03-26 14:10:41 +01:00
|
|
|
// run again in a few seconds, to detect a disconnect
|
2020-11-23 19:24:19 +01:00
|
|
|
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), 3000);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function setUpSocket() {
|
2011-07-07 19:59:34 +02:00
|
|
|
hiccupCount = 0;
|
2020-11-23 19:24:19 +01:00
|
|
|
setChannelState('CONNECTED');
|
2011-07-07 19:59:34 +02:00
|
|
|
doDeferredActions();
|
|
|
|
|
|
|
|
initialStartConnectTime = +new Date();
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
var hiccupCount = 0;
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function sendMessage(msg) {
|
2012-01-17 09:43:11 +01:00
|
|
|
getSocket().json.send(
|
2020-11-23 19:24:19 +01:00
|
|
|
{
|
|
|
|
type: 'COLLABROOM',
|
|
|
|
component: 'pad',
|
|
|
|
data: msg,
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function wrapRecordingErrors(catcher, func) {
|
2020-11-23 19:24:19 +01:00
|
|
|
return function () {
|
|
|
|
try {
|
2011-03-26 14:10:41 +01:00
|
|
|
return func.apply(this, Array.prototype.slice.call(arguments));
|
2020-11-23 19:24:19 +01:00
|
|
|
} catch (e) {
|
2011-03-26 14:10:41 +01:00
|
|
|
caughtErrors.push(e);
|
|
|
|
caughtErrorCatchers.push(catcher);
|
|
|
|
caughtErrorTimes.push(+new Date());
|
2020-11-23 19:24:19 +01:00
|
|
|
// console.dir({catcher: catcher, e: e});
|
2011-03-26 14:10:41 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function callCatchingErrors(catcher, func) {
|
2020-11-23 19:24:19 +01:00
|
|
|
try {
|
2011-03-26 14:10:41 +01:00
|
|
|
wrapRecordingErrors(catcher, func)();
|
2020-11-23 19:24:19 +01:00
|
|
|
} catch (e) { /* absorb*/
|
2011-07-07 19:59:34 +02:00
|
|
|
}
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function handleMessageFromServer(evt) {
|
2012-01-17 09:43:11 +01:00
|
|
|
if (!getSocket()) return;
|
2011-07-07 19:59:34 +02:00
|
|
|
if (!evt.data) return;
|
2020-11-23 19:24:19 +01:00
|
|
|
const wrapper = evt;
|
|
|
|
if (wrapper.type != 'COLLABROOM' && wrapper.type != 'CUSTOM') return;
|
|
|
|
const msg = wrapper.data;
|
2013-03-16 14:19:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (msg.type == 'NEW_CHANGES') {
|
2011-03-26 14:10:41 +01:00
|
|
|
var newRev = msg.newRev;
|
|
|
|
var changeset = msg.changeset;
|
|
|
|
var author = (msg.author || '');
|
|
|
|
var apool = msg.apool;
|
2012-09-13 16:19:53 +02:00
|
|
|
|
|
|
|
// When inInternationalComposition, msg pushed msgQueue.
|
2012-10-11 16:39:01 +02:00
|
|
|
if (msgQueue.length > 0 || editor.getInInternationalComposition()) {
|
2014-12-14 22:01:28 +01:00
|
|
|
if (msgQueue.length > 0) var oldRev = msgQueue[msgQueue.length - 1].newRev;
|
2012-09-13 16:19:53 +02:00
|
|
|
else oldRev = rev;
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (newRev != (oldRev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on NEW_CHANGES: ${newRev} not ${oldRev + 1}`);
|
2013-02-27 20:29:59 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_newchanges");
|
2012-09-13 16:19:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
msgQueue.push(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (newRev != (rev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on NEW_CHANGES: ${newRev} not ${rev + 1}`);
|
2013-02-27 20:29:59 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_newchanges");
|
2011-03-26 14:10:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
rev = newRev;
|
2020-06-12 13:26:33 +02:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
editor.applyChangesToBase(changeset, author, apool);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'ACCEPT_COMMIT') {
|
2011-03-26 14:10:41 +01:00
|
|
|
var newRev = msg.newRev;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (msgQueue.length > 0) {
|
|
|
|
if (newRev != (msgQueue[msgQueue.length - 1].newRev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on ACCEPT_COMMIT: ${newRev} not ${msgQueue[msgQueue.length - 1][0] + 1}`);
|
2013-02-27 20:29:59 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
2012-09-13 16:19:53 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
msgQueue.push(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (newRev != (rev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on ACCEPT_COMMIT: ${newRev} not ${rev + 1}`);
|
2013-02-27 20:29:59 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
2011-03-26 14:10:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
rev = newRev;
|
|
|
|
editor.applyPreparedChangesetToBase();
|
|
|
|
setStateIdle();
|
2020-11-23 19:24:19 +01:00
|
|
|
callCatchingErrors('onInternalAction', () => {
|
|
|
|
callbacks.onInternalAction('commitAcceptedByServer');
|
2011-03-26 14:10:41 +01:00
|
|
|
});
|
2020-11-23 19:24:19 +01:00
|
|
|
callCatchingErrors('onConnectionTrouble', () => {
|
|
|
|
callbacks.onConnectionTrouble('OK');
|
2011-03-26 14:10:41 +01:00
|
|
|
});
|
|
|
|
handleUserChanges();
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'CLIENT_RECONNECT') {
|
2018-04-03 15:21:14 +02:00
|
|
|
// Server sends a CLIENT_RECONNECT message when there is a client reconnect. Server also returns
|
|
|
|
// all pending revisions along with this CLIENT_RECONNECT message
|
2020-11-23 19:24:19 +01:00
|
|
|
if (msg.noChanges) {
|
2018-04-03 15:21:14 +02:00
|
|
|
// If no revisions are pending, just make everything normal
|
|
|
|
setIsPendingRevision(false);
|
2018-02-10 18:00:22 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const headRev = msg.headRev;
|
2018-02-10 18:00:22 +01:00
|
|
|
var newRev = msg.newRev;
|
|
|
|
var changeset = msg.changeset;
|
|
|
|
var author = (msg.author || '');
|
|
|
|
var apool = msg.apool;
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (msgQueue.length > 0) {
|
|
|
|
if (newRev != (msgQueue[msgQueue.length - 1].newRev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on CLIENT_RECONNECT: ${newRev} not ${msgQueue[msgQueue.length - 1][0] + 1}`);
|
2018-02-15 21:01:47 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
|
|
|
return;
|
2018-02-10 18:00:22 +01:00
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
msg.type = 'NEW_CHANGES';
|
2018-02-15 21:01:47 +01:00
|
|
|
msgQueue.push(msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (newRev != (rev + 1)) {
|
|
|
|
window.console.warn(`bad message revision on CLIENT_RECONNECT: ${newRev} not ${rev + 1}`);
|
2018-02-15 21:01:47 +01:00
|
|
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rev = newRev;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (author == pad.getUserId()) {
|
2018-02-15 21:01:47 +01:00
|
|
|
editor.applyPreparedChangesetToBase();
|
|
|
|
setStateIdle();
|
2020-11-23 19:24:19 +01:00
|
|
|
callCatchingErrors('onInternalAction', () => {
|
|
|
|
callbacks.onInternalAction('commitAcceptedByServer');
|
2018-02-15 21:01:47 +01:00
|
|
|
});
|
2020-11-23 19:24:19 +01:00
|
|
|
callCatchingErrors('onConnectionTrouble', () => {
|
|
|
|
callbacks.onConnectionTrouble('OK');
|
2018-02-15 21:01:47 +01:00
|
|
|
});
|
|
|
|
handleUserChanges();
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2018-02-10 18:00:22 +01:00
|
|
|
editor.applyChangesToBase(changeset, author, apool);
|
|
|
|
}
|
2018-02-15 21:01:47 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (newRev == headRev) {
|
2018-04-03 15:21:14 +02:00
|
|
|
// Once we have applied all pending revisions, make everything normal
|
|
|
|
setIsPendingRevision(false);
|
2018-02-10 18:00:22 +01:00
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'NO_COMMIT_PENDING') {
|
|
|
|
if (state == 'COMMITTING') {
|
2011-03-26 14:10:41 +01:00
|
|
|
// server missed our commit message; abort that commit
|
|
|
|
setStateIdle();
|
|
|
|
handleUserChanges();
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'USER_NEWINFO') {
|
2011-03-26 14:10:41 +01:00
|
|
|
var userInfo = msg.userInfo;
|
|
|
|
var id = userInfo.userId;
|
2012-09-15 23:48:04 +02:00
|
|
|
|
|
|
|
// Avoid a race condition when setting colors. If our color was set by a
|
|
|
|
// query param, ignore our own "new user" message's color value.
|
2020-11-23 19:24:19 +01:00
|
|
|
if (id === initialUserInfo.userId && initialUserInfo.globalUserColor) {
|
2012-09-15 23:48:04 +02:00
|
|
|
msg.userInfo.colorId = initialUserInfo.globalUserColor;
|
|
|
|
}
|
|
|
|
|
2015-12-31 13:19:23 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (userSet[id]) {
|
2011-03-26 14:10:41 +01:00
|
|
|
userSet[id] = userInfo;
|
|
|
|
callbacks.onUpdateUserInfo(userInfo);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
userSet[id] = userInfo;
|
|
|
|
callbacks.onUserJoin(userInfo);
|
|
|
|
}
|
|
|
|
tellAceActiveAuthorInfo(userInfo);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'USER_LEAVE') {
|
2011-03-26 14:10:41 +01:00
|
|
|
var userInfo = msg.userInfo;
|
|
|
|
var id = userInfo.userId;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (userSet[id]) {
|
2011-03-26 14:10:41 +01:00
|
|
|
delete userSet[userInfo.userId];
|
|
|
|
fadeAceAuthorInfo(userInfo);
|
|
|
|
callbacks.onUserLeave(userInfo);
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'DISCONNECT_REASON') {
|
2011-03-26 14:10:41 +01:00
|
|
|
appLevelDisconnectReason = msg.reason;
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'CLIENT_MESSAGE') {
|
2011-03-26 14:10:41 +01:00
|
|
|
callbacks.onClientMessage(msg.payload);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'CHAT_MESSAGE') {
|
2013-01-06 16:11:48 +01:00
|
|
|
chat.addMessage(msg, true, false);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (msg.type == 'CHAT_MESSAGES') {
|
|
|
|
for (let i = msg.messages.length - 1; i >= 0; i--) {
|
2013-01-06 16:11:48 +01:00
|
|
|
chat.addMessage(msg.messages[i], true, true);
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
if (!chat.gotInitalMessages) {
|
2013-01-06 16:11:48 +01:00
|
|
|
chat.scrollDown();
|
2013-01-07 17:36:03 +01:00
|
|
|
chat.gotInitalMessages = true;
|
|
|
|
chat.historyPointer = clientVars.chatHead - msg.messages.length;
|
2013-01-06 16:11:48 +01:00
|
|
|
}
|
2013-01-07 19:15:55 +01:00
|
|
|
|
|
|
|
// messages are loaded, so hide the loading-ball
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#chatloadmessagesball').css('display', 'none');
|
2013-01-07 19:15:55 +01:00
|
|
|
|
|
|
|
// there are less than 100 messages or we reached the top
|
2020-11-23 19:24:19 +01:00
|
|
|
if (chat.historyPointer <= 0) { $('#chatloadmessagesbutton').css('display', 'none'); } else // there are still more messages, re-show the load-button
|
|
|
|
{ $('#chatloadmessagesbutton').css('display', 'block'); }
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2015-12-31 13:19:23 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
// HACKISH: User messages do not have "payload" but "userInfo", so that all "handleClientMessage_USER_" hooks would work, populate payload
|
|
|
|
// FIXME: USER_* messages to have "payload" property instead of "userInfo", seems like a quite a big work
|
|
|
|
if (msg.type.indexOf('USER_') > -1) {
|
2015-12-31 13:19:23 +01:00
|
|
|
msg.payload = msg.userInfo;
|
|
|
|
}
|
2020-06-12 13:26:33 +02:00
|
|
|
// Similar for NEW_CHANGES
|
2020-11-23 19:24:19 +01:00
|
|
|
if (msg.type === 'NEW_CHANGES') msg.payload = msg;
|
2020-06-12 13:26:33 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
hooks.callAll(`handleClientMessage_${msg.type}`, {payload: msg.payload});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function updateUserInfo(userInfo) {
|
2011-03-26 14:10:41 +01:00
|
|
|
userInfo.userId = userId;
|
|
|
|
userSet[userId] = userInfo;
|
|
|
|
tellAceActiveAuthorInfo(userInfo);
|
2012-01-17 09:43:11 +01:00
|
|
|
if (!getSocket()) return;
|
2011-07-07 19:59:34 +02:00
|
|
|
sendMessage(
|
2020-11-23 19:24:19 +01:00
|
|
|
{
|
|
|
|
type: 'USERINFO_UPDATE',
|
|
|
|
userInfo,
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function tellAceActiveAuthorInfo(userInfo) {
|
2011-03-26 14:10:41 +01:00
|
|
|
tellAceAuthorInfo(userInfo.userId, userInfo.colorId);
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function tellAceAuthorInfo(userId, colorId, inactive) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (typeof colorId === 'number') {
|
2011-08-21 19:53:30 +02:00
|
|
|
colorId = clientVars.colorPalette[colorId];
|
|
|
|
}
|
2015-12-31 13:19:23 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const cssColor = colorId;
|
|
|
|
if (inactive) {
|
2011-08-20 19:22:10 +02:00
|
|
|
editor.setAuthorInfo(userId, {
|
|
|
|
bgcolor: cssColor,
|
2020-11-23 19:24:19 +01:00
|
|
|
fade: 0.5,
|
2011-08-20 19:22:10 +02:00
|
|
|
});
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-08-20 19:22:10 +02:00
|
|
|
editor.setAuthorInfo(userId, {
|
2020-11-23 19:24:19 +01:00
|
|
|
bgcolor: cssColor,
|
2011-08-20 19:22:10 +02:00
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function fadeAceAuthorInfo(userInfo) {
|
2011-03-26 14:10:41 +01:00
|
|
|
tellAceAuthorInfo(userInfo.userId, userInfo.colorId, true);
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function getConnectedUsers() {
|
2011-03-26 14:10:41 +01:00
|
|
|
return valuesArray(userSet);
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function tellAceAboutHistoricalAuthors(hadata) {
|
2020-11-23 19:24:19 +01:00
|
|
|
for (const author in hadata) {
|
|
|
|
const data = hadata[author];
|
|
|
|
if (!userSet[author]) {
|
2011-03-26 14:10:41 +01:00
|
|
|
tellAceAuthorInfo(author, data.colorId, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function setChannelState(newChannelState, moreInfo) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (newChannelState != channelState) {
|
2011-03-26 14:10:41 +01:00
|
|
|
channelState = newChannelState;
|
|
|
|
callbacks.onChannelStateChange(channelState, moreInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function valuesArray(obj) {
|
2020-11-23 19:24:19 +01:00
|
|
|
const array = [];
|
|
|
|
$.each(obj, (k, v) => {
|
2011-07-07 19:59:34 +02:00
|
|
|
array.push(v);
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to present a working interface even before the socket
|
|
|
|
// is connected for the first time.
|
2020-11-23 19:24:19 +01:00
|
|
|
let deferredActions = [];
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function defer(func, tag) {
|
2020-11-23 19:24:19 +01:00
|
|
|
return function () {
|
|
|
|
const that = this;
|
|
|
|
const args = arguments;
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function action() {
|
2011-03-26 14:10:41 +01:00
|
|
|
func.apply(that, args);
|
|
|
|
}
|
|
|
|
action.tag = tag;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (channelState == 'CONNECTING') {
|
2011-03-26 14:10:41 +01:00
|
|
|
deferredActions.push(action);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
action();
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
};
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function doDeferredActions(tag) {
|
2020-11-23 19:24:19 +01:00
|
|
|
const newArray = [];
|
|
|
|
for (let i = 0; i < deferredActions.length; i++) {
|
|
|
|
const a = deferredActions[i];
|
|
|
|
if ((!tag) || (tag == a.tag)) {
|
2011-03-26 14:10:41 +01:00
|
|
|
a();
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
newArray.push(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deferredActions = newArray;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function sendClientMessage(msg) {
|
2011-07-07 19:59:34 +02:00
|
|
|
sendMessage(
|
2020-11-23 19:24:19 +01:00
|
|
|
{
|
|
|
|
type: 'CLIENT_MESSAGE',
|
|
|
|
payload: msg,
|
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function getCurrentRevisionNumber() {
|
2011-03-26 14:10:41 +01:00
|
|
|
return rev;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function getMissedChanges() {
|
2020-11-23 19:24:19 +01:00
|
|
|
const obj = {};
|
2011-03-26 14:10:41 +01:00
|
|
|
obj.userInfo = userSet[userId];
|
|
|
|
obj.baseRev = rev;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (state == 'COMMITTING' && stateMessage) {
|
2011-03-26 14:10:41 +01:00
|
|
|
obj.committedChangeset = stateMessage.changeset;
|
|
|
|
obj.committedChangesetAPool = stateMessage.apool;
|
|
|
|
editor.applyPreparedChangesetToBase();
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
const userChangesData = editor.prepareUserChangeset();
|
|
|
|
if (userChangesData.changeset) {
|
2011-03-26 14:10:41 +01:00
|
|
|
obj.furtherChangeset = userChangesData.changeset;
|
|
|
|
obj.furtherChangesetAPool = userChangesData.apool;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function setStateIdle() {
|
2020-11-23 19:24:19 +01:00
|
|
|
state = 'IDLE';
|
|
|
|
callbacks.onInternalAction('newlyIdle');
|
2011-03-26 14:10:41 +01:00
|
|
|
schedulePerhapsCallIdleFuncs();
|
|
|
|
}
|
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function setIsPendingRevision(value) {
|
2018-04-03 15:21:14 +02:00
|
|
|
isPendingRevision = value;
|
2018-02-10 18:00:22 +01:00
|
|
|
}
|
2018-04-03 15:21:14 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function callWhenNotCommitting(func) {
|
2011-03-26 14:10:41 +01:00
|
|
|
idleFuncs.push(func);
|
|
|
|
schedulePerhapsCallIdleFuncs();
|
|
|
|
}
|
|
|
|
|
|
|
|
var idleFuncs = [];
|
2011-07-07 19:59:34 +02:00
|
|
|
|
2020-11-21 19:37:57 +01:00
|
|
|
function schedulePerhapsCallIdleFuncs() {
|
2020-11-23 19:24:19 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
if (state == 'IDLE') {
|
|
|
|
while (idleFuncs.length > 0) {
|
|
|
|
const f = idleFuncs.shift();
|
2011-03-26 14:10:41 +01:00
|
|
|
f();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const self = {
|
|
|
|
setOnUserJoin(cb) {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onUserJoin = cb;
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
setOnUserLeave(cb) {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onUserLeave = cb;
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
setOnUpdateUserInfo(cb) {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onUpdateUserInfo = cb;
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
setOnChannelStateChange(cb) {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onChannelStateChange = cb;
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
setOnClientMessage(cb) {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onClientMessage = cb;
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
setOnInternalAction(cb) {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onInternalAction = cb;
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
setOnConnectionTrouble(cb) {
|
2011-07-07 19:59:34 +02:00
|
|
|
callbacks.onConnectionTrouble = cb;
|
|
|
|
},
|
2011-03-26 14:10:41 +01:00
|
|
|
updateUserInfo: defer(updateUserInfo),
|
2020-11-23 19:24:19 +01:00
|
|
|
handleMessageFromServer,
|
|
|
|
getConnectedUsers,
|
|
|
|
sendClientMessage,
|
|
|
|
sendMessage,
|
|
|
|
getCurrentRevisionNumber,
|
|
|
|
getMissedChanges,
|
|
|
|
callWhenNotCommitting,
|
2011-11-26 00:24:10 +01:00
|
|
|
addHistoricalAuthors: tellAceAboutHistoricalAuthors,
|
2020-11-23 19:24:19 +01:00
|
|
|
setChannelState,
|
|
|
|
setStateIdle,
|
|
|
|
setIsPendingRevision,
|
2012-01-29 02:05:29 +01:00
|
|
|
};
|
|
|
|
|
2020-07-10 16:28:32 +02:00
|
|
|
setUpSocket();
|
2012-01-29 02:05:29 +01:00
|
|
|
return self;
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
|
2012-01-16 02:23:48 +01:00
|
|
|
exports.getCollabClient = getCollabClient;
|