added inactive channel purging
parent
46b6555b33
commit
b6bab6ae1e
|
@ -10,6 +10,7 @@ import {
|
|||
levels,
|
||||
isChannelModerator,
|
||||
getUserDetails,
|
||||
getAppearance,
|
||||
} from '../utility/_UAC.js';
|
||||
import {
|
||||
setChannelTripLevel,
|
||||
|
|
|
@ -4,8 +4,6 @@
|
|||
* @version 1.0.0
|
||||
* @description Display text on target users screen that only they can see
|
||||
* @module whisper
|
||||
* @todo This should be changed to it's own event type, instead of `info`
|
||||
and accept a `userid` rather than `nick`
|
||||
*/
|
||||
|
||||
import {
|
||||
|
@ -18,29 +16,9 @@ import {
|
|||
legacyWhisperOut,
|
||||
legacyWhisperReply,
|
||||
} from '../utility/_LegacyFunctions.js';
|
||||
|
||||
/**
|
||||
* Check and trim string provided by remote client
|
||||
* @param {string} text - Subject string
|
||||
* @private
|
||||
* @todo Move into utility module
|
||||
* @return {string|boolean}
|
||||
*/
|
||||
const parseText = (text) => {
|
||||
// verifies user input is text
|
||||
if (typeof text !== 'string') {
|
||||
return false;
|
||||
}
|
||||
|
||||
let sanitizedText = text;
|
||||
|
||||
// strip newlines from beginning and end
|
||||
sanitizedText = sanitizedText.replace(/^\s*\n|^\s+$|\n\s*$/g, '');
|
||||
// replace 3+ newlines with just 2 newlines
|
||||
sanitizedText = sanitizedText.replace(/\n{3,}/g, '\n\n');
|
||||
|
||||
return sanitizedText;
|
||||
};
|
||||
import {
|
||||
parseText,
|
||||
} from '../utility/_Text.js';
|
||||
|
||||
/**
|
||||
* Executes when invoked by a remote client
|
||||
|
|
|
@ -24,6 +24,7 @@ import {
|
|||
Errors,
|
||||
DefaultChannelSettings,
|
||||
MaxChannelTrips,
|
||||
InactiveAfter,
|
||||
} from './_Constants.js';
|
||||
|
||||
/**
|
||||
|
@ -58,16 +59,16 @@ export function getChannelHash(channel) {
|
|||
* Caches the target channel settings to storage
|
||||
* @public
|
||||
* @param {string} config Server config object
|
||||
* @param {string} channel Target channel
|
||||
* @param {string} channelHash Target channel hash
|
||||
* @param {function} cb Function to run after storing
|
||||
* @return {boolean}
|
||||
*/
|
||||
export function storeChannelSettings(config, channel) {
|
||||
const channelHash = getChannelHash(channel);
|
||||
export function storeChannelSettings(config, channelHash, cb) {
|
||||
const configPath = `./channels/${channelHash[0]}/${channelHash}.json`;
|
||||
|
||||
delete config.permissions[channelHash].channelHash;
|
||||
|
||||
writeFile(configPath, JSON.stringify(config.permissions[channelHash] || DefaultChannelSettings));
|
||||
writeFile(configPath, JSON.stringify(config.permissions[channelHash]), cb);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -141,9 +142,9 @@ export function getChannelSettings(config, channel) {
|
|||
...DefaultChannelSettings,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// @todo Check last access date here, if too old; delete file and use DefaultChannelSettings
|
||||
// @todo Check expire date here, if too old; delete file and use DefaultChannelSettings
|
||||
}
|
||||
}
|
||||
|
||||
config.permissions[channelHash].lastAccessed = new Date();
|
||||
|
@ -152,6 +153,31 @@ export function getChannelSettings(config, channel) {
|
|||
return config.permissions[channelHash];
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for and remove inactive channels from memory
|
||||
* @public
|
||||
* @param {object} config Core config settings
|
||||
* @return {boolean}
|
||||
*/
|
||||
export function purgeInactiveChannels(config) {
|
||||
const inactiveDate = Date.now() - InactiveAfter;
|
||||
const recordNames = Object.keys(config.permissions);
|
||||
|
||||
for (let i = 0; i < recordNames.length; i += 1) {
|
||||
if (config.permissions[recordNames[i]].lastAccessed <= inactiveDate) {
|
||||
if (config.permissions[recordNames[i]].owned) {
|
||||
storeChannelSettings(config, recordNames[i], () => {
|
||||
delete config.permissions[recordNames[i]];
|
||||
});
|
||||
} else {
|
||||
delete config.permissions[recordNames[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a new permission level to the provided trip, within the provided channel
|
||||
* @public
|
||||
|
@ -159,7 +185,7 @@ export function getChannelSettings(config, channel) {
|
|||
* @param {string} channel Target channel name
|
||||
* @param {string} trip Target trip
|
||||
* @param {number} level New level
|
||||
* @return {(string)}
|
||||
* @return {string}
|
||||
*/
|
||||
export function setChannelTripLevel(config, channel, trip, level) {
|
||||
const channelSettings = getChannelSettings(config, channel);
|
||||
|
|
|
@ -177,4 +177,16 @@ export const MaxChannelTrips = 250;
|
|||
*/
|
||||
export const ClaimExpirationDays = 7;
|
||||
|
||||
/**
|
||||
* Minutes between inactive channel checking
|
||||
* @typedef {number} ChannelCheckInterval
|
||||
*/
|
||||
export const ChannelCheckInterval = 1000 * 60 * 10; // 10 minutes
|
||||
|
||||
/**
|
||||
* How many minutes until a channel is considered inactive
|
||||
* @typedef {number} InactiveAfter
|
||||
*/
|
||||
export const InactiveAfter = 1000 * 60 * 60; // 1 hour
|
||||
|
||||
export default Errors;
|
||||
|
|
14
main.mjs
14
main.mjs
|
@ -9,6 +9,12 @@ import {
|
|||
import {
|
||||
CoreApp,
|
||||
} from 'hackchat-server';
|
||||
import {
|
||||
ChannelCheckInterval,
|
||||
} from './commands/utility/_Constants.js';
|
||||
import {
|
||||
purgeInactiveChannels,
|
||||
} from './commands/utility/_Channels.js';
|
||||
|
||||
// required file paths
|
||||
const SessionLocation = './session.key';
|
||||
|
@ -46,6 +52,14 @@ const adapter = new JSONFile(AppConfigLocation);
|
|||
server.appConfig = new Low(adapter);
|
||||
await server.appConfig.read();
|
||||
|
||||
// create channel memory management job
|
||||
setInterval(() => {
|
||||
purgeInactiveChannels(server.appConfig.data);
|
||||
}, ChannelCheckInterval);
|
||||
|
||||
// @todo create storage management job
|
||||
|
||||
// start the server
|
||||
server.init();
|
||||
|
||||
console.log('Websocket server ready');
|
||||
|
|
Loading…
Reference in New Issue