From d17da3861544eb60bebe1bde59ee5bc99c5fe9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9B=90=EB=8D=94?= Date: Sat, 9 Jan 2021 21:53:46 +0900 Subject: [PATCH] feat: added bot list methods --- utils/Fetch.ts | 58 ++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/utils/Fetch.ts b/utils/Fetch.ts index f9043ea..17099ef 100644 --- a/utils/Fetch.ts +++ b/utils/Fetch.ts @@ -11,19 +11,13 @@ const privateKey = fs.readFileSync('./private.key') const bot = new DataLoader( async (ids: string[]) => - (await Promise.all(ids.map(async (el: string) => getBot(el)))).map(row => ({ ...row })), - { - batchScheduleFn: callback => setTimeout(callback, 100), - } + (await Promise.all(ids.map(async (el: string) => getBot(el)))).map(row => ({ ...row })) ) new DataLoader(async ()=> { return []}) const botWithNoUser = new DataLoader( async (ids: string[]) => - (await Promise.all(ids.map(async (el: string) => getBot(el, false)))).map(row => ({ ...row })), - { - batchScheduleFn: callback => setTimeout(callback, 100), - } + (await Promise.all(ids.map(async (el: string) => getBot(el, false)))).map(row => ({ ...row })) ) const user = new DataLoader( @@ -36,9 +30,28 @@ const userWithNoBot = new DataLoader( (await Promise.all(ids.map((el: string) => getUser(el, false)))).map(row => ({ ...row })) ) -const botList = new DataLoader( - async (argument: botListArgument[]) => - (await Promise.all(argument.map((el) => getBotList(el.type, el.page, el.query)))).map(row => ({ ...row })) +const botListVotes = new DataLoader( + async (page: number[]) => + (await Promise.all(page.map((el) => getBotList('VOTE', el)))).map(row => ({ ...row })), + { + batchScheduleFn: callback => setTimeout(callback, 100), + } +) + +const botListNew = new DataLoader( + async (page: number[]) => + (await Promise.all(page.map((el) => getBotList('NEW', el)))).map(row => ({ ...row })), + { + batchScheduleFn: callback => setTimeout(callback, 100), + } +) + +const botListTrusted = new DataLoader( + async (page: number[]) => + (await Promise.all(page.map((el) => getBotList('TRUSTED', el)))).map(row => ({ ...row })), + { + batchScheduleFn: callback => setTimeout(callback, 100), + } ) async function getBot(id: string, owners = true): Promise { @@ -71,8 +84,8 @@ async function getBot(id: string, owners = true): Promise { 'banner', ]) .where({ id }) - .orWhere({ vanity: id, trusted: 1 }) - .orWhere({ vanity: id, partnered: 1 }) + .orWhere({ vanity: id, trusted: true }) + .orWhere({ vanity: id, partnered: true }) if (res[0]) { res[0].category = JSON.parse(res[0].category) res[0].owners = JSON.parse(res[0].owners) @@ -117,21 +130,18 @@ async function getBotList(type: ListType, page = 1, query?: string):Promise await getBot(el.id))), currentPage: page, totalPage: Math.ceil(Number(count) / 16) } + return { type, data: (await Promise.all(res.map(async el => await getBot(el.id)))).map(r=> ({...r})), currentPage: page, totalPage: Math.ceil(Number(count) / 16) } } -export default { bot, user, botList } +export default { bot, user, botListVotes, botListNew, botListTrusted }