mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-21 21:50:49 +02:00
🚀 feat: Banner (#3952)
* feat: Add banner schema and model * feat: Add optional JwtAuth To handle the conditional logic with and without authentication within the model. * feat: Add an endpoint to retrieve a banner * feat: Add implementation for client to use banner and access API * feat: Display a banner on UI * feat: Script for updating and deleting banners * style: Update banner style * fix: Adjust the height when the banner is displayed * fix: failed specs
This commit is contained in:
parent
07e5531b5b
commit
aea01f0bc5
26 changed files with 453 additions and 4 deletions
61
config/delete-banner.js
Normal file
61
config/delete-banner.js
Normal file
|
@ -0,0 +1,61 @@
|
|||
const path = require('path');
|
||||
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') });
|
||||
const { askQuestion, silentExit } = require('./helpers');
|
||||
const Banner = require('~/models/schema/banner');
|
||||
const connect = require('./connect');
|
||||
|
||||
(async () => {
|
||||
await connect();
|
||||
|
||||
console.purple('--------------------------');
|
||||
console.purple('Delete the banner!');
|
||||
console.purple('--------------------------');
|
||||
|
||||
const now = new Date();
|
||||
|
||||
try {
|
||||
const banner = await Banner.findOne({
|
||||
displayFrom: { $lte: now },
|
||||
$or: [{ displayTo: { $gte: now } }, { displayTo: null }],
|
||||
});
|
||||
|
||||
if (!banner) {
|
||||
console.yellow('No banner found to delete.');
|
||||
silentExit(0);
|
||||
}
|
||||
|
||||
console.purple('Current banner:');
|
||||
console.log(`Message: ${banner.message}`);
|
||||
console.log(`Display From: ${banner.displayFrom}`);
|
||||
console.log(`Display To: ${banner.displayTo || 'Not specified'}`);
|
||||
console.log(`Is Public: ${banner.isPublic}`);
|
||||
|
||||
const confirmDelete = await askQuestion('Do you want to delete this banner? (y/N): ');
|
||||
|
||||
if (confirmDelete.toLowerCase() === 'y') {
|
||||
await Banner.findByIdAndDelete(banner._id);
|
||||
console.green('Banner deleted successfully!');
|
||||
} else {
|
||||
console.yellow('Banner deletion cancelled.');
|
||||
}
|
||||
} catch (error) {
|
||||
console.red('Error: ' + error.message);
|
||||
console.error(error);
|
||||
silentExit(1);
|
||||
}
|
||||
|
||||
silentExit(0);
|
||||
})();
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
if (!err.message.includes('fetch failed')) {
|
||||
console.error('There was an uncaught error:');
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
if (err.message.includes('fetch failed')) {
|
||||
return;
|
||||
} else {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
|
@ -21,6 +21,27 @@ const askQuestion = (query) => {
|
|||
);
|
||||
};
|
||||
|
||||
const askMultiLineQuestion = (query) => {
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
|
||||
console.cyan(query);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
let lines = [];
|
||||
rl.on('line', (line) => {
|
||||
if (line.trim() === '.') {
|
||||
rl.close();
|
||||
resolve(lines.join('\n'));
|
||||
} else {
|
||||
lines.push(line);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
function isDockerRunning() {
|
||||
try {
|
||||
execSync('docker info');
|
||||
|
@ -56,6 +77,7 @@ console.gray = (msg) => console.log('\x1b[90m%s\x1b[0m', msg);
|
|||
|
||||
module.exports = {
|
||||
askQuestion,
|
||||
askMultiLineQuestion,
|
||||
silentExit,
|
||||
isDockerRunning,
|
||||
deleteNodeModules,
|
||||
|
|
147
config/update-banner.js
Normal file
147
config/update-banner.js
Normal file
|
@ -0,0 +1,147 @@
|
|||
const path = require('path');
|
||||
const { v5: uuidv5 } = require('uuid');
|
||||
require('module-alias')({ base: path.resolve(__dirname, '..', 'api') });
|
||||
const { askQuestion, askMultiLineQuestion, silentExit } = require('./helpers');
|
||||
const Banner = require('~/models/schema/banner');
|
||||
const connect = require('./connect');
|
||||
|
||||
(async () => {
|
||||
await connect();
|
||||
|
||||
/**
|
||||
* Show the welcome / help menu
|
||||
*/
|
||||
console.purple('--------------------------');
|
||||
console.purple('Update the banner!');
|
||||
console.purple('--------------------------');
|
||||
/**
|
||||
* Set up the variables we need and get the arguments if they were passed in
|
||||
*/
|
||||
let displayFrom = '';
|
||||
let displayTo = '';
|
||||
let message = '';
|
||||
let isPublic = undefined;
|
||||
// If we have the right number of arguments, lets use them
|
||||
if (process.argv.length >= 3) {
|
||||
displayFrom = process.argv[2];
|
||||
displayTo = process.argv[3];
|
||||
message = process.argv[4];
|
||||
isPublic = process.argv[5] === undefined ? undefined : process.argv[5] === 'true';
|
||||
} else {
|
||||
console.orange(
|
||||
'Usage: npm run update-banner <displayFrom(Format: yyyy-mm-ddTHH:MM:SSZ)> <displayTo(Format: yyyy-mm-ddTHH:MM:SSZ)> <message> <isPublic(true/false)>',
|
||||
);
|
||||
console.orange('Note: if you do not pass in the arguments, you will be prompted for them.');
|
||||
console.purple('--------------------------');
|
||||
}
|
||||
|
||||
/**
|
||||
* If we don't have the right number of arguments, lets prompt the user for them
|
||||
*/
|
||||
if (!displayFrom) {
|
||||
displayFrom = await askQuestion('Display From (Format: yyyy-mm-ddTHH:MM:SSZ, Default: now):');
|
||||
}
|
||||
|
||||
// Validate the displayFrom format (ISO 8601)
|
||||
const dateTimeRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
|
||||
if (displayFrom && !dateTimeRegex.test(displayFrom)) {
|
||||
console.red('Error: Invalid date format for displayFrom. Please use yyyy-mm-ddTHH:MM:SSZ.');
|
||||
silentExit(1);
|
||||
}
|
||||
|
||||
displayFrom = displayFrom ? new Date(displayFrom) : new Date();
|
||||
|
||||
if (!displayTo) {
|
||||
displayTo = await askQuestion(
|
||||
'Display To (Format: yyyy-mm-ddTHH:MM:SSZ, Default: not specified):',
|
||||
);
|
||||
}
|
||||
|
||||
if (displayTo && !dateTimeRegex.test(displayTo)) {
|
||||
console.red('Error: Invalid date format for displayTo. Please use yyyy-mm-ddTHH:MM:SSZ.');
|
||||
silentExit(1);
|
||||
}
|
||||
|
||||
displayTo = displayTo ? new Date(displayTo) : null;
|
||||
|
||||
if (!message) {
|
||||
message = await askMultiLineQuestion(
|
||||
'Enter your message ((Enter a single dot "." on a new line to finish)):',
|
||||
);
|
||||
}
|
||||
|
||||
if (message.trim() === '') {
|
||||
console.red('Error: Message cannot be empty!');
|
||||
silentExit(1);
|
||||
}
|
||||
|
||||
if (isPublic === undefined) {
|
||||
const isPublicInput = await askQuestion('Is public (y/N):');
|
||||
isPublic = isPublicInput.toLowerCase() === 'y' ? true : false;
|
||||
}
|
||||
|
||||
// Generate the same bannerId for the same message
|
||||
// This allows us to display only messages that haven't been shown yet
|
||||
const NAMESPACE = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; // Use an arbitrary namespace UUID
|
||||
const bannerId = uuidv5(message, NAMESPACE);
|
||||
|
||||
let result;
|
||||
try {
|
||||
// There is always only one Banner record in the DB.
|
||||
// If a Banner exists in the DB, it will be updated.
|
||||
// If it doesn't exist, a new one will be added.
|
||||
const existingBanner = await Banner.findOne();
|
||||
if (existingBanner) {
|
||||
result = await Banner.findByIdAndUpdate(
|
||||
existingBanner._id,
|
||||
{
|
||||
displayFrom,
|
||||
displayTo,
|
||||
message,
|
||||
bannerId,
|
||||
isPublic,
|
||||
},
|
||||
{ new: true },
|
||||
);
|
||||
} else {
|
||||
result = await Banner.create({
|
||||
displayFrom,
|
||||
displayTo,
|
||||
message,
|
||||
bannerId,
|
||||
isPublic,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.red('Error: ' + error.message);
|
||||
console.error(error);
|
||||
silentExit(1);
|
||||
}
|
||||
|
||||
if (!result) {
|
||||
console.red('Error: Something went wrong while updating the banner!');
|
||||
console.error(result);
|
||||
silentExit(1);
|
||||
}
|
||||
|
||||
console.green('Banner updated successfully!');
|
||||
console.purple(`bannerId: ${bannerId}`);
|
||||
console.purple(`from: ${displayFrom}`);
|
||||
console.purple(`to: ${displayTo || 'not specified'}`);
|
||||
console.purple(`Banner: ${message}`);
|
||||
console.purple(`isPublic: ${isPublic}`);
|
||||
silentExit(0);
|
||||
})();
|
||||
|
||||
process.on('uncaughtException', (err) => {
|
||||
if (!err.message.includes('fetch failed')) {
|
||||
console.error('There was an uncaught error:');
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
if (err.message.includes('fetch failed')) {
|
||||
return;
|
||||
} else {
|
||||
process.exit(1);
|
||||
}
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue