From 3b3755f7f5a458f847d9cd6ef5f75ca5eb6ecf00 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Tue, 29 Dec 2015 12:00:35 -0800 Subject: [PATCH] Add amOwner() and amWriter(). Reject sendMessage() if we lack write access. --- static/cosmopolite.js | 2 +- static/hogfather.js | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/static/cosmopolite.js b/static/cosmopolite.js index 797c4f0..61bddb3 100644 --- a/static/cosmopolite.js +++ b/static/cosmopolite.js @@ -413,7 +413,7 @@ Cosmopolite.prototype.sendMessage = function(subject, message) { (subject['readable_only_by'] || subject['writable_only_by'])) { console.log(this.loggingPrefix_(), 'local subjects can\'t have ACLs:', subject); - reject(); + reject(new Error('Local subject with ACL')); return; } diff --git a/static/hogfather.js b/static/hogfather.js index 5abd5cb..d747314 100644 --- a/static/hogfather.js +++ b/static/hogfather.js @@ -109,6 +109,22 @@ hogfather.PublicChat.prototype.getID = function() { }; +/** + * @return {boolean} + */ +hogfather.PublicChat.prototype.amOwner = function() { + return this.owners_.indexOf(this.cosmo_.currentProfile()) >= 0; +}; + + +/** + * @return {boolean} + */ +hogfather.PublicChat.prototype.amWriter = function() { + return this.writers_.indexOf(this.cosmo_.currentProfile()) >= 0; +}; + + /** * @private * @param {Cosmopolite.typeMessage} message @@ -162,10 +178,20 @@ hogfather.PublicChat.prototype.getMessages = function() { * @return {Promise} */ hogfather.PublicChat.prototype.sendMessage = function(message) { - return this.cosmo_.sendMessage(this.subject_, { - type: 'message', - message: message, - }); + return new Promise(function(resolve, reject) { + if (!this.amWriter()) { + reject(new Error('Write access denied')); + return; + } + this.cosmo_.sendMessage(this.subject_, { + type: 'message', + message: message, + }).then(function(msg) { + resolve(msg); + }).catch(function(err) { + reject(err); + }); + }.bind(this)); };