mirror of
https://github.com/PretendoNetwork/miiverse-api.git
synced 2026-08-28 03:24:22 -05:00
Fixed crash in /v1/people when only one PID input is given
This commit is contained in:
@@ -76,8 +76,8 @@ router.get('/', async function (request: express.Request, response: express.Resp
|
||||
return;
|
||||
}
|
||||
|
||||
const type: string | undefined = getValueFromQueryString(request.query, 'type');
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit');
|
||||
const type: string | undefined = getValueFromQueryString(request.query, 'type')[0];
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit')[0];
|
||||
|
||||
let limit: number = 4;
|
||||
if (limitString) {
|
||||
@@ -158,13 +158,13 @@ router.get('/:communityID/posts', async function (request: express.Request, resp
|
||||
message_to_pid: { $eq: null }
|
||||
};
|
||||
|
||||
const searchKey: string | undefined = getValueFromQueryString(request.query, 'search_key');
|
||||
const allowSpoiler: string | undefined = getValueFromQueryString(request.query, 'allow_spoiler');
|
||||
const postType: string | undefined = getValueFromQueryString(request.query, 'type');
|
||||
const queryBy: string | undefined = getValueFromQueryString(request.query, 'by');
|
||||
const distinctPID: string | undefined = getValueFromQueryString(request.query, 'distinct_pid');
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit');
|
||||
const withMii: string | undefined = getValueFromQueryString(request.query, 'with_mii');
|
||||
const searchKey: string | undefined = getValueFromQueryString(request.query, 'search_key')[0];
|
||||
const allowSpoiler: string | undefined = getValueFromQueryString(request.query, 'allow_spoiler')[0];
|
||||
const postType: string | undefined = getValueFromQueryString(request.query, 'type')[0];
|
||||
const queryBy: string | undefined = getValueFromQueryString(request.query, 'by')[0];
|
||||
const distinctPID: string | undefined = getValueFromQueryString(request.query, 'distinct_pid')[0];
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit')[0];
|
||||
const withMii: string | undefined = getValueFromQueryString(request.query, 'with_mii')[0];
|
||||
|
||||
let limit: number = 10;
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import { Snowflake } from 'node-snowflake';
|
||||
import moment from 'moment';
|
||||
import xmlbuilder from 'xmlbuilder';
|
||||
import { z } from 'zod';
|
||||
import { ParsedQs } from 'qs';
|
||||
import { GetUserDataResponse } from 'pretendo-grpc-ts/dist/account/get_user_data_rpc';
|
||||
import { getUserFriendPIDs, getUserAccountData, processPainting, uploadCDNAsset, getValueFromQueryString } from '@/util';
|
||||
import { getConversationByUsers, getUserSettings, getFriendMessages } from '@/database';
|
||||
@@ -215,7 +214,7 @@ router.post('/', upload.none(), async function (request: express.Request, respon
|
||||
router.get('/', async function (request: express.Request, response: express.Response): Promise<void> {
|
||||
response.type('application/xml');
|
||||
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit');
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit')[0];
|
||||
|
||||
// TODO - Is this the limit?
|
||||
let limit: number = 10;
|
||||
@@ -228,15 +227,14 @@ router.get('/', async function (request: express.Request, response: express.Resp
|
||||
limit = 10;
|
||||
}
|
||||
|
||||
// TODO - Update getValueFromQueryString to return arrays optionally
|
||||
const searchKey: string | ParsedQs | string[] | ParsedQs[] | undefined = request.query.search_key;
|
||||
|
||||
if (!searchKey) {
|
||||
if (!request.query.search_key) {
|
||||
response.sendStatus(404);
|
||||
return;
|
||||
}
|
||||
|
||||
const messages: HydratedPostDocument[] = await getFriendMessages(request.pid.toString(), searchKey as string[], limit);
|
||||
const searchKey: string[] = getValueFromQueryString(request.query, 'search_key');
|
||||
|
||||
const messages: HydratedPostDocument[] = await getFriendMessages(request.pid.toString(), searchKey, limit);
|
||||
|
||||
const postBody: FormattedMessage[] = [];
|
||||
for (const message of messages) {
|
||||
|
||||
@@ -30,10 +30,10 @@ router.get('/', async function (request: express.Request, response: express.Resp
|
||||
message_to_pid: { $eq: null }
|
||||
};
|
||||
|
||||
const relation: string | undefined = getValueFromQueryString(request.query, 'relation');
|
||||
const distinctPID: string | undefined = getValueFromQueryString(request.query, 'distinct_pid');
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit');
|
||||
const withMii: string | undefined = getValueFromQueryString(request.query, 'with_mii');
|
||||
const relation: string | undefined = getValueFromQueryString(request.query, 'relation')[0];
|
||||
const distinctPID: string | undefined = getValueFromQueryString(request.query, 'distinct_pid')[0];
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit')[0];
|
||||
const withMii: string | undefined = getValueFromQueryString(request.query, 'with_mii')[0];
|
||||
|
||||
let limit: number = 10;
|
||||
|
||||
@@ -50,8 +50,10 @@ router.get('/', async function (request: express.Request, response: express.Resp
|
||||
} else if (relation === 'following') {
|
||||
query.pid = { $in: userContent.followed_users };
|
||||
} else if (request.query.pid) {
|
||||
// TODO - Update getValueFromQueryString to return arrays optionally
|
||||
query.pid = { $in: (request.query.pid as string[]).map(pid => Number(pid)) };
|
||||
const pidInputs: string[] = getValueFromQueryString(request.query, 'pid');
|
||||
const pids: number[] = pidInputs.map(pid => Number(pid)).filter(pid => !isNaN(pid));
|
||||
|
||||
query.pid = { $in: pids };
|
||||
}
|
||||
|
||||
let posts: HydratedPostDocument[];
|
||||
|
||||
@@ -110,7 +110,7 @@ router.post('/:post_id/empathies', upload.none(), async function (request: expre
|
||||
router.get('/:post_id/replies', async function (request: express.Request, response: express.Response): Promise<void> {
|
||||
response.type('application/xml');
|
||||
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit');
|
||||
const limitString: string | undefined = getValueFromQueryString(request.query, 'limit')[0];
|
||||
|
||||
let limit: number = 10; // TODO - Is there a real limit?
|
||||
|
||||
@@ -159,7 +159,7 @@ router.get('/:post_id/replies', async function (request: express.Request, respon
|
||||
router.get('/', async function (request: express.Request, response: express.Response): Promise<void> {
|
||||
response.type('application/xml');
|
||||
|
||||
const postID: string | undefined = getValueFromQueryString(request.query, 'post_id');
|
||||
const postID: string | undefined = getValueFromQueryString(request.query, 'post_id')[0];
|
||||
|
||||
if (!postID) {
|
||||
response.type('application/xml');
|
||||
|
||||
@@ -5,9 +5,9 @@ import { getValueFromQueryString } from '@/util';
|
||||
const router: express.Router = express.Router();
|
||||
|
||||
router.get('/:pid/notifications', function(request: express.Request, response: express.Response): void {
|
||||
const type: string | undefined = getValueFromQueryString(request.query, 'type');
|
||||
const titleID: string | undefined = getValueFromQueryString(request.query, 'title_id');
|
||||
const pid: string | undefined = getValueFromQueryString(request.query, 'pid');
|
||||
const type: string | undefined = getValueFromQueryString(request.query, 'type')[0];
|
||||
const titleID: string | undefined = getValueFromQueryString(request.query, 'title_id')[0];
|
||||
const pid: string | undefined = getValueFromQueryString(request.query, 'pid')[0];
|
||||
|
||||
console.log(type);
|
||||
console.log(titleID);
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
export interface SafeQs {
|
||||
[key: string]: string | undefined
|
||||
}
|
||||
33
src/util.ts
33
src/util.ts
@@ -7,7 +7,6 @@ import aws from 'aws-sdk';
|
||||
import { createChannel, createClient, Metadata } from 'nice-grpc';
|
||||
import { ParsedQs } from 'qs';
|
||||
import crc32 from 'crc/crc32';
|
||||
import { SafeQs } from '@/types/common/safe-qs';
|
||||
import { ParamPack } from '@/types/common/param-pack';
|
||||
import { config } from '@/config-manager';
|
||||
import { Token } from '@/types/common/token';
|
||||
@@ -163,40 +162,18 @@ export function getUserAccountData(pid: number): Promise<GetUserDataResponse> {
|
||||
});
|
||||
}
|
||||
|
||||
export function makeSafeQs(query: ParsedQs): SafeQs {
|
||||
const entries = Object.entries(query);
|
||||
const output: SafeQs = {};
|
||||
|
||||
for (const [key, value] of entries) {
|
||||
if (typeof value !== 'string') {
|
||||
// * ignore non-strings
|
||||
continue;
|
||||
}
|
||||
|
||||
output[key] = value;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export function getValueFromQueryString(qs: ParsedQs, key: string): string | undefined {
|
||||
let property: string | ParsedQs | string[] | ParsedQs[] | SafeQs | undefined = qs[key];
|
||||
let value: string | undefined;
|
||||
export function getValueFromQueryString(qs: ParsedQs, key: string): string[] {
|
||||
const property: string | string[] | undefined = qs[key] as string | string[];
|
||||
|
||||
if (property) {
|
||||
if (Array.isArray(property)) {
|
||||
property = property[0];
|
||||
}
|
||||
|
||||
if (typeof property !== 'string') {
|
||||
property = makeSafeQs(<ParsedQs>property);
|
||||
value = (<SafeQs>property)[key];
|
||||
return property;
|
||||
} else {
|
||||
value = <string>property;
|
||||
return [property];
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
return [];
|
||||
}
|
||||
|
||||
export function getValueFromHeaders(headers: IncomingHttpHeaders, key: string): string | undefined {
|
||||
|
||||
Reference in New Issue
Block a user