Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ DATABASE_URL=mysql://username:password@hostname:port/database
TWILIO_ACCOUNT_SID=ACCOUNT_ID
TWILIO_AUTH_TOKEN=ACCOUNT_TOKEN
TWILIO_SERVICE_SID=XXX
RECAPTCHA_SITE_KEY=RECAPTCHA_KEY
RECAPTCHA_SECRET=RECAPTCHA_SECRET
CONVEYOR_POSTING_WIF=CONVEYOR_KEY
CONVEYOR_USERNAME=CONVEYOR_USERNAME
SENDGRID_API_KEY=SENDGRID_API_KEY
Expand All @@ -21,7 +19,6 @@ INFLUXDB_URL=http://influx.db:8080
SIFTSCIENCE_JS_SNIPPET_KEY=
REACT_DISABLE_ACCOUNT_CREATION=false
ANALYTICS_UPDATE_SUPERKEY=123456
RECAPTCHA_SWITCH=ON
INTERNAL_API_TOKEN=xxxx
PENDING_CLAIMED_ACCOUNTS_THRESHOLD=100
COUNTRY_CODE=cn
Expand All @@ -33,3 +30,6 @@ HIGH_FREQUENCY_TIME_RANGE=2
HIGH_FREQUENCY_COUNT=10
CREATOR_INFO=steem|steemcurator01|steemcurator02|booming01|booming02|booming03|booming04
GOOGLE_ANALYTICS_ID=
TURNSTILE_SWITCH=ON
TURNSTILE_SITE_KEY=TURNSTILE_SITE_KEY
TURNSTILE_SECRET=TURNSTILE_SECRET
4 changes: 1 addition & 3 deletions db/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"use_env_variable": "DATABASE_URL",
"operatorsAliases": "0",
"dialectOptions": {
"ssl" : {
"rejectUnauthorized": false
}
"ssl": false
}
},
"staging": {
Expand Down
35 changes: 35 additions & 0 deletions db/migrations/20251110062951-create-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('config', {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true,
},
c_key: {
type: Sequelize.STRING,
allowNull: false,
unique: true,
},
c_val: {
type: Sequelize.TEXT,
allowNull: false,
},
created_at: {
allowNull: false,
type: Sequelize.DATE,
},
updated_at: {
allowNull: false,
type: Sequelize.DATE,
},
});
await queryInterface.addIndex('config', { fields: ['c_key'], unique: true });
},

down: async (queryInterface) => {
await queryInterface.removeIndex('config', ['c_key']);
await queryInterface.dropTable('config');
},
};
17 changes: 17 additions & 0 deletions db/models/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = (sequelize, DataTypes) => (
sequelize.define('config', {
c_key: {
type: DataTypes.STRING,
allowNull: false,
unique: true,
},
c_val: {
type: DataTypes.TEXT,
allowNull: false,
},
}, {
freezeTableName: true,
underscored: true,
})
);

4 changes: 2 additions & 2 deletions helpers/__mocks__/services.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const recaptchaRequiredForIp = jest.fn(ip => ip === 'ip.requires.recaptcha');
const turnstileRequiredForIp = jest.fn(ip => ip === 'ip.requires.turnstile');

const verifyCaptcha = jest.fn(async () => true);

module.exports = {
...jest.genMockFromModule('../services'),
recaptchaRequiredForIp,
turnstileRequiredForIp,
verifyCaptcha,
};
109 changes: 109 additions & 0 deletions helpers/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Simple global in-memory cache module
* Used to cache config table data and reduce database queries
*/

class MemoryCache {
constructor() {
this.cache = new Map();
this.timers = new Map(); // Store timers for automatic cleanup of expired items
}

/**
* Set cache value
* @param {string} key - Cache key
* @param {*} value - Cache value
* @param {number} ttl - Time to live in seconds, default 300 seconds (5 minutes)
*/
set(key, value, ttl = 300) {
// Clear existing timer for this key if present
if (this.timers.has(key)) {
clearTimeout(this.timers.get(key));
}

// Set cache value
const expireAt = Date.now() + ttl * 1000;
this.cache.set(key, {
value,
expireAt,
});

// Set automatic cleanup timer
const timer = setTimeout(() => {
this.delete(key);
}, ttl * 1000);
this.timers.set(key, timer);
}

/**
* Get cache value
* @param {string} key - Cache key
* @returns {*} Cache value, returns undefined if not found or expired
*/
get(key) {
const item = this.cache.get(key);
if (!item) {
return undefined;
}

// Check if expired
if (Date.now() > item.expireAt) {
this.delete(key);
return undefined;
}

return item.value;
}

/**
* Delete cache item
* @param {string} key - Cache key
*/
delete(key) {
this.cache.delete(key);
if (this.timers.has(key)) {
clearTimeout(this.timers.get(key));
this.timers.delete(key);
}
}

/**
* Clear all cache
*/
clear() {
// Clear all timers
this.timers.forEach(timer => clearTimeout(timer));
this.timers.clear();
this.cache.clear();
}

/**
* Check if cache exists and is not expired
* @param {string} key - Cache key
* @returns {boolean}
*/
has(key) {
const item = this.cache.get(key);
if (!item) {
return false;
}
if (Date.now() > item.expireAt) {
this.delete(key);
return false;
}
return true;
}

/**
* Get cache size
* @returns {number}
*/
size() {
return this.cache.size;
}
}

// Create global singleton
const globalCache = new MemoryCache();

module.exports = globalCache;
68 changes: 68 additions & 0 deletions helpers/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { Op } = Sequelize;

const { ApiError } = require('./errortypes');
const { normalizeEmail } = require('./validator');
const cache = require('./cache');

/**
* Throws if user or ip exceeds number of allowed actions within time period.
Expand Down Expand Up @@ -118,6 +119,70 @@ const deletePhoneRecord = async where => db.phonecode.destroy({ where });

const deleteEmailRecord = async where => db.emailcode.destroy({ where });

/**
* Get config value from config table (with cache)
* @param {string} key - Config key name
* @param {*} defaultValue - Default value returned if config not found
* @param {number} ttl - Cache expiration time in seconds, default 300 seconds (5 minutes)
* @returns {Promise<*>} Config value
*/
async function getConfigValue(key, defaultValue = null, ttl = 300) {
const cacheKey = `config:${key}`;

// Try to get from cache first
const cachedValue = cache.get(cacheKey);
if (cachedValue !== undefined) {
return cachedValue;
}

// Cache miss, query from database
try {
const config = await db.config.findOne({
where: { c_key: key },
});

let value = defaultValue;
if (config && config.c_val) {
// Try to parse JSON, return raw string if parsing fails
try {
value = JSON.parse(config.c_val);
} catch (e) {
value = config.c_val;
}
}

// Store in cache
cache.set(cacheKey, value, ttl);

return value;
} catch (error) {
// Return default value on query failure, but don't cache error result
return defaultValue;
}
}

/**
* Clear cache for specified config
* @param {string} key - Config key name
*/
function clearConfigCache(key) {
const cacheKey = `config:${key}`;
cache.delete(cacheKey);
}

/**
* Get white email domain list from config table
* Returns default ['gmail.com'] if config not found or parse fails
*/
async function getWhiteEmailDomain() {
const domains = await getConfigValue(
'white_email_domain',
['gmail.com'],
300
);
return Array.isArray(domains) ? domains : ['gmail.com'];
}

/**
* remove user id references
* to remove username reserve mechanism
Expand Down Expand Up @@ -209,4 +274,7 @@ module.exports = {
deleteEmailRecord,
findLastSendSmsByCountryNumber,
countTryNumber,
getWhiteEmailDomain,
getConfigValue,
clearConfigCache,
};
8 changes: 7 additions & 1 deletion helpers/errortypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ class ApiError extends Error {
field = 'general',
status = 400,
cause,
data,
}) {
super(`${field}:${type}`);
this.type = type;
this.field = field;
this.status = status;
this.cause = cause;
this.data = data;
}

toJSON() {
return { type: this.type, field: this.field };
const result = { type: this.type, field: this.field };
if (this.data !== undefined) {
result.data = this.data;
}
return result;
}
}

Expand Down
4 changes: 2 additions & 2 deletions helpers/getClientConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/
function getClientConfig() {
const envVars = [
'RECAPTCHA_SITE_KEY',
'TURNSTILE_SITE_KEY',
'STEEMJS_URL',
'DEFAULT_REDIRECT_URI',
'SIFTSCIENCE_JS_SNIPPET_KEY',
'REACT_DISABLE_ACCOUNT_CREATION',
'RECAPTCHA_SWITCH',
'TURNSTILE_SWITCH',
'PENDING_CLAIMED_ACCOUNTS_THRESHOLD',
'CREATOR_INFO',
'GOOGLE_ANALYTICS_ID',
Expand Down
39 changes: 0 additions & 39 deletions helpers/recaptcha.js

This file was deleted.

Loading
Loading