Refactor username handling in API and v2 routes for improved normalization
This commit is contained in:
18
api.js
18
api.js
@@ -270,7 +270,7 @@ app.get('/generate', async (req, res) => {
|
||||
|
||||
const config = {
|
||||
displayName: req.query.displayName || "Anonymous",
|
||||
username: req.query.username || "@anonymous",
|
||||
username: normalizeUsername(req.query.username),
|
||||
avatarUrl: fixDataUri(req.query.avatarUrl) || null,
|
||||
text: req.query.text || "No text provided",
|
||||
imageUrl: fixDataUri(req.query.imageUrl) || null,
|
||||
@@ -336,12 +336,22 @@ function fixDataUri(dataUri) {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeUsername(username) {
|
||||
if (!username) return '@anonymous';
|
||||
|
||||
const trimmed = username.trim();
|
||||
|
||||
// If empty or just "@", return default
|
||||
if (!trimmed || trimmed === '@') return '@anonymous';
|
||||
|
||||
// Add @ if it doesn't start with it
|
||||
return trimmed.startsWith('@') ? trimmed : `@${trimmed}`;
|
||||
}
|
||||
|
||||
app.post('/generate', async (req, res) => {
|
||||
try {
|
||||
const timestamp = req.body.timestamp ? formatTimestamp(parseInt(req.body.timestamp)) : formatTimestamp(Date.now() / 1000);
|
||||
|
||||
const username = req.body.username?.trim();
|
||||
|
||||
// Only include engagement if all fields are provded
|
||||
let engagement = null;
|
||||
if (req.body.engagement &&
|
||||
@@ -359,7 +369,7 @@ app.post('/generate', async (req, res) => {
|
||||
|
||||
const config = {
|
||||
displayName: req.body.displayName || "Anonymous",
|
||||
username: (username && username !== "@") ? username : "@anonymous",
|
||||
username: normalizeUsername(req.body.username),
|
||||
avatarUrl: fixDataUri(req.body.avatarUrl) || null,
|
||||
text: req.body.text || "No text provided",
|
||||
imageUrl: fixDataUri(req.body.imageUrl) || null,
|
||||
|
||||
19
v2Routes.js
19
v2Routes.js
@@ -149,6 +149,18 @@ function fixDataUri(dataUri) {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeUsername(username) {
|
||||
if (!username) return undefined;
|
||||
|
||||
const trimmed = username.trim();
|
||||
|
||||
// If empty or just "@", return undefined
|
||||
if (!trimmed || trimmed === '@') return undefined;
|
||||
|
||||
// Add @ if it doesn't start with it
|
||||
return trimmed.startsWith('@') ? trimmed : `@${trimmed}`;
|
||||
}
|
||||
|
||||
|
||||
const TEMPLATE_PATH = path.join(__dirname, 'template.html');
|
||||
const templateHtml = fs.readFileSync(TEMPLATE_PATH, 'utf8');
|
||||
@@ -207,11 +219,9 @@ async function generateQuoteBuffer(config) {
|
||||
// POST /v2/quote - create new session
|
||||
router.post('/quote', (req, res) => {
|
||||
try {
|
||||
const username = req.body.username?.trim();
|
||||
|
||||
const data = {
|
||||
displayName: req.body.displayName?.trim(),
|
||||
username: (username && username !== "@") ? username : undefined,
|
||||
username: normalizeUsername(req.body.username),
|
||||
text: req.body.text,
|
||||
avatarUrl: fixDataUri(req.body.avatarUrl),
|
||||
imageUrl: fixDataUri(req.body.imageUrl),
|
||||
@@ -254,8 +264,7 @@ router.patch('/quote/:id', (req, res) => {
|
||||
|
||||
if (req.body.displayName !== undefined) data.displayName = req.body.displayName?.trim();
|
||||
if (req.body.username !== undefined) {
|
||||
const username = req.body.username?.trim();
|
||||
data.username = (username && username !== "@") ? username : undefined;
|
||||
data.username = normalizeUsername(req.body.username);
|
||||
}
|
||||
if (req.body.text !== undefined) data.text = req.body.text;
|
||||
if (req.body.avatarUrl !== undefined) data.avatarUrl = fixDataUri(req.body.avatarUrl);
|
||||
|
||||
Reference in New Issue
Block a user