mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-09-02 03:17:06 +02:00
Merge remote-tracking branch 'origin/master' into webserver.js-refactor
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var db = require('./database'),
|
||||
posts = require('./posts'),
|
||||
utils = require('./../public/src/utils'),
|
||||
@@ -13,7 +16,6 @@ var db = require('./database'),
|
||||
nconf = require('nconf');
|
||||
|
||||
(function(Categories) {
|
||||
"use strict";
|
||||
|
||||
Categories.create = function(data, callback) {
|
||||
db.incrObjectField('global', 'nextCid', function(err, cid) {
|
||||
@@ -102,12 +104,13 @@ var db = require('./database'),
|
||||
});
|
||||
}
|
||||
|
||||
var indices = {};
|
||||
for(var i=0; i<tids.length; ++i) {
|
||||
var indices = {},
|
||||
i = 0;
|
||||
for(i=0; i<tids.length; ++i) {
|
||||
indices[tids[i]] = start + i;
|
||||
}
|
||||
|
||||
for(var i=0; i<topics.length; ++i) {
|
||||
for(i=0; i<topics.length; ++i) {
|
||||
topics[i].index = indices[topics[i].tid];
|
||||
}
|
||||
|
||||
@@ -161,11 +164,11 @@ var db = require('./database'),
|
||||
|
||||
Categories.getAllCategories = function(uid, callback) {
|
||||
db.getSortedSetRange('categories:cid', 0, -1, function(err, cids) {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if(cids && cids.length === 0) {
|
||||
if (!cids || (cids && cids.length === 0)) {
|
||||
return callback(null, {categories : []});
|
||||
}
|
||||
|
||||
@@ -339,7 +342,6 @@ var db = require('./database'),
|
||||
'categories': categories
|
||||
});
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
Categories.isUserActiveIn = function(cid, uid, callback) {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
(function(module) {
|
||||
'use strict';
|
||||
|
||||
var winston = require('winston'),
|
||||
async = require('async'),
|
||||
nconf = require('nconf'),
|
||||
@@ -68,15 +69,15 @@
|
||||
});
|
||||
|
||||
if(typeof callback === 'function') {
|
||||
callback(null);
|
||||
callback();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.close = function() {
|
||||
db.close();
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// helper functions
|
||||
@@ -129,7 +130,7 @@
|
||||
winston.error('Error indexing ' + err.message);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.search = function(key, term, limit, callback) {
|
||||
db.command({text:"search" , search: term, filter: {key:key}, limit: limit }, function(err, result) {
|
||||
@@ -150,7 +151,7 @@
|
||||
callback(null, []);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.searchRemove = function(key, id, callback) {
|
||||
db.collection('search').remove({id:id, key:key}, function(err, result) {
|
||||
@@ -161,27 +162,22 @@
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.flushdb = function(callback) {
|
||||
db.dropDatabase(function(err, result) {
|
||||
if(err){
|
||||
winston.error(error);
|
||||
if(typeof callback === 'function') {
|
||||
if (err) {
|
||||
winston.error(err.message);
|
||||
if (typeof callback === 'function') {
|
||||
return callback(err);
|
||||
}
|
||||
}
|
||||
|
||||
if(typeof callback === 'function') {
|
||||
callback(null);
|
||||
if (typeof callback === 'function') {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.getFileName = function(callback) {
|
||||
throw new Error('not-implemented');
|
||||
}
|
||||
};
|
||||
|
||||
module.info = function(callback) {
|
||||
db.stats({scale:1024}, function(err, stats) {
|
||||
@@ -189,10 +185,6 @@
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
// TODO : if this it not deleted the templates break,
|
||||
// it is a nested object inside stats
|
||||
delete stats.dataFileVersion;
|
||||
|
||||
stats.avgObjSize = (stats.avgObjSize / 1024).toFixed(2);
|
||||
|
||||
stats.raw = JSON.stringify(stats, null, 4);
|
||||
@@ -201,7 +193,7 @@
|
||||
|
||||
callback(null, stats);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// key
|
||||
|
||||
@@ -209,7 +201,7 @@
|
||||
db.collection('objects').findOne({_key:key}, function(err, item) {
|
||||
callback(err, item !== undefined && item !== null);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.delete = function(key, callback) {
|
||||
db.collection('objects').remove({_key:key}, function(err, result) {
|
||||
@@ -217,22 +209,22 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.get = function(key, callback) {
|
||||
module.getObjectField(key, 'value', callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.set = function(key, value, callback) {
|
||||
var data = {value:value};
|
||||
module.setObject(key, data, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.keys = function(key, callback) {
|
||||
db.collection('objects').find( { _key: { $regex: key /*, $options: 'i'*/ } }, function(err, result) {
|
||||
callback(err, result);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.rename = function(oldKey, newKey, callback) {
|
||||
db.collection('objects').update({_key: oldKey}, {$set:{_key: newKey}}, function(err, result) {
|
||||
@@ -240,25 +232,25 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.expire = function(key, seconds, callback) {
|
||||
module.expireAt(key, Math.round(Date.now() / 1000) + seconds, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.expireAt = function(key, timestamp, callback) {
|
||||
module.setObjectField(key, 'expireAt', new Date(timestamp * 1000), callback);
|
||||
}
|
||||
};
|
||||
|
||||
//hashes
|
||||
module.setObject = function(key, data, callback) {
|
||||
data['_key'] = key;
|
||||
data._key = key;
|
||||
db.collection('objects').update({_key:key}, {$set:data}, {upsert:true, w: 1}, function(err, result) {
|
||||
if(typeof callback === 'function') {
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.setObjectField = function(key, field, value, callback) {
|
||||
var data = {};
|
||||
@@ -273,7 +265,7 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObject = function(key, callback) {
|
||||
db.collection('objects').findOne({_key:key}, function(err, item) {
|
||||
@@ -281,7 +273,7 @@
|
||||
|
||||
callback(err, item);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjects = function(keys, callback) {
|
||||
|
||||
@@ -299,7 +291,7 @@
|
||||
|
||||
callback(null, returnData);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectField = function(key, field, callback) {
|
||||
module.getObjectFields(key, [field], function(err, data) {
|
||||
@@ -309,7 +301,7 @@
|
||||
|
||||
callback(null, data[field]);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectFields = function(key, fields, callback) {
|
||||
|
||||
@@ -342,7 +334,7 @@
|
||||
|
||||
callback(null, item);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectKeys = function(key, callback) {
|
||||
module.getObject(key, function(err, data) {
|
||||
@@ -356,7 +348,7 @@
|
||||
callback(null, []);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectValues = function(key, callback) {
|
||||
module.getObject(key, function(err, data) {
|
||||
@@ -370,7 +362,7 @@
|
||||
}
|
||||
callback(null, values);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.isObjectField = function(key, field, callback) {
|
||||
var data = {};
|
||||
@@ -385,7 +377,7 @@
|
||||
}
|
||||
callback(err, !!item && item[field] !== undefined && item[field] !== null);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.deleteObjectField = function(key, field, callback) {
|
||||
var data = {};
|
||||
@@ -399,15 +391,15 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.incrObjectField = function(key, field, callback) {
|
||||
module.incrObjectFieldBy(key, field, 1, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.decrObjectField = function(key, field, callback) {
|
||||
module.incrObjectFieldBy(key, field, -1, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.incrObjectFieldBy = function(key, field, value, callback) {
|
||||
var data = {};
|
||||
@@ -422,7 +414,7 @@
|
||||
callback(err, result ? result[field] : null);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// sets
|
||||
@@ -437,19 +429,12 @@
|
||||
});
|
||||
|
||||
|
||||
db.collection('objects').update({_key:key},
|
||||
{
|
||||
$addToSet: { members: { $each: value } }
|
||||
},
|
||||
{
|
||||
upsert:true, w: 1
|
||||
}
|
||||
, function(err, result) {
|
||||
db.collection('objects').update({_key: key}, { $addToSet: { members: { $each: value } } }, { upsert: true, w: 1 }, function(err, result) {
|
||||
if(typeof callback === 'function') {
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.setRemove = function(key, value, callback) {
|
||||
if(!Array.isArray(value)) {
|
||||
@@ -465,7 +450,7 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.isSetMember = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -475,7 +460,7 @@
|
||||
db.collection('objects').findOne({_key:key, members: value}, function(err, item) {
|
||||
callback(err, item !== null && item !== undefined);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.isMemberOfSets = function(sets, value, callback) {
|
||||
|
||||
@@ -498,7 +483,7 @@
|
||||
|
||||
callback(err, result);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getSetMembers = function(key, callback) {
|
||||
db.collection('objects').findOne({_key:key}, {members:1}, function(err, data) {
|
||||
@@ -512,7 +497,7 @@
|
||||
callback(null, data.members);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.setCount = function(key, callback) {
|
||||
db.collection('objects').findOne({_key:key}, function(err, data) {
|
||||
@@ -525,7 +510,7 @@
|
||||
|
||||
callback(null, data.members.length);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.setRemoveRandom = function(key, callback) {
|
||||
db.collection('objects').findOne({_key:key}, function(err, data) {
|
||||
@@ -551,7 +536,7 @@
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// sorted sets
|
||||
@@ -570,7 +555,7 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetRemove = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -582,7 +567,7 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
function getSortedSetRange(key, start, stop, sort, callback) {
|
||||
db.collection('objects').find({_key:key}, {fields:{value:1}})
|
||||
@@ -590,26 +575,29 @@
|
||||
.skip(start)
|
||||
.sort({score: sort})
|
||||
.toArray(function(err, data) {
|
||||
if(err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
// maybe this can be done with mongo?
|
||||
if (!data) {
|
||||
return callback(null, null);
|
||||
}
|
||||
|
||||
data = data.map(function(item) {
|
||||
return item.value;
|
||||
});
|
||||
|
||||
callback(err, data);
|
||||
callback(null, data);
|
||||
});
|
||||
}
|
||||
|
||||
module.getSortedSetRange = function(key, start, stop, callback) {
|
||||
getSortedSetRange(key, start, stop, 1, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getSortedSetRevRange = function(key, start, stop, callback) {
|
||||
getSortedSetRange(key, start, stop, -1, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getSortedSetRevRangeByScore = function(args, callback) {
|
||||
|
||||
@@ -640,7 +628,7 @@
|
||||
|
||||
callback(err, data);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetCount = function(key, min, max, callback) {
|
||||
db.collection('objects').count({_key:key, score: {$gte:min, $lte:max}}, function(err, count) {
|
||||
@@ -653,7 +641,7 @@
|
||||
}
|
||||
callback(null,count);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetCard = function(key, callback) {
|
||||
db.collection('objects').count({_key:key}, function(err, count) {
|
||||
@@ -666,7 +654,7 @@
|
||||
}
|
||||
callback(null, count);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetRank = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -683,7 +671,7 @@
|
||||
|
||||
callback(null, rank);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetRevRank = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -702,7 +690,7 @@
|
||||
|
||||
callback(null, rank);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetScore = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -718,13 +706,13 @@
|
||||
|
||||
callback(err, null);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.isSortedSetMember = function(key, value, callback) {
|
||||
module.sortedSetScore(key, value, function(err, score) {
|
||||
callback(err, !!score);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetsScore = function(keys, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -745,13 +733,14 @@
|
||||
|
||||
callback(null, returnData);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// lists
|
||||
module.listPrepend = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
value = value.toString();
|
||||
}
|
||||
|
||||
module.isObjectField(key, 'array', function(err, exists) {
|
||||
if(err) {
|
||||
if(typeof callback === 'function') {
|
||||
@@ -762,17 +751,16 @@
|
||||
}
|
||||
|
||||
if(exists) {
|
||||
db.collection('objects').update({_key:key}, {'$set': {'array.-1': value}}, {upsert:true, w:1 }, function(err, result) {
|
||||
db.collection('objects').update({_key:key}, {'$set': {'array.-1': value}}, {upsert:true, w:1 }, function(err, result) {
|
||||
if(typeof callback === 'function') {
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
module.listAppend(key, value, callback);
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
});
|
||||
} else {
|
||||
module.listAppend(key, value, callback);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
module.listAppend = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -783,7 +771,7 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.listRemoveLast = function(key, callback) {
|
||||
module.getListRange(key, -1, 0, function(err, value) {
|
||||
@@ -808,7 +796,7 @@
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.listRemoveAll = function(key, value, callback) {
|
||||
if(value !== null && value !== undefined) {
|
||||
@@ -820,7 +808,7 @@
|
||||
callback(err, result);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getListRange = function(key, start, stop, callback) {
|
||||
|
||||
@@ -867,8 +855,7 @@
|
||||
callback(null, []);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}(exports));
|
||||
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
(function(module) {
|
||||
'use strict';
|
||||
|
||||
var winston = require('winston'),
|
||||
nconf = require('nconf'),
|
||||
path = require('path'),
|
||||
express = require('express'),
|
||||
redis_socket_or_host = nconf.get('redis:host'),
|
||||
utils = require('./../../public/src/utils.js'),
|
||||
redis,
|
||||
connectRedis,
|
||||
reds,
|
||||
redisClient;
|
||||
redisClient,
|
||||
postSearch,
|
||||
topicSearch;
|
||||
|
||||
try {
|
||||
redis = require('redis');
|
||||
@@ -21,73 +24,68 @@
|
||||
process.exit();
|
||||
}
|
||||
|
||||
module.init = function(callback) {
|
||||
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) {
|
||||
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
|
||||
redisClient = redis.createClient(nconf.get('redis:host'));
|
||||
} else {
|
||||
/* Else, connect over tcp/ip */
|
||||
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
|
||||
}
|
||||
|
||||
if (redis_socket_or_host && redis_socket_or_host.indexOf('/')>=0) {
|
||||
/* If redis.host contains a path name character, use the unix dom sock connection. ie, /tmp/redis.sock */
|
||||
redisClient = redis.createClient(nconf.get('redis:host'));
|
||||
} else {
|
||||
/* Else, connect over tcp/ip */
|
||||
redisClient = redis.createClient(nconf.get('redis:port'), nconf.get('redis:host'));
|
||||
}
|
||||
if (nconf.get('redis:password')) {
|
||||
redisClient.auth(nconf.get('redis:password'));
|
||||
} else {
|
||||
winston.warn('You have no redis password setup!');
|
||||
}
|
||||
|
||||
if (nconf.get('redis:password')) {
|
||||
redisClient.auth(nconf.get('redis:password'));
|
||||
} else {
|
||||
winston.warn('You have no redis password setup!');
|
||||
}
|
||||
redisClient.on('error', function (err) {
|
||||
winston.error(err.message);
|
||||
process.exit();
|
||||
});
|
||||
|
||||
redisClient.on('error', function (err) {
|
||||
winston.error(err.message);
|
||||
process.exit();
|
||||
});
|
||||
module.client = redisClient;
|
||||
|
||||
module.sessionStore = new connectRedis({
|
||||
client: redisClient,
|
||||
ttl: 60 * 60 * 24 * 14
|
||||
});
|
||||
|
||||
module.client = redisClient;
|
||||
reds.createClient = function () {
|
||||
return reds.client || (reds.client = redisClient);
|
||||
};
|
||||
|
||||
module.sessionStore = new connectRedis({
|
||||
client: redisClient,
|
||||
ttl: 60 * 60 * 24 * 14
|
||||
});
|
||||
|
||||
reds.createClient = function () {
|
||||
return reds.client || (reds.client = redisClient);
|
||||
};
|
||||
|
||||
var userSearch = reds.createSearch('nodebbusersearch'),
|
||||
postSearch = reds.createSearch('nodebbpostsearch'),
|
||||
topicSearch = reds.createSearch('nodebbtopicsearch');
|
||||
|
||||
var db = parseInt(nconf.get('redis:database'), 10);
|
||||
var db = parseInt(nconf.get('redis:database'), 10);
|
||||
|
||||
if (db){
|
||||
redisClient.select(db, function(error) {
|
||||
if(error) {
|
||||
winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message);
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
}
|
||||
if (db) {
|
||||
redisClient.select(db, function(error) {
|
||||
if(error) {
|
||||
winston.error("NodeBB could not connect to your Redis database. Redis returned the following error: " + error.message);
|
||||
process.exit();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.init = function(callback) {
|
||||
callback(null);
|
||||
}
|
||||
callback();
|
||||
};
|
||||
|
||||
module.close = function() {
|
||||
redisClient.quit();
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// Exported functions
|
||||
//
|
||||
module.searchIndex = function(key, content, id) {
|
||||
if(key === 'post') {
|
||||
if (key === 'post') {
|
||||
postSearch.index(content, id);
|
||||
} else if(key === 'topic') {
|
||||
topicSearch.index(content, id);
|
||||
} else if(key === 'user') {
|
||||
userSearch.index(content, id);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.search = function(key, term, limit, callback) {
|
||||
function search(searchObj, callback) {
|
||||
@@ -102,55 +100,30 @@
|
||||
search(postSearch, callback);
|
||||
} else if(key === 'topic') {
|
||||
search(topicSearch, callback);
|
||||
} else if(key === 'user') {
|
||||
search(userSearch, callback);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.searchRemove = function(key, id, callback) {
|
||||
if(key === 'post') {
|
||||
postSearch.remove(id);
|
||||
} else if(key === 'topic') {
|
||||
topicSearch.remove(id);
|
||||
} else if(key === 'user') {
|
||||
userSearch.remove(id);
|
||||
}
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
callback()
|
||||
callback();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.flushdb = function(callback) {
|
||||
redisClient.send_command('flushdb', [], function(err) {
|
||||
if(err){
|
||||
winston.error(error);
|
||||
return callback(err);
|
||||
}
|
||||
callback(null);
|
||||
});
|
||||
}
|
||||
|
||||
module.getFileName = function(callback) {
|
||||
var multi = redisClient.multi();
|
||||
|
||||
multi.config('get', 'dir');
|
||||
multi.config('get', 'dbfilename');
|
||||
multi.exec(function (err, results) {
|
||||
if (err) {
|
||||
winston.error(err.message);
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
results = results.reduce(function (memo, config) {
|
||||
memo[config[0]] = config[1];
|
||||
return memo;
|
||||
}, {});
|
||||
|
||||
var dbFile = path.join(results.dir, results.dbfilename);
|
||||
callback(null, dbFile);
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.info = function(callback) {
|
||||
redisClient.info(function (err, data) {
|
||||
@@ -172,7 +145,7 @@
|
||||
|
||||
callback(null, redisData);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// key
|
||||
|
||||
@@ -180,35 +153,35 @@
|
||||
redisClient.exists(key, function(err, exists) {
|
||||
callback(err, exists === 1);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.delete = function(key, callback) {
|
||||
redisClient.del(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.get = function(key, callback) {
|
||||
redisClient.get(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.set = function(key, value, callback) {
|
||||
redisClient.set(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.keys = function(key, callback) {
|
||||
redisClient.keys(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.rename = function(oldKey, newKey, callback) {
|
||||
redisClient.rename(oldKey, newKey, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.expire = function(key, seconds, callback) {
|
||||
redisClient.expire(key, seconds, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.expireAt = function(key, timestamp, callback) {
|
||||
redisClient.expireat(key, timestamp, callback);
|
||||
}
|
||||
};
|
||||
|
||||
//hashes
|
||||
|
||||
@@ -219,15 +192,15 @@
|
||||
callback(err, res);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.setObjectField = function(key, field, value, callback) {
|
||||
redisClient.hset(key, field, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getObject = function(key, callback) {
|
||||
redisClient.hgetall(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjects = function(keys, callback) {
|
||||
var multi = redisClient.multi();
|
||||
@@ -239,7 +212,7 @@
|
||||
multi.exec(function (err, replies) {
|
||||
callback(err, replies);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectField = function(key, field, callback) {
|
||||
module.getObjectFields(key, [field], function(err, data) {
|
||||
@@ -249,7 +222,7 @@
|
||||
|
||||
callback(null, data[field]);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectFields = function(key, fields, callback) {
|
||||
redisClient.hmget(key, fields, function(err, data) {
|
||||
@@ -265,48 +238,48 @@
|
||||
|
||||
callback(null, returnData);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectKeys = function(key, callback) {
|
||||
redisClient.hkeys(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getObjectValues = function(key, callback) {
|
||||
redisClient.hvals(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.isObjectField = function(key, field, callback) {
|
||||
redisClient.hexists(key, field, function(err, exists) {
|
||||
callback(err, exists === 1);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.deleteObjectField = function(key, field, callback) {
|
||||
redisClient.hdel(key, field, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.incrObjectField = function(key, field, callback) {
|
||||
redisClient.hincrby(key, field, 1, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.decrObjectField = function(key, field, callback) {
|
||||
redisClient.hincrby(key, field, -1, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.incrObjectFieldBy = function(key, field, value, callback) {
|
||||
redisClient.hincrby(key, field, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// sets
|
||||
|
||||
module.setAdd = function(key, value, callback) {
|
||||
redisClient.sadd(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.setRemove = function(key, value, callback) {
|
||||
redisClient.srem(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.isSetMember = function(key, value, callback) {
|
||||
redisClient.sismember(key, value, function(err, result) {
|
||||
@@ -316,7 +289,7 @@
|
||||
|
||||
callback(null, result === 1);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.isMemberOfSets = function(sets, value, callback) {
|
||||
var batch = redisClient.multi();
|
||||
@@ -326,67 +299,67 @@
|
||||
}
|
||||
|
||||
batch.exec(callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getSetMembers = function(key, callback) {
|
||||
redisClient.smembers(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.setCount = function(key, callback) {
|
||||
redisClient.scard(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.setRemoveRandom = function(key, callback) {
|
||||
redisClient.spop(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
// sorted sets
|
||||
|
||||
module.sortedSetAdd = function(key, score, value, callback) {
|
||||
redisClient.zadd(key, score, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetRemove = function(key, value, callback) {
|
||||
redisClient.zrem(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getSortedSetRange = function(key, start, stop, callback) {
|
||||
redisClient.zrange(key, start, stop, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getSortedSetRevRange = function(key, start, stop, callback) {
|
||||
redisClient.zrevrange(key, start, stop, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getSortedSetRevRangeByScore = function(args, callback) {
|
||||
redisClient.zrevrangebyscore(args, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetCount = function(key, min, max, callback) {
|
||||
redisClient.zcount(key, min, max, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetCard = function(key, callback) {
|
||||
redisClient.zcard(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetRank = function(key, value, callback) {
|
||||
redisClient.zrank(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetRevRank = function(key, value, callback) {
|
||||
redisClient.zrevrank(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetScore = function(key, value, callback) {
|
||||
redisClient.zscore(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.isSortedSetMember = function(key, value, callback) {
|
||||
module.sortedSetScore(key, value, function(err, score) {
|
||||
callback(err, !!score);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.sortedSetsScore = function(keys, value, callback) {
|
||||
var multi = redisClient.multi();
|
||||
@@ -396,31 +369,28 @@
|
||||
}
|
||||
|
||||
multi.exec(callback);
|
||||
}
|
||||
};
|
||||
|
||||
// lists
|
||||
module.listPrepend = function(key, value, callback) {
|
||||
redisClient.lpush(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.listAppend = function(key, value, callback) {
|
||||
redisClient.rpush(key, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.listRemoveLast = function(key, callback) {
|
||||
redisClient.rpop(key, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.listRemoveAll = function(key, value, callback) {
|
||||
redisClient.lrem(key, 0, value, callback);
|
||||
}
|
||||
};
|
||||
|
||||
module.getListRange = function(key, start, stop, callback) {
|
||||
redisClient.lrange(key, start, stop, callback);
|
||||
}
|
||||
|
||||
|
||||
|
||||
};
|
||||
|
||||
}(exports));
|
||||
|
||||
|
||||
@@ -297,12 +297,6 @@ var fs = require('fs'),
|
||||
}
|
||||
};
|
||||
|
||||
Meta.db = {
|
||||
getFile: function (callback) {
|
||||
db.getFileName(callback);
|
||||
}
|
||||
};
|
||||
|
||||
Meta.css = {
|
||||
cache: undefined
|
||||
};
|
||||
|
||||
124
src/postTools.js
124
src/postTools.js
@@ -1,3 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
var winston = require('winston'),
|
||||
async = require('async'),
|
||||
nconf = require('nconf'),
|
||||
@@ -14,15 +16,20 @@ var winston = require('winston'),
|
||||
meta = require('./meta');
|
||||
|
||||
(function(PostTools) {
|
||||
|
||||
PostTools.isMain = function(pid, tid, callback) {
|
||||
db.getSortedSetRange('tid:' + tid + ':posts', 0, 0, function(err, pids) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if(!Array.isArray(pids) || !pids.length) {
|
||||
callback(null, false);
|
||||
}
|
||||
|
||||
callback(null, parseInt(pids[0], 10) === parseInt(pid, 10));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
PostTools.privileges = function(pid, uid, callback) {
|
||||
async.parallel({
|
||||
@@ -48,7 +55,6 @@ var winston = require('winston'),
|
||||
});
|
||||
}
|
||||
}
|
||||
// [getThreadPrivileges, isOwnPost, hasEnoughRep]
|
||||
}, function(err, results) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
@@ -61,71 +67,78 @@ var winston = require('winston'),
|
||||
move: results.topicPrivs.admin || results.topicPrivs.moderator
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
PostTools.edit = function(uid, pid, title, content, options) {
|
||||
options || (options = {});
|
||||
PostTools.edit = function(uid, pid, title, content, options, callback) {
|
||||
options = options || {};
|
||||
|
||||
var websockets = require('./socket.io'),
|
||||
success = function() {
|
||||
posts.setPostFields(pid, {
|
||||
edited: Date.now(),
|
||||
editor: uid,
|
||||
content: content
|
||||
});
|
||||
function success(postData) {
|
||||
posts.setPostFields(pid, {
|
||||
edited: Date.now(),
|
||||
editor: uid,
|
||||
content: postData.content
|
||||
});
|
||||
|
||||
events.logPostEdit(uid, pid);
|
||||
events.logPostEdit(uid, pid);
|
||||
|
||||
async.parallel([
|
||||
function(next) {
|
||||
posts.getPostField(pid, 'tid', function(err, tid) {
|
||||
PostTools.isMain(pid, tid, function(err, isMainPost) {
|
||||
if (isMainPost) {
|
||||
title = title.trim();
|
||||
var slug = tid + '/' + utils.slugify(title);
|
||||
async.parallel({
|
||||
topic: function(next) {
|
||||
var tid = postData.tid;
|
||||
PostTools.isMain(pid, tid, function(err, isMainPost) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
topics.setTopicField(tid, 'title', title);
|
||||
topics.setTopicField(tid, 'slug', slug);
|
||||
if (isMainPost) {
|
||||
title = title.trim();
|
||||
var slug = tid + '/' + utils.slugify(title);
|
||||
|
||||
topics.setTopicField(tid, 'thumb', options.topic_thumb);
|
||||
topics.setTopicField(tid, 'title', title);
|
||||
topics.setTopicField(tid, 'slug', slug);
|
||||
|
||||
plugins.fireHook('action:topic.edit', tid);
|
||||
}
|
||||
topics.setTopicField(tid, 'thumb', options.topic_thumb);
|
||||
|
||||
posts.getPostData(pid, function(err, postData) {
|
||||
plugins.fireHook('action:post.edit', postData);
|
||||
});
|
||||
plugins.fireHook('action:topic.edit', tid);
|
||||
}
|
||||
|
||||
next(null, {
|
||||
tid: tid,
|
||||
isMainPost: isMainPost
|
||||
});
|
||||
});
|
||||
plugins.fireHook('action:post.edit', postData);
|
||||
|
||||
next(null, {
|
||||
tid: tid,
|
||||
title: validator.escape(title),
|
||||
isMainPost: isMainPost
|
||||
});
|
||||
},
|
||||
function(next) {
|
||||
PostTools.parse(content, next);
|
||||
}
|
||||
], function(err, results) {
|
||||
websockets.in('topic_' + results[0].tid).emit('event:post_edited', {
|
||||
pid: pid,
|
||||
title: validator.escape(title),
|
||||
isMainPost: results[0].isMainPost,
|
||||
content: results[1]
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
},
|
||||
content: function(next) {
|
||||
PostTools.parse(postData.content, next);
|
||||
}
|
||||
}, callback);
|
||||
}
|
||||
|
||||
PostTools.privileges(pid, uid, function(err, privileges) {
|
||||
if (privileges.editable) {
|
||||
plugins.fireHook('filter:post.save', content, function(err, parsedContent) {
|
||||
if (!err) content = parsedContent;
|
||||
success();
|
||||
});
|
||||
if (err || !privileges.editable) {
|
||||
return callback(err || new Error('not-privileges-to-edit'));
|
||||
}
|
||||
|
||||
posts.getPostData(pid, function(err, postData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
postData.content = content;
|
||||
plugins.fireHook('filter:post.save', postData, function(err, postData) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
success(postData);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
PostTools.delete = function(uid, pid, callback) {
|
||||
var success = function() {
|
||||
@@ -183,7 +196,7 @@ var winston = require('winston'),
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
PostTools.restore = function(uid, pid, callback) {
|
||||
var success = function() {
|
||||
@@ -238,7 +251,7 @@ var winston = require('winston'),
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
PostTools.parse = function(raw, callback) {
|
||||
raw = raw || '';
|
||||
@@ -246,7 +259,7 @@ var winston = require('winston'),
|
||||
plugins.fireHook('filter:post.parse', raw, function(err, parsed) {
|
||||
callback(null, !err ? parsed : raw);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
PostTools.parseSignature = function(raw, callback) {
|
||||
raw = raw || '';
|
||||
@@ -254,5 +267,6 @@ var winston = require('winston'),
|
||||
plugins.fireHook('filter:post.parseSignature', raw, function(err, parsedSignature) {
|
||||
callback(null, !err ? parsedSignature : raw);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
}(exports));
|
||||
|
||||
92
src/posts.js
92
src/posts.js
@@ -29,61 +29,49 @@ var db = require('./database'),
|
||||
toPid = data.toPid;
|
||||
|
||||
if (uid === null) {
|
||||
return callback(new Error('invalid-user'), null);
|
||||
return callback(new Error('invalid-user'));
|
||||
}
|
||||
|
||||
var timestamp = Date.now(),
|
||||
postData;
|
||||
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
topics.isLocked(tid, next);
|
||||
},
|
||||
function(locked, next) {
|
||||
if(locked) {
|
||||
return next(new Error('topic-locked'));
|
||||
}
|
||||
|
||||
db.incrObjectField('global', 'nextPid', next);
|
||||
},
|
||||
function(pid, next) {
|
||||
plugins.fireHook('filter:post.save', content, function(err, newContent) {
|
||||
next(err, pid, newContent);
|
||||
});
|
||||
},
|
||||
function(pid, newContent, next) {
|
||||
var timestamp = Date.now(),
|
||||
postData = {
|
||||
'pid': pid,
|
||||
'uid': uid,
|
||||
'tid': tid,
|
||||
'content': newContent,
|
||||
'timestamp': timestamp,
|
||||
'reputation': '0',
|
||||
'votes': '0',
|
||||
'editor': '',
|
||||
'edited': 0,
|
||||
'deleted': 0
|
||||
};
|
||||
|
||||
postData = {
|
||||
'pid': pid,
|
||||
'uid': uid,
|
||||
'tid': tid,
|
||||
'content': content,
|
||||
'timestamp': timestamp,
|
||||
'reputation': 0,
|
||||
'votes': 0,
|
||||
'editor': '',
|
||||
'edited': 0,
|
||||
'deleted': 0
|
||||
};
|
||||
|
||||
if (toPid) {
|
||||
postData.toPid = toPid;
|
||||
}
|
||||
|
||||
db.setObject('post:' + pid, postData, function(err) {
|
||||
if(err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
db.sortedSetAdd('posts:pid', timestamp, pid);
|
||||
|
||||
db.incrObjectField('global', 'postCount');
|
||||
|
||||
topics.onNewPostMade(tid, pid, timestamp);
|
||||
categories.onNewPostMade(uid, tid, pid, timestamp);
|
||||
user.onNewPostMade(uid, tid, pid, timestamp);
|
||||
|
||||
next(null, postData);
|
||||
});
|
||||
plugins.fireHook('filter:post.save', postData, next);
|
||||
},
|
||||
function(postData, next) {
|
||||
db.setObject('post:' + postData.pid, postData, next);
|
||||
},
|
||||
function(result, next) {
|
||||
db.sortedSetAdd('posts:pid', timestamp, postData.pid);
|
||||
|
||||
db.incrObjectField('global', 'postCount');
|
||||
|
||||
topics.onNewPostMade(tid, postData.pid, timestamp);
|
||||
categories.onNewPostMade(uid, tid, postData.pid, timestamp);
|
||||
user.onNewPostMade(uid, tid, postData.pid, timestamp);
|
||||
|
||||
plugins.fireHook('filter:post.get', postData, next);
|
||||
},
|
||||
function(postData, next) {
|
||||
@@ -103,36 +91,34 @@ var db = require('./database'),
|
||||
};
|
||||
|
||||
Posts.getPostsByTid = function(tid, start, end, reverse, callback) {
|
||||
if (typeof reverse === 'function') {
|
||||
callback = reverse;
|
||||
reverse = false;
|
||||
}
|
||||
|
||||
db[reverse ? 'getSortedSetRevRange' : 'getSortedSetRange']('tid:' + tid + ':posts', start, end, function(err, pids) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if(!pids.length) {
|
||||
if(!Array.isArray(pids) || !pids.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
plugins.fireHook('filter:post.getTopic', pids, function(err, posts) {
|
||||
Posts.getPostsByPids(pids, function(err, posts) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
if(!posts.length) {
|
||||
if(!Array.isArray(posts) || !posts.length) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
|
||||
Posts.getPostsByPids(pids, function(err, posts) {
|
||||
plugins.fireHook('filter:post.getPosts', {tid: tid, posts: posts}, function(err, data) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
plugins.fireHook('action:post.gotTopic', posts);
|
||||
callback(null, posts);
|
||||
|
||||
if(!data || !Array.isArray(data.posts)) {
|
||||
return callback(null, []);
|
||||
}
|
||||
|
||||
callback(null, data.posts);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -347,28 +347,6 @@ var nconf = require('nconf'),
|
||||
res.json(data);
|
||||
});
|
||||
});
|
||||
|
||||
// app.get('/export', function (req, res) {
|
||||
// meta.db.getFile(function (err, dbFile) {
|
||||
// if (!err) {
|
||||
// res.download(dbFile, 'redis.rdb', function (err) {
|
||||
// console.log(err);
|
||||
// res.send(500);
|
||||
// if (err) {
|
||||
// res.send(500);
|
||||
// switch (err.code) {
|
||||
// case 'EACCES':
|
||||
// res.send(500, 'Require permissions from Redis database file: ', dbFile);
|
||||
// break;
|
||||
// default:
|
||||
// res.send(500);
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// } else res.send(500);
|
||||
// });
|
||||
// });
|
||||
});
|
||||
|
||||
app.get('/events', function(req, res, next) {
|
||||
|
||||
@@ -235,7 +235,11 @@ function isUserOnline(uid) {
|
||||
Sockets.updateRoomBrowsingText = updateRoomBrowsingText;
|
||||
function updateRoomBrowsingText(roomName) {
|
||||
|
||||
function getUidsInRoom(room) {
|
||||
if (!roomName) {
|
||||
return;
|
||||
}
|
||||
|
||||
function getUidsInRoom() {
|
||||
var uids = [];
|
||||
var clients = io.sockets.clients(roomName);
|
||||
for(var i=0; i<clients.length; ++i) {
|
||||
@@ -246,7 +250,7 @@ function updateRoomBrowsingText(roomName) {
|
||||
return uids;
|
||||
}
|
||||
|
||||
function getAnonymousCount(roomName) {
|
||||
function getAnonymousCount() {
|
||||
var clients = io.sockets.clients(roomName);
|
||||
var anonCount = 0;
|
||||
|
||||
@@ -258,8 +262,10 @@ function updateRoomBrowsingText(roomName) {
|
||||
return anonCount;
|
||||
}
|
||||
|
||||
var uids = getUidsInRoom(roomName),
|
||||
anonymousCount = getAnonymousCount(roomName);
|
||||
var uids = getUidsInRoom(),
|
||||
anonymousCount = getAnonymousCount();
|
||||
|
||||
|
||||
|
||||
user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], function(err, users) {
|
||||
if(!err) {
|
||||
|
||||
@@ -85,7 +85,7 @@ SocketMeta.rooms.enter = function(socket, data) {
|
||||
|
||||
socket.join(data.enter);
|
||||
|
||||
if (data.leave) {
|
||||
if (data.leave && data.leave !== data.enter) {
|
||||
module.parent.exports.updateRoomBrowsingText(data.leave);
|
||||
}
|
||||
|
||||
|
||||
@@ -12,13 +12,37 @@ var posts = require('../posts'),
|
||||
async = require('async'),
|
||||
S = require('string'),
|
||||
winston = require('winston'),
|
||||
_ = require('underscore'),
|
||||
server = require('./'),
|
||||
|
||||
SocketModules = {};
|
||||
|
||||
/* Posts Composer */
|
||||
|
||||
SocketModules.composer = {};
|
||||
SocketModules.composer = {
|
||||
replyHash: {}
|
||||
};
|
||||
|
||||
var stopTracking = function(replyObj) {
|
||||
if (isLast(replyObj.uid, replyObj.tid)) {
|
||||
server.in('topic_' + replyObj.tid).emit('event:topic.replyStop', replyObj.uid);
|
||||
}
|
||||
|
||||
clearInterval(replyObj.timer);
|
||||
delete SocketModules.composer.replyHash[replyObj.uuid];
|
||||
},
|
||||
isLast = function(uid, tid) {
|
||||
return _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) {
|
||||
if (
|
||||
parseInt(replyObj.tid, 10) === parseInt(tid, 10) &&
|
||||
parseInt(replyObj.uid, 10) === parseInt(uid, 10)
|
||||
) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}).length === 1;
|
||||
};
|
||||
|
||||
SocketModules.composer.push = function(socket, pid, callback) {
|
||||
if (socket.uid || parseInt(meta.config.allowGuestPosting, 10)) {
|
||||
@@ -74,6 +98,50 @@ SocketModules.composer.renderHelp = function(socket, data, callback) {
|
||||
plugins.fireHook('filter:composer.help', '', callback);
|
||||
};
|
||||
|
||||
SocketModules.composer.register = function(socket, data) {
|
||||
var now = Date.now();
|
||||
|
||||
server.in('topic_' + data.tid).emit('event:topic.replyStart', data.uid);
|
||||
|
||||
data.socket = socket;
|
||||
data.lastPing = now;
|
||||
data.lastAnswer = now;
|
||||
data.timer = setInterval(function() {
|
||||
if (data.lastPing === data.lastAnswer) {
|
||||
// Ping the socket to see if the composer is still active
|
||||
data.lastPing = Date.now();
|
||||
socket.emit('event:composer.ping', data.uuid);
|
||||
} else {
|
||||
stopTracking(data);
|
||||
}
|
||||
}, 1000*5); // Every 5 seconds...
|
||||
|
||||
SocketModules.composer.replyHash[data.uuid] = data;
|
||||
};
|
||||
|
||||
SocketModules.composer.unregister = function(socket, uuid) {
|
||||
var replyObj = SocketModules.composer.replyHash[uuid];
|
||||
if (uuid && replyObj) {
|
||||
stopTracking(replyObj);
|
||||
}
|
||||
};
|
||||
|
||||
SocketModules.composer.pingActive = function(socket, uuid) {
|
||||
var data = SocketModules.composer.replyHash[uuid];
|
||||
if (data) {
|
||||
data.lastAnswer = data.lastPing;
|
||||
}
|
||||
};
|
||||
|
||||
SocketModules.composer.getUsersByTid = function(socket, tid, callback) {
|
||||
// Return uids with active composers
|
||||
callback(null, _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) {
|
||||
return parseInt(replyObj.tid, 10) === parseInt(tid, 10);
|
||||
}).map(function(replyObj) {
|
||||
return replyObj.uid
|
||||
}));
|
||||
}
|
||||
|
||||
/* Chat */
|
||||
|
||||
SocketModules.chats = {};
|
||||
|
||||
@@ -151,8 +151,20 @@ SocketPosts.edit = function(socket, data, callback) {
|
||||
return callback(new Error('content-too-short'));
|
||||
}
|
||||
|
||||
postTools.edit(socket.uid, data.pid, data.title, data.content, {topic_thumb: data.topic_thumb});
|
||||
callback();
|
||||
postTools.edit(socket.uid, data.pid, data.title, data.content, {topic_thumb: data.topic_thumb}, function(err, results) {
|
||||
if(err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
index.server.sockets.in('topic_' + results.topic.tid).emit('event:post_edited', {
|
||||
pid: data.pid,
|
||||
title: results.topic.title,
|
||||
isMainPost: results.topic.isMainPost,
|
||||
content: results.content
|
||||
});
|
||||
|
||||
callback();
|
||||
});
|
||||
};
|
||||
|
||||
SocketPosts.delete = function(socket, data, callback) {
|
||||
|
||||
@@ -171,6 +171,14 @@ var async = require('async'),
|
||||
if (!topicExists) {
|
||||
return next(new Error('topic doesn\'t exist'));
|
||||
}
|
||||
|
||||
Topics.isLocked(tid, next);
|
||||
},
|
||||
function(locked, next) {
|
||||
if (locked) {
|
||||
return next(new Error('topic-locked'));
|
||||
}
|
||||
|
||||
threadTools.privileges(tid, uid, next);
|
||||
},
|
||||
function(privilegesData, next) {
|
||||
|
||||
Reference in New Issue
Block a user