mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	fix: don't crash in flags.validate if user blocked target
This commit is contained in:
		
							
								
								
									
										24
									
								
								src/flags.js
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								src/flags.js
									
									
									
									
									
								
							| @@ -236,7 +236,6 @@ Flags.list = function (filters, uid, callback) { | |||||||
|  |  | ||||||
| Flags.validate = function (payload, callback) { | Flags.validate = function (payload, callback) { | ||||||
| 	async.parallel({ | 	async.parallel({ | ||||||
| 		targetExists: async.apply(Flags.targetExists, payload.type, payload.id), |  | ||||||
| 		target: async.apply(Flags.getTarget, payload.type, payload.id, payload.uid), | 		target: async.apply(Flags.getTarget, payload.type, payload.id, payload.uid), | ||||||
| 		reporter: async.apply(user.getUserData, payload.uid), | 		reporter: async.apply(user.getUserData, payload.uid), | ||||||
| 	}, function (err, data) { | 	}, function (err, data) { | ||||||
| @@ -244,8 +243,12 @@ Flags.validate = function (payload, callback) { | |||||||
| 			return callback(err); | 			return callback(err); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if (data.target.deleted) { | 		if (!data.target) { | ||||||
|  | 			return callback(new Error('[[error:invalid-data]]')); | ||||||
|  | 		} else if (data.target.deleted) { | ||||||
| 			return callback(new Error('[[error:post-deleted]]')); | 			return callback(new Error('[[error:post-deleted]]')); | ||||||
|  | 		} else if (!data.reporter || !data.reporter.userslug) { | ||||||
|  | 			return callback(new Error('[[error:no-user]]')); | ||||||
| 		} else if (data.reporter.banned) { | 		} else if (data.reporter.banned) { | ||||||
| 			return callback(new Error('[[error:user-banned]]')); | 			return callback(new Error('[[error:user-banned]]')); | ||||||
| 		} | 		} | ||||||
| @@ -422,13 +425,18 @@ Flags.getTarget = function (type, id, uid, callback) { | |||||||
| 				switch (type) { | 				switch (type) { | ||||||
| 				case 'post': | 				case 'post': | ||||||
| 					async.waterfall([ | 					async.waterfall([ | ||||||
| 						async.apply(posts.getPostsByPids, [id], uid), | 						async.apply(posts.getPostsData, [id]), | ||||||
| 						function (posts, next) { | 						function (postData, next) { | ||||||
| 							topics.addPostData(posts, uid, next); | 							async.map(postData, posts.parsePost, next); | ||||||
| 						}, | 						}, | ||||||
| 					], function (err, posts) { | 						function (postData, next) { | ||||||
| 						next(err, posts[0]); | 							postData = postData.filter(Boolean); | ||||||
| 					}); | 							topics.addPostData(postData, uid, next); | ||||||
|  | 						}, | ||||||
|  | 						function (postData, next) { | ||||||
|  | 							next(null, postData[0]); | ||||||
|  | 						}, | ||||||
|  | 					], callback); | ||||||
| 					break; | 					break; | ||||||
|  |  | ||||||
| 				case 'user': | 				case 'user': | ||||||
|   | |||||||
| @@ -401,6 +401,43 @@ describe('Flags', function () { | |||||||
| 				}); | 				}); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
|  | 		it('should not error if user blocked target', function (done) { | ||||||
|  | 			var SocketFlags = require('../src/socket.io/flags.js'); | ||||||
|  | 			var reporterUid; | ||||||
|  | 			var reporteeUid; | ||||||
|  | 			async.waterfall([ | ||||||
|  | 				function (next) { | ||||||
|  | 					User.create({ username: 'reporter' }, next); | ||||||
|  | 				}, | ||||||
|  | 				function (uid, next) { | ||||||
|  | 					reporterUid = uid; | ||||||
|  | 					User.create({ username: 'reportee' }, next); | ||||||
|  | 				}, | ||||||
|  | 				function (uid, next) { | ||||||
|  | 					reporteeUid = uid; | ||||||
|  | 					User.blocks.add(reporteeUid, reporterUid, next); | ||||||
|  | 				}, | ||||||
|  | 				function (next) { | ||||||
|  | 					Topics.post({ | ||||||
|  | 						cid: 1, | ||||||
|  | 						uid: reporteeUid, | ||||||
|  | 						title: 'Another topic', | ||||||
|  | 						content: 'This is flaggable content', | ||||||
|  | 					}, next); | ||||||
|  | 				}, | ||||||
|  | 				function (data, next) { | ||||||
|  | 					SocketFlags.create({ uid: reporterUid }, { type: 'post', id: data.postData.pid, reason: 'spam' }, next); | ||||||
|  | 				}, | ||||||
|  | 			], done); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should send back error if reporter does not exist', function (done) { | ||||||
|  | 			Flags.validate({ uid: 123123123, id: 1, type: 'post' }, function (err) { | ||||||
|  | 				assert.equal(err.message, '[[error:no-user]]'); | ||||||
|  | 				done(); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	describe('.appendNote()', function () { | 	describe('.appendNote()', function () { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user