mirror of
https://github.com/wassname/talk.git
synced 2026-07-06 05:17:19 +08:00
Suggestions for refactoring
This commit is contained in:
+1
-1
@@ -53,7 +53,7 @@ class Context {
|
||||
this.plugins = decorateContextPlugins(this, contextPlugins);
|
||||
|
||||
// Bind the publish/subscribe to the context.
|
||||
this.pubsub = pubsub();
|
||||
this.pubsub = pubsub.createClient();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ const setupFunctions = plugins.get('server', 'setupFunctions').reduce((acc, {plu
|
||||
const createSubscriptionManager = (server) => new SubscriptionServer({
|
||||
subscriptionManager: new SubscriptionManager({
|
||||
schema,
|
||||
pubsub: pubsub(),
|
||||
pubsub: pubsub.createClient(),
|
||||
setupFunctions,
|
||||
}),
|
||||
onConnect: ({token}, connection) => {
|
||||
|
||||
+2
-2
@@ -8,7 +8,7 @@ const kue = module.exports.kue = require('kue');
|
||||
// Note that unlike what the name createQueue suggests, it currently returns a
|
||||
// singleton Queue instance. So you can configure and use only a single Queue
|
||||
// object within your node.js process.
|
||||
let Queue = module.exports.queue;
|
||||
let Queue = module.exports.queue = null;
|
||||
|
||||
class Task {
|
||||
|
||||
@@ -22,7 +22,7 @@ class Task {
|
||||
if (!Queue) {
|
||||
module.exports.queue = Queue = kue.createQueue({
|
||||
redis: {
|
||||
createClientFactory: () => redis.createClient()
|
||||
createClientFactory: redis.createClientFactory()
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
+5
-4
@@ -69,6 +69,7 @@ if (SMTP_PORT) {
|
||||
}
|
||||
|
||||
const defaultTransporter = nodemailer.createTransport(options);
|
||||
let taskInstance = null;
|
||||
|
||||
const mailer = module.exports = {
|
||||
|
||||
@@ -77,15 +78,15 @@ const mailer = module.exports = {
|
||||
*/
|
||||
_task: null,
|
||||
get task() {
|
||||
if (mailer._task) {
|
||||
return mailer._task;
|
||||
if (taskInstance) {
|
||||
return taskInstance;
|
||||
}
|
||||
|
||||
mailer._task = new kue.Task({
|
||||
taskInstance = new kue.Task({
|
||||
name: 'mailer'
|
||||
});
|
||||
|
||||
return mailer._task;
|
||||
return taskInstance;
|
||||
},
|
||||
|
||||
sendSimple({template, locals, to, subject}) {
|
||||
|
||||
+3
-13
@@ -13,17 +13,7 @@ const bowser = require('bowser');
|
||||
const ms = require('ms');
|
||||
|
||||
// Create a redis client to use for authentication.
|
||||
const {createClient} = require('./redis');
|
||||
let _client = null;
|
||||
const getClient = () => {
|
||||
if (_client) {
|
||||
return _client;
|
||||
}
|
||||
|
||||
_client = createClient();
|
||||
|
||||
return _client;
|
||||
};
|
||||
const {createClientFactory} = require('./redis');
|
||||
|
||||
const {
|
||||
JWT_SECRET,
|
||||
@@ -157,7 +147,7 @@ const HandleLogout = (req, res, next) => {
|
||||
const now = new Date();
|
||||
const expiry = (jwt.exp - now.getTime() / 1000).toFixed(0);
|
||||
|
||||
getClient().set(`jtir[${jwt.jti}]`, now.toISOString(), 'EX', expiry, (err) => {
|
||||
createClientFactory().set(`jtir[${jwt.jti}]`, now.toISOString(), 'EX', expiry, (err) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
@@ -168,7 +158,7 @@ const HandleLogout = (req, res, next) => {
|
||||
};
|
||||
|
||||
const checkGeneralTokenBlacklist = (jwt) => new Promise((resolve, reject) => {
|
||||
getClient().get(`jtir[${jwt.jti}]`, (err, expiry) => {
|
||||
createClientFactory().get(`jtir[${jwt.jti}]`, (err, expiry) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
+10
-8
@@ -2,13 +2,15 @@ const {RedisPubSub} = require('graphql-redis-subscriptions');
|
||||
|
||||
const {connectionOptions} = require('./redis');
|
||||
|
||||
let pubsub = null;
|
||||
module.exports = () => {
|
||||
if (pubsub) {
|
||||
return pubsub;
|
||||
let pubsubInstance = null;
|
||||
module.exports = {
|
||||
createClient: () => {
|
||||
if (pubsubInstance) {
|
||||
return pubsubInstance;
|
||||
}
|
||||
|
||||
pubsubInstance = new RedisPubSub({connection: connectionOptions});
|
||||
|
||||
return pubsubInstance;
|
||||
}
|
||||
|
||||
pubsub = new RedisPubSub({connection: connectionOptions});
|
||||
|
||||
return pubsub;
|
||||
};
|
||||
|
||||
+25
-10
@@ -29,21 +29,36 @@ const connectionOptions = {
|
||||
}
|
||||
};
|
||||
|
||||
const createClient = () => {
|
||||
let client = redis.createClient(connectionOptions);
|
||||
|
||||
client.ping((err) => {
|
||||
if (err) {
|
||||
console.error('Can\'t ping the redis server!');
|
||||
|
||||
throw err;
|
||||
}
|
||||
|
||||
debug('connection established');
|
||||
});
|
||||
|
||||
return client;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
connectionOptions,
|
||||
createClient() {
|
||||
let client = redis.createClient(connectionOptions);
|
||||
createClient,
|
||||
createClientFactory: () => {
|
||||
let client = null;
|
||||
|
||||
client.ping((err) => {
|
||||
if (err) {
|
||||
console.error('Can\'t ping the redis server!');
|
||||
|
||||
throw err;
|
||||
return () => {
|
||||
if (client) {
|
||||
return client;
|
||||
}
|
||||
|
||||
debug('connection established');
|
||||
});
|
||||
client = createClient();
|
||||
|
||||
return client;
|
||||
return client;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
+6
-5
@@ -5,6 +5,8 @@ const AssetsService = require('./assets');
|
||||
|
||||
const metascraper = require('metascraper');
|
||||
|
||||
let taskInstance = null;
|
||||
|
||||
/**
|
||||
* Exposes a service object to allow operations to execute against the scraper.
|
||||
* @type {Object}
|
||||
@@ -14,17 +16,16 @@ const scraper = {
|
||||
/**
|
||||
* Create the new Task kue singleton.
|
||||
*/
|
||||
_task: null,
|
||||
get task() {
|
||||
if (scraper._task) {
|
||||
return scraper._task;
|
||||
if (taskInstance) {
|
||||
return taskInstance;
|
||||
}
|
||||
|
||||
scraper._task = new kue.Task({
|
||||
taskInstance = new kue.Task({
|
||||
name: 'scraper'
|
||||
});
|
||||
|
||||
return scraper._task;
|
||||
return taskInstance;
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
+4
-14
@@ -30,17 +30,7 @@ const PASSWORD_RESET_JWT_SUBJECT = 'password_reset';
|
||||
const SALT_ROUNDS = 10;
|
||||
|
||||
// Create a redis client to use for authentication.
|
||||
const {createClient} = require('./redis');
|
||||
let _client = null;
|
||||
const getClient = () => {
|
||||
if (_client) {
|
||||
return _client;
|
||||
}
|
||||
|
||||
_client = createClient();
|
||||
|
||||
return _client;
|
||||
};
|
||||
const {createClientFactory} = require('./redis');
|
||||
|
||||
// UsersService is the interface for the application to interact with the
|
||||
// UserModel through.
|
||||
@@ -77,7 +67,7 @@ module.exports = class UsersService {
|
||||
const rdskey = `la[${email.toLowerCase().trim()}]`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient()
|
||||
createClientFactory()
|
||||
.multi()
|
||||
.incr(rdskey)
|
||||
.expire(rdskey, RECAPTCHA_WINDOW_SECONDS)
|
||||
@@ -90,7 +80,7 @@ module.exports = class UsersService {
|
||||
if (replies[0] === 1 || replies[1] === -1) {
|
||||
|
||||
// then expire it after the timeout
|
||||
getClient().expire(rdskey, RECAPTCHA_WINDOW_SECONDS);
|
||||
createClientFactory().expire(rdskey, RECAPTCHA_WINDOW_SECONDS);
|
||||
}
|
||||
|
||||
if (replies[0] >= RECAPTCHA_INCORRECT_TRIGGER) {
|
||||
@@ -112,7 +102,7 @@ module.exports = class UsersService {
|
||||
const rdskey = `la[${email.toLowerCase().trim()}]`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
getClient()
|
||||
createClientFactory()
|
||||
.get(rdskey, (err, reply) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
|
||||
Reference in New Issue
Block a user