Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zod parse feed #147

Merged
merged 4 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions handlers/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
2 changes: 2 additions & 0 deletions server/boards/examples/create-thread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions server/feeds/examples/feed-cursor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions server/feeds/examples/feed-gore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions server/feeds/queries.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
7 changes: 6 additions & 1 deletion server/feeds/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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));
});

/**
Expand Down Expand Up @@ -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:
Expand Down
18 changes: 9 additions & 9 deletions server/feeds/tests/queries.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -30,7 +30,7 @@ describe("feed activity queries", () => {
cursor: null,
updatedOnly: true,
ownOnly: true,
})) as DbFeedType;
})) as ZodDbFeedType;

expect(feed.activity.map(extractActivity)).toEqual([
{
Expand All @@ -56,7 +56,7 @@ describe("feed activity queries", () => {
cursor: null,
updatedOnly: false,
ownOnly: true,
})) as DbFeedType;
})) as ZodDbFeedType;

expect(feed.activity.map(extractActivity)).toEqual([
{
Expand Down Expand Up @@ -95,7 +95,7 @@ describe("feed activity queries", () => {
cursor: null,
updatedOnly: true,
ownOnly: false,
})) as DbFeedType;
})) as ZodDbFeedType;

expect(feed.activity.map(extractActivity)).toEqual([
{
Expand All @@ -121,7 +121,7 @@ describe("feed activity queries", () => {
cursor: null,
updatedOnly: false,
ownOnly: false,
})) as DbFeedType;
})) as ZodDbFeedType;

expect(feed.activity.map(extractActivity)).toEqual([
{
Expand Down Expand Up @@ -174,7 +174,7 @@ describe("feed activity queries", () => {
updatedOnly: false,
ownOnly: false,
pageSize: 1,
})) as DbFeedType;
})) as ZodDbFeedType;

expect(feed.cursor).toBe(
"eyJsYXN0X2FjdGl2aXR5X2N1cnNvciI6IjIwMjAtMDUtMjNUMDU6NTI6MDAuMDAwMDAwIiwicGFnZV9zaXplIjoxfQ=="
Expand Down Expand Up @@ -205,7 +205,7 @@ describe("feed activity queries", () => {
updatedOnly: false,
ownOnly: false,
pageSize: 1,
})) as DbFeedType;
})) as ZodDbFeedType;

expect(feed.activity.map(extractActivity)).toEqual([
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions server/feeds/tests/user-feed-tags.test.ts
Original file line number Diff line number Diff line change
@@ -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");
}
Expand All @@ -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);
};
Expand Down
2 changes: 2 additions & 0 deletions server/threads/examples/threads-get-response.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions server/threads/queries.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
14 changes: 6 additions & 8 deletions types/open-api/generated/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -134,6 +133,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(),
Expand Down Expand Up @@ -168,20 +169,16 @@ 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
.object({ content: z.string() })
.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(),
Expand Down Expand Up @@ -365,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;
Expand Down
3 changes: 3 additions & 0 deletions types/open-api/generated/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export type IdentityParams = z.infer<typeof schemas.IdentityParamsSchema>;
export type postContribution_Body = z.infer<
typeof schemas.postContribution_BodySchema
>;
export type CommentRequestBody = z.infer<
typeof schemas.CommentRequestBodySchema
>;
export type postComment_Body = z.infer<typeof schemas.postComment_BodySchema>;
export type BaseBlock = z.infer<typeof schemas.BaseBlockSchema>;
export type TextBlock = z.infer<typeof schemas.TextBlockSchema>;
Expand Down
6 changes: 6 additions & 0 deletions types/open-api/thread.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -57,6 +61,8 @@ components:
- id
- parent_board_slug
- parent_board_id
- parent_realm_slug
- parent_realm_id
Reptapog marked this conversation as resolved.
Show resolved Hide resolved
- starter
- default_view
- new
Expand Down
5 changes: 2 additions & 3 deletions utils/response-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ 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,
DbBoardTextDescription,
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";
Expand Down
File renamed without changes.
Loading