Skip to content

Commit

Permalink
[keyserver] Move validators to endpoint array
Browse files Browse the repository at this point in the history
Summary:
(ENG-3879)[https://linear.app/comm/issue/ENG-3879/move-output-and-input-validators-outside-of-the-responders]

Followup to the id schema work. Moving all the validation to the endpoint array makes it less likely for the devs to forgot to add it (which already happened a few times). Additionaly it moves the validation logic outside of the responder and now they are fully focused on the business logic.

An additional change that needed to be made was exporting the input validators, so they could be imported in the `endpoints.js`.

I choose to not split it into multiple diffs, because I'm doing the same thing everywhere, but if it's hard to review I can split into more diffs.

Test Plan:
- run `yarn flow-all` (note: for some reason flow allows wrong output responders, even they are correctly typed with generics)
- play around the app and check if everything works correctly (in particular I tested log in, register and siwe flows)

Reviewers: kamil, tomek

Reviewed By: tomek

Subscribers: ashoat

Differential Revision: https://phab.comm.dev/D8661
  • Loading branch information
MichalGniadek committed Aug 2, 2023
1 parent f5fdef7 commit 7492050
Show file tree
Hide file tree
Showing 22 changed files with 961 additions and 1,156 deletions.
724 changes: 478 additions & 246 deletions keyserver/src/endpoints.js

Large diffs are not rendered by default.

49 changes: 15 additions & 34 deletions keyserver/src/responders/activity-responders.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// @flow

import t from 'tcomb';
import type { TList } from 'tcomb';
import t, { type TInterface, type TList } from 'tcomb';

import {
type UpdateActivityResult,
type UpdateActivityRequest,
type SetThreadUnreadStatusRequest,
type SetThreadUnreadStatusResult,
type ActivityUpdate,
setThreadUnreadStatusResult,
updateActivityResultValidator,
} from 'lib/types/activity-types.js';
import { tShape, tID } from 'lib/utils/validation-utils.js';

Expand All @@ -19,7 +16,6 @@ import {
activityUpdater,
setThreadUnreadStatus,
} from '../updaters/activity-updaters.js';
import { validateInput, validateOutput } from '../utils/validation-utils.js';

const activityUpdatesInputValidator: TList<Array<ActivityUpdate>> = t.list(
tShape({
Expand All @@ -29,44 +25,29 @@ const activityUpdatesInputValidator: TList<Array<ActivityUpdate>> = t.list(
}),
);

const inputValidator = tShape<UpdateActivityRequest>({
updates: activityUpdatesInputValidator,
});
export const updateActivityResponderInputValidator: TInterface<UpdateActivityRequest> =
tShape<UpdateActivityRequest>({
updates: activityUpdatesInputValidator,
});

async function updateActivityResponder(
viewer: Viewer,
input: mixed,
request: UpdateActivityRequest,
): Promise<UpdateActivityResult> {
const request = await validateInput(viewer, inputValidator, input);
const result = await activityUpdater(viewer, request);
return validateOutput(
viewer.platformDetails,
updateActivityResultValidator,
result,
);
return await activityUpdater(viewer, request);
}

const setThreadUnreadStatusValidator = tShape<SetThreadUnreadStatusRequest>({
threadID: tID,
unread: t.Bool,
latestMessage: t.maybe(tID),
});
export const setThreadUnreadStatusValidator: TInterface<SetThreadUnreadStatusRequest> =
tShape<SetThreadUnreadStatusRequest>({
threadID: tID,
unread: t.Bool,
latestMessage: t.maybe(tID),
});
async function threadSetUnreadStatusResponder(
viewer: Viewer,
input: mixed,
request: SetThreadUnreadStatusRequest,
): Promise<SetThreadUnreadStatusResult> {
const request = await validateInput(
viewer,
setThreadUnreadStatusValidator,
input,
);

const result = await setThreadUnreadStatus(viewer, request);
return validateOutput(
viewer.platformDetails,
setThreadUnreadStatusResult,
result,
);
return await setThreadUnreadStatus(viewer, request);
}

export {
Expand Down
7 changes: 2 additions & 5 deletions keyserver/src/responders/device-responders.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ import { tShape, tPlatformDetails } from 'lib/utils/validation-utils.js';

import type { Viewer } from '../session/viewer.js';
import { deviceTokenUpdater } from '../updaters/device-token-updaters.js';
import { validateInput } from '../utils/validation-utils.js';

const deviceTokenUpdateRequestInputValidator: TInterface<DeviceTokenUpdateRequest> =
tShape({
tShape<DeviceTokenUpdateRequest>({
deviceToken: t.maybe(t.String),
deviceType: t.maybe(t.enums.of(['ios', 'android'])),
platformDetails: t.maybe(tPlatformDetails),
});

async function deviceTokenUpdateResponder(
viewer: Viewer,
input: any,
request: DeviceTokenUpdateRequest,
): Promise<void> {
const request: DeviceTokenUpdateRequest = input;
await validateInput(viewer, deviceTokenUpdateRequestInputValidator, request);
await deviceTokenUpdater(viewer, request);
}

Expand Down
Loading

0 comments on commit 7492050

Please sign in to comment.