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,
|
||||
|
||||
Reference in New Issue
Block a user