Skip to content

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(api)!: remove the Reddit analytics routes

spacesdrivebc047b15c5af
diff --git a/backend/container.ts b/backend/container.ts
index 7105718..918997b 100644
--- a/backend/container.ts
+++ b/backend/container.ts
@@ -14,7 +14,6 @@ import { createHealthRepository } from './repositories/healthRepository.ts';
import { createAccountService } from './services/accountService.ts';
import { createYouTubeAnalyticsService } from './services/youtubeAnalyticsService.ts';
import { createInstagramAnalyticsService } from './services/instagramAnalyticsService.ts';
-import { createRedditAnalyticsService } from './services/redditAnalyticsService.ts';
import { createPublishingService } from './services/publishingService.ts';
import { createScheduledPublisher } from './services/scheduledPublisher.ts';
import { createScheduledPostService } from './services/scheduledPostService.ts';
@@ -36,7 +35,6 @@ import type { AccountService } from './services/accountService.ts';
import type { HealthService } from './services/healthService.ts';
import type { YouTubeAnalyticsService } from './services/youtubeAnalyticsService.ts';
import type { InstagramAnalyticsService } from './services/instagramAnalyticsService.ts';
-import type { RedditAnalyticsService } from './services/redditAnalyticsService.ts';
import type { PublishingService } from './services/publishingService.ts';
import type { ScheduledPublisher } from './services/scheduledPublisher.ts';
import type { ScheduledPostService } from './services/scheduledPostService.ts';
@@ -68,7 +66,6 @@ export interface Services {
accounts: AccountService;
youtubeAnalytics: YouTubeAnalyticsService;
instagramAnalytics: InstagramAnalyticsService;
- redditAnalytics: RedditAnalyticsService;
publishing: PublishingService;
scheduledPosts: ScheduledPostService;
scheduledPublisher: ScheduledPublisher;
@@ -107,7 +104,6 @@ export function createServices(clients: Clients, config: AppConfig, context: Req
accounts: createAccountService({ accounts, cache, audit, config, context }),
youtubeAnalytics: createYouTubeAnalyticsService({ accounts, cache, config, context }),
instagramAnalytics: createInstagramAnalyticsService({ accounts, cache, context }),
- redditAnalytics: createRedditAnalyticsService({ accounts, cache, context }),
publishing: createPublishingService({ accounts, config, context }),
scheduledPosts: createScheduledPostService({ accounts, posts, publisher: scheduledPublisher }),
scheduledPublisher,
diff --git a/backend/controllers/redditController.ts b/backend/controllers/redditController.ts
deleted file mode 100644
index ede55e0..0000000
--- a/backend/controllers/redditController.ts
+++ /dev/null
@@ -1,14 +0,0 @@
-import { controller, pathParam, servicesFor } from './http.ts';
-import { queryParams } from '../middleware/validate.ts';
-import { parseFreshQuery } from '../validators/analyticsValidators.ts';
-import type { RedditCacheData, RedditPost } from '../../shared/types/reddit.ts';
-
-export const getRedditAnalytics = controller.createHandlers(queryParams(parseFreshQuery), async c => {
- const result = await servicesFor(c).redditAnalytics.analytics(c.get('userId'), pathParam(c, 'id'), c.req.valid('query'));
- return c.json(result satisfies RedditCacheData);
-});
-
-export const getRedditPosts = controller.createHandlers(async c => {
- const posts = await servicesFor(c).redditAnalytics.posts(c.get('userId'), pathParam(c, 'id'));
- return c.json(posts satisfies RedditPost[]);
-});
diff --git a/backend/routes/index.ts b/backend/routes/index.ts
index a2fb22e..ae29cd4 100644
--- a/backend/routes/index.ts
+++ b/backend/routes/index.ts
@@ -7,7 +7,6 @@ import instagramAuthRoutes from './instagramAuthRoutes.ts';
import instagramWebhookRoutes from './instagramWebhookRoutes.ts';
import overviewRoutes from './overviewRoutes.ts';
import publishingRoutes from './publishingRoutes.ts';
-import redditRoutes from './redditRoutes.ts';
import scheduledPostRoutes from './scheduledPostRoutes.ts';
import settingsRoutes from './settingsRoutes.ts';
import trackedContentRoutes from './trackedContentRoutes.ts';
@@ -29,7 +28,7 @@ publicRoutes.route('/', instagramWebhookRoutes);
// mounted behind requireAuth and the per-user rate limits in app.ts
export const protectedRoutes = new Hono<AppEnv>();
for (const routes of [
- accountRoutes, analyticsRoutes, redditRoutes, publishingRoutes, scheduledPostRoutes, trackedContentRoutes,
+ accountRoutes, analyticsRoutes, publishingRoutes, scheduledPostRoutes, trackedContentRoutes,
overviewRoutes, settingsRoutes, instagramAuthRoutes, automationRoutes,
]) {
protectedRoutes.route('/', routes);
diff --git a/backend/routes/redditRoutes.ts b/backend/routes/redditRoutes.ts
deleted file mode 100644
index 5f85d8e..0000000
--- a/backend/routes/redditRoutes.ts
+++ /dev/null
@@ -1,10 +0,0 @@
-import { Hono } from 'hono';
-import { getRedditAnalytics, getRedditPosts } from '../controllers/redditController.ts';
-import type { AppEnv } from '../types/app.ts';
-
-const router = new Hono<AppEnv>();
-
-router.get('/accounts/:id/reddit-analytics', ...getRedditAnalytics);
-router.get('/accounts/:id/reddit-posts', ...getRedditPosts);
-
-export default router;
diff --git a/backend/services/redditAnalyticsService.ts b/backend/services/redditAnalyticsService.ts
deleted file mode 100644
index 4a12ab7..0000000
--- a/backend/services/redditAnalyticsService.ts
+++ /dev/null
@@ -1,70 +0,0 @@
-import { fetchRedditProfile, fetchRedditPosts } from './reddit/redditApi.ts';
-import { computeRedditAnalytics } from './reddit/redditAnalytics.ts';
-import { NO_COOKIE_MESSAGE } from './accountService.ts';
-import { NotFoundError, ValidationError } from '../lib/errors.ts';
-import type { AccountRepository } from '../repositories/accountRepository.ts';
-import type { AnalyticsCacheRepository } from '../repositories/analyticsCacheRepository.ts';
-import type { RequestContext } from '../types/context.ts';
-import type { RedditCacheData, RedditPost } from '../../shared/types/reddit.ts';
-
-export interface RedditAnalyticsDeps {
- accounts: AccountRepository;
- cache: AnalyticsCacheRepository;
- context: RequestContext;
-}
-
-export interface RedditAnalyticsService {
- analytics(userId: string, accountId: string, options?: { fresh?: boolean }): Promise<RedditCacheData>;
- posts(userId: string, accountId: string): Promise<RedditPost[]>;
-}
-
-interface RedditCredentials {
- username: string;
- cookie: string;
-}
-
-const POST_LIMIT = 100;
-
-export function createRedditAnalyticsService({ accounts, cache, context }: RedditAnalyticsDeps): RedditAnalyticsService {
- const { signal } = context;
-
- async function credentialsFor(userId: string, accountId: string): Promise<RedditCredentials> {
- const account = await accounts.findForUser(accountId, userId);
- if (!account || account.platform !== 'reddit') throw new NotFoundError('Reddit account not found');
- if (!account.cookie) throw new ValidationError(NO_COOKIE_MESSAGE);
- if (!account.username) throw new ValidationError('No Reddit username stored - delete this account and add it again');
- return { username: account.username, cookie: account.cookie };
- }
-
- return {
- // a fresh load is what a person's Refresh asks for; without it the cache answers and its age is shown
- async analytics(userId, accountId, { fresh = false } = {}) {
- const { username, cookie } = await credentialsFor(userId, accountId);
- if (!fresh) {
- const cached = await cache.getReddit(userId, accountId);
- if (cached) return cached;
- }
-
- const [profile, posts] = await Promise.all([
- fetchRedditProfile(username, cookie, signal),
- fetchRedditPosts(username, cookie, POST_LIMIT, signal),
- ]);
- const result: RedditCacheData = {
- profile,
- analytics: computeRedditAnalytics(profile, posts),
- posts,
- fetchedAt: new Date().toISOString(),
- };
- await cache.setReddit(userId, accountId, result);
- return result;
- },
-
- // the post list alone is not cached; only a full analytics load writes the cache
- async posts(userId, accountId) {
- const { username, cookie } = await credentialsFor(userId, accountId);
- const cached = await cache.getReddit(userId, accountId);
- if (cached?.posts) return cached.posts;
- return fetchRedditPosts(username, cookie, POST_LIMIT, signal);
- },
- };
-}
diff --git a/backend/tests/routes/index.test.ts b/backend/tests/routes/index.test.ts
new file mode 100644
index 0000000..7c55be4
--- /dev/null
+++ b/backend/tests/routes/index.test.ts
@@ -0,0 +1,23 @@
+import { test, describe } from 'node:test';
+import assert from 'node:assert/strict';
+import { protectedRoutes } from '../../routes/index.ts';
+import { fakeSupabase } from '../fixtures/fakeSupabase.ts';
+import { mountWithContext, callRoute } from '../fixtures/routeHarness.ts';
+
+async function call(method: string, path: string, json?: unknown) {
+ const app = mountWithContext(app => app.route('/', protectedRoutes), { supabase: fakeSupabase([]), redis: null, userId: 'user-1' });
+ const response = await callRoute(app, method, path, { json });
+ return { status: response.status, body: JSON.parse(response.body) as unknown };
+}
+
+function notFound(method: string, path: string) {
+ return { status: 404, body: { success: false, code: 'NOT_FOUND', message: `No API route matches ${method} ${path}`, requestId: 'req-test' } };
+}
+
+describe('The protected route table', () => {
+ test('no longer answers the removed Reddit analytics routes', async () => {
+ for (const path of ['/accounts/acc-1/reddit-analytics', '/accounts/acc-1/reddit-posts']) {
+ assert.deepEqual(await call('GET', path), notFound('GET', path));
+ }
+ });
+});
diff --git a/backend/tests/routes/redditRoutes.test.ts b/backend/tests/routes/redditRoutes.test.ts
deleted file mode 100644
index 1132326..0000000
--- a/backend/tests/routes/redditRoutes.test.ts
+++ /dev/null
@@ -1,90 +0,0 @@
-import { test, describe, afterEach, mock } from 'node:test';
-import assert from 'node:assert/strict';
-import redditRoutes from '../../routes/redditRoutes.ts';
-import { installFetch, reply } from '../fixtures/fakeFetch.ts';
-import type { FetchMock } from '../fixtures/fakeFetch.ts';
-import { fakeSupabase, accountRow } from '../fixtures/fakeSupabase.ts';
-import { mountWithContext, callRoute } from '../fixtures/routeHarness.ts';
-
-const ACCOUNTS = [
- accountRow({ id: 'rd1', platform: 'reddit', username: 'ribbon', cookie: 'COOKIE' }),
- accountRow({ id: 'rdNoCookie', platform: 'reddit', username: 'nocookie' }),
- accountRow({ id: 'yt1', platform: 'youtube', channelId: 'UC1' }),
-];
-
-const CACHED = { profile: { username: 'ribbon' }, analytics: { fetchedPosts: 1 }, posts: [{ id: 'p1' }] };
-
-function storeRedis(store: Record<string, unknown>) {
- const writes: string[] = [];
- return {
- writes,
- async get(key: string) { return store[key] ?? null; },
- async set(key: string, value: unknown) { writes.push(key); store[key] = value; },
- };
-}
-
-let fetchMock: FetchMock | null = null;
-afterEach(() => {
- fetchMock?.restore();
- mock.restoreAll();
-});
-
-async function call(path: string, redis: unknown) {
- mock.method(console, 'warn', () => undefined);
- const app = mountWithContext(app => app.route('/', redditRoutes), { supabase: fakeSupabase(ACCOUNTS), redis, userId: 'user-1' });
- const response = await callRoute(app, 'GET', path);
- return { status: response.status, body: JSON.parse(response.body) as unknown };
-}
-
-describe('Reddit analytics routes', () => {
- test('refuse other platforms and accounts without a session cookie, before any Reddit call', async () => {
- fetchMock = installFetch(() => reply({}));
- const failure = (code: string, message: string) => ({ success: false, code, message, requestId: 'req-test' });
- assert.deepEqual(await call('/accounts/yt1/reddit-analytics', null), { status: 404, body: failure('NOT_FOUND', 'Reddit account not found') });
- assert.deepEqual(await call('/accounts/rdNoCookie/reddit-posts', null), {
- status: 400, body: failure('VALIDATION_ERROR', 'No session cookie stored - re-add this account with a session cookie from reddit.com DevTools'),
- });
- assert.deepEqual(fetchMock.requests, []);
- });
-
- test('serve the cache when it has the account', async () => {
- fetchMock = installFetch(() => reply({}));
- const redis = storeRedis({ 'reddit:user-1:rd1': CACHED });
- assert.deepEqual(await call('/accounts/rd1/reddit-analytics', redis), { status: 200, body: CACHED });
- assert.deepEqual(await call('/accounts/rd1/reddit-posts', redis), { status: 200, body: CACHED.posts });
- assert.deepEqual(fetchMock.requests, []);
- });
-
- test('fetch live analytics with the stored cookie and cache them', async () => {
- fetchMock = installFetch(request => (request.url.includes('/about.json')
- ? reply({ data: { name: 'ribbon', total_karma: 5 } })
- : reply({ data: { children: [], after: null } })));
- const redis = storeRedis({});
- const { status, body } = await call('/accounts/rd1/reddit-analytics', redis);
- assert.equal(status, 200);
- assert.equal((body as { profile: { totalKarma: number } }).profile.totalKarma, 5);
- assert.deepEqual(redis.writes, ['reddit:user-1:rd1']);
- assert.deepEqual(fetchMock.requests.map(request => (request.headers as Record<string, string>).Cookie), ['reddit_session=COOKIE', 'reddit_session=COOKIE']);
- });
-
- test('a fresh load skips the cache, fetches from Reddit and records when it fetched', async () => {
- fetchMock = installFetch(request => (request.url.includes('/about.json')
- ? reply({ data: { name: 'ribbon', total_karma: 9 } })
- : reply({ data: { children: [], after: null } })));
- const redis = storeRedis({ 'reddit:user-1:rd1': CACHED });
- const { status, body } = await call('/accounts/rd1/reddit-analytics?fresh=1', redis);
- assert.equal(status, 200);
- const fresh = body as { profile: { totalKarma: number }; fetchedAt: string };
- assert.equal(fresh.profile.totalKarma, 9);
- assert.ok(!Number.isNaN(Date.parse(fresh.fetchedAt)));
- assert.deepEqual(redis.writes, ['reddit:user-1:rd1']);
- assert.equal(fetchMock.requests.length, 2);
- });
-
- test('a fresh value other than 1 is refused before any Reddit call', async () => {
- fetchMock = installFetch(() => reply({}));
- const { status } = await call('/accounts/rd1/reddit-analytics?fresh=yes', storeRedis({}));
- assert.equal(status, 400);
- assert.deepEqual(fetchMock.requests, []);
- });
-});
diff --git a/backend/validators/analyticsValidators.ts b/backend/validators/analyticsValidators.ts
index 1b7206a..d32e1d5 100644
--- a/backend/validators/analyticsValidators.ts
+++ b/backend/validators/analyticsValidators.ts
@@ -15,7 +15,7 @@ export interface FreshQuery {
fresh: boolean;
}
-// ?fresh=1 on a cached read (reddit-analytics, videos, ig-media) skips the cache, for a person's explicit refresh
+// ?fresh=1 on a cached read (videos, ig-media) skips the cache, for a person's explicit refresh
export function parseFreshQuery(query: Fields): FreshQuery {
const fresh = optionalString(query, 'fresh');
if (fresh && fresh !== '1') throw new ValidationError('fresh must be 1 when given');