| 
									
										
										
										
											2014-03-06 14:51:43 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | const async = require('async'); | 
					
						
							|  |  |  | const assert = require('assert'); | 
					
						
							|  |  |  | const validator = require('validator'); | 
					
						
							|  |  |  | const nconf = require('nconf'); | 
					
						
							|  |  |  | const request = require('request'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | const topics = require('../src/topics'); | 
					
						
							|  |  |  | const posts = require('../src/posts'); | 
					
						
							|  |  |  | const categories = require('../src/categories'); | 
					
						
							|  |  |  | const privileges = require('../src/privileges'); | 
					
						
							|  |  |  | const meta = require('../src/meta'); | 
					
						
							|  |  |  | const User = require('../src/user'); | 
					
						
							|  |  |  | const groups = require('../src/groups'); | 
					
						
							|  |  |  | const helpers = require('./helpers'); | 
					
						
							|  |  |  | const socketPosts = require('../src/socket.io/posts'); | 
					
						
							|  |  |  | const socketTopics = require('../src/socket.io/topics'); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | describe('Topic\'s', function () { | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 	var topic; | 
					
						
							|  |  |  | 	var categoryObj; | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 	var adminUid; | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 	var adminJar; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	before(async function () { | 
					
						
							|  |  |  | 		adminUid = await User.create({ username: 'admin', password: '123456' }); | 
					
						
							|  |  |  | 		await groups.join('administrators', adminUid); | 
					
						
							|  |  |  | 		adminJar = await helpers.loginUser('admin', '123456'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		categoryObj = await categories.create({ | 
					
						
							|  |  |  | 			name: 'Test Category', | 
					
						
							|  |  |  | 			description: 'Test category created by testing script', | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		topic = { | 
					
						
							|  |  |  | 			userId: adminUid, | 
					
						
							|  |  |  | 			categoryId: categoryObj.cid, | 
					
						
							|  |  |  | 			title: 'Test Topic Title', | 
					
						
							|  |  |  | 			content: 'The content of test topic', | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2013-11-17 23:50:19 +02:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('.post', function () { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 		it('should fail to create topic with invalid data', function (done) { | 
					
						
							|  |  |  | 			socketTopics.post({ uid: 0 }, null, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should create a new topic with proper parameters', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(result); | 
					
						
							|  |  |  | 				topic.tid = result.topicData.tid; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 		it('should get post count', function (done) { | 
					
						
							|  |  |  | 			socketTopics.postcount({ uid: adminUid }, topic.tid, function (err, count) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(count, 1); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 		it('should load topic', function (done) { | 
					
						
							|  |  |  | 			socketTopics.getTopic({ uid: adminUid }, topic.tid, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.tid, topic.tid); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should fail to create new topic with invalid user id', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: null, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err) { | 
					
						
							| 
									
										
										
										
											2014-11-11 23:03:20 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-11 23:12:37 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should fail to create new topic with empty title', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: '', content: topic.content, cid: topic.categoryId }, function (err) { | 
					
						
							| 
									
										
										
										
											2014-11-12 01:19:40 -05:00
										 |  |  | 				assert.ok(err); | 
					
						
							| 
									
										
										
										
											2014-11-11 23:12:37 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should fail to create new topic with empty content', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: '', cid: topic.categoryId }, function (err) { | 
					
						
							| 
									
										
										
										
											2014-11-12 01:19:40 -05:00
										 |  |  | 				assert.ok(err); | 
					
						
							| 
									
										
										
										
											2014-11-11 23:12:37 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should fail to create new topic with non-existant category id', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: 99 }, function (err) { | 
					
						
							| 
									
										
										
										
											2014-11-12 01:22:50 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:no-category]]', 'received no error'); | 
					
						
							| 
									
										
										
										
											2014-11-11 23:12:37 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should return false for falsy uid', function (done) { | 
					
						
							|  |  |  | 			topics.isOwner(topic.tid, 0, function (err, isOwner) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(!isOwner); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('.reply', function () { | 
					
						
							| 
									
										
										
										
											2014-11-12 14:34:02 -05:00
										 |  |  | 		var newTopic; | 
					
						
							|  |  |  | 		var newPost; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-12 14:34:02 -05:00
										 |  |  | 				newTopic = result.topicData; | 
					
						
							|  |  |  | 				newPost = result.postData; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should create a new reply with proper parameters', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.reply({ uid: topic.userId, content: 'test post', tid: newTopic.tid }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2014-11-12 14:34:02 -05:00
										 |  |  | 				assert.equal(err, null, 'was created with error'); | 
					
						
							|  |  |  | 				assert.ok(result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-12 15:06:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-28 09:18:09 -05:00
										 |  |  | 		it('should handle direct replies', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.reply({ uid: topic.userId, content: 'test reply', tid: newTopic.tid, toPid: newPost.pid }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-10-28 09:18:09 -05:00
										 |  |  | 				assert.equal(err, null, 'was created with error'); | 
					
						
							|  |  |  | 				assert.ok(result); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				socketPosts.getReplies({ uid: 0 }, newPost.pid, function (err, postData) { | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-28 09:18:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-31 11:48:58 +03:00
										 |  |  | 					assert.ok(postData); | 
					
						
							| 
									
										
										
										
											2016-10-28 09:18:09 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-31 11:48:58 +03:00
										 |  |  | 					assert.equal(postData.length, 1, 'should have 1 result'); | 
					
						
							|  |  |  | 					assert.equal(postData[0].pid, result.pid, 'result should be the reply we added'); | 
					
						
							| 
									
										
										
										
											2016-10-28 09:18:09 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 17:02:54 +03:00
										 |  |  | 		it('should error if pid is not a number', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 			socketPosts.getReplies({ uid: 0 }, 'abc', function (err) { | 
					
						
							| 
									
										
										
										
											2017-02-23 17:02:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should fail to create new reply with invalid user id', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.reply({ uid: null, content: 'test post', tid: newTopic.tid }, function (err) { | 
					
						
							| 
									
										
										
										
											2014-11-12 15:06:55 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should fail to create new reply with empty content', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.reply({ uid: topic.userId, content: '', tid: newTopic.tid }, function (err) { | 
					
						
							| 
									
										
										
										
											2014-11-12 15:06:55 -05:00
										 |  |  | 				assert.ok(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should fail to create new reply with invalid topic id', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.reply({ uid: null, content: 'test post', tid: 99 }, function (err) { | 
					
						
							| 
									
										
										
										
											2014-11-12 15:06:55 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:no-topic]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-17 16:00:39 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail to create new reply with invalid toPid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.reply({ uid: topic.userId, content: 'test post', tid: newTopic.tid, toPid: '"onmouseover=alert(1);//' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-17 16:00:39 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-pid]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-12 14:34:02 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('Get methods', function () { | 
					
						
							| 
									
										
										
										
											2013-11-17 23:50:19 +02:00
										 |  |  | 		var	newTopic; | 
					
						
							|  |  |  | 		var newPost; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-17 23:50:19 +02:00
										 |  |  | 				newTopic = result.topicData; | 
					
						
							|  |  |  | 				newPost = result.postData; | 
					
						
							|  |  |  | 				done(); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:05:51 +03:00
										 |  |  | 		it('should not receive errors', function (done) { | 
					
						
							| 
									
										
										
										
											2018-10-21 19:33:46 -04:00
										 |  |  | 			topics.getTopicData(newTopic.tid, function (err, topicData) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(typeof topicData.tid === 'number'); | 
					
						
							|  |  |  | 				assert(typeof topicData.uid === 'number'); | 
					
						
							|  |  |  | 				assert(typeof topicData.cid === 'number'); | 
					
						
							|  |  |  | 				assert(typeof topicData.mainPid === 'number'); | 
					
						
							| 
									
										
										
										
											2018-10-25 19:58:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-21 19:33:46 -04:00
										 |  |  | 				assert(typeof topicData.timestamp === 'number'); | 
					
						
							| 
									
										
										
										
											2018-10-26 13:01:34 -04:00
										 |  |  | 				assert.strictEqual(topicData.postcount, 1); | 
					
						
							|  |  |  | 				assert.strictEqual(topicData.viewcount, 0); | 
					
						
							| 
									
										
										
										
											2018-10-25 19:58:01 -04:00
										 |  |  | 				assert.strictEqual(topicData.upvotes, 0); | 
					
						
							|  |  |  | 				assert.strictEqual(topicData.downvotes, 0); | 
					
						
							|  |  |  | 				assert.strictEqual(topicData.votes, 0); | 
					
						
							|  |  |  | 				assert.strictEqual(topicData.deleted, 0); | 
					
						
							|  |  |  | 				assert.strictEqual(topicData.locked, 0); | 
					
						
							|  |  |  | 				assert.strictEqual(topicData.pinned, 0); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get a single field', function (done) { | 
					
						
							|  |  |  | 			topics.getTopicFields(newTopic.tid, ['slug'], function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Object.keys(data).length === 1); | 
					
						
							|  |  |  | 				assert(data.hasOwnProperty('slug')); | 
					
						
							| 
									
										
										
										
											2018-10-21 19:33:46 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-23 23:05:51 +03:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get topic title by pid', function (done) { | 
					
						
							|  |  |  | 			topics.getTitleByPid(newPost.pid, function (err, title) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(title, topic.title); | 
					
						
							|  |  |  | 				done(); | 
					
						
							| 
									
										
										
										
											2013-11-17 23:50:19 +02:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-08-13 01:20:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-23 23:05:51 +03:00
										 |  |  | 		it('should get topic data by pid', function (done) { | 
					
						
							|  |  |  | 			topics.getTopicDataByPid(newPost.pid, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.tid, newTopic.tid); | 
					
						
							|  |  |  | 				done(); | 
					
						
							| 
									
										
										
										
											2013-11-17 23:50:19 +02:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-08-13 01:20:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		describe('.getTopicWithPosts', function () { | 
					
						
							|  |  |  | 			it('should get a topic with posts and other data', function (done) { | 
					
						
							|  |  |  | 				topics.getTopicData(newTopic.tid, function (err, topicData) { | 
					
						
							| 
									
										
										
										
											2016-08-13 01:20:19 +03:00
										 |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return done(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					topics.getTopicWithPosts(topicData, 'tid:' + newTopic.tid + ':posts', topic.userId, 0, -1, false, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-08-13 01:20:19 +03:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return done(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						assert(data); | 
					
						
							|  |  |  | 						assert.equal(data.category.cid, topic.categoryId); | 
					
						
							|  |  |  | 						assert.equal(data.unreplied, true); | 
					
						
							|  |  |  | 						assert.equal(data.deleted, false); | 
					
						
							|  |  |  | 						assert.equal(data.locked, false); | 
					
						
							|  |  |  | 						assert.equal(data.pinned, false); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('Title escaping', function () { | 
					
						
							|  |  |  | 		it('should properly escape topic title', function (done) { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 			var title = '"<script>alert(\'ok1\');</script> new topic test'; | 
					
						
							|  |  |  | 			var titleEscaped = validator.escape(title); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: title, content: topic.content, cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				topics.getTopicData(result.topicData.tid, function (err, topicData) { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-02-18 20:26:09 +02:00
										 |  |  | 					assert.strictEqual(topicData.titleRaw, title); | 
					
						
							|  |  |  | 					assert.strictEqual(topicData.title, titleEscaped); | 
					
						
							| 
									
										
										
										
											2016-02-18 18:46:51 +02:00
										 |  |  | 					done(); | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 	describe('tools/delete/restore/purge', function () { | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 		var newTopic; | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 		var followerUid; | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 		var moveCid; | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						newTopic = result.topicData; | 
					
						
							|  |  |  | 						next(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					User.create({ username: 'topicFollower', password: '123456' }, next); | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (_uid, next) { | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 					followerUid = _uid; | 
					
						
							|  |  |  | 					topics.follow(newTopic.tid, _uid, next); | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					categories.create({ | 
					
						
							|  |  |  | 						name: 'Test Category', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 						description: 'Test category created by testing script', | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					}, function (err, category) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						moveCid = category.cid; | 
					
						
							|  |  |  | 						next(); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 			], done); | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 		it('should load topic tools', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.loadTopicTools({ uid: adminUid }, { tid: newTopic.tid }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should delete the topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.delete({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 		it('should restore the topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.restore({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 		it('should lock topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.lock({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.isLocked(newTopic.tid, function (err, isLocked) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(isLocked); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should unlock topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.unlock({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.isLocked(newTopic.tid, function (err, isLocked) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!isLocked); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should pin topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.pin({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:02:59 -04:00
										 |  |  | 				topics.getTopicField(newTopic.tid, 'pinned', function (err, pinned) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:02:59 -04:00
										 |  |  | 					assert.strictEqual(pinned, 1); | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should unpin topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.unpin({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:02:59 -04:00
										 |  |  | 				topics.getTopicField(newTopic.tid, 'pinned', function (err, pinned) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2018-10-25 17:02:59 -04:00
										 |  |  | 					assert.strictEqual(pinned, 0); | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should move all topics', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.moveAll({ uid: adminUid }, { cid: moveCid, currentCid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.getTopicField(newTopic.tid, 'cid', function (err, cid) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(cid, moveCid); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should move a topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.move({ uid: adminUid }, { cid: categoryObj.cid, tids: [newTopic.tid] }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.getTopicField(newTopic.tid, 'cid', function (err, cid) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(cid, categoryObj.cid); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 		it('should properly update sets when post is moved', function (done) { | 
					
						
							|  |  |  | 			var movedPost; | 
					
						
							|  |  |  | 			var previousPost; | 
					
						
							|  |  |  | 			var topic2LastReply; | 
					
						
							|  |  |  | 			var tid1; | 
					
						
							|  |  |  | 			var tid2; | 
					
						
							|  |  |  | 			var cid1 = topic.categoryId; | 
					
						
							|  |  |  | 			var cid2; | 
					
						
							|  |  |  | 			function checkCidSets(post1, post2, callback) { | 
					
						
							|  |  |  | 				async.waterfall([ | 
					
						
							|  |  |  | 					function (next) { | 
					
						
							|  |  |  | 						async.parallel({ | 
					
						
							|  |  |  | 							topicData: function (next) { | 
					
						
							|  |  |  | 								topics.getTopicsFields([tid1, tid2], ['lastposttime', 'postcount'], next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							scores1: function (next) { | 
					
						
							|  |  |  | 								db.sortedSetsScore([ | 
					
						
							|  |  |  | 									'cid:' + cid1 + ':tids', | 
					
						
							|  |  |  | 									'cid:' + cid1 + ':tids:lastposttime', | 
					
						
							|  |  |  | 									'cid:' + cid1 + ':tids:posts', | 
					
						
							|  |  |  | 								], tid1, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							scores2: function (next) { | 
					
						
							|  |  |  | 								db.sortedSetsScore([ | 
					
						
							|  |  |  | 									'cid:' + cid2 + ':tids', | 
					
						
							|  |  |  | 									'cid:' + cid2 + ':tids:lastposttime', | 
					
						
							|  |  |  | 									'cid:' + cid2 + ':tids:posts', | 
					
						
							|  |  |  | 								], tid2, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							| 
									
										
										
										
											2018-10-12 12:58:11 -04:00
										 |  |  | 							posts1: function (next) { | 
					
						
							|  |  |  | 								db.getSortedSetRangeWithScores('tid:' + tid1 + ':posts', 0, -1, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							posts2: function (next) { | 
					
						
							|  |  |  | 								db.getSortedSetRangeWithScores('tid:' + tid2 + ':posts', 0, -1, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 						}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function (results, next) { | 
					
						
							| 
									
										
										
										
											2018-10-12 12:58:11 -04:00
										 |  |  | 						var assertMsg = JSON.stringify(results.posts1) + '\n' + JSON.stringify(results.posts2); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[0].postcount, results.scores1[2], assertMsg); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[1].postcount, results.scores2[2], assertMsg); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[0].lastposttime, post1.timestamp, assertMsg); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[1].lastposttime, post2.timestamp, assertMsg); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[0].lastposttime, results.scores1[0], assertMsg); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[1].lastposttime, results.scores2[0], assertMsg); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[0].lastposttime, results.scores1[1], assertMsg); | 
					
						
							|  |  |  | 						assert.equal(results.topicData[1].lastposttime, results.scores2[1], assertMsg); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 						next(); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				], callback); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					categories.create({ | 
					
						
							|  |  |  | 						name: 'move to this category', | 
					
						
							|  |  |  | 						description: 'Test category created by testing script', | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					cid2 = category.cid; | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, title: 'topic1', content: 'topic 1 mainPost', cid: cid1 }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (result, next) { | 
					
						
							|  |  |  | 					tid1 = result.topicData.tid; | 
					
						
							|  |  |  | 					topics.reply({ uid: adminUid, content: 'topic 1 reply 1', tid: tid1 }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (postData, next) { | 
					
						
							|  |  |  | 					previousPost = postData; | 
					
						
							|  |  |  | 					topics.reply({ uid: adminUid, content: 'topic 1 reply 2', tid: tid1 }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (postData, next) { | 
					
						
							|  |  |  | 					movedPost = postData; | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, title: 'topic2', content: 'topic 2 mainpost', cid: cid2 }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (results, next) { | 
					
						
							|  |  |  | 					tid2 = results.topicData.tid; | 
					
						
							|  |  |  | 					topics.reply({ uid: adminUid, content: 'topic 2 reply 1', tid: tid2 }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (postData, next) { | 
					
						
							|  |  |  | 					topic2LastReply = postData; | 
					
						
							|  |  |  | 					checkCidSets(movedPost, postData, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					db.isMemberOfSortedSets(['cid:' + cid1 + ':pids', 'cid:' + cid2 + ':pids'], movedPost.pid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (isMember, next) { | 
					
						
							|  |  |  | 					assert.deepEqual(isMember, [true, false]); | 
					
						
							|  |  |  | 					categories.getCategoriesFields([cid1, cid2], ['post_count'], next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (categoryData, next) { | 
					
						
							|  |  |  | 					assert.equal(categoryData[0].post_count, 4); | 
					
						
							|  |  |  | 					assert.equal(categoryData[1].post_count, 2); | 
					
						
							| 
									
										
										
										
											2018-10-23 17:50:36 -04:00
										 |  |  | 					topics.movePostToTopic(1, movedPost.pid, tid2, next); | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					checkCidSets(previousPost, topic2LastReply, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					db.isMemberOfSortedSets(['cid:' + cid1 + ':pids', 'cid:' + cid2 + ':pids'], movedPost.pid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (isMember, next) { | 
					
						
							|  |  |  | 					assert.deepEqual(isMember, [false, true]); | 
					
						
							|  |  |  | 					categories.getCategoriesFields([cid1, cid2], ['post_count'], next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (categoryData, next) { | 
					
						
							|  |  |  | 					assert.equal(categoryData[0].post_count, 3); | 
					
						
							|  |  |  | 					assert.equal(categoryData[1].post_count, 3); | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 		it('should fail to purge topic if user does not have privilege', function (done) { | 
					
						
							|  |  |  | 			var globalModUid; | 
					
						
							|  |  |  | 			var tid; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.post({ | 
					
						
							|  |  |  | 						uid: adminUid, | 
					
						
							|  |  |  | 						title: 'topic for purge test', | 
					
						
							|  |  |  | 						content: 'topic content', | 
					
						
							|  |  |  | 						cid: categoryObj.cid, | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (result, next) { | 
					
						
							|  |  |  | 					tid = result.topicData.tid; | 
					
						
							|  |  |  | 					User.create({ username: 'global mod' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (uid, next) { | 
					
						
							|  |  |  | 					globalModUid = uid; | 
					
						
							|  |  |  | 					groups.join('Global Moderators', uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					privileges.categories.rescind(['purge'], categoryObj.cid, 'Global Moderators', next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					socketTopics.purge({ uid: globalModUid }, { tids: [tid], cid: categoryObj.cid }, function (err) { | 
					
						
							|  |  |  | 						assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 						privileges.categories.give(['purge'], categoryObj.cid, 'Global Moderators', next); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should purge the topic', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.purge({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.isSortedSetMember('uid:' + followerUid + ':followed_tids', newTopic.tid, function (err, isMember) { | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.strictEqual(false, isMember); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 	describe('order pinned topics', function () { | 
					
						
							|  |  |  | 		var tid1; | 
					
						
							|  |  |  | 		var tid2; | 
					
						
							|  |  |  | 		var tid3; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			function createTopic(callback) { | 
					
						
							|  |  |  | 				topics.post({ | 
					
						
							|  |  |  | 					uid: topic.userId, | 
					
						
							|  |  |  | 					title: 'topic for test', | 
					
						
							|  |  |  | 					content: 'topic content', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					cid: topic.categoryId, | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				}, callback); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			async.series({ | 
					
						
							|  |  |  | 				topic1: function (next) { | 
					
						
							|  |  |  | 					createTopic(next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				topic2: function (next) { | 
					
						
							|  |  |  | 					createTopic(next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				topic3: function (next) { | 
					
						
							|  |  |  | 					createTopic(next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 			}, function (err, results) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				tid1 = results.topic1.topicData.tid; | 
					
						
							|  |  |  | 				tid2 = results.topic2.topicData.tid; | 
					
						
							|  |  |  | 				tid3 = results.topic3.topicData.tid; | 
					
						
							|  |  |  | 				async.series([ | 
					
						
							|  |  |  | 					function (next) { | 
					
						
							|  |  |  | 						topics.tools.pin(tid1, adminUid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function (next) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 						// artificial timeout so pin time is different on redis sometimes scores are indentical
 | 
					
						
							|  |  |  | 						setTimeout(function () { | 
					
						
							|  |  |  | 							topics.tools.pin(tid2, adminUid, next); | 
					
						
							|  |  |  | 						}, 5); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				], done); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.orderPinnedTopics({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.orderPinnedTopics({ uid: adminUid }, [null, null], function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error with unprivileged user', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.orderPinnedTopics({ uid: 0 }, [{ tid: tid1 }, { tid: tid2 }], function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not do anything if topics are not pinned', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.orderPinnedTopics({ uid: adminUid }, [{ tid: tid3 }], function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.isSortedSetMember('cid:' + topic.categoryId + ':tids:pinned', tid3, function (err, isMember) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!isMember); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should order pinned topics', function (done) { | 
					
						
							|  |  |  | 			db.getSortedSetRevRange('cid:' + topic.categoryId + ':tids:pinned', 0, -1, function (err, pinnedTids) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(pinnedTids[0], tid2); | 
					
						
							|  |  |  | 				assert.equal(pinnedTids[1], tid1); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				socketTopics.orderPinnedTopics({ uid: adminUid }, [{ tid: tid1, order: 1 }, { tid: tid2, order: 0 }], function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					db.getSortedSetRevRange('cid:' + topic.categoryId + ':tids:pinned', 0, -1, function (err, pinnedTids) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert.equal(pinnedTids[0], tid1); | 
					
						
							|  |  |  | 						assert.equal(pinnedTids[1], tid2); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2017-01-27 20:37:54 +03:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 	describe('.ignore', function () { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 		var newTid; | 
					
						
							|  |  |  | 		var uid; | 
					
						
							|  |  |  | 		var newTopic; | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			uid = topic.userId; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: topic.userId, title: 'Topic to be ignored', content: 'Just ignore me, please!', cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return done(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 						newTopic = result.topicData; | 
					
						
							|  |  |  | 						newTid = newTopic.tid; | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					topics.markUnread(newTid, uid, done); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:31:47 -07:00
										 |  |  | 			], done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 		it('should not appear in the unread list', function (done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					topics.ignore(newTid, uid, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-24 12:46:40 -05:00
										 |  |  | 					topics.getUnreadTopics({ cid: 0, uid: uid, start: 0, stop: -1, filter: '' }, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (results, done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 					var topics = results.topics; | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					var tids = topics.map(function (topic) { return topic.tid; }); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 					assert.equal(tids.indexOf(newTid), -1, 'The topic appeared in the unread list.'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 		it('should not appear as unread in the recent list', function (done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					topics.ignore(newTid, uid, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2019-02-22 14:39:05 -05:00
										 |  |  | 					topics.getLatestTopics({ | 
					
						
							|  |  |  | 						uid: uid, | 
					
						
							|  |  |  | 						start: 0, | 
					
						
							|  |  |  | 						stop: -1, | 
					
						
							|  |  |  | 						term: 'year', | 
					
						
							|  |  |  | 					}, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (results, done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 					var topics = results.topics; | 
					
						
							|  |  |  | 					var topic; | 
					
						
							|  |  |  | 					var i; | 
					
						
							| 
									
										
										
										
											2017-02-18 01:52:56 -07:00
										 |  |  | 					for (i = 0; i < topics.length; i += 1) { | 
					
						
							| 
									
										
										
										
											2018-10-23 21:36:00 -04:00
										 |  |  | 						if (topics[i].tid === parseInt(newTid, 10)) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 							assert.equal(false, topics[i].unread, 'ignored topic was marked as unread in recent list'); | 
					
						
							|  |  |  | 							return done(); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					assert.ok(topic, 'topic didn\'t appear in the recent list'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 		it('should appear as unread again when marked as reading', function (done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					topics.ignore(newTid, uid, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					topics.follow(newTid, uid, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-24 12:46:40 -05:00
										 |  |  | 					topics.getUnreadTopics({ cid: 0, uid: uid, start: 0, stop: -1, filter: '' }, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (results, done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 					var topics = results.topics; | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					var tids = topics.map(function (topic) { return topic.tid; }); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 					assert.notEqual(tids.indexOf(newTid), -1, 'The topic did not appear in the unread list.'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 		it('should appear as unread again when marked as following', function (done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					topics.ignore(newTid, uid, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					topics.follow(newTid, uid, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-24 12:46:40 -05:00
										 |  |  | 					topics.getUnreadTopics({ cid: 0, uid: uid, start: 0, stop: -1, filter: '' }, done); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (results, done) { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 					var topics = results.topics; | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 					var tids = topics.map(function (topic) { return topic.tid; }); | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 					assert.notEqual(tids.indexOf(newTid), -1, 'The topic did not appear in the unread list.'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 	describe('.fork', function () { | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 		var newTopic; | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 		var replies = []; | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 		var topicPids; | 
					
						
							| 
									
										
										
										
											2019-10-07 12:46:48 -04:00
										 |  |  | 		var originalBookmark = 6; | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 		function postReply(next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.reply({ uid: topic.userId, content: 'test post ' + replies.length, tid: newTopic.tid }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2017-02-18 02:38:03 -07:00
										 |  |  | 				assert.equal(err, null, 'was created with error'); | 
					
						
							|  |  |  | 				assert.ok(result); | 
					
						
							|  |  |  | 				replies.push(result); | 
					
						
							|  |  |  | 				next(); | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 					groups.join('administrators', topic.userId, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 						assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 						newTopic = result.topicData; | 
					
						
							|  |  |  | 						next(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { postReply(next); }, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topicPids = replies.map(function (reply) { return reply.pid; }); | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 					socketTopics.bookmark({ uid: topic.userId }, { tid: newTopic.tid, index: originalBookmark }, next); | 
					
						
							| 
									
										
										
										
											2017-10-01 16:19:10 -06:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 19:21:56 +03:00
										 |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							|  |  |  | 			socketTopics.bookmark({ uid: topic.userId }, null, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should have 12 replies', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 			assert.equal(12, replies.length); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							|  |  |  | 			socketTopics.createTopicFromPosts({ uid: 0 }, null, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:not-logged-in]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			socketTopics.createTopicFromPosts({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 		it('should not update the user\'s bookmark', function (done) { | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 					socketTopics.createTopicFromPosts({ uid: topic.userId }, { | 
					
						
							|  |  |  | 						title: 'Fork test, no bookmark update', | 
					
						
							|  |  |  | 						pids: topicPids.slice(-2), | 
					
						
							|  |  |  | 						fromTid: newTopic.tid, | 
					
						
							|  |  |  | 					}, next); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 				function (forkedTopicData, next) { | 
					
						
							|  |  |  | 					topics.getUserBookmark(newTopic.tid, topic.userId, next); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 				function (bookmark, next) { | 
					
						
							|  |  |  | 					assert.equal(originalBookmark, bookmark); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 					next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:31:47 -07:00
										 |  |  | 			], done); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 		it('should update the user\'s bookmark ', function (done) { | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 					topics.createTopicFromPosts( | 
					
						
							|  |  |  | 						topic.userId, | 
					
						
							|  |  |  | 						'Fork test, no bookmark update', | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 						topicPids.slice(1, 3), | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 						newTopic.tid, | 
					
						
							| 
									
										
										
										
											2017-10-01 16:19:10 -06:00
										 |  |  | 						next | 
					
						
							|  |  |  | 					); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 				function (forkedTopicData, next) { | 
					
						
							|  |  |  | 					topics.getUserBookmark(newTopic.tid, topic.userId, next); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 				function (bookmark, next) { | 
					
						
							|  |  |  | 					assert.equal(originalBookmark - 2, bookmark); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 					next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2017-02-18 01:31:47 -07:00
										 |  |  | 			], done); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 	describe('controller', function () { | 
					
						
							|  |  |  | 		var topicData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			topics.post({ | 
					
						
							|  |  |  | 				uid: topic.userId, | 
					
						
							|  |  |  | 				title: 'topic for controller test', | 
					
						
							|  |  |  | 				content: 'topic content', | 
					
						
							|  |  |  | 				cid: topic.categoryId, | 
					
						
							| 
									
										
										
										
											2019-07-09 12:46:49 -04:00
										 |  |  | 				thumb: 'http://i.imgur.com/64iBdBD.jpg', | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 			}, function (err, result) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.ok(result); | 
					
						
							|  |  |  | 				topicData = result.topicData; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load topic', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/topic/' + topicData.slug, function (err, response, body) { | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 if post index is invalid', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/topic/' + topicData.slug + '/derp', function (err, response) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 if topic does not exist', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/topic/123123/does-not-exist', function (err, response) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 401 if not allowed to read as guest', function (done) { | 
					
						
							|  |  |  | 			var privileges = require('../src/privileges'); | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			privileges.categories.rescind(['topics:read'], topicData.cid, 'guests', function (err) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				request(nconf.get('url') + '/api/topic/' + topicData.slug, function (err, response, body) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(response.statusCode, 401); | 
					
						
							|  |  |  | 					assert(body); | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 					privileges.categories.give(['topics:read'], topicData.cid, 'guests', done); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should redirect to correct topic if slug is missing', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/topic/' + topicData.tid + '/herpderp/1?page=2', function (err, response, body) { | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should redirect if post index is out of range', function (done) { | 
					
						
							| 
									
										
										
										
											2017-07-02 16:26:31 -06:00
										 |  |  | 			request(nconf.get('url') + '/api/topic/' + topicData.slug + '/-1', { json: true }, function (err, res, body) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-07-02 16:26:31 -06:00
										 |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 				assert.equal(res.headers['x-redirect'], '/topic/' + topicData.tid + '/topic-for-controller-test'); | 
					
						
							|  |  |  | 				assert.equal(body, '/topic/' + topicData.tid + '/topic-for-controller-test'); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 if page is out of bounds', function (done) { | 
					
						
							|  |  |  | 			var meta = require('../src/meta'); | 
					
						
							|  |  |  | 			meta.config.usePagination = 1; | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/topic/' + topicData.slug + '?page=100', function (err, response) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should mark topic read', function (done) { | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 			request(nconf.get('url') + '/topic/' + topicData.slug, { | 
					
						
							|  |  |  | 				jar: adminJar, | 
					
						
							|  |  |  | 			}, function (err, res) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 				assert.equal(res.statusCode, 200); | 
					
						
							|  |  |  | 				topics.hasReadTopics([topicData.tid], adminUid, function (err, hasRead) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 					assert.equal(hasRead[0], true); | 
					
						
							|  |  |  | 					done(); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 if tid is not a number', function (done) { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 			request(nconf.get('url') + '/api/topic/teaser/nan', { json: true }, function (err, response) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 403 if cant read', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-24 11:45:04 -05:00
										 |  |  | 			request(nconf.get('url') + '/api/topic/teaser/' + 123123, { json: true }, function (err, response, body) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 403); | 
					
						
							|  |  |  | 				assert.equal(body, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load topic teaser', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-24 11:45:04 -05:00
										 |  |  | 			request(nconf.get('url') + '/api/topic/teaser/' + topicData.tid, { json: true }, function (err, response, body) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				assert.equal(body.tid, topicData.tid); | 
					
						
							|  |  |  | 				assert.equal(body.content, 'topic content'); | 
					
						
							|  |  |  | 				assert(body.user); | 
					
						
							|  |  |  | 				assert(body.topic); | 
					
						
							|  |  |  | 				assert(body.category); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 if tid is not a number', function (done) { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 			request(nconf.get('url') + '/api/topic/pagination/nan', { json: true }, function (err, response) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should 404 if tid does not exist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 			request(nconf.get('url') + '/api/topic/pagination/1231231', { json: true }, function (err, response) { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load pagination', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-24 11:45:04 -05:00
										 |  |  | 			request(nconf.get('url') + '/api/topic/pagination/' + topicData.tid, { json: true }, function (err, response, body) { | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.deepEqual(body, { | 
					
						
							|  |  |  | 					prev: { page: 1, active: false }, | 
					
						
							|  |  |  | 					next: { page: 1, active: false }, | 
					
						
							| 
									
										
										
										
											2019-03-19 11:51:40 -04:00
										 |  |  | 					first: { page: 1, active: true }, | 
					
						
							|  |  |  | 					last: { page: 1, active: true }, | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 					rel: [], | 
					
						
							|  |  |  | 					pages: [], | 
					
						
							|  |  |  | 					currentPage: 1, | 
					
						
							| 
									
										
										
										
											2017-02-24 11:45:04 -05:00
										 |  |  | 					pageCount: 1, | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 	describe('infinitescroll', function () { | 
					
						
							|  |  |  | 		var socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		var tid; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				tid = result.topicData.tid; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.loadMore({ uid: adminUid }, {}, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should infinite load topic posts', function (done) { | 
					
						
							| 
									
										
										
										
											2017-03-10 12:50:19 -05:00
										 |  |  | 			socketTopics.loadMore({ uid: adminUid }, { tid: tid, after: 0, count: 10 }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data.mainPost); | 
					
						
							|  |  |  | 				assert(data.posts); | 
					
						
							|  |  |  | 				assert(data.privileges); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2018-10-27 06:26:50 -04:00
										 |  |  | 			socketTopics.loadMoreSortedTopics({ uid: adminUid }, { after: 'invalid' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load more unread topics', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2018-10-27 06:26:50 -04:00
										 |  |  | 				socketTopics.loadMoreSortedTopics({ uid: adminUid }, { cid: topic.categoryId, after: 0, count: 10, sort: 'unread' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(data); | 
					
						
							|  |  |  | 					assert(Array.isArray(data.topics)); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2018-10-27 06:26:50 -04:00
										 |  |  | 			socketTopics.loadMoreSortedTopics({ uid: adminUid }, { after: 'invalid' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load more recent topics', function (done) { | 
					
						
							| 
									
										
										
										
											2018-10-27 06:26:50 -04:00
										 |  |  | 			socketTopics.loadMoreSortedTopics({ uid: adminUid }, { cid: topic.categoryId, after: 0, count: 10, sort: 'recent' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				assert(Array.isArray(data.topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.loadMoreFromSet({ uid: adminUid }, { after: 'invalid' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load more from custom set', function (done) { | 
					
						
							| 
									
										
										
										
											2017-03-10 12:50:19 -05:00
										 |  |  | 			socketTopics.loadMoreFromSet({ uid: adminUid }, { set: 'uid:' + adminUid + ':topics', after: 0, count: 10 }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				assert(Array.isArray(data.topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 	describe('suggested topics', function () { | 
					
						
							|  |  |  | 		var tid1; | 
					
						
							| 
									
										
										
										
											2018-06-14 17:55:19 -04:00
										 |  |  | 		var tid3; | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2018-09-28 23:57:00 -04:00
										 |  |  | 			async.series({ | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 				topic1: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: adminUid, tags: ['nodebb'], title: 'topic title 1', content: 'topic 1 content', cid: topic.categoryId }, next); | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				topic2: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: adminUid, tags: ['nodebb'], title: 'topic title 2', content: 'topic 2 content', cid: topic.categoryId }, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-06-14 17:55:19 -04:00
										 |  |  | 				topic3: function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, tags: [], title: 'topic title 3', content: 'topic 3 content', cid: topic.categoryId }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 			}, function (err, results) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				tid1 = results.topic1.topicData.tid; | 
					
						
							| 
									
										
										
										
											2018-06-14 17:55:19 -04:00
										 |  |  | 				tid3 = results.topic3.topicData.tid; | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return suggested topics', function (done) { | 
					
						
							|  |  |  | 			topics.getSuggestedTopics(tid1, adminUid, 0, -1, function (err, topics) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-06-14 17:55:19 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should return suggested topics', function (done) { | 
					
						
							|  |  |  | 			topics.getSuggestedTopics(tid3, adminUid, 0, 2, function (err, topics) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 	describe('unread', function () { | 
					
						
							|  |  |  | 		var socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		var tid; | 
					
						
							|  |  |  | 		var mainPid; | 
					
						
							|  |  |  | 		var uid; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				topic: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: topic.userId, title: 'unread topic', content: 'unread topic content', cid: topic.categoryId }, next); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2019-09-26 21:55:49 -04:00
										 |  |  | 				joeUid: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					User.create({ username: 'regularJoe' }, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 			}, function (err, results) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				tid = results.topic.topicData.tid; | 
					
						
							|  |  |  | 				mainPid = results.topic.postData.pid; | 
					
						
							| 
									
										
										
										
											2019-09-26 21:55:49 -04:00
										 |  |  | 				uid = results.joeUid; | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markUnread({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail if topic does not exist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markUnread({ uid: adminUid }, 1231082, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-topic]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should mark topic unread', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.hasReadTopic(tid, adminUid, function (err, hasRead) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(hasRead, false); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAsRead({ uid: 0 }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should mark topic read', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAsRead({ uid: adminUid }, [tid], function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.hasReadTopic(tid, adminUid, function (err, hasRead) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(hasRead); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markTopicNotificationsRead({ uid: 0 }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should mark topic notifications read', function (done) { | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketTopics.follow({ uid: adminUid }, tid, next); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2018-04-25 13:21:39 -04:00
										 |  |  | 					topics.reply({ uid: uid, timestamp: Date.now(), content: 'some content', tid: tid }, next); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (data, next) { | 
					
						
							|  |  |  | 					setTimeout(next, 2500); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					User.notifications.getUnreadCount(adminUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (count, next) { | 
					
						
							|  |  |  | 					assert.equal(count, 1); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketTopics.markTopicNotificationsRead({ uid: adminUid }, [tid], next); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					User.notifications.getUnreadCount(adminUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (count, next) { | 
					
						
							|  |  |  | 					assert.equal(count, 0); | 
					
						
							|  |  |  | 					next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 			], function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAllRead({ uid: 0 }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-uid]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should mark all read', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				socketTopics.markAllRead({ uid: adminUid }, {}, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					topics.hasReadTopic(tid, adminUid, function (err, hasRead) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(hasRead); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-30 15:26:12 -04:00
										 |  |  | 		it('should mark category topics read', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				socketTopics.markCategoryTopicsRead({ uid: adminUid }, topic.categoryId, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					topics.hasReadTopic(tid, adminUid, function (err, hasRead) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(hasRead); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAsUnreadForAll({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-tid]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAsUnreadForAll({ uid: 0 }, [tid], function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail if user is not admin', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAsUnreadForAll({ uid: uid }, [tid], function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail if topic does not exist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAsUnreadForAll({ uid: uid }, [12312313], function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-topic]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should mark topic unread for everyone', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.markAsUnreadForAll({ uid: adminUid }, [tid], function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				async.parallel({ | 
					
						
							|  |  |  | 					adminRead: function (next) { | 
					
						
							|  |  |  | 						topics.hasReadTopic(tid, adminUid, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					regularRead: function (next) { | 
					
						
							|  |  |  | 						topics.hasReadTopic(tid, uid, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				}, function (err, results) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(results.adminRead, false); | 
					
						
							|  |  |  | 					assert.equal(results.regularRead, false); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-03-09 22:36:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not do anything if tids is empty array', function (done) { | 
					
						
							|  |  |  | 			socketTopics.markAsRead({ uid: adminUid }, [], function (err, markedRead) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(!markedRead); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-09-25 12:07:56 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not return topics in category you cant read', function (done) { | 
					
						
							|  |  |  | 			var privateCid; | 
					
						
							|  |  |  | 			var privateTid; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					categories.create({ | 
					
						
							|  |  |  | 						name: 'private category', | 
					
						
							|  |  |  | 						description: 'private category', | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					privateCid = category.cid; | 
					
						
							| 
									
										
										
										
											2019-09-26 21:55:49 -04:00
										 |  |  | 					privileges.categories.rescind(['topics:read'], category.cid, 'registered-users', next); | 
					
						
							| 
									
										
										
										
											2018-09-25 12:07:56 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, title: 'topic in private category', content: 'registered-users cant see this', cid: privateCid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (data, next) { | 
					
						
							|  |  |  | 					privateTid = data.topicData.tid; | 
					
						
							|  |  |  | 					topics.getUnreadTids({ uid: uid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (unreadTids, next) { | 
					
						
							|  |  |  | 					unreadTids = unreadTids.map(String); | 
					
						
							|  |  |  | 					assert(!unreadTids.includes(String(privateTid))); | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-10-15 11:09:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 		it('should not return topics in category you ignored/not watching', function (done) { | 
					
						
							|  |  |  | 			var ignoredCid; | 
					
						
							|  |  |  | 			var tid; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					categories.create({ | 
					
						
							|  |  |  | 						name: 'ignored category', | 
					
						
							|  |  |  | 						description: 'ignored category', | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					ignoredCid = category.cid; | 
					
						
							| 
									
										
										
										
											2019-09-26 21:55:49 -04:00
										 |  |  | 					privileges.categories.rescind(['topics:read'], category.cid, 'registered-users', next); | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, title: 'topic in private category', content: 'registered-users cant see this', cid: ignoredCid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (data, next) { | 
					
						
							|  |  |  | 					tid = data.topicData.tid; | 
					
						
							|  |  |  | 					User.ignoreCategory(uid, ignoredCid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.getUnreadTids({ uid: uid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (unreadTids, next) { | 
					
						
							|  |  |  | 					unreadTids = unreadTids.map(String); | 
					
						
							|  |  |  | 					assert(!unreadTids.includes(String(tid))); | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 11:09:57 -04:00
										 |  |  | 		it('should not return topic as unread if new post is from blocked user', function (done) { | 
					
						
							|  |  |  | 			var blockedUid; | 
					
						
							|  |  |  | 			var topic; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, title: 'will not get as unread', content: 'not unread', cid: categoryObj.cid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (result, next) { | 
					
						
							|  |  |  | 					topic = result.topicData; | 
					
						
							|  |  |  | 					User.create({ username: 'blockedunread' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (uid, next) { | 
					
						
							|  |  |  | 					blockedUid = uid; | 
					
						
							|  |  |  | 					User.blocks.add(uid, adminUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.reply({ uid: blockedUid, content: 'post from blocked user', tid: topic.tid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (result, next) { | 
					
						
							|  |  |  | 					topics.getUnreadTids({ cid: 0, uid: adminUid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (unreadTids, next) { | 
					
						
							| 
									
										
										
										
											2018-10-23 21:36:00 -04:00
										 |  |  | 					assert(!unreadTids.includes(topic.tid)); | 
					
						
							| 
									
										
										
										
											2018-10-15 11:09:57 -04:00
										 |  |  | 					User.blocks.remove(blockedUid, adminUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2019-10-09 18:03:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not return topic as unread if topic is deleted', async function () { | 
					
						
							|  |  |  | 			const uid = await User.create({ username: 'regularJoe' }); | 
					
						
							|  |  |  | 			const result = await topics.post({ uid: adminUid, title: 'deleted unread', content: 'not unread', cid: categoryObj.cid }); | 
					
						
							|  |  |  | 			await topics.delete(result.topicData.tid, adminUid); | 
					
						
							|  |  |  | 			const unreadTids = await topics.getUnreadTids({ cid: 0, uid: uid }); | 
					
						
							|  |  |  | 			assert(!unreadTids.includes(result.topicData.tid)); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 	describe('tags', function () { | 
					
						
							|  |  |  | 		var socketTopics = require('../src/socket.io/topics'); | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 		var socketAdmin = require('../src/socket.io/admin'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2018-09-26 10:56:19 -04:00
										 |  |  | 			async.series([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: adminUid, tags: ['php', 'nosql', 'psql', 'nodebb'], title: 'topic title 1', content: 'topic 1 content', cid: topic.categoryId }, next); | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-09-26 10:56:19 -04:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					topics.post({ uid: adminUid, tags: ['javascript', 'mysql', 'python', 'nodejs'], title: 'topic title 2', content: 'topic 2 content', cid: topic.categoryId }, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2018-09-26 10:56:19 -04:00
										 |  |  | 			], function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 		it('should return empty array if query is falsy', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.autocompleteTags({ uid: adminUid }, { query: '' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual([], data); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 		it('should autocomplete tags', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.autocompleteTags({ uid: adminUid }, { query: 'p' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				['php', 'psql', 'python'].forEach(function (tag) { | 
					
						
							|  |  |  | 					assert.notEqual(data.indexOf(tag), -1); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return empty array if query is falsy', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.searchTags({ uid: adminUid }, { query: '' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual([], data); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 		it('should search tags', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.searchTags({ uid: adminUid }, { query: 'no' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				['nodebb', 'nodejs', 'nosql'].forEach(function (tag) { | 
					
						
							|  |  |  | 					assert.notEqual(data.indexOf(tag), -1); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return empty array if query is falsy', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.searchAndLoadTags({ uid: adminUid }, { query: '' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.matchCount, 0); | 
					
						
							|  |  |  | 				assert.equal(data.pageCount, 1); | 
					
						
							|  |  |  | 				assert.deepEqual(data.tags, []); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should search and load tags', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.searchAndLoadTags({ uid: adminUid }, { query: 'no' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.matchCount, 3); | 
					
						
							|  |  |  | 				assert.equal(data.pageCount, 1); | 
					
						
							|  |  |  | 				var tagData = [ | 
					
						
							| 
									
										
										
										
											2018-01-10 16:33:22 -05:00
										 |  |  | 					{ value: 'nodebb', valueEscaped: 'nodebb', color: '', bgColor: '', score: 3 }, | 
					
						
							|  |  |  | 					{ value: 'nodejs', valueEscaped: 'nodejs', color: '', bgColor: '', score: 1 }, | 
					
						
							|  |  |  | 					{ value: 'nosql', valueEscaped: 'nosql', color: '', bgColor: '', score: 1 }, | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				]; | 
					
						
							|  |  |  | 				assert.deepEqual(data.tags, tagData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 		it('should return error if data is invalid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.loadMoreTags({ uid: adminUid }, { after: 'asd' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load more tags', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.loadMoreTags({ uid: adminUid }, { after: 0 }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data.tags)); | 
					
						
							|  |  |  | 				assert.equal(data.nextStart, 100); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if data is invalid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.create({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if tag is invalid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: '' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-tag]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if tag is too short', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'as' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:tag-too-short]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should create empty tag', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'emptytag' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.sortedSetScore('tags:topic:count', 'emptytag', function (err, score) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(score, 0); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should do nothing if tag exists', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'emptytag' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.sortedSetScore('tags:topic:count', 'emptytag', function (err, score) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(score, 0); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if data is invalid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.update({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-16 18:20:57 -05:00
										 |  |  | 		it('should error if data is not an array', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.update({ uid: adminUid }, { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				bgColor: '#ff0000', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				color: '#00ff00', | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 			}, function (err) { | 
					
						
							| 
									
										
										
										
											2018-01-16 18:20:57 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should update tag', function (done) { | 
					
						
							| 
									
										
										
										
											2018-01-16 18:20:57 -05:00
										 |  |  | 			socketAdmin.tags.update({ uid: adminUid }, [{ | 
					
						
							|  |  |  | 				value: 'emptytag', | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				bgColor: '#ff0000', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				color: '#00ff00', | 
					
						
							| 
									
										
										
										
											2018-01-16 18:20:57 -05:00
										 |  |  | 			}], function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.getObject('tag:emptytag', function (err, data) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(data.bgColor, '#ff0000'); | 
					
						
							|  |  |  | 					assert.equal(data.color, '#00ff00'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-16 18:20:57 -05:00
										 |  |  | 		it('should rename tags', function (done) { | 
					
						
							| 
									
										
										
										
											2018-10-04 14:07:24 -04:00
										 |  |  | 			async.series({ | 
					
						
							| 
									
										
										
										
											2018-01-16 18:20:57 -05:00
										 |  |  | 				topic1: function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, tags: ['plugins'], title: 'topic tagged with plugins', content: 'topic 1 content', cid: topic.categoryId }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				topic2: function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, tags: ['plugin'], title: 'topic tagged with plugin', content: 'topic 2 content', cid: topic.categoryId }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, function (err, result) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				socketAdmin.tags.rename({ uid: adminUid }, [{ | 
					
						
							|  |  |  | 					value: 'plugin', | 
					
						
							|  |  |  | 					newName: 'plugins', | 
					
						
							|  |  |  | 				}], function (err) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					topics.getTagTids('plugins', 0, -1, function (err, tids) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert.equal(tids.length, 2); | 
					
						
							|  |  |  | 						topics.getTopicTags(result.topic2.topicData.tid, function (err, tags) { | 
					
						
							|  |  |  | 							assert.ifError(err); | 
					
						
							|  |  |  | 							assert.equal(tags.length, 1); | 
					
						
							|  |  |  | 							assert.equal(tags[0], 'plugins'); | 
					
						
							|  |  |  | 							done(); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 		it('should return related topics', function (done) { | 
					
						
							|  |  |  | 			var meta = require('../src/meta'); | 
					
						
							|  |  |  | 			meta.config.maximumRelatedTopics = 2; | 
					
						
							|  |  |  | 			var topicData = { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				tags: [{ value: 'javascript' }], | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 			topics.getRelatedTopics(topicData, 0, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				assert.equal(data[0].title, 'topic title 2'); | 
					
						
							|  |  |  | 				meta.config.maximumRelatedTopics = 0; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 		it('should return error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.deleteTags({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should do nothing if arrays is empty', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.deleteTags({ uid: adminUid }, { tags: [] }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should delete tags', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'emptytag2' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				socketAdmin.tags.deleteTags({ uid: adminUid }, { tags: ['emptytag', 'emptytag2', 'nodebb', 'nodejs'] }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					db.getObjects(['tag:emptytag', 'tag:emptytag2'], function (err, data) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(!data[0]); | 
					
						
							|  |  |  | 						assert(!data[1]); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should delete tag', function (done) { | 
					
						
							|  |  |  | 			topics.deleteTag('javascript', function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.getObject('tag:javascript', function (err, data) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!data); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 	describe('follow/unfollow', function () { | 
					
						
							|  |  |  | 		var socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		var tid; | 
					
						
							|  |  |  | 		var followerUid; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			User.create({ username: 'follower' }, function (err, uid) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				followerUid = uid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				topics.post({ uid: adminUid, title: 'topic title', content: 'some content', cid: topic.categoryId }, function (err, result) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return done(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					tid = result.topicData.tid; | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 		it('should error if not logged in', function (done) { | 
					
						
							|  |  |  | 			socketTopics.changeWatching({ uid: 0 }, { tid: tid, type: 'ignore' }, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:not-logged-in]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 		it('should filter ignoring uids', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'ignore' }, function (err) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.filterIgnoringUids(tid, [adminUid, followerUid], function (err, uids) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(uids.length, 1); | 
					
						
							|  |  |  | 					assert.equal(uids[0], adminUid); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.changeWatching({ uid: followerUid }, {}, function (err) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-09 13:22:03 +03:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 		it('should error with invalid type', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'derp' }, function (err) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-command]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-02-09 13:22:03 +03:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should follow topic', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:22:03 +03:00
										 |  |  | 			topics.toggleFollow(tid, followerUid, function (err, isFollowing) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(isFollowing); | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				socketTopics.isFollowed({ uid: followerUid }, tid, function (err, isFollowing) { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(isFollowing); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 	describe('topics search', function () { | 
					
						
							|  |  |  | 		it('should error with invalid data', function (done) { | 
					
						
							|  |  |  | 			socketTopics.search({ uid: adminUid }, null, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return results', function (done) { | 
					
						
							|  |  |  | 			var plugins = require('../src/plugins'); | 
					
						
							|  |  |  | 			plugins.registerHook('myTestPlugin', { | 
					
						
							|  |  |  | 				hook: 'filter:topic.search', | 
					
						
							|  |  |  | 				method: function (data, callback) { | 
					
						
							|  |  |  | 					callback(null, [1, 2, 3]); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			socketTopics.search({ uid: adminUid }, { tid: topic.tid, term: 'test' }, function (err, results) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual(results, [1, 2, 3]); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should check if user is moderator', function (done) { | 
					
						
							|  |  |  | 		socketTopics.isModerator({ uid: adminUid }, topic.tid, function (err, isModerator) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(!isModerator); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 	describe('teasers', function () { | 
					
						
							|  |  |  | 		var topic1; | 
					
						
							|  |  |  | 		var topic2; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			async.series([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, title: 'topic 1', content: 'content 1', cid: categoryObj.cid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.post({ uid: adminUid, title: 'topic 2', content: 'content 2', cid: categoryObj.cid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], function (err, results) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topic1 = results[0]; | 
					
						
							|  |  |  | 				topic2 = results[1]; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		after(function (done) { | 
					
						
							|  |  |  | 			meta.config.teaserPost = ''; | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return empty array if first param is empty', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			topics.getTeasers([], 1, function (err, teasers) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(0, teasers.length); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get teasers with 2 params', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			topics.getTeasers([topic1.topicData, topic2.topicData], 1, function (err, teasers) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual([undefined, undefined], teasers); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get teasers with first posts', function (done) { | 
					
						
							|  |  |  | 			meta.config.teaserPost = 'first'; | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			topics.getTeasers([topic1.topicData, topic2.topicData], 1, function (err, teasers) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(2, teasers.length); | 
					
						
							|  |  |  | 				assert(teasers[0]); | 
					
						
							|  |  |  | 				assert(teasers[1]); | 
					
						
							|  |  |  | 				assert(teasers[0].tid, topic1.topicData.tid); | 
					
						
							|  |  |  | 				assert(teasers[0].content, 'content 1'); | 
					
						
							|  |  |  | 				assert(teasers[0].user.username, 'admin'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get teasers even if one topic is falsy', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			topics.getTeasers([null, topic2.topicData], 1, function (err, teasers) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(2, teasers.length); | 
					
						
							|  |  |  | 				assert.equal(undefined, teasers[0]); | 
					
						
							|  |  |  | 				assert(teasers[1]); | 
					
						
							|  |  |  | 				assert(teasers[1].tid, topic2.topicData.tid); | 
					
						
							|  |  |  | 				assert(teasers[1].content, 'content 2'); | 
					
						
							|  |  |  | 				assert(teasers[1].user.username, 'admin'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 11:09:57 -04:00
										 |  |  | 		it('should get teasers with last posts', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 			meta.config.teaserPost = 'last-post'; | 
					
						
							|  |  |  | 			topics.reply({ uid: adminUid, content: 'reply 1 content', tid: topic1.topicData.tid }, function (err, result) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topic1.topicData.teaserPid = result.pid; | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 				topics.getTeasers([topic1.topicData, topic2.topicData], 1, function (err, teasers) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(teasers[0]); | 
					
						
							|  |  |  | 					assert(teasers[1]); | 
					
						
							|  |  |  | 					assert(teasers[0].tid, topic1.topicData.tid); | 
					
						
							|  |  |  | 					assert(teasers[0].content, 'reply 1 content'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get teasers by tids', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			topics.getTeasersByTids([topic2.topicData.tid, topic1.topicData.tid], 1, function (err, teasers) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(2, teasers.length); | 
					
						
							|  |  |  | 				assert.equal(teasers[1].content, 'reply 1 content'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return empty array ', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			topics.getTeasersByTids([], 1, function (err, teasers) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(0, teasers.length); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get teaser by tid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-19 19:35:10 -04:00
										 |  |  | 			topics.getTeaser(topic2.topicData.tid, 1, function (err, teaser) { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(teaser); | 
					
						
							|  |  |  | 				assert.equal(teaser.content, 'content 2'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-10-15 11:09:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not return teaser if user is blocked', function (done) { | 
					
						
							|  |  |  | 			var blockedUid; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					User.create({ username: 'blocked' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (uid, next) { | 
					
						
							|  |  |  | 					blockedUid = uid; | 
					
						
							|  |  |  | 					User.blocks.add(uid, adminUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					topics.reply({ uid: blockedUid, content: 'post from blocked user', tid: topic2.topicData.tid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (result, next) { | 
					
						
							|  |  |  | 					topics.getTeaser(topic2.topicData.tid, adminUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (teaser, next) { | 
					
						
							|  |  |  | 					assert.equal(teaser.content, 'content 2'); | 
					
						
							|  |  |  | 					User.blocks.remove(blockedUid, adminUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe('tag privilege', function () { | 
					
						
							|  |  |  | 		var uid; | 
					
						
							|  |  |  | 		var cid; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					User.create({ username: 'tag_poster' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (_uid, next) { | 
					
						
							|  |  |  | 					uid = _uid; | 
					
						
							|  |  |  | 					categories.create({ name: 'tag category' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (categoryObj, next) { | 
					
						
							|  |  |  | 					cid = categoryObj.cid; | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail to post if user does not have tag privilege', function (done) { | 
					
						
							|  |  |  | 			privileges.categories.rescind(['topics:tag'], cid, 'registered-users', function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.post({ uid: uid, cid: cid, tags: ['tag1'], title: 'topic with tags', content: 'some content here' }, function (err) { | 
					
						
							|  |  |  | 					assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should fail to edit if user does not have tag privilege', function (done) { | 
					
						
							|  |  |  | 			topics.post({ uid: uid, cid: cid, title: 'topic with tags', content: 'some content here' }, function (err, result) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				var pid = result.postData.pid; | 
					
						
							|  |  |  | 				posts.edit({ pid: pid, uid: uid, content: 'edited content', tags: ['tag2'] }, function (err) { | 
					
						
							|  |  |  | 					assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should be able to edit topic and add tags if allowed', function (done) { | 
					
						
							|  |  |  | 			privileges.categories.give(['topics:tag'], cid, 'registered-users', function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topics.post({ uid: uid, cid: cid, tags: ['tag1'], title: 'topic with tags', content: 'some content here' }, function (err, result) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					posts.edit({ pid: result.postData.pid, uid: uid, content: 'edited content', tags: ['tag1', 'tag2'] }, function (err, result) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-06-16 15:02:06 -04:00
										 |  |  | 						var tags = result.topic.tags.map(function (tag) { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 							return tag.value; | 
					
						
							| 
									
										
										
										
											2017-06-16 15:02:06 -04:00
										 |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2018-10-20 14:40:48 -04:00
										 |  |  | 						assert(tags.includes('tag1')); | 
					
						
							|  |  |  | 						assert(tags.includes('tag2')); | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-04 14:56:14 -05:00
										 |  |  | 	describe('topic merge', function () { | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 		var uid; | 
					
						
							|  |  |  | 		var topic1Data; | 
					
						
							|  |  |  | 		var topic2Data; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					User.create({ username: 'mergevictim' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (_uid, next) { | 
					
						
							|  |  |  | 					uid = _uid; | 
					
						
							|  |  |  | 					topics.post({ uid: uid, cid: categoryObj.cid, title: 'topic 1', content: 'topic 1 OP' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (result, next) { | 
					
						
							|  |  |  | 					topic1Data = result.topicData; | 
					
						
							|  |  |  | 					topics.post({ uid: uid, cid: categoryObj.cid, title: 'topic 2', content: 'topic 2 OP' }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (result, next) { | 
					
						
							|  |  |  | 					topic2Data = result.topicData; | 
					
						
							|  |  |  | 					topics.reply({ uid: uid, content: 'topic 1 reply', tid: topic1Data.tid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (postData, next) { | 
					
						
							|  |  |  | 					topics.reply({ uid: uid, content: 'topic 2 reply', tid: topic2Data.tid }, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if data is not an array', function (done) { | 
					
						
							|  |  |  | 			socketTopics.merge({ uid: 0 }, null, function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if user does not have privileges', function (done) { | 
					
						
							|  |  |  | 			socketTopics.merge({ uid: 0 }, [topic2Data.tid, topic1Data.tid], function (err) { | 
					
						
							|  |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 		it('should merge 2 topics', async function () { | 
					
						
							|  |  |  | 			await socketTopics.merge({ uid: adminUid }, [topic2Data.tid, topic1Data.tid]); | 
					
						
							|  |  |  | 			async function getTopic(tid) { | 
					
						
							|  |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							|  |  |  | 				return await topics.getTopicWithPosts(topicData, 'tid:' + topicData.tid + ':posts', adminUid, 0, 19, false); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			const [topic1, topic2] = await Promise.all([ | 
					
						
							|  |  |  | 				getTopic(topic1Data.tid), | 
					
						
							|  |  |  | 				getTopic(topic2Data.tid), | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.equal(topic1.posts.length, 4); | 
					
						
							|  |  |  | 			assert.equal(topic2.posts.length, 0); | 
					
						
							|  |  |  | 			assert.equal(topic2.deleted, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.equal(topic1.posts[0].content, 'topic 1 OP'); | 
					
						
							|  |  |  | 			assert.equal(topic1.posts[1].content, 'topic 2 OP'); | 
					
						
							|  |  |  | 			assert.equal(topic1.posts[2].content, 'topic 1 reply'); | 
					
						
							|  |  |  | 			assert.equal(topic1.posts[3].content, 'topic 2 reply'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return properly for merged topic', function (done) { | 
					
						
							|  |  |  | 			request(nconf.get('url') + '/api/topic/' + topic2Data.slug, { jar: adminJar, json: true }, function (err, response, body) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				assert.deepStrictEqual(body.posts, []); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-06-15 15:23:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 	describe('sorted topics', function () { | 
					
						
							|  |  |  | 		it('should get sorted topics in category', function (done) { | 
					
						
							| 
									
										
										
										
											2018-06-15 15:23:27 -04:00
										 |  |  | 			var filters = ['', 'watched', 'unreplied', 'new']; | 
					
						
							|  |  |  | 			async.map(filters, function (filter, next) { | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 				topics.getSortedTopics({ | 
					
						
							|  |  |  | 					cids: [topic.categoryId], | 
					
						
							|  |  |  | 					uid: topic.userId, | 
					
						
							|  |  |  | 					start: 0, | 
					
						
							|  |  |  | 					stop: -1, | 
					
						
							|  |  |  | 					filter: filter, | 
					
						
							|  |  |  | 					sort: 'votes', | 
					
						
							|  |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2018-06-15 15:23:27 -04:00
										 |  |  | 			}, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				data.forEach(function (filterTopics) { | 
					
						
							|  |  |  | 					assert(Array.isArray(filterTopics.topics)); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }); |