Files
cosmopolite/static/hogfather.js

218 lines
4.7 KiB
JavaScript
Raw Normal View History

2015-10-17 15:55:43 +02:00
/**
* @license
* Copyright 2015, Ian Gulliver
*
* 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.
*/
// Namespace
var hogfather = {};
2015-10-17 15:55:43 +02:00
/**
* @constructor
* @param {Cosmopolite} cosmo
* @param {string} id
2015-10-17 15:55:43 +02:00
*/
hogfather.PublicChat = function(cosmo, id) {
2015-12-24 15:12:25 -08:00
this.cosmo_ = cosmo;
this.id_ = id;
this.subject_ = '/hogfather/public/' + id;
2015-12-27 21:40:00 -08:00
2015-12-29 10:43:47 -08:00
this.owners_ = [];
this.writers_ = [];
this.messages_ = [];
2015-12-29 10:43:47 -08:00
2015-12-27 21:40:00 -08:00
/**
* @type {DocumentFragment}
* @private
* Weep for all our souls.
*/
this.eventTarget_ = document.createDocumentFragment();
this.addEventListener =
this.eventTarget_.addEventListener.bind(this.eventTarget_);
this.removeEventListener =
this.eventTarget_.removeEventListener.bind(this.eventTarget_);
this.dispatchEvent =
this.eventTarget_.dispatchEvent.bind(this.eventTarget_);
this.cosmo_.addEventListener('message', this.onMessage_.bind(this));
};
2015-12-24 15:12:25 -08:00
/**
* @param {Cosmopolite} cosmo
* @return {Promise}
*/
hogfather.PublicChat.Create = function(cosmo) {
var id = cosmo.uuid();
return hogfather.PublicChat.Join(cosmo, id);
2015-12-24 15:12:25 -08:00
};
/**
2015-12-25 14:23:04 -08:00
* @param {Cosmopolite} cosmo
* @param {string} id
2015-12-25 14:23:04 -08:00
* @return {Promise}
2015-12-24 15:12:25 -08:00
*/
hogfather.PublicChat.Join = function(cosmo, id) {
2015-12-25 14:23:04 -08:00
return new Promise(function(resolve, reject) {
var chat = new hogfather.PublicChat(cosmo, id);
chat.Start().then(function() {
resolve(chat);
2015-12-25 14:23:04 -08:00
}).catch(function(err) {
reject(err);
});
});
2015-10-17 15:55:43 +02:00
};
2015-10-17 16:04:37 +02:00
/**
* @return {Promise}
*/
hogfather.PublicChat.prototype.Start = function() {
return new Promise(function(resolve, reject) {
this.cosmo_.subscribe(this.subject_, -1).then(function() {
console.log(this.loggingPrefix_(), 'ready');
resolve();
}.bind(this)).catch(function(err) {
reject(err);
});
}.bind(this));
};
2015-10-17 16:04:37 +02:00
/**
*/
hogfather.PublicChat.prototype.Shutdown = function() {
2015-10-17 16:04:37 +02:00
};
2015-12-25 14:23:04 -08:00
2015-12-27 21:21:23 -08:00
/**
* @return {string}
*/
2015-12-27 20:06:27 -08:00
hogfather.PublicChat.prototype.getID = function() {
return this.id_;
};
/**
* @private
* @param {Cosmopolite.typeMessage} message
* @param {Array.<string>} owners
* @param {Array.<string>} writers
2015-12-29 11:48:33 -08:00
* @return {boolean}
*/
hogfather.PublicChat.prototype.checkMessage_ = function(
message, owners, writers) {
// Bootstrapping for new groups
if (!owners.length) {
owners.push(message.sender);
}
if (!writers.length) {
writers.push(message.sender);
}
var acl;
switch (message.message.type) {
case 'message':
acl = writers;
break;
default:
console.log('Unknown message type:', message);
return false;
2015-12-29 11:48:33 -08:00
}
if (acl.indexOf(message.sender) == -1) {
2015-12-29 11:48:33 -08:00
console.log(this.loggingPrefix_(), 'message from unauthorized source:',
message, acl);
return false;
} else {
return true;
}
};
2015-12-27 21:21:23 -08:00
/**
* @return {Array.<Cosmopolite.typeMessage>}
*/
2015-12-27 20:48:50 -08:00
hogfather.PublicChat.prototype.getMessages = function() {
return this.messages_;
2015-12-27 20:48:50 -08:00
};
2015-12-27 21:21:23 -08:00
/**
* @param {!*} message
* @return {Promise}
*/
2015-12-27 20:48:50 -08:00
hogfather.PublicChat.prototype.sendMessage = function(message) {
return this.cosmo_.sendMessage(this.subject_, {
type: 'message',
message: message,
});
};
2015-12-27 21:40:00 -08:00
/**
* @private
* @param {Event} e
*/
hogfather.PublicChat.prototype.onMessage_ = function(e) {
var message = e.detail;
if (!this.checkMessage_(message, this.owners_, this.writers_)) {
return;
2015-12-29 10:43:47 -08:00
}
2015-12-27 21:40:00 -08:00
switch (message.message.type) {
case 'message':
var cleanMessage = this.cleanMessage_(message);
this.messages_.push(cleanMessage);
2015-12-27 21:40:00 -08:00
var e2 = new CustomEvent('message', {
'detail': cleanMessage,
2015-12-27 21:40:00 -08:00
});
this.dispatchEvent(e2);
break;
}
};
/**
* @private
* @param {Cosmopolite.typeMessage} message
* @return {Cosmopolite.typeMessage}
*/
hogfather.PublicChat.prototype.cleanMessage_ = function(message) {
// Copy message so we can modify it.
message = /** @type {Cosmopolite.typeMessage} */ (
JSON.parse(JSON.stringify(message)));
// message == cosmopolite message
// message.message = hogfather message
// message.message.message == application message
message.message = message.message.message;
return message;
};
2015-12-25 14:23:04 -08:00
/**
* @private
* @return {string}
*/
hogfather.PublicChat.prototype.loggingPrefix_ = function() {
return 'hogfather (' + this.id_ + '):';
2015-12-25 14:23:04 -08:00
};