Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
feat(accounts): accept u/name, @name and profile links for a Reddit username
spacesdrive232b7f1054b4
diff --git a/backend/tests/fixtures/accountsRoutes.addReddit.golden.json b/backend/tests/fixtures/accountsRoutes.addReddit.golden.jsonindex 2353714..ecf58d6 100644--- a/backend/tests/fixtures/accountsRoutes.addReddit.golden.json+++ b/backend/tests/fixtures/accountsRoutes.addReddit.golden.json@@ -2,7 +2,7 @@"responses": [{"status": 400,- "body": "{\"success\":false,\"code\":\"VALIDATION_ERROR\",\"message\":\"Username is required\",\"requestId\":\"req-test\"}"+ "body": "{\"success\":false,\"code\":\"VALIDATION_ERROR\",\"message\":\"Enter the Reddit username, such as u/name\",\"requestId\":\"req-test\"}"},{"status": 400,diff --git a/backend/tests/validators/accountValidators.test.ts b/backend/tests/validators/accountValidators.test.tsnew file mode 100644index 0000000..86b5e73--- /dev/null+++ b/backend/tests/validators/accountValidators.test.ts@@ -0,0 +1,18 @@+import { test } from 'node:test';+import assert from 'node:assert/strict';+import { normalizeRedditUsername, parseRedditAccount } from '../../validators/accountValidators.ts';+import { ValidationError } from '../../lib/errors.ts';++test('a Reddit username is taken from u/name, @name or a pasted profile address', () => {+ assert.equal(normalizeRedditUsername(' u/spez '), 'spez');+ assert.equal(normalizeRedditUsername('/u/spez'), 'spez');+ assert.equal(normalizeRedditUsername('@spez'), 'spez');+ assert.equal(normalizeRedditUsername('https://www.reddit.com/user/spez/'), 'spez');+ assert.equal(normalizeRedditUsername('reddit.com/u/spez?sort=new'), 'spez');+ assert.equal(normalizeRedditUsername('spez'), 'spez');+});++test('the Reddit account body keeps only the username and refuses one that is only a prefix', () => {+ assert.deepEqual(parseRedditAccount({ username: 'u/spez', cookie: ' abc ' }), { username: 'spez', cookie: 'abc' });+ assert.throws(() => parseRedditAccount({ username: 'u/', cookie: 'abc' }), new ValidationError('Enter the Reddit username, such as u/name'));+});diff --git a/backend/validators/accountValidators.ts b/backend/validators/accountValidators.tsindex 1b7650c..f7e3104 100644--- a/backend/validators/accountValidators.ts+++ b/backend/validators/accountValidators.ts@@ -1,3 +1,4 @@+import { ValidationError } from '../lib/errors.ts';import { requiredString } from './fields.ts';import type { Fields } from './fields.ts';import type { RedditAccountInput } from '../services/accountService.ts';@@ -14,9 +15,18 @@ export function parseChannelInput(body: Fields): ChannelInput {return { input: requiredString(body, 'input', 'Enter a channel URL, @handle or channel ID') };}+// people paste "u/name", "/u/name", "@name" or the address of their profile; only the name is kept+export function normalizeRedditUsername(input: string): string {+ const text = input.trim();+ const fromUrl = text.match(/reddit\.com\/(?:user|u)\/([^/?#\s]+)/i);+ return (fromUrl ? fromUrl[1] : text).replace(/^\/?u\//i, '').replace(/^@/, '').trim();+}+export function parseRedditAccount(body: Fields): RedditAccountInput {+ const username = normalizeRedditUsername(requiredString(body, 'username', 'Enter the Reddit username, such as u/name'));+ if (!username) throw new ValidationError('Enter the Reddit username, such as u/name');return {- username: requiredString(body, 'username', 'Username is required'),+ username,cookie: requiredString(body, 'cookie', 'Session cookie is required - open reddit.com in your browser, copy the reddit_session value from DevTools (Application > Cookies > reddit.com), and paste it here'),};}