From b30cf4d7144631b4d757fc6ecd89c0527061520c Mon Sep 17 00:00:00 2001 From: Reptapog Date: Fri, 11 Aug 2023 23:07:52 -0700 Subject: [PATCH 1/4] Added parse to board activity feed endpoint --- server/feeds/routes.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server/feeds/routes.ts b/server/feeds/routes.ts index fc096d58..96c0d7a2 100644 --- a/server/feeds/routes.ts +++ b/server/feeds/routes.ts @@ -10,6 +10,9 @@ import { } from "./queries"; import { Feed } from "types/rest/threads"; +import { + FeedActivitySchema +} from "types/open-api/generated/schemas"; import { ZodFeed } from "types/rest/zodthreads"; import debug from "debug"; import { ensureBoardAccess } from "handlers/permissions"; @@ -113,7 +116,7 @@ router.get("/boards/:board_id", ensureBoardAccess, async (req, res) => { log( `Returning board activity data for board ${boardExternalId} for user ${req.currentUser?.uid}.` ); - res.status(200).json(response); + res.status(200).json(FeedActivitySchema.parse(response)); }); /** @@ -180,6 +183,8 @@ router.get("/users/@me", ensureLoggedIn, async (req, res) => { res.status(200).json(response); }); +// TODO: star feed needs 4xx response + /** * @openapi * /feeds/users/@me/stars: From 87d06b440b4c8df00d1c1ab81c20864648ef1e5a Mon Sep 17 00:00:00 2001 From: Reptapog <18347043+Reptapog@users.noreply.github.com> Date: Sat, 12 Aug 2023 01:25:06 -0700 Subject: [PATCH 2/4] Added parent_realm_id and parent_realm_slug to api to fix REST feed tests --- types/open-api/generated/schemas.ts | 2 ++ types/open-api/thread.yaml | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/types/open-api/generated/schemas.ts b/types/open-api/generated/schemas.ts index c48eb588..29bfd8c6 100644 --- a/types/open-api/generated/schemas.ts +++ b/types/open-api/generated/schemas.ts @@ -134,6 +134,8 @@ const ThreadSummary = z id: z.string().uuid(), parent_board_slug: z.string(), parent_board_id: z.string(), + parent_realm_slug: z.string(), + parent_realm_id: z.string(), starter: Contribution, default_view: z.enum(["thread", "gallery", "timeline"]), new: z.boolean(), diff --git a/types/open-api/thread.yaml b/types/open-api/thread.yaml index 1fcfb294..46fe6390 100644 --- a/types/open-api/thread.yaml +++ b/types/open-api/thread.yaml @@ -35,6 +35,10 @@ components: type: string parent_board_id: type: string + parent_realm_slug: + type: string + parent_realm_id: + type: string starter: description: "The contribution that starts the thread." $ref: "#/components/schemas/Contribution" @@ -57,6 +61,8 @@ components: - id - parent_board_slug - parent_board_id + - parent_realm_slug + - parent_realm_id - starter - default_view - new From eeff6b0e5fcdb407fa722e574fa73aa95b99fab2 Mon Sep 17 00:00:00 2001 From: Reptapog <18347043+Reptapog@users.noreply.github.com> Date: Sat, 12 Aug 2023 16:12:58 -0700 Subject: [PATCH 3/4] fixing up api examples --- server/boards/examples/create-thread.yaml | 2 ++ server/feeds/examples/feed-cursor.yaml | 20 +++++++++++++++++++ server/feeds/examples/feed-gore.yaml | 6 ++++++ .../examples/threads-get-response.yaml | 2 ++ types/open-api/generated/schemas.ts | 12 ++++------- types/open-api/generated/types.ts | 3 +++ 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/server/boards/examples/create-thread.yaml b/server/boards/examples/create-thread.yaml index b2129d97..4151d665 100644 --- a/server/boards/examples/create-thread.yaml +++ b/server/boards/examples/create-thread.yaml @@ -21,6 +21,8 @@ components: id: 60634506-6ab1-4083-9867-905cef85cef6 parent_board_slug: gore parent_board_id: c6d3d10e-8e49-4d73-b28a-9d652b41beec + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' starter: id: 30f7b263-2406-495a-8ab7-4bd559738510 parent_thread_id: 60634506-6ab1-4083-9867-905cef85cef6 diff --git a/server/feeds/examples/feed-cursor.yaml b/server/feeds/examples/feed-cursor.yaml index 475aeb43..cf676dc0 100644 --- a/server/feeds/examples/feed-cursor.yaml +++ b/server/feeds/examples/feed-cursor.yaml @@ -30,6 +30,8 @@ components: id: 78d98a6a-c24b-4b7a-8e06-4a30862afa2d parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -65,6 +67,8 @@ components: id: c5004e2b-4358-43df-8078-905b608e9ceb parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -100,6 +104,8 @@ components: id: 8d0be581-5cc7-47c1-947c-f8242898682d parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -135,6 +141,8 @@ components: id: 1c92ab43-4309-454f-be27-d85d3fd78808 parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -170,6 +178,8 @@ components: id: 3e9a317f-2bcd-4db3-abbb-619fa7c03f24 parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -205,6 +215,8 @@ components: id: 7006e0dc-7c7b-4cc5-8c08-61362e8d288b parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -240,6 +252,8 @@ components: id: 72470830-7984-4bf6-a0bf-dc5af7d4779b parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -275,6 +289,8 @@ components: id: 06ba998d-82d8-4e0b-8bbb-a3b3455c4167 parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -310,6 +326,8 @@ components: id: 8d227983-7649-49c9-9fa4-b3c398f4648c parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 @@ -345,6 +363,8 @@ components: id: bcbde425-7f84-4e68-acd5-65aaae95714c parent_board_slug: long parent_board_id: db8dc5b3-5b4a-4bfe-a303-e176c9b00b83 + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 diff --git a/server/feeds/examples/feed-gore.yaml b/server/feeds/examples/feed-gore.yaml index bae75dcd..c38018e3 100644 --- a/server/feeds/examples/feed-gore.yaml +++ b/server/feeds/examples/feed-gore.yaml @@ -33,6 +33,8 @@ components: id: 8b2646af-2778-487e-8e44-7ae530c2549c parent_board_slug: gore parent_board_id: c6d3d10e-8e49-4d73-b28a-9d652b41beec + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 2 @@ -71,6 +73,8 @@ components: id: 29d1b2da-3289-454a-9089-2ed47db4967b parent_board_slug: gore parent_board_id: c6d3d10e-8e49-4d73-b28a-9d652b41beec + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 2 @@ -109,6 +113,8 @@ components: id: a5c903df-35e8-43b2-a41a-208c43154671 parent_board_slug: gore parent_board_id: c6d3d10e-8e49-4d73-b28a-9d652b41beec + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' new_posts_amount: 0 new_comments_amount: 0 total_comments_amount: 0 diff --git a/server/threads/examples/threads-get-response.yaml b/server/threads/examples/threads-get-response.yaml index 9c2f1a40..2f781180 100644 --- a/server/threads/examples/threads-get-response.yaml +++ b/server/threads/examples/threads-get-response.yaml @@ -6,6 +6,8 @@ components: id: 8b2646af-2778-487e-8e44-7ae530c2549c parent_board_slug: gore parent_board_id: c6d3d10e-8e49-4d73-b28a-9d652b41beec + parent_realm_slug: 'twisted-minds' + parent_realm_id: '76ef4cc3-1603-4278-95d7-99c59f481d2e' starter: id: ff9f2ae2-a254-4069-9791-3ac5e6dff5bb parent_thread_id: 8b2646af-2778-487e-8e44-7ae530c2549c diff --git a/types/open-api/generated/schemas.ts b/types/open-api/generated/schemas.ts index 29bfd8c6..b7ca2141 100644 --- a/types/open-api/generated/schemas.ts +++ b/types/open-api/generated/schemas.ts @@ -70,7 +70,6 @@ const CreateThread = z.object({ content: z.string(), forceAnonymous: z.union([z.boolean(), z.null()]).optional(), defaultView: z.enum(["thread", "gallery", "timeline"]).optional(), - large: z.union([z.boolean(), z.null()]).optional(), identityId: z.string().uuid().optional(), accessoryId: z.string().uuid().optional(), whisper_tags: z.array(z.string()).optional(), @@ -170,6 +169,7 @@ const IdentityParams = z .object({ accessory_id: z.union([z.string(), z.null()]), identity_id: z.union([z.string(), z.null()]), + forceAnonymous: z.union([z.boolean(), z.null()]), }) .partial(); const postContribution_Body = z @@ -177,13 +177,8 @@ const postContribution_Body = z .partial() .and(Tags) .and(IdentityParams); -const postComment_Body = z - .object({ - contents: z.array(Comment), - reply_to_comment_id: z.union([z.string(), z.null()]), - }) - .partial() - .and(IdentityParams); +const CommentRequestBody = z.object({ contents: z.array(z.string()) }); +const postComment_Body = CommentRequestBody.and(IdentityParams); const BaseBlock = z.object({ id: z.string().uuid(), index: z.number(), @@ -367,6 +362,7 @@ export const CursorSchema = Cursor; export const FeedActivitySchema = FeedActivity; export const IdentityParamsSchema = IdentityParams; export const postContribution_BodySchema = postContribution_Body; +export const CommentRequestBodySchema = CommentRequestBody; export const postComment_BodySchema = postComment_Body; export const BaseBlockSchema = BaseBlock; export const TextBlockSchema = TextBlock; diff --git a/types/open-api/generated/types.ts b/types/open-api/generated/types.ts index 70187224..bee4d389 100644 --- a/types/open-api/generated/types.ts +++ b/types/open-api/generated/types.ts @@ -40,6 +40,9 @@ export type IdentityParams = z.infer; export type postContribution_Body = z.infer< typeof schemas.postContribution_BodySchema >; +export type CommentRequestBody = z.infer< + typeof schemas.CommentRequestBodySchema +>; export type postComment_Body = z.infer; export type BaseBlock = z.infer; export type TextBlock = z.infer; From 4f6bef44677fc673108ad7c408b2d4c8499da0ce Mon Sep 17 00:00:00 2001 From: Reptapog <18347043+Reptapog@users.noreply.github.com> Date: Mon, 14 Aug 2023 17:04:48 -0700 Subject: [PATCH 4/4] zod feed cleanup --- handlers/permissions.ts | 3 +-- server/feeds/queries.ts | 3 +-- server/feeds/tests/queries.test.ts | 18 +++++++++--------- server/feeds/tests/user-feed-tags.test.ts | 6 +++--- server/threads/queries.ts | 3 +-- utils/response-utils.ts | 5 ++--- server/feeds/sql/zodtypes.ts => zodtypes.ts | 0 7 files changed, 17 insertions(+), 21 deletions(-) rename server/feeds/sql/zodtypes.ts => zodtypes.ts (100%) diff --git a/handlers/permissions.ts b/handlers/permissions.ts index 9c634db6..a44a6644 100644 --- a/handlers/permissions.ts +++ b/handlers/permissions.ts @@ -23,9 +23,8 @@ import { } from "server/threads/queries"; import { BoardByExternalId } from "server/boards/sql/types"; -import { DbThreadType } from "Types"; import { Internal500Error } from "types/errors/api"; -import { ZodDbThreadType } from "server/feeds/sql/zodtypes"; +import { ZodDbThreadType } from "zodtypes"; import { getBoardByExternalId } from "server/boards/queries"; import { getPostByExternalId } from "server/posts/queries"; diff --git a/server/feeds/queries.ts b/server/feeds/queries.ts index f8777016..56008fa3 100644 --- a/server/feeds/queries.ts +++ b/server/feeds/queries.ts @@ -1,7 +1,6 @@ import { decodeCursor, encodeCursor } from "utils/queries-utils"; -import { DbFeedType } from "Types"; -import { ZodDbFeedType } from "server/feeds/sql/zodtypes"; +import { ZodDbFeedType } from "zodtypes"; import debug from "debug"; import pool from "server/db-pool"; import sql from "./sql"; diff --git a/server/feeds/tests/queries.test.ts b/server/feeds/tests/queries.test.ts index 465896dc..40ec77b7 100644 --- a/server/feeds/tests/queries.test.ts +++ b/server/feeds/tests/queries.test.ts @@ -1,8 +1,8 @@ import { BOBATAN_USER_ID, ONCEST_USER_ID } from "test/data/auth"; import { FAVORITE_CHARACTER_THREAD_ID, FAVORITE_MURDER_THREAD_ID } from "test/data/threads"; -import { DbFeedType } from "Types"; import { TWISTED_MINDS_REALM_EXTERNAL_ID } from "test/data/realms"; +import { ZodDbFeedType } from "zodtypes"; import { extractActivity } from "utils/test-utils"; import { getUserActivity } from "../queries"; import { getUserFromFirebaseId } from "../../users/queries"; @@ -30,7 +30,7 @@ describe("feed activity queries", () => { cursor: null, updatedOnly: true, ownOnly: true, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.activity.map(extractActivity)).toEqual([ { @@ -56,7 +56,7 @@ describe("feed activity queries", () => { cursor: null, updatedOnly: false, ownOnly: true, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.activity.map(extractActivity)).toEqual([ { @@ -95,7 +95,7 @@ describe("feed activity queries", () => { cursor: null, updatedOnly: true, ownOnly: false, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.activity.map(extractActivity)).toEqual([ { @@ -121,7 +121,7 @@ describe("feed activity queries", () => { cursor: null, updatedOnly: false, ownOnly: false, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.activity.map(extractActivity)).toEqual([ { @@ -174,7 +174,7 @@ describe("feed activity queries", () => { updatedOnly: false, ownOnly: false, pageSize: 1, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.cursor).toBe( "eyJsYXN0X2FjdGl2aXR5X2N1cnNvciI6IjIwMjAtMDUtMjNUMDU6NTI6MDAuMDAwMDAwIiwicGFnZV9zaXplIjoxfQ==" @@ -205,7 +205,7 @@ describe("feed activity queries", () => { updatedOnly: false, ownOnly: false, pageSize: 1, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.activity.map(extractActivity)).toEqual([ { @@ -233,7 +233,7 @@ describe("feed activity queries", () => { updatedOnly: false, ownOnly: false, pageSize: 10, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.cursor).toBe(null); expect(feed.activity.length).toEqual(3); @@ -264,7 +264,7 @@ describe("feed activity queries", () => { updatedOnly: true, ownOnly: false, pageSize: 10, - })) as DbFeedType; + })) as ZodDbFeedType; expect(feed.cursor).toBe(null); // Ensure that the post in !long with the dismissed board notifications diff --git a/server/feeds/tests/user-feed-tags.test.ts b/server/feeds/tests/user-feed-tags.test.ts index 8346e4df..dbce4982 100644 --- a/server/feeds/tests/user-feed-tags.test.ts +++ b/server/feeds/tests/user-feed-tags.test.ts @@ -1,11 +1,11 @@ -import { DbFeedType, DbThreadSummaryType } from "Types"; import { FAVORITE_CHARACTER_THREAD_ID, FAVORITE_MURDER_THREAD_ID } from "test/data/threads"; +import { ZodDbFeedType, ZodDbThreadSummaryType } from "zodtypes"; import { BOBATAN_USER_ID } from "test/data/auth"; import { TWISTED_MINDS_REALM_EXTERNAL_ID } from "test/data/realms"; import { getUserActivity } from "../queries"; -export const extractTags = (thread: DbThreadSummaryType | null | undefined) => { +export const extractTags = (thread: ZodDbThreadSummaryType | null | undefined) => { if (!thread) { throw new Error("Extracting tags from null thread"); } @@ -20,7 +20,7 @@ export const extractTags = (thread: DbThreadSummaryType | null | undefined) => { export const getThreadFromActivity = ( threadExternalId: string, - activity: DbFeedType + activity: ZodDbFeedType ) => { return activity.activity.find((thread) => thread.thread_id == threadExternalId); }; diff --git a/server/threads/queries.ts b/server/threads/queries.ts index 4701ed9e..f49a45b5 100644 --- a/server/threads/queries.ts +++ b/server/threads/queries.ts @@ -1,7 +1,6 @@ import { THREAD_OWNER_PERMISSIONS, ThreadPermissions } from "types/permissions"; -import { DbThreadType } from "Types"; -import { ZodDbThreadType } from "server/feeds/sql/zodtypes"; +import { ZodDbThreadType } from "zodtypes"; import debug from "debug"; import { extractThreadPermissions } from "utils/permissions-utils"; import { getBoardByExternalId } from "../boards/queries"; diff --git a/utils/response-utils.ts b/utils/response-utils.ts index 6fb008de..b14c28c4 100644 --- a/utils/response-utils.ts +++ b/utils/response-utils.ts @@ -2,7 +2,7 @@ import { BoardMetadata, LoggedInBoardMetadata, } from "types/open-api/generated/types"; -import { Comment, Post, Thread, ThreadSummary } from "types/rest/threads"; +import { Comment, Post, Thread } from "types/rest/threads"; // TODO: deprecate these import { DbBoardCategoryDescription, @@ -10,12 +10,11 @@ import { DbCommentType, DbPostType, DbThreadSummaryType, - DbThreadType, } from "Types"; import { ZodDbThreadSummaryType, ZodDbThreadType, -} from "server/feeds/sql/zodtypes"; +} from "zodtypes"; import { BoardByExternalId } from "server/boards/sql/types"; import { BoardRestrictions } from "types/permissions"; diff --git a/server/feeds/sql/zodtypes.ts b/zodtypes.ts similarity index 100% rename from server/feeds/sql/zodtypes.ts rename to zodtypes.ts