| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2021-03-31 00:51:23 +03:00
										 |  |  | const mockdate = require('mockdate'); | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | const nconf = require('nconf'); | 
					
						
							|  |  |  | const request = require('request'); | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | const util = require('util'); | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | const requestType = util.promisify((type, url, opts, cb) => { | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 	request[type](url, opts, (err, res, body) => cb(err, { res: res, body: body })); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | describe('Topic\'s', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	let topic; | 
					
						
							|  |  |  | 	let categoryObj; | 
					
						
							|  |  |  | 	let adminUid; | 
					
						
							|  |  |  | 	let adminJar; | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 	let csrf_token; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 	let fooUid; | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	before(async () => { | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 		adminUid = await User.create({ username: 'admin', password: '123456' }); | 
					
						
							| 
									
										
										
										
											2020-01-28 13:03:58 -05:00
										 |  |  | 		fooUid = await User.create({ username: 'foo' }); | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 		await groups.join('administrators', adminUid); | 
					
						
							|  |  |  | 		adminJar = await helpers.loginUser('admin', '123456'); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 		csrf_token = (await requestType('get', `${nconf.get('url')}/api/config`, { json: true, jar: adminJar })).body.csrf_token; | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('.post', () => { | 
					
						
							|  |  |  | 		it('should fail to create topic with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.post({ uid: 0 }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should create a new topic with proper parameters', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 02:07:29 -07:00
										 |  |  | 			topics.post({ | 
					
						
							|  |  |  | 				uid: topic.userId, | 
					
						
							|  |  |  | 				title: topic.title, | 
					
						
							|  |  |  | 				content: topic.content, | 
					
						
							|  |  |  | 				cid: topic.categoryId, | 
					
						
							|  |  |  | 			}, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get post count', (done) => { | 
					
						
							|  |  |  | 			socketTopics.postcount({ uid: adminUid }, topic.tid, (err, count) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(count, 1); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.getTopic({ uid: adminUid }, topic.tid, (err, data) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.tid, topic.tid); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new topic with invalid user id', (done) => { | 
					
						
							|  |  |  | 			topics.post({ uid: null, title: topic.title, content: topic.content, cid: topic.categoryId }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new topic with empty title', (done) => { | 
					
						
							|  |  |  | 			topics.post({ uid: topic.userId, title: '', content: topic.content, cid: topic.categoryId }, (err) => { | 
					
						
							| 
									
										
										
										
											2014-11-12 01:19:40 -05:00
										 |  |  | 				assert.ok(err); | 
					
						
							| 
									
										
										
										
											2014-11-11 23:12:37 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new topic with empty content', (done) => { | 
					
						
							|  |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: '', cid: topic.categoryId }, (err) => { | 
					
						
							| 
									
										
										
										
											2014-11-12 01:19:40 -05:00
										 |  |  | 				assert.ok(err); | 
					
						
							| 
									
										
										
										
											2014-11-11 23:12:37 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new topic with non-existant category id', (done) => { | 
					
						
							|  |  |  | 			topics.post({ uid: topic.userId, title: topic.title, content: topic.content, cid: 99 }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return false for falsy uid', (done) => { | 
					
						
							|  |  |  | 			topics.isOwner(topic.tid, 0, (err, isOwner) => { | 
					
						
							| 
									
										
										
										
											2017-05-26 16:56:26 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(!isOwner); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to post a topic as guest if no privileges', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 			const categoryObj = await categories.create({ | 
					
						
							|  |  |  | 				name: 'Test Category', | 
					
						
							|  |  |  | 				description: 'Test category created by testing script', | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			const result = await requestType('post', `${nconf.get('url')}/api/v3/topics`, { | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 				form: { | 
					
						
							|  |  |  | 					title: 'just a title', | 
					
						
							|  |  |  | 					cid: categoryObj.cid, | 
					
						
							|  |  |  | 					content: 'content for the main post', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2021-07-09 11:53:30 -04:00
										 |  |  | 			assert.strictEqual(result.body.status.message, 'You do not have enough privileges for this action.'); | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should post a topic as guest if guest group has privileges', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 			const categoryObj = await categories.create({ | 
					
						
							|  |  |  | 				name: 'Test Category', | 
					
						
							|  |  |  | 				description: 'Test category created by testing script', | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			await privileges.categories.give(['groups:topics:create'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 			await privileges.categories.give(['groups:topics:reply'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			const result = await requestType('post', `${nconf.get('url')}/api/v3/topics`, { | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 				form: { | 
					
						
							|  |  |  | 					title: 'just a title', | 
					
						
							|  |  |  | 					cid: categoryObj.cid, | 
					
						
							|  |  |  | 					content: 'content for the main post', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.strictEqual(result.body.status.code, 'ok'); | 
					
						
							|  |  |  | 			assert.strictEqual(result.body.response.title, 'just a title'); | 
					
						
							|  |  |  | 			assert.strictEqual(result.body.response.user.username, '[[global:guest]]'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			const replyResult = await requestType('post', `${nconf.get('url')}/api/v3/topics/${result.body.response.tid}`, { | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 				form: { | 
					
						
							|  |  |  | 					content: 'a reply by guest', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			assert.strictEqual(replyResult.body.response.content, 'a reply by guest'); | 
					
						
							| 
									
										
										
										
											2021-03-15 14:03:35 -04:00
										 |  |  | 			assert.strictEqual(replyResult.body.response.user.username, '[[global:guest]]'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should post a topic/reply as guest with handle if guest group has privileges', async () => { | 
					
						
							|  |  |  | 			const categoryObj = await categories.create({ | 
					
						
							|  |  |  | 				name: 'Test Category', | 
					
						
							|  |  |  | 				description: 'Test category created by testing script', | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			await privileges.categories.give(['groups:topics:create'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 			await privileges.categories.give(['groups:topics:reply'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 			const oldValue = meta.config.allowGuestHandles; | 
					
						
							|  |  |  | 			meta.config.allowGuestHandles = 1; | 
					
						
							|  |  |  | 			const result = await requestType('post', `${nconf.get('url')}/api/v3/topics`, { | 
					
						
							|  |  |  | 				form: { | 
					
						
							|  |  |  | 					title: 'just a title', | 
					
						
							|  |  |  | 					cid: categoryObj.cid, | 
					
						
							|  |  |  | 					content: 'content for the main post', | 
					
						
							|  |  |  | 					handle: 'guest123', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.strictEqual(result.body.status.code, 'ok'); | 
					
						
							|  |  |  | 			assert.strictEqual(result.body.response.title, 'just a title'); | 
					
						
							|  |  |  | 			assert.strictEqual(result.body.response.user.username, 'guest123'); | 
					
						
							|  |  |  | 			assert.strictEqual(result.body.response.user.displayname, 'guest123'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const replyResult = await requestType('post', `${nconf.get('url')}/api/v3/topics/${result.body.response.tid}`, { | 
					
						
							|  |  |  | 				form: { | 
					
						
							|  |  |  | 					content: 'a reply by guest', | 
					
						
							|  |  |  | 					handle: 'guest124', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			assert.strictEqual(replyResult.body.response.content, 'a reply by guest'); | 
					
						
							|  |  |  | 			assert.strictEqual(replyResult.body.response.user.username, 'guest124'); | 
					
						
							|  |  |  | 			assert.strictEqual(replyResult.body.response.user.displayname, 'guest124'); | 
					
						
							|  |  |  | 			meta.config.allowGuestHandles = oldValue; | 
					
						
							| 
									
										
										
										
											2020-11-29 15:43:40 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('.reply', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let newTopic; | 
					
						
							|  |  |  | 		let newPost; | 
					
						
							| 
									
										
										
										
											2014-11-12 14:34:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 02:07:29 -07:00
										 |  |  | 			topics.post({ | 
					
						
							|  |  |  | 				uid: topic.userId, | 
					
						
							|  |  |  | 				title: topic.title, | 
					
						
							|  |  |  | 				content: topic.content, | 
					
						
							|  |  |  | 				cid: topic.categoryId, | 
					
						
							|  |  |  | 			}, (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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should create a new reply with proper parameters', (done) => { | 
					
						
							|  |  |  | 			topics.reply({ uid: topic.userId, content: 'test post', tid: newTopic.tid }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should handle direct replies', (done) => { | 
					
						
							|  |  |  | 			topics.reply({ uid: topic.userId, content: 'test reply', tid: newTopic.tid, toPid: newPost.pid }, (err, result) => { | 
					
						
							| 
									
										
										
										
											2016-10-28 09:18:09 -05:00
										 |  |  | 				assert.equal(err, null, 'was created with error'); | 
					
						
							|  |  |  | 				assert.ok(result); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				socketPosts.getReplies({ uid: 0 }, newPost.pid, (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(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if pid is not a number', (done) => { | 
					
						
							|  |  |  | 			socketPosts.getReplies({ uid: 0 }, 'abc', (err) => { | 
					
						
							| 
									
										
										
										
											2017-02-23 17:02:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new reply with invalid user id', (done) => { | 
					
						
							|  |  |  | 			topics.reply({ uid: null, content: 'test post', tid: newTopic.tid }, (err) => { | 
					
						
							| 
									
										
										
										
											2014-11-12 15:06:55 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new reply with empty content', (done) => { | 
					
						
							|  |  |  | 			topics.reply({ uid: topic.userId, content: '', tid: newTopic.tid }, (err) => { | 
					
						
							| 
									
										
										
										
											2014-11-12 15:06:55 -05:00
										 |  |  | 				assert.ok(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new reply with invalid topic id', (done) => { | 
					
						
							|  |  |  | 			topics.reply({ uid: null, content: 'test post', tid: 99 }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to create new reply with invalid toPid', (done) => { | 
					
						
							|  |  |  | 			topics.reply({ uid: topic.userId, content: 'test post', tid: newTopic.tid, toPid: '"onmouseover=alert(1);//' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-17 16:00:39 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-pid]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-11-26 12:04:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should delete nested relies properly', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-26 12:04:01 -05:00
										 |  |  | 			const result = await topics.post({ uid: fooUid, title: 'nested test', content: 'main post', cid: topic.categoryId }); | 
					
						
							|  |  |  | 			const reply1 = await topics.reply({ uid: fooUid, content: 'reply post 1', tid: result.topicData.tid }); | 
					
						
							|  |  |  | 			const reply2 = await topics.reply({ uid: fooUid, content: 'reply post 2', tid: result.topicData.tid, toPid: reply1.pid }); | 
					
						
							|  |  |  | 			let replies = await socketPosts.getReplies({ uid: fooUid }, reply1.pid); | 
					
						
							|  |  |  | 			assert.strictEqual(replies.length, 1); | 
					
						
							|  |  |  | 			assert.strictEqual(replies[0].content, 'reply post 2'); | 
					
						
							|  |  |  | 			let toPid = await posts.getPostField(reply2.pid, 'toPid'); | 
					
						
							|  |  |  | 			assert.strictEqual(parseInt(toPid, 10), parseInt(reply1.pid, 10)); | 
					
						
							|  |  |  | 			await posts.purge(reply1.pid, fooUid); | 
					
						
							|  |  |  | 			replies = await socketPosts.getReplies({ uid: fooUid }, reply1.pid); | 
					
						
							|  |  |  | 			assert.strictEqual(replies.length, 0); | 
					
						
							|  |  |  | 			toPid = await posts.getPostField(reply2.pid, 'toPid'); | 
					
						
							|  |  |  | 			assert.strictEqual(toPid, null); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-12 14:34:02 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('Get methods', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let	newTopic; | 
					
						
							|  |  |  | 		let newPost; | 
					
						
							| 
									
										
										
										
											2013-11-17 23:50:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 02:07:29 -07:00
										 |  |  | 			topics.post({ | 
					
						
							|  |  |  | 				uid: topic.userId, | 
					
						
							|  |  |  | 				title: topic.title, | 
					
						
							|  |  |  | 				content: topic.content, | 
					
						
							|  |  |  | 				cid: topic.categoryId, | 
					
						
							|  |  |  | 			}, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not receive errors', (done) => { | 
					
						
							|  |  |  | 			topics.getTopicData(newTopic.tid, (err, topicData) => { | 
					
						
							| 
									
										
										
										
											2018-10-21 19:33:46 -04:00
										 |  |  | 				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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get a single field', (done) => { | 
					
						
							|  |  |  | 			topics.getTopicFields(newTopic.tid, ['slug'], (err, data) => { | 
					
						
							| 
									
										
										
										
											2018-10-25 19:58:01 -04:00
										 |  |  | 				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
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get topic title by pid', (done) => { | 
					
						
							|  |  |  | 			topics.getTitleByPid(newPost.pid, (err, title) => { | 
					
						
							| 
									
										
										
										
											2017-02-23 23:05:51 +03:00
										 |  |  | 				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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get topic data by pid', (done) => { | 
					
						
							|  |  |  | 			topics.getTopicDataByPid(newPost.pid, (err, data) => { | 
					
						
							| 
									
										
										
										
											2017-02-23 23:05:51 +03:00
										 |  |  | 				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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		describe('.getTopicWithPosts', () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 			let tid; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			before(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const result = await topics.post({ uid: topic.userId, title: 'page test', content: 'main post', cid: topic.categoryId }); | 
					
						
							|  |  |  | 				tid = result.topicData.tid; | 
					
						
							|  |  |  | 				for (let i = 0; i < 30; i++) { | 
					
						
							|  |  |  | 					// eslint-disable-next-line no-await-in-loop
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 					await topics.reply({ uid: adminUid, content: `topic reply ${i + 1}`, tid: tid }); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should get a topic with posts and other data', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, 0, -1, false); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert(data); | 
					
						
							|  |  |  | 				assert.equal(data.category.cid, topic.categoryId); | 
					
						
							|  |  |  | 				assert.equal(data.unreplied, false); | 
					
						
							|  |  |  | 				assert.equal(data.deleted, false); | 
					
						
							|  |  |  | 				assert.equal(data.locked, false); | 
					
						
							|  |  |  | 				assert.equal(data.pinned, false); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should return first 3 posts including main post', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, 0, 2, false); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 3); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'main post'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 1'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[2].content, 'topic reply 2'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should return 3 posts from 1 to 3 excluding main post', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							|  |  |  | 				const start = 1; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, start, 3, false); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 3); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'topic reply 1'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 2'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[2].content, 'topic reply 3'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index + start); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should return main post and last 2 posts', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, 0, 2, true); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 3); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'main post'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 30'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[2].content, 'topic reply 29'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should return last 3 posts and not main post', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							|  |  |  | 				const start = 1; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, start, 3, true); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 3); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'topic reply 30'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 29'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[2].content, 'topic reply 28'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index + start); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should return posts 29 to 27 posts and not main post', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							|  |  |  | 				const start = 2; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, start, 4, true); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 3); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'topic reply 29'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 28'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[2].content, 'topic reply 27'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index + start); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should return 3 posts in reverse', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							|  |  |  | 				const start = 28; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, start, 30, true); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 3); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'topic reply 3'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 2'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[2].content, 'topic reply 1'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index + start); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should get all posts with main post at the start', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, 0, -1, false); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 31); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'main post'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 1'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[data.posts.length - 1].content, 'topic reply 30'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			it('should get all posts in reverse with main post at the start followed by reply 30', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				const topicData = await topics.getTopicData(tid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 				const data = await topics.getTopicWithPosts(topicData, `tid:${tid}:posts`, topic.userId, 0, -1, true); | 
					
						
							| 
									
										
										
										
											2020-11-18 14:25:39 -05:00
										 |  |  | 				assert.strictEqual(data.posts.length, 31); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[0].content, 'main post'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[1].content, 'topic reply 30'); | 
					
						
							|  |  |  | 				assert.strictEqual(data.posts[data.posts.length - 1].content, 'topic reply 1'); | 
					
						
							|  |  |  | 				data.posts.forEach((post, index) => { | 
					
						
							|  |  |  | 					assert.strictEqual(post.index, index); | 
					
						
							| 
									
										
										
										
											2016-08-13 01:20:19 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-15 16:16:50 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('Title escaping', () => { | 
					
						
							|  |  |  | 		it('should properly escape topic title', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const title = '"<script>alert(\'ok1\');</script> new topic test'; | 
					
						
							|  |  |  | 			const titleEscaped = validator.escape(title); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			topics.post({ uid: topic.userId, title: title, content: topic.content, cid: topic.categoryId }, (err, result) => { | 
					
						
							| 
									
										
										
										
											2016-02-18 18:32:08 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.getTopicData(result.topicData.tid, (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
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('tools/delete/restore/purge', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let newTopic; | 
					
						
							|  |  |  | 		let followerUid; | 
					
						
							|  |  |  | 		let moveCid; | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2016-08-17 12:12:07 +03:00
										 |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2021-02-04 02:07:29 -07:00
										 |  |  | 					topics.post({ | 
					
						
							|  |  |  | 						uid: topic.userId, | 
					
						
							|  |  |  | 						title: topic.title, | 
					
						
							|  |  |  | 						content: topic.content, | 
					
						
							|  |  |  | 						cid: topic.categoryId, | 
					
						
							|  |  |  | 					}, (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', | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					}, (err, category) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 						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
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load topic tools', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadTopicTools({ uid: adminUid }, { tid: newTopic.tid }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should delete the topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.delete({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should restore the topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.restore({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should lock topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.lock({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.isLocked(newTopic.tid, (err, isLocked) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(isLocked); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should unlock topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.unlock({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.isLocked(newTopic.tid, (err, isLocked) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!isLocked); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should pin topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.pin({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.getTopicField(newTopic.tid, 'pinned', (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(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should unpin topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.unpin({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.getTopicField(newTopic.tid, 'pinned', (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(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should move all topics', (done) => { | 
					
						
							|  |  |  | 			socketTopics.moveAll({ uid: adminUid }, { cid: moveCid, currentCid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.getTopicField(newTopic.tid, 'cid', (err, cid) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(cid, moveCid); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should move a topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.move({ uid: adminUid }, { cid: categoryObj.cid, tids: [newTopic.tid] }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.getTopicField(newTopic.tid, 'cid', (err, cid) => { | 
					
						
							| 
									
										
										
										
											2016-11-01 14:05:53 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(cid, categoryObj.cid); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should properly update sets when post is moved', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			let movedPost; | 
					
						
							|  |  |  | 			let previousPost; | 
					
						
							|  |  |  | 			let topic2LastReply; | 
					
						
							|  |  |  | 			let tid1; | 
					
						
							|  |  |  | 			let tid2; | 
					
						
							|  |  |  | 			const cid1 = topic.categoryId; | 
					
						
							|  |  |  | 			let cid2; | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 			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([ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 									`cid:${cid1}:tids`, | 
					
						
							|  |  |  | 									`cid:${cid1}:tids:lastposttime`, | 
					
						
							|  |  |  | 									`cid:${cid1}:tids:posts`, | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 								], tid1, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							scores2: function (next) { | 
					
						
							|  |  |  | 								db.sortedSetsScore([ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 									`cid:${cid2}:tids`, | 
					
						
							|  |  |  | 									`cid:${cid2}:tids:lastposttime`, | 
					
						
							|  |  |  | 									`cid:${cid2}:tids:posts`, | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 								], tid2, next); | 
					
						
							|  |  |  | 							}, | 
					
						
							| 
									
										
										
										
											2018-10-12 12:58:11 -04:00
										 |  |  | 							posts1: function (next) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 								db.getSortedSetRangeWithScores(`tid:${tid1}:posts`, 0, -1, next); | 
					
						
							| 
									
										
										
										
											2018-10-12 12:58:11 -04:00
										 |  |  | 							}, | 
					
						
							|  |  |  | 							posts2: function (next) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 								db.getSortedSetRangeWithScores(`tid:${tid2}:posts`, 0, -1, next); | 
					
						
							| 
									
										
										
										
											2018-10-12 12:58:11 -04:00
										 |  |  | 							}, | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 						}, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function (results, next) { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 						const assertMsg = `${JSON.stringify(results.posts1)}\n${JSON.stringify(results.posts2)}`; | 
					
						
							| 
									
										
										
										
											2018-10-12 12:58:11 -04:00
										 |  |  | 						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) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 					db.isMemberOfSortedSets([`cid:${cid1}:pids`, `cid:${cid2}:pids`], movedPost.pid, next); | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				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) { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 					db.isMemberOfSortedSets([`cid:${cid1}:pids`, `cid:${cid2}:pids`], movedPost.pid, next); | 
					
						
							| 
									
										
										
										
											2018-10-11 16:00:22 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to purge topic if user does not have privilege', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			let globalModUid; | 
					
						
							|  |  |  | 			let tid; | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			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) { | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 					privileges.categories.rescind(['groups:purge'], categoryObj.cid, 'Global Moderators', next); | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					socketTopics.purge({ uid: globalModUid }, { tids: [tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 						assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 						privileges.categories.give(['groups:purge'], categoryObj.cid, 'Global Moderators', next); | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should purge the topic', (done) => { | 
					
						
							|  |  |  | 			socketTopics.purge({ uid: adminUid }, { tids: [newTopic.tid], cid: categoryObj.cid }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.isSortedSetMember(`uid:${followerUid}:followed_tids`, newTopic.tid, (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
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-01-28 13:03:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not allow user to restore their topic if it was deleted by an admin', async () => { | 
					
						
							| 
									
										
										
										
											2020-01-28 13:03:58 -05:00
										 |  |  | 			const result = await topics.post({ | 
					
						
							|  |  |  | 				uid: fooUid, | 
					
						
							|  |  |  | 				title: 'topic for restore test', | 
					
						
							|  |  |  | 				content: 'topic content', | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			await socketTopics.delete({ uid: adminUid }, { tids: [result.topicData.tid], cid: categoryObj.cid }); | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				await socketTopics.restore({ uid: fooUid }, { tids: [result.topicData.tid], cid: categoryObj.cid }); | 
					
						
							|  |  |  | 			} catch (err) { | 
					
						
							|  |  |  | 				assert.strictEqual(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-02-25 18:04:32 +02:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('order pinned topics', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let tid1; | 
					
						
							|  |  |  | 		let tid2; | 
					
						
							|  |  |  | 		let tid3; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 			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
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, results) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				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
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 						setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 							topics.tools.pin(tid2, adminUid, next); | 
					
						
							|  |  |  | 						}, 5); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				], done); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const socketTopics = require('../src/socket.io/topics'); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.orderPinnedTopics({ uid: adminUid }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.orderPinnedTopics({ uid: adminUid }, [null, null], (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with unprivileged user', (done) => { | 
					
						
							|  |  |  | 			socketTopics.orderPinnedTopics({ uid: 0 }, [{ tid: tid1 }, { tid: tid2 }], (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not do anything if topics are not pinned', (done) => { | 
					
						
							|  |  |  | 			socketTopics.orderPinnedTopics({ uid: adminUid }, [{ tid: tid3 }], (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.isSortedSetMember(`cid:${topic.categoryId}:tids:pinned`, tid3, (err, isMember) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!isMember); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should order pinned topics', (done) => { | 
					
						
							|  |  |  | 			db.getSortedSetRevRange(`cid:${topic.categoryId}:tids:pinned`, 0, -1, (err, pinnedTids) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(pinnedTids[0], tid2); | 
					
						
							|  |  |  | 				assert.equal(pinnedTids[1], tid1); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				socketTopics.orderPinnedTopics({ uid: adminUid }, [{ tid: tid1, order: 1 }, { tid: tid2, order: 0 }], (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					db.getSortedSetRevRange(`cid:${topic.categoryId}:tids:pinned`, 0, -1, (err, pinnedTids) => { | 
					
						
							| 
									
										
										
										
											2016-12-22 01:48:41 +03:00
										 |  |  | 						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
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('.ignore', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let newTid; | 
					
						
							|  |  |  | 		let uid; | 
					
						
							|  |  |  | 		let newTopic; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2016-05-18 19:02:43 +03:00
										 |  |  | 			uid = topic.userId; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-25 21:34:47 +02:00
										 |  |  | 				function (done) { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					topics.post({ uid: topic.userId, title: 'Topic to be ignored', content: 'Just ignore me, please!', cid: topic.categoryId }, (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
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not appear in the unread list', (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) { | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 					const { topics } = results; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 					const tids = topics.map(topic => 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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not appear as unread in the recent list', (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) { | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 					const { topics } = results; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 					let topic; | 
					
						
							|  |  |  | 					let 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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should appear as unread again when marked as reading', (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) { | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 					const { topics } = results; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 					const tids = topics.map(topic => 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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should appear as unread again when marked as following', (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) { | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 					const { topics } = results; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 					const tids = topics.map(topic => 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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('.fork', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let newTopic; | 
					
						
							|  |  |  | 		const replies = []; | 
					
						
							|  |  |  | 		let topicPids; | 
					
						
							|  |  |  | 		const originalBookmark = 6; | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 		function postReply(next) { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			topics.reply({ uid: topic.userId, content: `test post ${replies.length}`, tid: newTopic.tid }, (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
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 			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) { | 
					
						
							| 
									
										
										
										
											2021-02-04 02:07:29 -07:00
										 |  |  | 					topics.post({ | 
					
						
							|  |  |  | 						uid: topic.userId, | 
					
						
							|  |  |  | 						title: topic.title, | 
					
						
							|  |  |  | 						content: topic.content, | 
					
						
							|  |  |  | 						cid: topic.categoryId, | 
					
						
							|  |  |  | 					}, (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) { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					topicPids = replies.map(reply => 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
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.bookmark({ uid: topic.userId }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:21:56 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should have 12 replies', (done) => { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:21:34 -07:00
										 |  |  | 			assert.equal(12, replies.length); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.createTopicFromPosts({ uid: 0 }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:not-logged-in]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.createTopicFromPosts({ uid: adminUid }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not update the user\'s bookmark', (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
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should update the user\'s bookmark ', (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
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-05-15 22:10:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should properly update topic vote count after forking', async () => { | 
					
						
							|  |  |  | 			const result = await topics.post({ uid: fooUid, cid: categoryObj.cid, title: 'fork vote test', content: 'main post' }); | 
					
						
							|  |  |  | 			const reply1 = await topics.reply({ tid: result.topicData.tid, uid: fooUid, content: 'test reply 1' }); | 
					
						
							|  |  |  | 			const reply2 = await topics.reply({ tid: result.topicData.tid, uid: fooUid, content: 'test reply 2' }); | 
					
						
							|  |  |  | 			const reply3 = await topics.reply({ tid: result.topicData.tid, uid: fooUid, content: 'test reply 3' }); | 
					
						
							|  |  |  | 			await posts.upvote(result.postData.pid, adminUid); | 
					
						
							|  |  |  | 			await posts.upvote(reply1.pid, adminUid); | 
					
						
							|  |  |  | 			assert.strictEqual(await db.sortedSetScore('topics:votes', result.topicData.tid), 1); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.strictEqual(await db.sortedSetScore(`cid:${categoryObj.cid}:tids:votes`, result.topicData.tid), 1); | 
					
						
							| 
									
										
										
										
											2020-05-15 22:10:50 -04:00
										 |  |  | 			const newTopic = await topics.createTopicFromPosts(adminUid, 'Fork test, vote update', [reply1.pid, reply2.pid], result.topicData.tid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.strictEqual(await db.sortedSetScore('topics:votes', newTopic.tid), 1); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.strictEqual(await db.sortedSetScore(`cid:${categoryObj.cid}:tids:votes`, newTopic.tid), 1); | 
					
						
							| 
									
										
										
										
											2020-05-15 22:10:50 -04:00
										 |  |  | 			assert.strictEqual(await topics.getTopicField(newTopic.tid, 'upvotes'), 1); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-05-04 10:29:43 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('controller', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let topicData; | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 			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', | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, result) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.ok(result); | 
					
						
							|  |  |  | 				topicData = result.topicData; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load topic', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/topic/${topicData.slug}`, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load topic api data', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/${topicData.slug}`, { json: true }, (err, response, body) => { | 
					
						
							| 
									
										
										
										
											2019-12-30 22:19:00 -05:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert.strictEqual(body._header.tags.meta.find(t => t.name === 'description').content, 'topic content'); | 
					
						
							|  |  |  | 				assert.strictEqual(body._header.tags.meta.find(t => t.property === 'og:description').content, 'topic content'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 404 if post index is invalid', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/topic/${topicData.slug}/derp`, (err, response) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 404 if topic does not exist', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/topic/123123/does-not-exist`, (err, response) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 401 if not allowed to read as guest', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const privileges = require('../src/privileges'); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			privileges.categories.rescind(['groups:topics:read'], topicData.cid, 'guests', (err) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				request(`${nconf.get('url')}/api/topic/${topicData.slug}`, (err, response, body) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(response.statusCode, 401); | 
					
						
							|  |  |  | 					assert(body); | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 					privileges.categories.give(['groups:topics:read'], topicData.cid, 'guests', done); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should redirect to correct topic if slug is missing', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/topic/${topicData.tid}/herpderp/1?page=2`, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should redirect if post index is out of range', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/${topicData.slug}/-1`, { json: true }, (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); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07: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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 404 if page is out of bounds', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 			meta.config.usePagination = 1; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			request(`${nconf.get('url')}/topic/${topicData.slug}?page=100`, (err, response) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should mark topic read', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			request(`${nconf.get('url')}/topic/${topicData.slug}`, { | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 				jar: adminJar, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (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); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.hasReadTopics([topicData.tid], adminUid, (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
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 404 if tid is not a number', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/teaser/nan`, { json: true }, (err, response) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 403 if cant read', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/teaser/${123123}`, { json: true }, (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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load topic teaser', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/teaser/${topicData.tid}`, { json: true }, (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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 404 if tid is not a number', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/pagination/nan`, { json: true }, (err, response) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should 404 if tid does not exist', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/pagination/1231231`, { json: true }, (err, response) => { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 404); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load pagination', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/pagination/${topicData.tid}`, { json: true }, (err, response, body) => { | 
					
						
							| 
									
										
										
										
											2016-10-16 06:55:12 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							| 
									
										
										
										
											2020-04-23 21:50:08 -04:00
										 |  |  | 				assert.deepEqual(body.pagination, { | 
					
						
							| 
									
										
										
										
											2017-02-24 16:26:19 +03:00
										 |  |  | 					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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('infinitescroll', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		let tid; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 02:07:29 -07:00
										 |  |  | 			topics.post({ | 
					
						
							|  |  |  | 				uid: topic.userId, | 
					
						
							|  |  |  | 				title: topic.title, | 
					
						
							|  |  |  | 				content: topic.content, | 
					
						
							|  |  |  | 				cid: topic.categoryId, | 
					
						
							|  |  |  | 			}, (err, result) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				tid = result.topicData.tid; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMore({ uid: adminUid }, {}, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should infinite load topic posts', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMore({ uid: adminUid }, { tid: tid, after: 0, count: 10 }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data.mainPost); | 
					
						
							|  |  |  | 				assert(data.posts); | 
					
						
							|  |  |  | 				assert(data.privileges); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMoreSortedTopics({ uid: adminUid }, { after: 'invalid' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load more unread topics', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				socketTopics.loadMoreSortedTopics({ uid: adminUid }, { cid: topic.categoryId, after: 0, count: 10, sort: 'unread' }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(data); | 
					
						
							|  |  |  | 					assert(Array.isArray(data.topics)); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMoreSortedTopics({ uid: adminUid }, { after: 'invalid' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load more recent topics', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMoreSortedTopics({ uid: adminUid }, { cid: topic.categoryId, after: 0, count: 10, sort: 'recent' }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				assert(Array.isArray(data.topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMoreFromSet({ uid: adminUid }, { after: 'invalid' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load more from custom set', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMoreFromSet({ uid: adminUid }, { set: `uid:${adminUid}:topics`, after: 0, count: 10 }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				assert(Array.isArray(data.topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('suggested topics', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let tid1; | 
					
						
							|  |  |  | 		let tid3; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((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); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, results) => { | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 				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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return suggested topics', (done) => { | 
					
						
							|  |  |  | 			topics.getSuggestedTopics(tid1, adminUid, 0, -1, (err, topics) => { | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-06-14 17:55:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return suggested topics', (done) => { | 
					
						
							|  |  |  | 			topics.getSuggestedTopics(tid3, adminUid, 0, 2, (err, topics) => { | 
					
						
							| 
									
										
										
										
											2018-06-14 17:55:19 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(topics)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-22 15:29:58 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('unread', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		let tid; | 
					
						
							|  |  |  | 		let mainPid; | 
					
						
							|  |  |  | 		let uid; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 			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
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, results) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markUnread({ uid: adminUid }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail if topic does not exist', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markUnread({ uid: adminUid }, 1231082, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-topic]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should mark topic unread', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.hasReadTopic(tid, adminUid, (err, hasRead) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(hasRead, false); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsRead({ uid: 0 }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should mark topic read', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsRead({ uid: adminUid }, [tid], (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.hasReadTopic(tid, adminUid, (err, hasRead) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(hasRead); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markTopicNotificationsRead({ uid: 0 }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should mark topic notifications read', (done) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 			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
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			], (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAllRead({ uid: 0 }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-uid]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should mark all read', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				socketTopics.markAllRead({ uid: adminUid }, {}, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					topics.hasReadTopic(tid, adminUid, (err, hasRead) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(hasRead); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should mark category topics read', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markUnread({ uid: adminUid }, tid, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				socketTopics.markCategoryTopicsRead({ uid: adminUid }, topic.categoryId, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					topics.hasReadTopic(tid, adminUid, (err, hasRead) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(hasRead); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsUnreadForAll({ uid: adminUid }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-tid]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsUnreadForAll({ uid: 0 }, [tid], (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail if user is not admin', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsUnreadForAll({ uid: uid }, [tid], (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail if topic does not exist', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsUnreadForAll({ uid: uid }, [12312313], (err) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:no-topic]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should mark topic unread for everyone', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsUnreadForAll({ uid: adminUid }, [tid], (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
										 |  |  | 					}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				}, (err, results) => { | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(results.adminRead, false); | 
					
						
							|  |  |  | 					assert.equal(results.regularRead, false); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-03-09 22:36:28 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not do anything if tids is empty array', (done) => { | 
					
						
							|  |  |  | 			socketTopics.markAsRead({ uid: adminUid }, [], (err, markedRead) => { | 
					
						
							| 
									
										
										
										
											2017-03-09 22:36:28 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(!markedRead); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-09-25 12:07:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not return topics in category you cant read', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			let privateCid; | 
					
						
							|  |  |  | 			let privateTid; | 
					
						
							| 
									
										
										
										
											2018-09-25 12:07:56 -04:00
										 |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					categories.create({ | 
					
						
							|  |  |  | 						name: 'private category', | 
					
						
							|  |  |  | 						description: 'private category', | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					privateCid = category.cid; | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 					privileges.categories.rescind(['groups: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not return topics in category you ignored/not watching', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			let ignoredCid; | 
					
						
							|  |  |  | 			let tid; | 
					
						
							| 
									
										
										
										
											2018-12-14 16:24:17 -05:00
										 |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					categories.create({ | 
					
						
							|  |  |  | 						name: 'ignored category', | 
					
						
							|  |  |  | 						description: 'ignored category', | 
					
						
							|  |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					ignoredCid = category.cid; | 
					
						
							| 
									
										
										
										
											2020-05-26 21:57:38 -04:00
										 |  |  | 					privileges.categories.rescind(['groups: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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not return topic as unread if new post is from blocked user', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			let blockedUid; | 
					
						
							|  |  |  | 			let topic; | 
					
						
							| 
									
										
										
										
											2018-10-15 11:09:57 -04:00
										 |  |  | 			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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not return topic as unread if topic is deleted', async () => { | 
					
						
							| 
									
										
										
										
											2019-10-09 18:03:07 -04:00
										 |  |  | 			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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('tags', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		const socketAdmin = require('../src/socket.io/admin'); | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((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
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			], (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-28 14:15:20 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return empty array if query is falsy', (done) => { | 
					
						
							|  |  |  | 			socketTopics.autocompleteTags({ uid: adminUid }, { query: '' }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should autocomplete tags', (done) => { | 
					
						
							|  |  |  | 			socketTopics.autocompleteTags({ uid: adminUid }, { query: 'p' }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				['php', 'psql', 'python'].forEach((tag) => { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 					assert.notEqual(data.indexOf(tag), -1); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return empty array if query is falsy', (done) => { | 
					
						
							|  |  |  | 			socketTopics.searchTags({ uid: adminUid }, { query: '' }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should search tags', (done) => { | 
					
						
							|  |  |  | 			socketTopics.searchTags({ uid: adminUid }, { query: 'no' }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				['nodebb', 'nodejs', 'nosql'].forEach((tag) => { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 					assert.notEqual(data.indexOf(tag), -1); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return empty array if query is falsy', (done) => { | 
					
						
							|  |  |  | 			socketTopics.searchAndLoadTags({ uid: adminUid }, { query: '' }, (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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should search and load tags', (done) => { | 
					
						
							|  |  |  | 			socketTopics.searchAndLoadTags({ uid: adminUid }, { query: 'no' }, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.matchCount, 3); | 
					
						
							|  |  |  | 				assert.equal(data.pageCount, 1); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 				const tagData = [ | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 					{ value: 'nodebb', valueEscaped: 'nodebb', score: 3 }, | 
					
						
							|  |  |  | 					{ value: 'nodejs', valueEscaped: 'nodejs', score: 1 }, | 
					
						
							|  |  |  | 					{ value: 'nosql', valueEscaped: 'nosql', score: 1 }, | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				]; | 
					
						
							|  |  |  | 				assert.deepEqual(data.tags, tagData); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return error if data is invalid', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMoreTags({ uid: adminUid }, { after: 'asd' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-05 15:09:13 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should load more tags', (done) => { | 
					
						
							|  |  |  | 			socketTopics.loadMoreTags({ uid: adminUid }, { after: 0 }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if data is invalid', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.create({ uid: adminUid }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if tag is invalid', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: '' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-tag]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if tag is too short', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'as' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:tag-too-short]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should create empty tag', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'emptytag' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.sortedSetScore('tags:topic:count', 'emptytag', (err, score) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(score, 0); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should do nothing if tag exists', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'emptytag' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.sortedSetScore('tags:topic:count', 'emptytag', (err, score) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(score, 0); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 		it('should rename tags', async () => { | 
					
						
							|  |  |  | 			const result1 = await topics.post({ uid: adminUid, tags: ['plugins'], title: 'topic tagged with plugins', content: 'topic 1 content', cid: topic.categoryId }); | 
					
						
							|  |  |  | 			const result2 = await topics.post({ uid: adminUid, tags: ['plugin'], title: 'topic tagged with plugin', content: 'topic 2 content', cid: topic.categoryId }); | 
					
						
							|  |  |  | 			const data1 = await topics.getTopicData(result2.topicData.tid); | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 			await socketAdmin.tags.rename({ uid: adminUid }, [{ | 
					
						
							|  |  |  | 				value: 'plugin', | 
					
						
							|  |  |  | 				newName: 'plugins', | 
					
						
							|  |  |  | 			}]); | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 			const tids = await topics.getTagTids('plugins', 0, -1); | 
					
						
							|  |  |  | 			assert.strictEqual(tids.length, 2); | 
					
						
							|  |  |  | 			const tags = await topics.getTopicTags(result2.topicData.tid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const data = await topics.getTopicData(result2.topicData.tid); | 
					
						
							|  |  |  | 			assert.strictEqual(tags.length, 1); | 
					
						
							|  |  |  | 			assert.strictEqual(tags[0], 'plugins'); | 
					
						
							| 
									
										
										
										
											2018-01-16 18:20:57 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return related topics', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 			meta.config.maximumRelatedTopics = 2; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const topicData = { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				tags: [{ value: 'javascript' }], | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			topics.getRelatedTopics(topicData, 0, (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				assert.equal(data[0].title, 'topic title 2'); | 
					
						
							|  |  |  | 				meta.config.maximumRelatedTopics = 0; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.deleteTags({ uid: adminUid }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should do nothing if arrays is empty', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.deleteTags({ uid: adminUid }, { tags: [] }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should delete tags', (done) => { | 
					
						
							|  |  |  | 			socketAdmin.tags.create({ uid: adminUid }, { tag: 'emptytag2' }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				socketAdmin.tags.deleteTags({ uid: adminUid }, { tags: ['emptytag', 'emptytag2', 'nodebb', 'nodejs'] }, (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					db.getObjects(['tag:emptytag', 'tag:emptytag2'], (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 17:26:54 +03:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(!data[0]); | 
					
						
							|  |  |  | 						assert(!data[1]); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should delete tag', (done) => { | 
					
						
							|  |  |  | 			topics.deleteTag('javascript', (err) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				db.getObject('tag:javascript', (err, data) => { | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!data); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-05-12 23:00:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should delete category tag as well', async () => { | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			const category = await categories.create({ name: 'delete category' }); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 			const { cid } = category; | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			await topics.post({ uid: adminUid, tags: ['willbedeleted', 'notthis'], title: 'tag topic', content: 'topic 1 content', cid: cid }); | 
					
						
							|  |  |  | 			let categoryTags = await topics.getCategoryTags(cid, 0, -1); | 
					
						
							|  |  |  | 			assert(categoryTags.includes('willbedeleted')); | 
					
						
							|  |  |  | 			assert(categoryTags.includes('notthis')); | 
					
						
							|  |  |  | 			await topics.deleteTags(['willbedeleted']); | 
					
						
							|  |  |  | 			categoryTags = await topics.getCategoryTags(cid, 0, -1); | 
					
						
							|  |  |  | 			assert(!categoryTags.includes('willbedeleted')); | 
					
						
							|  |  |  | 			assert(categoryTags.includes('notthis')); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 23:00:45 -04:00
										 |  |  | 		it('should add and remove tags from topics properly', async () => { | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			const category = await categories.create({ name: 'add/remove category' }); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 			const { cid } = category; | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			const result = await topics.post({ uid: adminUid, tags: ['tag4', 'tag2', 'tag1', 'tag3'], title: 'tag topic', content: 'topic 1 content', cid: cid }); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 			const { tid } = result.topicData; | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 23:00:45 -04:00
										 |  |  | 			let tags = await topics.getTopicTags(tid); | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			let categoryTags = await topics.getCategoryTags(cid, 0, -1); | 
					
						
							| 
									
										
										
										
											2021-04-08 15:19:35 -04:00
										 |  |  | 			assert.deepStrictEqual(tags.sort(), ['tag1', 'tag2', 'tag3', 'tag4']); | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			assert.deepStrictEqual(categoryTags.sort(), ['tag1', 'tag2', 'tag3', 'tag4']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 23:00:45 -04:00
										 |  |  | 			await topics.addTags(['tag7', 'tag6', 'tag5'], [tid]); | 
					
						
							|  |  |  | 			tags = await topics.getTopicTags(tid); | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			categoryTags = await topics.getCategoryTags(cid, 0, -1); | 
					
						
							| 
									
										
										
										
											2021-04-08 15:19:35 -04:00
										 |  |  | 			assert.deepStrictEqual(tags.sort(), ['tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6', 'tag7']); | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			assert.deepStrictEqual(categoryTags.sort(), ['tag1', 'tag2', 'tag3', 'tag4', 'tag5', 'tag6', 'tag7']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-12 23:00:45 -04:00
										 |  |  | 			await topics.removeTags(['tag1', 'tag3', 'tag5', 'tag7'], [tid]); | 
					
						
							|  |  |  | 			tags = await topics.getTopicTags(tid); | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			categoryTags = await topics.getCategoryTags(cid, 0, -1); | 
					
						
							| 
									
										
										
										
											2021-04-08 15:19:35 -04:00
										 |  |  | 			assert.deepStrictEqual(tags.sort(), ['tag2', 'tag4', 'tag6']); | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			assert.deepStrictEqual(categoryTags.sort(), ['tag2', 'tag4', 'tag6']); | 
					
						
							| 
									
										
										
										
											2020-05-12 23:00:45 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-06-24 16:02:57 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should respect minTags', async () => { | 
					
						
							|  |  |  | 			const oldValue = meta.config.minimumTagsPerTopic; | 
					
						
							|  |  |  | 			meta.config.minimumTagsPerTopic = 2; | 
					
						
							|  |  |  | 			let err; | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				await topics.post({ uid: adminUid, tags: ['tag4'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); | 
					
						
							|  |  |  | 			} catch (_err) { | 
					
						
							|  |  |  | 				err = _err; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(err.message, `[[error:not-enough-tags, ${meta.config.minimumTagsPerTopic}]]`); | 
					
						
							| 
									
										
										
										
											2020-06-24 16:02:57 -04:00
										 |  |  | 			meta.config.minimumTagsPerTopic = oldValue; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should respect maxTags', async () => { | 
					
						
							|  |  |  | 			const oldValue = meta.config.maximumTagsPerTopic; | 
					
						
							|  |  |  | 			meta.config.maximumTagsPerTopic = 2; | 
					
						
							|  |  |  | 			let err; | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				await topics.post({ uid: adminUid, tags: ['tag1', 'tag2', 'tag3'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); | 
					
						
							|  |  |  | 			} catch (_err) { | 
					
						
							|  |  |  | 				err = _err; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(err.message, `[[error:too-many-tags, ${meta.config.maximumTagsPerTopic}]]`); | 
					
						
							| 
									
										
										
										
											2020-06-24 16:02:57 -04:00
										 |  |  | 			meta.config.maximumTagsPerTopic = oldValue; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should respect minTags per category', async () => { | 
					
						
							|  |  |  | 			const minTags = 2; | 
					
						
							|  |  |  | 			await categories.setCategoryField(topic.categoryId, 'minTags', minTags); | 
					
						
							|  |  |  | 			let err; | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				await topics.post({ uid: adminUid, tags: ['tag4'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); | 
					
						
							|  |  |  | 			} catch (_err) { | 
					
						
							|  |  |  | 				err = _err; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(err.message, `[[error:not-enough-tags, ${minTags}]]`); | 
					
						
							|  |  |  | 			await db.deleteObjectField(`category:${topic.categoryId}`, 'minTags'); | 
					
						
							| 
									
										
										
										
											2020-06-24 16:02:57 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should respect maxTags per category', async () => { | 
					
						
							|  |  |  | 			const maxTags = 2; | 
					
						
							|  |  |  | 			await categories.setCategoryField(topic.categoryId, 'maxTags', maxTags); | 
					
						
							|  |  |  | 			let err; | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				await topics.post({ uid: adminUid, tags: ['tag1', 'tag2', 'tag3'], title: 'tag topic', content: 'topic 1 content', cid: topic.categoryId }); | 
					
						
							|  |  |  | 			} catch (_err) { | 
					
						
							|  |  |  | 				err = _err; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			assert.equal(err.message, `[[error:too-many-tags, ${maxTags}]]`); | 
					
						
							|  |  |  | 			await db.deleteObjectField(`category:${topic.categoryId}`, 'maxTags'); | 
					
						
							| 
									
										
										
										
											2020-06-24 16:02:57 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should create and delete category tags properly', async () => { | 
					
						
							|  |  |  | 			const category = await categories.create({ name: 'tag category 2' }); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 			const { cid } = category; | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			const title = 'test title'; | 
					
						
							|  |  |  | 			const postResult = await topics.post({ uid: adminUid, tags: ['cattag1', 'cattag2', 'cattag3'], title: title, content: 'topic 1 content', cid: cid }); | 
					
						
							|  |  |  | 			await topics.post({ uid: adminUid, tags: ['cattag1', 'cattag2'], title: title, content: 'topic 1 content', cid: cid }); | 
					
						
							|  |  |  | 			await topics.post({ uid: adminUid, tags: ['cattag1'], title: title, content: 'topic 1 content', cid: cid }); | 
					
						
							|  |  |  | 			let result = await topics.getCategoryTagsData(cid, 0, -1); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(result, [ | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 				{ value: 'cattag1', score: 3, valueEscaped: 'cattag1' }, | 
					
						
							|  |  |  | 				{ value: 'cattag2', score: 2, valueEscaped: 'cattag2' }, | 
					
						
							|  |  |  | 				{ value: 'cattag3', score: 1, valueEscaped: 'cattag3' }, | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// after purging values should update properly
 | 
					
						
							|  |  |  | 			await topics.purge(postResult.topicData.tid, adminUid); | 
					
						
							|  |  |  | 			result = await topics.getCategoryTagsData(cid, 0, -1); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(result, [ | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 				{ value: 'cattag1', score: 2, valueEscaped: 'cattag1' }, | 
					
						
							|  |  |  | 				{ value: 'cattag2', score: 1, valueEscaped: 'cattag2' }, | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should update counts correctly if topic is moved between categories', async () => { | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			const category1 = await categories.create({ name: 'tag category 2' }); | 
					
						
							|  |  |  | 			const category2 = await categories.create({ name: 'tag category 2' }); | 
					
						
							|  |  |  | 			const cid1 = category1.cid; | 
					
						
							|  |  |  | 			const cid2 = category2.cid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const title = 'test title'; | 
					
						
							|  |  |  | 			const postResult = await topics.post({ uid: adminUid, tags: ['movedtag1', 'movedtag2'], title: title, content: 'topic 1 content', cid: cid1 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			await topics.post({ uid: adminUid, tags: ['movedtag1'], title: title, content: 'topic 1 content', cid: cid1 }); | 
					
						
							|  |  |  | 			await topics.post({ uid: adminUid, tags: ['movedtag2'], title: title, content: 'topic 1 content', cid: cid2 }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			let result1 = await topics.getCategoryTagsData(cid1, 0, -1); | 
					
						
							|  |  |  | 			let result2 = await topics.getCategoryTagsData(cid2, 0, -1); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(result1, [ | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 				{ value: 'movedtag1', score: 2, valueEscaped: 'movedtag1' }, | 
					
						
							|  |  |  | 				{ value: 'movedtag2', score: 1, valueEscaped: 'movedtag2' }, | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(result2, [ | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 				{ value: 'movedtag2', score: 1, valueEscaped: 'movedtag2' }, | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// after moving values should update properly
 | 
					
						
							|  |  |  | 			await topics.tools.move(postResult.topicData.tid, { cid: cid2, uid: adminUid }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			result1 = await topics.getCategoryTagsData(cid1, 0, -1); | 
					
						
							|  |  |  | 			result2 = await topics.getCategoryTagsData(cid2, 0, -1); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(result1, [ | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 				{ value: 'movedtag1', score: 1, valueEscaped: 'movedtag1' }, | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(result2, [ | 
					
						
							| 
									
										
										
										
											2021-07-12 19:25:04 -04:00
										 |  |  | 				{ value: 'movedtag2', score: 2, valueEscaped: 'movedtag2' }, | 
					
						
							|  |  |  | 				{ value: 'movedtag1', score: 1, valueEscaped: 'movedtag1' }, | 
					
						
							| 
									
										
										
										
											2020-12-06 12:36:40 -05:00
										 |  |  | 			]); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2021-02-17 14:44:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not allow regular user to use system tags', async () => { | 
					
						
							|  |  |  | 			const oldValue = meta.config.systemTags; | 
					
						
							|  |  |  | 			meta.config.systemTags = 'moved,locked'; | 
					
						
							|  |  |  | 			let err; | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				await topics.post({ | 
					
						
							|  |  |  | 					uid: fooUid, | 
					
						
							|  |  |  | 					tags: ['locked'], | 
					
						
							|  |  |  | 					title: 'i cant use this', | 
					
						
							|  |  |  | 					content: 'topic 1 content', | 
					
						
							|  |  |  | 					cid: categoryObj.cid, | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} catch (_err) { | 
					
						
							|  |  |  | 				err = _err; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			assert.strictEqual(err.message, '[[error:cant-use-system-tag]]'); | 
					
						
							|  |  |  | 			meta.config.systemTags = oldValue; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should allow admin user to use system tags', async () => { | 
					
						
							|  |  |  | 			const oldValue = meta.config.systemTags; | 
					
						
							|  |  |  | 			meta.config.systemTags = 'moved,locked'; | 
					
						
							|  |  |  | 			const result = await topics.post({ | 
					
						
							|  |  |  | 				uid: adminUid, | 
					
						
							|  |  |  | 				tags: ['locked'], | 
					
						
							|  |  |  | 				title: 'I can use this tag', | 
					
						
							|  |  |  | 				content: 'topic 1 content', | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			assert.strictEqual(result.topicData.tags[0].value, 'locked'); | 
					
						
							|  |  |  | 			meta.config.systemTags = oldValue; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2021-06-22 11:09:32 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not error if regular user edits topic after admin adds system tags', async () => { | 
					
						
							|  |  |  | 			const oldValue = meta.config.systemTags; | 
					
						
							|  |  |  | 			meta.config.systemTags = 'moved,locked'; | 
					
						
							|  |  |  | 			const result = await topics.post({ | 
					
						
							|  |  |  | 				uid: fooUid, | 
					
						
							|  |  |  | 				tags: ['one', 'two'], | 
					
						
							|  |  |  | 				title: 'topic with 2 tags', | 
					
						
							|  |  |  | 				content: 'topic content', | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			await posts.edit({ | 
					
						
							|  |  |  | 				pid: result.postData.pid, | 
					
						
							|  |  |  | 				uid: adminUid, | 
					
						
							|  |  |  | 				content: 'edited content', | 
					
						
							|  |  |  | 				tags: ['one', 'two', 'moved'], | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			await posts.edit({ | 
					
						
							|  |  |  | 				pid: result.postData.pid, | 
					
						
							|  |  |  | 				uid: fooUid, | 
					
						
							|  |  |  | 				content: 'edited content', | 
					
						
							|  |  |  | 				tags: ['one', 'moved', 'two'], | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			const tags = await topics.getTopicTags(result.topicData.tid); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(tags.sort(), ['moved', 'one', 'two']); | 
					
						
							|  |  |  | 			meta.config.systemTags = oldValue; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('follow/unfollow', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		const socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		let tid; | 
					
						
							|  |  |  | 		let followerUid; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							|  |  |  | 			User.create({ username: 'follower' }, (err, uid) => { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				followerUid = uid; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.post({ uid: adminUid, title: 'topic title', content: 'some content', cid: topic.categoryId }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if not logged in', (done) => { | 
					
						
							|  |  |  | 			socketTopics.changeWatching({ uid: 0 }, { tid: tid, type: 'ignore' }, (err) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:not-logged-in]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should filter ignoring uids', (done) => { | 
					
						
							|  |  |  | 			socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'ignore' }, (err) => { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.filterIgnoringUids(tid, [adminUid, followerUid], (err, uids) => { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(uids.length, 1); | 
					
						
							|  |  |  | 					assert.equal(uids[0], adminUid); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-15 18:06:49 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.changeWatching({ uid: followerUid }, {}, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error with invalid type', (done) => { | 
					
						
							|  |  |  | 			socketTopics.changeWatching({ uid: followerUid }, { tid: tid, type: 'derp' }, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should follow topic', (done) => { | 
					
						
							|  |  |  | 			topics.toggleFollow(tid, followerUid, (err, isFollowing) => { | 
					
						
							| 
									
										
										
										
											2017-02-09 13:15:37 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(isFollowing); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				socketTopics.isFollowed({ uid: followerUid }, tid, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('topics search', () => { | 
					
						
							|  |  |  | 		it('should error with invalid data', (done) => { | 
					
						
							|  |  |  | 			socketTopics.search({ uid: adminUid }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return results', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const plugins = require('../src/plugins'); | 
					
						
							| 
									
										
										
										
											2021-01-27 17:36:58 -05:00
										 |  |  | 			plugins.hooks.register('myTestPlugin', { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				hook: 'filter:topic.search', | 
					
						
							|  |  |  | 				method: function (data, callback) { | 
					
						
							|  |  |  | 					callback(null, [1, 2, 3]); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			socketTopics.search({ uid: adminUid }, { tid: topic.tid, term: 'test' }, (err, results) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual(results, [1, 2, 3]); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	it('should check if user is moderator', (done) => { | 
					
						
							|  |  |  | 		socketTopics.isModerator({ uid: adminUid }, topic.tid, (err, isModerator) => { | 
					
						
							| 
									
										
										
										
											2017-03-02 19:03:49 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert(!isModerator); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-08 18:14:30 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('teasers', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let topic1; | 
					
						
							|  |  |  | 		let topic2; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			], (err, results) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topic1 = results[0]; | 
					
						
							|  |  |  | 				topic2 = results[1]; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		after((done) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 			meta.config.teaserPost = ''; | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return empty array if first param is empty', (done) => { | 
					
						
							|  |  |  | 			topics.getTeasers([], 1, (err, teasers) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(0, teasers.length); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get teasers with 2 params', (done) => { | 
					
						
							|  |  |  | 			topics.getTeasers([topic1.topicData, topic2.topicData], 1, (err, teasers) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual([undefined, undefined], teasers); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get teasers with first posts', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 			meta.config.teaserPost = 'first'; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			topics.getTeasers([topic1.topicData, topic2.topicData], 1, (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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get teasers even if one topic is falsy', (done) => { | 
					
						
							|  |  |  | 			topics.getTeasers([null, topic2.topicData], 1, (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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get teasers with last posts', (done) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 			meta.config.teaserPost = 'last-post'; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			topics.reply({ uid: adminUid, content: 'reply 1 content', tid: topic1.topicData.tid }, (err, result) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				topic1.topicData.teaserPid = result.pid; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.getTeasers([topic1.topicData, topic2.topicData], 1, (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(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get teasers by tids', (done) => { | 
					
						
							|  |  |  | 			topics.getTeasersByTids([topic2.topicData.tid, topic1.topicData.tid], 1, (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(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return empty array ', (done) => { | 
					
						
							|  |  |  | 			topics.getTeasersByTids([], 1, (err, teasers) => { | 
					
						
							| 
									
										
										
										
											2017-05-12 20:50:01 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(0, teasers.length); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get teaser by tid', (done) => { | 
					
						
							|  |  |  | 			topics.getTeaser(topic2.topicData.tid, 1, (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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should not return teaser if user is blocked', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			let blockedUid; | 
					
						
							| 
									
										
										
										
											2018-10-15 11:09:57 -04:00
										 |  |  | 			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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('tag privilege', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let uid; | 
					
						
							|  |  |  | 		let cid; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to post if user does not have tag privilege', (done) => { | 
					
						
							|  |  |  | 			privileges.categories.rescind(['groups:topics:tag'], cid, 'registered-users', (err) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.post({ uid: uid, cid: cid, tags: ['tag1'], title: 'topic with tags', content: 'some content here' }, (err) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 					assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should fail to edit if user does not have tag privilege', (done) => { | 
					
						
							|  |  |  | 			topics.post({ uid: uid, cid: cid, title: 'topic with tags', content: 'some content here' }, (err, result) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-06 14:10:15 -07:00
										 |  |  | 				const { pid } = result.postData; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				posts.edit({ pid: pid, uid: uid, content: 'edited content', tags: ['tag2'] }, (err) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 					assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should be able to edit topic and add tags if allowed', (done) => { | 
					
						
							|  |  |  | 			privileges.categories.give(['groups:topics:tag'], cid, 'registered-users', (err) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				topics.post({ uid: uid, cid: cid, tags: ['tag1'], title: 'topic with tags', content: 'some content here' }, (err, result) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 					posts.edit({ pid: result.postData.pid, uid: uid, content: 'edited content', tags: ['tag1', 'tag2'] }, (err, result) => { | 
					
						
							| 
									
										
										
										
											2017-06-16 14:25:40 -04:00
										 |  |  | 						assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 						const tags = result.topic.tags.map(tag => tag.value); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('topic merge', () => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 		let uid; | 
					
						
							|  |  |  | 		let topic1Data; | 
					
						
							|  |  |  | 		let topic2Data; | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-10 12:48:32 -04:00
										 |  |  | 		async function getTopic(tid) { | 
					
						
							|  |  |  | 			const topicData = await topics.getTopicData(tid); | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			return await topics.getTopicWithPosts(topicData, `tid:${topicData.tid}:posts`, adminUid, 0, 19, false); | 
					
						
							| 
									
										
										
										
											2020-06-10 12:48:32 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		before((done) => { | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 			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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if data is not an array', (done) => { | 
					
						
							|  |  |  | 			socketTopics.merge({ uid: 0 }, null, (err) => { | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should error if user does not have privileges', (done) => { | 
					
						
							|  |  |  | 			socketTopics.merge({ uid: 0 }, { tids: [topic2Data.tid, topic1Data.tid] }, (err) => { | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 				assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should merge 2 topics', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-10 12:48:32 -04:00
										 |  |  | 			await socketTopics.merge({ uid: adminUid }, { | 
					
						
							|  |  |  | 				tids: [topic2Data.tid, topic1Data.tid], | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 			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'); | 
					
						
							| 
									
										
										
										
											2020-06-10 12:48:32 -04:00
										 |  |  | 			assert.equal(topic1.title, 'topic 1'); | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should return properly for merged topic', (done) => { | 
					
						
							|  |  |  | 			request(`${nconf.get('url')}/api/topic/${topic2Data.slug}`, { jar: adminJar, json: true }, (err, response, body) => { | 
					
						
							| 
									
										
										
										
											2019-09-20 11:44:49 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 				assert(body); | 
					
						
							|  |  |  | 				assert.deepStrictEqual(body.posts, []); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2020-06-10 12:48:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should merge 2 topics with options mainTid', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-10 12:48:32 -04:00
										 |  |  | 			const topic1Result = await topics.post({ uid: uid, cid: categoryObj.cid, title: 'topic 1', content: 'topic 1 OP' }); | 
					
						
							|  |  |  | 			const topic2Result = await topics.post({ uid: uid, cid: categoryObj.cid, title: 'topic 2', content: 'topic 2 OP' }); | 
					
						
							|  |  |  | 			await topics.reply({ uid: uid, content: 'topic 1 reply', tid: topic1Result.topicData.tid }); | 
					
						
							|  |  |  | 			await topics.reply({ uid: uid, content: 'topic 2 reply', tid: topic2Result.topicData.tid }); | 
					
						
							|  |  |  | 			await socketTopics.merge({ uid: adminUid }, { | 
					
						
							|  |  |  | 				tids: [topic2Result.topicData.tid, topic1Result.topicData.tid], | 
					
						
							|  |  |  | 				options: { | 
					
						
							|  |  |  | 					mainTid: topic2Result.topicData.tid, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const [topic1, topic2] = await Promise.all([ | 
					
						
							|  |  |  | 				getTopic(topic1Result.topicData.tid), | 
					
						
							|  |  |  | 				getTopic(topic2Result.topicData.tid), | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.equal(topic1.posts.length, 0); | 
					
						
							|  |  |  | 			assert.equal(topic2.posts.length, 4); | 
					
						
							|  |  |  | 			assert.equal(topic1.deleted, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.equal(topic2.posts[0].content, 'topic 2 OP'); | 
					
						
							|  |  |  | 			assert.equal(topic2.posts[1].content, 'topic 1 OP'); | 
					
						
							|  |  |  | 			assert.equal(topic2.posts[2].content, 'topic 1 reply'); | 
					
						
							|  |  |  | 			assert.equal(topic2.posts[3].content, 'topic 2 reply'); | 
					
						
							|  |  |  | 			assert.equal(topic2.title, 'topic 2'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should merge 2 topics with options newTopicTitle', async () => { | 
					
						
							| 
									
										
										
										
											2020-06-10 12:48:32 -04:00
										 |  |  | 			const topic1Result = await topics.post({ uid: uid, cid: categoryObj.cid, title: 'topic 1', content: 'topic 1 OP' }); | 
					
						
							|  |  |  | 			const topic2Result = await topics.post({ uid: uid, cid: categoryObj.cid, title: 'topic 2', content: 'topic 2 OP' }); | 
					
						
							|  |  |  | 			await topics.reply({ uid: uid, content: 'topic 1 reply', tid: topic1Result.topicData.tid }); | 
					
						
							|  |  |  | 			await topics.reply({ uid: uid, content: 'topic 2 reply', tid: topic2Result.topicData.tid }); | 
					
						
							|  |  |  | 			const mergeTid = await socketTopics.merge({ uid: adminUid }, { | 
					
						
							|  |  |  | 				tids: [topic2Result.topicData.tid, topic1Result.topicData.tid], | 
					
						
							|  |  |  | 				options: { | 
					
						
							|  |  |  | 					newTopicTitle: 'new merge topic', | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const [topic1, topic2, topic3] = await Promise.all([ | 
					
						
							|  |  |  | 				getTopic(topic1Result.topicData.tid), | 
					
						
							|  |  |  | 				getTopic(topic2Result.topicData.tid), | 
					
						
							|  |  |  | 				getTopic(mergeTid), | 
					
						
							|  |  |  | 			]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.equal(topic1.posts.length, 0); | 
					
						
							|  |  |  | 			assert.equal(topic2.posts.length, 0); | 
					
						
							|  |  |  | 			assert.equal(topic3.posts.length, 4); | 
					
						
							|  |  |  | 			assert.equal(topic1.deleted, true); | 
					
						
							|  |  |  | 			assert.equal(topic2.deleted, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.equal(topic3.posts[0].content, 'topic 1 OP'); | 
					
						
							|  |  |  | 			assert.equal(topic3.posts[1].content, 'topic 2 OP'); | 
					
						
							|  |  |  | 			assert.equal(topic3.posts[2].content, 'topic 1 reply'); | 
					
						
							|  |  |  | 			assert.equal(topic3.posts[3].content, 'topic 2 reply'); | 
					
						
							|  |  |  | 			assert.equal(topic3.title, 'new merge topic'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-11-30 12:39:03 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-06-15 15:23:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	describe('sorted topics', () => { | 
					
						
							| 
									
										
										
										
											2021-04-06 12:43:32 -04:00
										 |  |  | 		let category; | 
					
						
							|  |  |  | 		before(async () => { | 
					
						
							|  |  |  | 			category = await categories.create({ name: 'sorted' }); | 
					
						
							|  |  |  | 			const topic1Result = await topics.post({ uid: topic.userId, cid: category.cid, title: 'old replied', content: 'topic 1 OP' }); | 
					
						
							|  |  |  | 			const topic2Result = await topics.post({ uid: topic.userId, cid: category.cid, title: 'most recent replied', content: 'topic 2 OP' }); | 
					
						
							|  |  |  | 			await topics.reply({ uid: topic.userId, content: 'topic 1 reply', tid: topic1Result.topicData.tid }); | 
					
						
							|  |  |  | 			await topics.reply({ uid: topic.userId, content: 'topic 2 reply', tid: topic2Result.topicData.tid }); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 		it('should get sorted topics in category', (done) => { | 
					
						
							| 
									
										
										
										
											2021-02-04 00:06:15 -07:00
										 |  |  | 			const filters = ['', 'watched', 'unreplied', 'new']; | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			async.map(filters, (filter, next) => { | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 				topics.getSortedTopics({ | 
					
						
							| 
									
										
										
										
											2021-04-06 12:43:32 -04:00
										 |  |  | 					cids: [category.cid], | 
					
						
							| 
									
										
										
										
											2018-06-18 14:37:32 -04:00
										 |  |  | 					uid: topic.userId, | 
					
						
							|  |  |  | 					start: 0, | 
					
						
							|  |  |  | 					stop: -1, | 
					
						
							|  |  |  | 					filter: filter, | 
					
						
							|  |  |  | 					sort: 'votes', | 
					
						
							|  |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 			}, (err, data) => { | 
					
						
							| 
									
										
										
										
											2018-06-15 15:23:27 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 				data.forEach((filterTopics) => { | 
					
						
							| 
									
										
										
										
											2018-06-15 15:23:27 -04:00
										 |  |  | 					assert(Array.isArray(filterTopics.topics)); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2021-04-06 12:43:32 -04:00
										 |  |  | 		it('should get topics recent replied first', async () => { | 
					
						
							|  |  |  | 			const data = await topics.getSortedTopics({ | 
					
						
							|  |  |  | 				cids: [category.cid], | 
					
						
							|  |  |  | 				uid: topic.userId, | 
					
						
							|  |  |  | 				start: 0, | 
					
						
							|  |  |  | 				stop: -1, | 
					
						
							|  |  |  | 				sort: 'recent', | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			assert.strictEqual(data.topics[0].title, 'most recent replied'); | 
					
						
							|  |  |  | 			assert.strictEqual(data.topics[1].title, 'old replied'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get topics recent replied last', async () => { | 
					
						
							|  |  |  | 			const data = await topics.getSortedTopics({ | 
					
						
							|  |  |  | 				cids: [category.cid], | 
					
						
							|  |  |  | 				uid: topic.userId, | 
					
						
							|  |  |  | 				start: 0, | 
					
						
							|  |  |  | 				stop: -1, | 
					
						
							|  |  |  | 				sort: 'old', | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			assert.strictEqual(data.topics[0].title, 'old replied'); | 
					
						
							|  |  |  | 			assert.strictEqual(data.topics[1].title, 'most recent replied'); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2018-06-15 15:23:27 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe('scheduled topics', () => { | 
					
						
							|  |  |  | 		let categoryObj; | 
					
						
							|  |  |  | 		let topicData; | 
					
						
							|  |  |  | 		let topic; | 
					
						
							|  |  |  | 		let adminApiOpts; | 
					
						
							|  |  |  | 		let postData; | 
					
						
							|  |  |  | 		const replyData = { | 
					
						
							|  |  |  | 			form: { | 
					
						
							|  |  |  | 				content: 'a reply by guest', | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			json: true, | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		before(async () => { | 
					
						
							|  |  |  | 			adminApiOpts = { | 
					
						
							|  |  |  | 				json: true, | 
					
						
							|  |  |  | 				jar: adminJar, | 
					
						
							|  |  |  | 				headers: { | 
					
						
							|  |  |  | 					'x-csrf-token': csrf_token, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 			categoryObj = await categories.create({ | 
					
						
							|  |  |  | 				name: 'Another Test Category', | 
					
						
							|  |  |  | 				description: 'Another test category created by testing script', | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			topic = { | 
					
						
							|  |  |  | 				uid: adminUid, | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							|  |  |  | 				title: 'Scheduled Test Topic Title', | 
					
						
							|  |  |  | 				content: 'The content of scheduled test topic', | 
					
						
							|  |  |  | 				timestamp: new Date(Date.now() + 86400000).getTime(), | 
					
						
							|  |  |  | 			}; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should create a scheduled topic as pinned, deleted, included in "topics:scheduled" zset and with a timestamp in future', async () => { | 
					
						
							|  |  |  | 			topicData = (await topics.post(topic)).topicData; | 
					
						
							|  |  |  | 			topicData = await topics.getTopicData(topicData.tid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert(topicData.pinned); | 
					
						
							|  |  |  | 			assert(topicData.deleted); | 
					
						
							|  |  |  | 			assert(topicData.scheduled); | 
					
						
							|  |  |  | 			assert(topicData.timestamp > Date.now()); | 
					
						
							|  |  |  | 			const score = await db.sortedSetScore('topics:scheduled', topicData.tid); | 
					
						
							|  |  |  | 			assert(score); | 
					
						
							|  |  |  | 			// should not be in regular category zsets
 | 
					
						
							|  |  |  | 			const isMember = await db.isMemberOfSortedSets([ | 
					
						
							|  |  |  | 				`cid:${categoryObj.cid}:tids`, | 
					
						
							|  |  |  | 				`cid:${categoryObj.cid}:tids:votes`, | 
					
						
							|  |  |  | 				`cid:${categoryObj.cid}:tids:posts`, | 
					
						
							|  |  |  | 			], topicData.tid); | 
					
						
							|  |  |  | 			assert.deepStrictEqual(isMember, [false, false, false]); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 00:51:23 +03:00
										 |  |  | 		it('should update poster\'s lastposttime with "action time"', async () => { | 
					
						
							|  |  |  | 			// src/user/posts.js:56
 | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 			const data = await User.getUsersFields([adminUid], ['lastposttime']); | 
					
						
							|  |  |  | 			assert.notStrictEqual(data[0].lastposttime, topicData.lastposttime); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not load topic for an unprivileged user', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('get', `${nconf.get('url')}/topic/${topicData.slug}`); | 
					
						
							|  |  |  | 			assert.strictEqual(response.statusCode, 404); | 
					
						
							|  |  |  | 			assert(response.body); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load topic for a privileged user', async () => { | 
					
						
							|  |  |  | 			const response = (await requestType('get', `${nconf.get('url')}/topic/${topicData.slug}`, { jar: adminJar })).res; | 
					
						
							|  |  |  | 			assert.strictEqual(response.statusCode, 200); | 
					
						
							|  |  |  | 			assert(response.body); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not be amongst topics of the category for an unprivileged user', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('get', `${nconf.get('url')}/api/category/${categoryObj.slug}`, { json: true }); | 
					
						
							|  |  |  | 			assert.strictEqual(response.body.topics.filter(topic => topic.tid === topicData.tid).length, 0); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should be amongst topics of the category for a privileged user', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('get', `${nconf.get('url')}/api/category/${categoryObj.slug}`, { json: true, jar: adminJar }); | 
					
						
							|  |  |  | 			const topic = response.body.topics.filter(topic => topic.tid === topicData.tid)[0]; | 
					
						
							|  |  |  | 			assert.strictEqual(topic && topic.tid, topicData.tid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load topic for guests if privilege is given', async () => { | 
					
						
							|  |  |  | 			await privileges.categories.give(['groups:topics:schedule'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 			const response = await requestType('get', `${nconf.get('url')}/topic/${topicData.slug}`); | 
					
						
							|  |  |  | 			assert.strictEqual(response.statusCode, 200); | 
					
						
							|  |  |  | 			assert(response.body); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should be amongst topics of the category for guests if privilege is given', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('get', `${nconf.get('url')}/api/category/${categoryObj.slug}`, { json: true }); | 
					
						
							|  |  |  | 			const topic = response.body.topics.filter(topic => topic.tid === topicData.tid)[0]; | 
					
						
							|  |  |  | 			assert.strictEqual(topic && topic.tid, topicData.tid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not allow deletion of a scheduled topic', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('delete', `${nconf.get('url')}/api/v3/topics/${topicData.tid}/state`, adminApiOpts); | 
					
						
							|  |  |  | 			assert.strictEqual(response.res.statusCode, 400); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not allow to unpin a scheduled topic', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('delete', `${nconf.get('url')}/api/v3/topics/${topicData.tid}/pin`, adminApiOpts); | 
					
						
							|  |  |  | 			assert.strictEqual(response.res.statusCode, 400); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not allow to restore a scheduled topic', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('put', `${nconf.get('url')}/api/v3/topics/${topicData.tid}/state`, adminApiOpts); | 
					
						
							|  |  |  | 			assert.strictEqual(response.res.statusCode, 400); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not allow unprivileged to reply', async () => { | 
					
						
							|  |  |  | 			await privileges.categories.rescind(['groups:topics:schedule'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 			await privileges.categories.give(['groups:topics:reply'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 			const response = await requestType('post', `${nconf.get('url')}/api/v3/topics/${topicData.tid}`, replyData); | 
					
						
							|  |  |  | 			assert.strictEqual(response.res.statusCode, 403); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should allow guests to reply if privilege is given', async () => { | 
					
						
							|  |  |  | 			await privileges.categories.give(['groups:topics:schedule'], categoryObj.cid, 'guests'); | 
					
						
							|  |  |  | 			const response = await requestType('post', `${nconf.get('url')}/api/v3/topics/${topicData.tid}`, replyData); | 
					
						
							|  |  |  | 			assert.strictEqual(response.body.response.content, 'a reply by guest'); | 
					
						
							|  |  |  | 			assert.strictEqual(response.body.response.user.username, '[[global:guest]]'); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should have replies with greater timestamp than the scheduled topics itself', async () => { | 
					
						
							|  |  |  | 			const response = await requestType('get', `${nconf.get('url')}/api/topic/${topicData.slug}`, { json: true }); | 
					
						
							|  |  |  | 			postData = response.body.posts[1]; | 
					
						
							|  |  |  | 			assert(postData.timestamp > response.body.posts[0].timestamp); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should have post edits with greater timestamp than the original', async () => { | 
					
						
							|  |  |  | 			const editData = { ...adminApiOpts, form: { content: 'an edit by the admin' } }; | 
					
						
							|  |  |  | 			const result = await requestType('put', `${nconf.get('url')}/api/v3/posts/${postData.pid}`, editData); | 
					
						
							|  |  |  | 			assert(result.body.response.edited > postData.timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const diffsResult = await requestType('get', `${nconf.get('url')}/api/v3/posts/${postData.pid}/diffs`, adminApiOpts); | 
					
						
							|  |  |  | 			const { revisions } = diffsResult.body.response; | 
					
						
							|  |  |  | 			// diffs are LIFO
 | 
					
						
							|  |  |  | 			assert(revisions[0].timestamp > revisions[1].timestamp); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 00:51:23 +03:00
										 |  |  | 		it('should able to reschedule', async () => { | 
					
						
							|  |  |  | 			const newDate = new Date(Date.now() + (5 * 86400000)).getTime(); | 
					
						
							|  |  |  | 			const editData = { ...adminApiOpts, form: { ...topic, pid: topicData.mainPid, timestamp: newDate } }; | 
					
						
							|  |  |  | 			const response = await requestType('put', `${nconf.get('url')}/api/v3/posts/${topicData.mainPid}`, editData); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 00:51:23 +03:00
										 |  |  | 			const editedTopic = await topics.getTopicFields(topicData.tid, ['lastposttime', 'timestamp']); | 
					
						
							|  |  |  | 			const editedPost = await posts.getPostFields(postData.pid, ['timestamp']); | 
					
						
							|  |  |  | 			assert(editedTopic.timestamp === newDate); | 
					
						
							|  |  |  | 			assert(editedPost.timestamp > editedTopic.timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			const scores = await db.sortedSetsScore([ | 
					
						
							|  |  |  | 				'topics:scheduled', | 
					
						
							|  |  |  | 				`uid:${adminUid}:topics`, | 
					
						
							|  |  |  | 				'topics:tid', | 
					
						
							|  |  |  | 				`cid:${topicData.cid}:uid:${adminUid}:tids`, | 
					
						
							|  |  |  | 			], topicData.tid); | 
					
						
							|  |  |  | 			assert(scores.every(publishTime => publishTime === editedTopic.timestamp)); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should able to publish a scheduled topic', async () => { | 
					
						
							| 
									
										
										
										
											2021-03-31 00:51:23 +03:00
										 |  |  | 			const topicTimestamp = await topics.getTopicField(topicData.tid, 'timestamp'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			mockdate.set(topicTimestamp); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 			await topics.scheduled.handleExpired(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			topicData = await topics.getTopicData(topicData.tid); | 
					
						
							|  |  |  | 			assert(!topicData.pinned); | 
					
						
							|  |  |  | 			assert(!topicData.deleted); | 
					
						
							|  |  |  | 			// Should remove from topics:scheduled upon publishing
 | 
					
						
							|  |  |  | 			const score = await db.sortedSetScore('topics:scheduled', topicData.tid); | 
					
						
							|  |  |  | 			assert(!score); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should update poster\'s lastposttime after a ST published', async () => { | 
					
						
							|  |  |  | 			const data = await User.getUsersFields([adminUid], ['lastposttime']); | 
					
						
							| 
									
										
										
										
											2021-03-31 00:51:23 +03:00
										 |  |  | 			assert.strictEqual(adminUid, topicData.uid); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 			assert.strictEqual(data[0].lastposttime, topicData.lastposttime); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2021-03-31 00:51:23 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should not be able to schedule a "published" topic', async () => { | 
					
						
							|  |  |  | 			const newDate = new Date(Date.now() + 86400000).getTime(); | 
					
						
							|  |  |  | 			const editData = { ...adminApiOpts, form: { ...topic, pid: topicData.mainPid, timestamp: newDate } }; | 
					
						
							|  |  |  | 			const response = await requestType('put', `${nconf.get('url')}/api/v3/posts/${topicData.mainPid}`, editData); | 
					
						
							|  |  |  | 			assert.strictEqual(response.body.response.timestamp, Date.now()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			mockdate.reset(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should allow to purge a scheduled topic', async () => { | 
					
						
							|  |  |  | 			topicData = (await topics.post(topic)).topicData; | 
					
						
							|  |  |  | 			const response = await requestType('delete', `${nconf.get('url')}/api/v3/topics/${topicData.tid}`, adminApiOpts); | 
					
						
							|  |  |  | 			assert.strictEqual(response.res.statusCode, 200); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should remove from topics:scheduled on purge', async () => { | 
					
						
							|  |  |  | 			const score = await db.sortedSetScore('topics:scheduled', topicData.tid); | 
					
						
							|  |  |  | 			assert(!score); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2021-03-24 21:28:02 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }); |