mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	tested filter-type hooks, working! :)
This commit is contained in:
		| @@ -71,24 +71,24 @@ var	fs = require('fs'), | |||||||
| 		fire_hook: function(hook, args, callback) { | 		fire_hook: function(hook, args, callback) { | ||||||
| 			// TODO: Implement priority hook firing | 			// TODO: Implement priority hook firing | ||||||
| 			var	_self = this | 			var	_self = this | ||||||
| 				hookList = loaded_hooks[hook]; | 				hookList = this.loaded_hooks[hook]; | ||||||
|  |  | ||||||
| 			if(loaded_hooks[hook] && Array.isArray(hookList)) { | 			if(hookList && Array.isArray(hookList)) { | ||||||
| 				if (global.env === 'development') console.log('Info: [plugins] Firing hook: ' + hook); | 				if (global.env === 'development') console.log('Info: [plugins] Firing hook: \'' + hook + '\''); | ||||||
| 				var	hookType = hook.split(':')[0]; | 				var	hookType = hook.split(':')[0]; | ||||||
| 				switch(hookType) { | 				switch(hookType) { | ||||||
| 					case 'filter': | 					case 'filter': | ||||||
| 						// Filters only take one argument, so only args[0] will be passed in | 						// Filters only take one argument, so only args[0] will be passed in | ||||||
| 						var	returnVal = args[0]; | 						var	returnVal = (Array.isArray(args) ? args[0] : args); | ||||||
|  |  | ||||||
| 						async.each(hookList, function(hookObj, next) { | 						async.each(hookList, function(hookObj, next) { | ||||||
| 							if (hookObj.callbacked) { | 							if (hookObj.callbacked) { | ||||||
| 								_self.libraries[hookObj.id][hookObj.method](returnVal, function(err, afterVal) { | 								_self.libraries[hookObj[0]][hookObj[1]](returnVal, function(err, afterVal) { | ||||||
| 									returnVal = afterVal; | 									returnVal = afterVal; | ||||||
| 									next(err); | 									next(err); | ||||||
| 								}); | 								}); | ||||||
| 							} else { | 							} else { | ||||||
| 								returnVal = _self.libraries[hookObj.id][hookObj.method](returnVal); | 								returnVal = _self.libraries[hookObj[0]][hookObj[1]](returnVal); | ||||||
| 								next(); | 								next(); | ||||||
| 							} | 							} | ||||||
| 						}, function(err) { | 						}, function(err) { | ||||||
|   | |||||||
							
								
								
									
										151
									
								
								src/posts.js
									
									
									
									
									
								
							
							
						
						
									
										151
									
								
								src/posts.js
									
									
									
									
									
								
							| @@ -7,7 +7,8 @@ var	RDB = require('./redis.js'), | |||||||
| 	threadTools = require('./threadTools.js'), | 	threadTools = require('./threadTools.js'), | ||||||
| 	postTools = require('./postTools'), | 	postTools = require('./postTools'), | ||||||
| 	feed = require('./feed.js'), | 	feed = require('./feed.js'), | ||||||
| 	async = require('async'); | 	async = require('async'), | ||||||
|  | 	plugins = require('./plugins'); | ||||||
|  |  | ||||||
| (function(Posts) { | (function(Posts) { | ||||||
|  |  | ||||||
| @@ -275,91 +276,91 @@ var	RDB = require('./redis.js'), | |||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| 		topics.isLocked(tid, function(locked) { | 		topics.isLocked(tid, function(locked) { | ||||||
|  |  | ||||||
| 			if (!locked || locked === '0') { | 			if (!locked || locked === '0') { | ||||||
| 				RDB.incr('global:next_post_id', function(err, pid) { | 				RDB.incr('global:next_post_id', function(err, pid) { | ||||||
| 					RDB.handle(err); | 					RDB.handle(err); | ||||||
| 					 |  | ||||||
| 					var timestamp = Date.now(); |  | ||||||
| 					 |  | ||||||
| 					var postData = { |  | ||||||
| 						'pid': pid, |  | ||||||
| 						'uid': uid, |  | ||||||
| 						'tid': tid, |  | ||||||
| 						'content': content, |  | ||||||
| 						'timestamp': timestamp, |  | ||||||
| 						'reputation': 0, |  | ||||||
| 						'editor': '', |  | ||||||
| 						'edited': 0, |  | ||||||
| 						'deleted': 0, |  | ||||||
| 						'uploadedImages': '' |  | ||||||
| 					}; |  | ||||||
| 					 |  | ||||||
| 					RDB.hmset('post:' + pid, postData); |  | ||||||
|  |  | ||||||
| 					topics.increasePostCount(tid); | 					plugins.fire_hook('filter:save_post_content', content, function(content) { | ||||||
| 					topics.updateTimestamp(tid, timestamp); | 						var timestamp = Date.now(), | ||||||
|  | 							postData = { | ||||||
| 					RDB.incr('totalpostcount'); | 								'pid': pid, | ||||||
|  | 								'uid': uid, | ||||||
|  | 								'tid': tid, | ||||||
|  | 								'content': content, | ||||||
|  | 								'timestamp': timestamp, | ||||||
|  | 								'reputation': 0, | ||||||
|  | 								'editor': '', | ||||||
|  | 								'edited': 0, | ||||||
|  | 								'deleted': 0, | ||||||
|  | 								'uploadedImages': '' | ||||||
|  | 							}; | ||||||
| 						 | 						 | ||||||
| 					topics.getTopicField(tid, 'cid', function(cid) { | 						RDB.hmset('post:' + pid, postData); | ||||||
| 						RDB.handle(err); |  | ||||||
|  |  | ||||||
| 						feed.updateTopic(tid, cid); | 						topics.increasePostCount(tid); | ||||||
|  | 						topics.updateTimestamp(tid, timestamp); | ||||||
|  |  | ||||||
| 						RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid); | 						RDB.incr('totalpostcount'); | ||||||
|  | 							 | ||||||
|  | 						topics.getTopicField(tid, 'cid', function(cid) { | ||||||
|  | 							RDB.handle(err); | ||||||
|  |  | ||||||
| 						// this is a bit of a naive implementation, defn something to look at post-MVP | 							feed.updateTopic(tid, cid); | ||||||
| 						RDB.scard('cid:' + cid + ':active_users', function(amount) { |  | ||||||
| 							if (amount > 10) { |  | ||||||
| 								RDB.spop('cid:' + cid + ':active_users'); |  | ||||||
| 							} |  | ||||||
|  |  | ||||||
| 							RDB.sadd('cid:' + cid + ':active_users', uid); | 							RDB.zadd('categories:recent_posts:cid:' + cid, Date.now(), pid); | ||||||
| 						}); |  | ||||||
| 					});					 |  | ||||||
| 					 |  | ||||||
| 					user.onNewPostMade(uid, tid, pid, timestamp);					 |  | ||||||
|  |  | ||||||
| 					var imgur = require('./imgur'); | 							// this is a bit of a naive implementation, defn something to look at post-MVP | ||||||
| 					// move clientID to config | 							RDB.scard('cid:' + cid + ':active_users', function(amount) { | ||||||
| 					imgur.setClientID('09f3955fee9a0a6'); | 								if (amount > 10) { | ||||||
| 					 | 									RDB.spop('cid:' + cid + ':active_users'); | ||||||
| 					var uploadedImages = [];					 |  | ||||||
|  |  | ||||||
| 					function uploadImage(image, callback) { |  | ||||||
| 						imgur.upload(image.data, 'base64', function(err, data) { |  | ||||||
| 							if(err) { |  | ||||||
| 								callback(err); |  | ||||||
| 							} else { |  | ||||||
| 								if(data.success) { |  | ||||||
| 									var img= {url:data.data.link, name:image.name}; |  | ||||||
| 									uploadedImages.push(img); |  | ||||||
| 									callback(null); |  | ||||||
| 								} else { |  | ||||||
| 									callback(data); |  | ||||||
| 								} | 								} | ||||||
| 							} |  | ||||||
| 						});			 | 								RDB.sadd('cid:' + cid + ':active_users', uid); | ||||||
| 					}				 | 							}); | ||||||
| 					 | 						});					 | ||||||
| 					if(!images) { | 						 | ||||||
| 						postData.uploadedImages = JSON.stringify(uploadedImages); | 						user.onNewPostMade(uid, tid, pid, timestamp);					 | ||||||
| 						Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages); |  | ||||||
| 						callback(postData); | 						var imgur = require('./imgur'); | ||||||
| 					} else { | 						// move clientID to config | ||||||
| 						async.each(images, uploadImage, function(err) { | 						imgur.setClientID('09f3955fee9a0a6'); | ||||||
| 							if(!err) { | 						 | ||||||
| 								postData.uploadedImages = JSON.stringify(uploadedImages); | 						var uploadedImages = [];					 | ||||||
| 								Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages); |  | ||||||
| 	 | 						function uploadImage(image, callback) { | ||||||
| 								callback(postData); | 							imgur.upload(image.data, 'base64', function(err, data) { | ||||||
| 							} else { | 								if(err) { | ||||||
| 								console.log(err); | 									callback(err); | ||||||
| 								callback(null); | 								} else { | ||||||
| 							} | 									if(data.success) { | ||||||
| 						}); | 										var img= {url:data.data.link, name:image.name}; | ||||||
| 					} | 										uploadedImages.push(img); | ||||||
|  | 										callback(null); | ||||||
|  | 									} else { | ||||||
|  | 										callback(data); | ||||||
|  | 									} | ||||||
|  | 								} | ||||||
|  | 							});			 | ||||||
|  | 						}				 | ||||||
|  | 						 | ||||||
|  | 						if(!images) { | ||||||
|  | 							postData.uploadedImages = JSON.stringify(uploadedImages); | ||||||
|  | 							Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages); | ||||||
|  | 							callback(postData); | ||||||
|  | 						} else { | ||||||
|  | 							async.each(images, uploadImage, function(err) { | ||||||
|  | 								if(!err) { | ||||||
|  | 									postData.uploadedImages = JSON.stringify(uploadedImages); | ||||||
|  | 									Posts.setPostField(pid, 'uploadedImages', postData.uploadedImages); | ||||||
|  | 		 | ||||||
|  | 									callback(postData); | ||||||
|  | 								} else { | ||||||
|  | 									console.log(err); | ||||||
|  | 									callback(null); | ||||||
|  | 								} | ||||||
|  | 							}); | ||||||
|  | 						} | ||||||
|  | 					}); | ||||||
| 				}); | 				}); | ||||||
| 			} else { | 			} else { | ||||||
| 				callback(null); | 				callback(null); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user