From 025c18a2892b133ccf81e0ede9359a80e5ec9452 Mon Sep 17 00:00:00 2001 From: rotorsoft Date: Fri, 11 Oct 2024 16:15:20 -0400 Subject: [PATCH] fix tests --- libs/model/src/feed/GetUserActivity.query.ts | 21 ++++++++++--- libs/schemas/src/queries/feed.schemas.ts | 8 ++--- .../integration/api/userDashboard.spec.ts | 30 +++++++++++-------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/libs/model/src/feed/GetUserActivity.query.ts b/libs/model/src/feed/GetUserActivity.query.ts index 3ba52fe73e2..0845006900c 100644 --- a/libs/model/src/feed/GetUserActivity.query.ts +++ b/libs/model/src/feed/GetUserActivity.query.ts @@ -1,5 +1,6 @@ import { Query } from '@hicommonwealth/core'; import * as schemas from '@hicommonwealth/schemas'; +import { models } from '../database'; import { GlobalActivityCache, getUserActivityFeed, @@ -10,13 +11,25 @@ export function GetUserActivity(): Query { ...schemas.ActivityFeed, auth: [], secure: false, - body: async ({ actor, payload }) => - payload.is_global + body: async ({ actor, payload }) => { + // ensure we have a user id when not global + const user_id = payload.is_global + ? 0 + : (actor.user?.id ?? + ( + await models.Address.findOne({ + where: { address: actor.address }, + attributes: ['user_id'], + }) + )?.user_id ?? + 0); + return payload.is_global ? await GlobalActivityCache.getInstance().getGlobalActivity() : await getUserActivityFeed({ - user_id: actor.user?.id ?? 0, + user_id, thread_limit: Math.min(payload.thread_limit ?? 50, 50), comment_limit: Math.min(payload.comment_limit ?? 3, 5), - }), + }); + }, }; } diff --git a/libs/schemas/src/queries/feed.schemas.ts b/libs/schemas/src/queries/feed.schemas.ts index 7c59bc97ebd..a76a6666f16 100644 --- a/libs/schemas/src/queries/feed.schemas.ts +++ b/libs/schemas/src/queries/feed.schemas.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; import { DiscordMetaSchema, PG_INT } from '../utils'; export const ActivityComment = z.object({ - id: PG_INT, + id: z.number(), address: z.string(), user_id: z.number().nullish(), profile_name: z.string().nullish(), @@ -18,8 +18,8 @@ export const ActivityComment = z.object({ export const ActivityThread = z.object({ community_id: z.string(), community_icon: z.string().nullish(), - id: PG_INT, - user_id: PG_INT, + id: z.number(), + user_id: z.number(), user_address: z.string(), profile_name: z.string().nullish(), profile_avatar: z.string().nullish(), @@ -38,7 +38,7 @@ export const ActivityThread = z.object({ has_poll: z.boolean().nullish(), discord_meta: DiscordMetaSchema.nullish(), topic: z.object({ - id: PG_INT, + id: z.number(), name: z.string(), description: z.string(), }), diff --git a/packages/commonwealth/test/integration/api/userDashboard.spec.ts b/packages/commonwealth/test/integration/api/userDashboard.spec.ts index 539a55dc0d2..ab24733b216 100644 --- a/packages/commonwealth/test/integration/api/userDashboard.spec.ts +++ b/packages/commonwealth/test/integration/api/userDashboard.spec.ts @@ -162,13 +162,17 @@ describe('User Dashboard API', () => { }); describe('/GetUserActivity', () => { - const apiUrl = '/api/internal/trpc/feed.getUserActivity'; + const queryArgs = { is_global: false }; + const apiUrl = + '/api/internal/trpc/feed.getUserActivity?input=' + + encodeURIComponent(JSON.stringify(queryArgs)); test('should fail without JWT', async () => { const res = await chai.request .agent(server.app) .get(apiUrl) .set('Accept', 'application/json') + .set('address', userAddress) .send({ chain }); expect(res).to.not.be.null; expect(res.error).to.not.be.null; @@ -179,14 +183,14 @@ describe('User Dashboard API', () => { .agent(server.app) .get(apiUrl) .set('Accept', 'application/json') + .set('address', userAddress) .send({ chain, jwt: userJWT }); expect(res.status).to.be.equal(200); - expect(res.body.status).to.be.equal('Success'); - expect(res.body).to.not.be.null; - expect(res.body.result).to.not.be.null; + expect(res.text).to.not.be.null; - const threadIds = res.body.result.map((a) => a.thread.id); + const resBody = JSON.parse(res.text); + const threadIds = resBody.result.data.map((a) => a.id); const chains = await server.models.Thread.findAll({ attributes: attributesOf('community_id'), where: { @@ -213,14 +217,14 @@ describe('User Dashboard API', () => { .agent(server.app) .get(apiUrl) .set('Accept', 'application/json') + .set('address', userAddress) .send({ chain, jwt: userJWT }); expect(res.status).to.be.equal(200); - expect(res.body.status).to.be.equal('Success'); - expect(res.body).to.not.be.null; - expect(res.body.result).to.not.be.null; + expect(res.text).to.not.be.null; - const threadIds = res.body.result.map((a) => a.thread.id); + const resBody = JSON.parse(res.text); + const threadIds = resBody.result.data.map((a) => a.id); const chains = await server.models.Thread.findAll({ attributes: attributesOf('community_id'), where: { @@ -261,14 +265,14 @@ describe('User Dashboard API', () => { .agent(server.app) .get(apiUrl) .set('Accept', 'application/json') + .set('address', userAddress) .send({ chain, jwt: userJWT }); expect(res.status).to.be.equal(200); - expect(res.body.status).to.be.equal('Success'); - expect(res.body).to.not.be.null; - expect(res.body.result).to.not.be.null; + expect(res.text).to.not.be.null; - const threadIds = res.body.result.map((a) => a.thread_id); + const resBody = JSON.parse(res.text); + const threadIds = resBody.result.data.map((a) => a.id); const chains = ( await server.models.Thread.findAll({ attributes: attributesOf('community_id'),