Skip to content

Commit

Permalink
Merge pull request #392 from SockDrawer/ignore-categories-#383
Browse files Browse the repository at this point in the history
Ignore categories #383
  • Loading branch information
AccaliaDeElementia authored Dec 2, 2016
2 parents 7436514 + 75bf350 commit 0d0cb5e
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 24 deletions.
11 changes: 10 additions & 1 deletion docs/api/providers/nodebb/notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ NodeBB provider module Notification class
* [.id](#sockbot.providers.nodebb.module_Notification..Notification+id) : <code>string</code>
* [.postId](#sockbot.providers.nodebb.module_Notification..Notification+postId) : <code>number</code>
* [.topicId](#sockbot.providers.nodebb.module_Notification..Notification+topicId) : <code>number</code>
* [.categoryId](#sockbot.providers.nodebb.module_Notification..Notification+categoryId) : <code>number</code>
* [.userId](#sockbot.providers.nodebb.module_Notification..Notification+userId) : <code>number</code>
* [.type](#sockbot.providers.nodebb.module_Notification..Notification+type) : <code>notificationType</code>
* [.subtype](#sockbot.providers.nodebb.module_Notification..Notification+subtype) : <code>string</code>
Expand Down Expand Up @@ -53,7 +54,7 @@ Create a Notification class and bind it to a forum instance
#### bindNotification~notificationType
Notification types enum

**Kind**: inner constant of <code>[bindNotification](#sockbot.providers.nodebb.module_Notification.bindNotification)</code>
**Kind**: inner enum of <code>[bindNotification](#sockbot.providers.nodebb.module_Notification.bindNotification)</code>
**Read only**: true
**Properties**

Expand All @@ -79,6 +80,7 @@ Represents a forum notification
* [.id](#sockbot.providers.nodebb.module_Notification..Notification+id) : <code>string</code>
* [.postId](#sockbot.providers.nodebb.module_Notification..Notification+postId) : <code>number</code>
* [.topicId](#sockbot.providers.nodebb.module_Notification..Notification+topicId) : <code>number</code>
* [.categoryId](#sockbot.providers.nodebb.module_Notification..Notification+categoryId) : <code>number</code>
* [.userId](#sockbot.providers.nodebb.module_Notification..Notification+userId) : <code>number</code>
* [.type](#sockbot.providers.nodebb.module_Notification..Notification+type) : <code>notificationType</code>
* [.subtype](#sockbot.providers.nodebb.module_Notification..Notification+subtype) : <code>string</code>
Expand Down Expand Up @@ -130,6 +132,13 @@ Post id this notification refers to
#### notification.topicId : <code>number</code>
Topic id this post refers to

**Kind**: instance property of <code>[Notification](#sockbot.providers.nodebb.module_Notification..Notification)</code>
**Access:** public
<a name="sockbot.providers.nodebb.module_Notification..Notification+categoryId"></a>

#### notification.categoryId : <code>number</code>
Category id this post refers to

**Kind**: instance property of <code>[Notification](#sockbot.providers.nodebb.module_Notification..Notification)</code>
**Access:** public
<a name="sockbot.providers.nodebb.module_Notification..Notification+userId"></a>
Expand Down
9 changes: 9 additions & 0 deletions docs/api/providers/nodebb/topic.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ NodeBB provider module Topic class
* [~Topic](#sockbot.providers.nodebb.module_Topic..Topic)
* [new Topic(payload)](#new_sockbot.providers.nodebb.module_Topic..Topic_new)
* _instance_
* [.categoryId](#sockbot.providers.nodebb.module_Topic..Topic+categoryId) : <code>number</code>
* [.authorId](#sockbot.providers.nodebb.module_Topic..Topic+authorId) : <code>number</code>
* [.title](#sockbot.providers.nodebb.module_Topic..Topic+title) : <code>string</code>
* [.posted](#sockbot.providers.nodebb.module_Topic..Topic+posted) : <code>Date</code>
Expand Down Expand Up @@ -61,6 +62,7 @@ Represends a forum topic
* [~Topic](#sockbot.providers.nodebb.module_Topic..Topic)
* [new Topic(payload)](#new_sockbot.providers.nodebb.module_Topic..Topic_new)
* _instance_
* [.categoryId](#sockbot.providers.nodebb.module_Topic..Topic+categoryId) : <code>number</code>
* [.authorId](#sockbot.providers.nodebb.module_Topic..Topic+authorId) : <code>number</code>
* [.title](#sockbot.providers.nodebb.module_Topic..Topic+title) : <code>string</code>
* [.posted](#sockbot.providers.nodebb.module_Topic..Topic+posted) : <code>Date</code>
Expand Down Expand Up @@ -97,6 +99,13 @@ This constructor is intended for private use only, if you need top construct a t
| --- | --- | --- |
| payload | <code>\*</code> | Payload to construct the User object out of |

<a name="sockbot.providers.nodebb.module_Topic..Topic+categoryId"></a>

#### topic.categoryId : <code>number</code>
Forum specific ID for topic category

**Kind**: instance property of <code>[Topic](#sockbot.providers.nodebb.module_Topic..Topic)</code>
**Access:** public
<a name="sockbot.providers.nodebb.module_Topic..Topic+authorId"></a>

#### topic.authorId : <code>number</code>
Expand Down
86 changes: 66 additions & 20 deletions providers/nodebb/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ exports.bindNotification = function bindNotification(forum) {
id: payload.nid,
postId: payload.pid,
topicId: payload.tid,
categoryId: payload.cid,
userId: payload.from,
read: payload.read,
date: new Date(payload.datetime),
Expand Down Expand Up @@ -114,6 +115,17 @@ exports.bindNotification = function bindNotification(forum) {
get topicId() {
return utils.mapGet(this, 'topicId');
}

/**
* Category id this post refers to
*
* @public
*
* @type {number}
*/
get categoryId() {
return utils.mapGet(this, 'categoryId');
}

/**
* User id that generated this notification
Expand Down Expand Up @@ -373,29 +385,63 @@ exports.bindNotification = function bindNotification(forum) {
*/
function notifyHandler(data) {
const notification = Notification.parse(data);
//debug(`Notification ${notification.id}: ${notification.label} received`);
forum.emit('log', `Notification ${notification.id}: ${notification.label} received`);
return evalBlacklist(notification)
.then(() => {
forum.emit('log', `Notification ${notification.id}: ${notification.label} received`);

const ids = {
post: notification.postId,
topic: notification.topicId,
user: notification.userId,
room: -1,
chat: -1
};
return notification.getText()
.then((postData) => forum.Commands.get(ids,
postData, (content) => forum.Post.reply(notification.topicId, notification.postId, content)))
.then((commands) => {
if (commands.commands.length === 0) {
debug(`Emitting events: 'notification' and 'notification:${notification.type}'`);
forum.emit(`notification:${notification.type}`, notification);
forum.emit('notification', notification);
}
return commands;
})
.then((commands) => commands.execute());
}).catch((err) => {
if (err === 'Ignoring notification') {
//We do not process the notification, but we can continue with life
return Promise.resolve();
}
throw err;
});
}

/**
* Evaluate the blacklist.
*
* Determine if we want to process this notification or not based on config settings
*
* @private
*
* @param {*} notification Notification we are parsing
* @returns {Promise} Rejects with "Ignoring notification" if we do not process this. Resolves with the notification otherwise.
*/
function evalBlacklist(notification) {
return new Promise((resolve, reject) => {
const ignoreCategories = forum.config.core.ignoreCategories || [];

const ids = {
post: notification.postId,
topic: notification.topicId,
user: notification.userId,
room: -1,
chat: -1
};
return notification.getText()
.then((postData) => forum.Commands.get(ids,
postData, (content) => forum.Post.reply(notification.topicId, notification.postId, content)))
.then((commands) => {
if (commands.commands.length === 0) {
debug(`Emitting events: 'notification' and 'notification:${notification.type}'`);
forum.emit(`notification:${notification.type}`, notification);
forum.emit('notification', notification);
//if there's no blacklist, we can ignore the hit for getting the category
if (ignoreCategories) {
if (ignoreCategories.some((elem) => elem.toString() === notification.categoryId.toString())) {
forum.emit('log', `Notification from category ${notification.categoryId} ignored`);
return reject('Ignoring notification');
}
return commands;
})
.then((commands) => commands.execute());
}
return resolve(notification);
});
}

return Notification;
};

14 changes: 13 additions & 1 deletion providers/nodebb/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,23 @@ exports.bindTopic = function bindTopic(forum) {
lastPosted: new Date(payload.lastposttime),
id: payload.tid,
mainPostId: payload.mainPid,
postCount: payload.postcount
postCount: payload.postcount,
categoryId: payload.cid
};
utils.mapSet(this, values);
}

/**
* Forum specific ID for topic category
*
* @public
*
* @type {!number}
*/
get categoryId() {
return utils.mapGet(this, 'categoryId');
}

/**
* Forum specific ID for topic author
*
Expand Down
45 changes: 44 additions & 1 deletion test/providers/nodebb/notificationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('providers/nodebb/notification', () => {
['id', 'nid'],
['postId', 'pid'],
['topicId', 'tid'],
['categoryId', 'cid'],
['userId', 'from'],
['read', 'read'],
['url', 'path']
Expand Down Expand Up @@ -146,7 +147,7 @@ describe('providers/nodebb/notification', () => {
notification = new Notification({});
data = utils.mapGet(notification);
});
['id', 'postId', 'topicId', 'userId', 'type', 'subtype', 'read',
['id', 'postId', 'topicId', 'userId', 'categoryId', 'type', 'subtype', 'read',
'date', 'label', 'body'
].forEach((key) => {
it(`should get value from utils.storage for ${key}`, () => {
Expand Down Expand Up @@ -567,6 +568,9 @@ describe('providers/nodebb/notification', () => {
forum.Post = {
reply: sinon.stub().resolves()
};
forum.config = {
core: {}
};
Notification.activate();
notifyHandler = forum.socket.on.firstCall.args[1];
});
Expand Down Expand Up @@ -668,6 +672,45 @@ describe('providers/nodebb/notification', () => {
forum.emit.should.not.have.been.calledWith('notification', notifier);
});
});
it('should not emit notifications in blacklisted categories', () => {
forum.config.core = {
ignoreCategories: [3]
};

commands.commands = [];
notifier.categoryId = 3;
notifier.type = `a${Math.random()}b`;
return notifyHandler(42).then(() => {
forum.emit.should.not.have.been.calledWith('notification', notifier);
});
});
it('should emit notifications in non-blacklisted categories', () => {
forum.config.core = {
ignoreCategories: [2]
};

commands.commands = [];
notifier.categoryId = 3;
notifier.type = `a${Math.random()}b`;
return notifyHandler(42).then(() => {
forum.emit.should.have.been.calledWith('notification', notifier);
});
});
it('should not reject if the blacklist rejects', () => {
forum.config.core = {
ignoreCategories: [3]
};
commands.commands = [];
notifier.categoryId = 3;
notifier.type = `a${Math.random()}b`;

return notifyHandler(5).should.be.fulfilled;
});
it('should reject if something throws a wobbly other than the blacklist', () => {
notifier.getText.rejects('Bad wolf');

return notifyHandler(5).should.be.rejectedWith('Bad wolf');
});
});
});
});
3 changes: 2 additions & 1 deletion test/providers/nodebb/topicTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('providers/nodebb/topic', () => {
['title', 'title'],
['url', 'slug'],
['id', 'tid'],
['categoryId', 'cid'],
['mainPostId', 'mainPid'],
['postCount', 'postcount']
].forEach((keys) => {
Expand Down Expand Up @@ -76,7 +77,7 @@ describe('providers/nodebb/topic', () => {
data = utils.mapGet(topic);
});
['authorId', 'title', 'id', 'mainPostId', 'postCount', 'posted',
'lastPosted'
'lastPosted', 'categoryId'
].forEach((key) => {
it(`should get value from utils.storage for ${key}`, () => {
const expected = `${Math.random()}${Math.random()}`;
Expand Down

0 comments on commit 0d0cb5e

Please sign in to comment.