diff --git a/docs/api/providers/nodebb/category.md b/docs/api/providers/nodebb/category.md index 4859f535..f900ba81 100644 --- a/docs/api/providers/nodebb/category.md +++ b/docs/api/providers/nodebb/category.md @@ -23,6 +23,7 @@ NodeBB provider module Category class * [.url()](#sockbot.providers.nodebb.module_Category..Category+url) ⇒ Promise.<string> * [.getAllTopics(eachTopic)](#sockbot.providers.nodebb.module_Category..Category+getAllTopics) ⇒ Promise * [.getRecentTopics(eachTopic)](#sockbot.providers.nodebb.module_Category..Category+getRecentTopics) ⇒ Promise + * [.addTopic(title, body)](#sockbot.providers.nodebb.module_Category..Category+addTopic) ⇒ Promise.<Topic> * [.watch()](#sockbot.providers.nodebb.module_Category..Category+watch) ⇒ Promise.<Category> * [.unwatch()](#sockbot.providers.nodebb.module_Category..Category+unwatch) ⇒ Promise.<Category> * [.mute()](#sockbot.providers.nodebb.module_Category..Category+mute) ⇒ Promise.<Category> @@ -66,6 +67,7 @@ Represents a forum category. * [.url()](#sockbot.providers.nodebb.module_Category..Category+url) ⇒ Promise.<string> * [.getAllTopics(eachTopic)](#sockbot.providers.nodebb.module_Category..Category+getAllTopics) ⇒ Promise * [.getRecentTopics(eachTopic)](#sockbot.providers.nodebb.module_Category..Category+getRecentTopics) ⇒ Promise + * [.addTopic(title, body)](#sockbot.providers.nodebb.module_Category..Category+addTopic) ⇒ Promise.<Topic> * [.watch()](#sockbot.providers.nodebb.module_Category..Category+watch) ⇒ Promise.<Category> * [.unwatch()](#sockbot.providers.nodebb.module_Category..Category+unwatch) ⇒ Promise.<Category> * [.mute()](#sockbot.providers.nodebb.module_Category..Category+mute) ⇒ Promise.<Category> @@ -170,6 +172,20 @@ Get all recently active Topics in the category | --- | --- | --- | | eachTopic | TopicProcessor | A function to process each topic | + + +#### category.addTopic(title, body) ⇒ Promise.<Topic> +Add a topic to this category + +**Kind**: instance method of [Category](#sockbot.providers.nodebb.module_Category..Category) +**Returns**: Promise.<Topic> - Resolves to the newly created topic +**Access:** public + +| Param | Type | Description | +| --- | --- | --- | +| title | string | The title of the topic | +| body | string | The body of the first post of the topic | + #### category.watch() ⇒ Promise.<Category> diff --git a/package.json b/package.json index 06757297..732f22fa 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,6 @@ { "name": "sockbot", + "version": "0.0.0-semantic-release", "description": "A sockpuppet bot to use on http://what.thedailywtf.com.", "repository": { "type": "git", diff --git a/providers/nodebb/category.js b/providers/nodebb/category.js index dbcc04cd..8bf4f820 100644 --- a/providers/nodebb/category.js +++ b/providers/nodebb/category.js @@ -225,6 +225,29 @@ exports.bindCategory = function bindCategory(forum) { return forum._emit(action, payload) .then(() => this); } + + /** + * Add a topic to this category + * + * @public + * + * @param {string} title The title of the topic + * @param {string} body The body of the first post of the topic + * + * @returns {Promise} Resolves to the newly created topic + */ + addTopic(title, body) { + const payload = { + 'category_id': this.id, + title: title, + content: body, + tags: [], + 'topic_thumb': '' + }; + + return forum._emit('topics.post', payload) + .then((results) => forum.Topic.parse(results)); + } /** * Watch this category for new activity @@ -286,6 +309,7 @@ exports.bindCategory = function bindCategory(forum) { return forum.fetchObject('categories.getCategory', categoryId, Category.parse); } + /** * Parse a category from payload data * diff --git a/test/providers/nodebb/categoryTest.js b/test/providers/nodebb/categoryTest.js index 81ece144..ddf8822c 100644 --- a/test/providers/nodebb/categoryTest.js +++ b/test/providers/nodebb/categoryTest.js @@ -25,6 +25,9 @@ describe('providers/nodebb/categor', () => { const Category = categoryModule.bindCategory(forum); beforeEach(() => { forum._emit = sinon.stub().resolves(); + forum.Topic = { + parse: sinon.stub().resolves() + }; forum.fetchObject = sinon.stub().resolves(); }); describe('ctor()', () => { @@ -94,6 +97,18 @@ describe('providers/nodebb/categor', () => { return category.url().should.become(expected); }); }); + + describe('addTopic()', () => { + let category; + beforeEach(() => { + category = new Category({}); + }); + it('should emit `topics.post`', () => { + return category.addTopic('title', 'body').then(() => { + forum._emit.should.have.been.calledWith('topics.post').once; + }); + }); + }); describe('getAllTopics()', () => { let category, data, spy; beforeEach(() => {