From b2bb5738ebc8270ebcc7496d7276a59264842629 Mon Sep 17 00:00:00 2001 From: Yami Date: Tue, 11 Apr 2017 14:38:35 +0000 Subject: [PATCH] feat(topic): Added lock and unlock These should reject if you're not a moderator --- providers/nodebb/topic.js | 38 ++++++++++++++++++++++ test/providers/nodebb/topicTest.js | 52 ++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/providers/nodebb/topic.js b/providers/nodebb/topic.js index 1b8435e3..85ef5f01 100644 --- a/providers/nodebb/topic.js +++ b/providers/nodebb/topic.js @@ -347,6 +347,44 @@ exports.bindTopic = function bindTopic(forum) { unmute() { return Promise.resolve(this); } + + /** + * Locks the topic. Will reject if you're not a moderator. + * + * @public + * + * @returns {Promise} Resolves to self on completion + * + * @promise + * @fulfill {Topic} Source Topic + * @reject {Error} An Error that occured while posting + */ + lock() { + return forum._emit('topics.lock', { + tids: [this.id], + cid: this.categoryId + }) + .then(() => this); + } + + /** + * Unlocks the topic. Will reject if you're not a moderator. + * + * @public + * + * @returns {Promise} Resolves to self on completion + * + * @promise + * @fulfill {Topic} Source Topic + * @reject {Error} An Error that occured while posting + */ + unlock() { + return forum._emit('topics.unlock', { + tids: [this.id], + cid: this.categoryId + }) + .then(() => this); + } /** * Retrieve a topic by topic id diff --git a/test/providers/nodebb/topicTest.js b/test/providers/nodebb/topicTest.js index 0d7bb25c..cf1e5eac 100644 --- a/test/providers/nodebb/topicTest.js +++ b/test/providers/nodebb/topicTest.js @@ -369,6 +369,58 @@ describe('providers/nodebb/topic', () => { return topic.unmute().should.become(topic); }); }); + describe('lock()', () => { + let topic, data; + beforeEach(() => { + topic = new Topic({}); + data = utils.mapGet(topic); + }); + it('should emit `topics.lock` via websocket', () => { + const id = Math.random(); + const cid = Math.random(); + data.id = id; + data.categoryId = cid; + return topic.lock().then(() => { + forum._emit.calledWith('topics.lock', { + tids: [id], + cid: cid + }).should.be.true; + }); + }); + it('should resolve to locked topic', () => { + return topic.lock().should.become(topic); + }); + it('should reject if websocket rejects', () => { + forum._emit.rejects('bad'); + return topic.lock().should.be.rejected; + }); + }); + describe('unlock()', () => { + let topic, data; + beforeEach(() => { + topic = new Topic({}); + data = utils.mapGet(topic); + }); + it('should emit `topics.unlock` via websocket', () => { + const id = Math.random(); + const cid = Math.random(); + data.id = id; + data.categoryId = cid; + return topic.unlock().then(() => { + forum._emit.calledWith('topics.unlock', { + tids: [id], + cid: cid + }).should.be.true; + }); + }); + it('should resolve to locked topic', () => { + return topic.unlock().should.become(topic); + }); + it('should reject if websocket rejects', () => { + forum._emit.rejects('bad'); + return topic.unlock().should.be.rejected; + }); + }); describe('static functions', () => { describe('static getRecentTopics()', () => { let spy = () => 0;