From 36d8fd1d2c3296746e5adb6d5fed05c57b63e63f Mon Sep 17 00:00:00 2001 From: Xeltica Date: Thu, 10 Sep 2020 00:56:39 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=9E=E3=82=A4=E3=83=9A=E3=83=BC=E3=82=B8?= =?UTF-8?q?=E3=81=AB=E3=83=AD=E3=82=B0=E3=82=A4=E3=83=B3=E3=81=97=E3=81=9F?= =?UTF-8?q?=E3=81=BE=E3=81=BEMisskey=E5=81=B4=E3=81=A7=E9=80=A3=E6=90=BA?= =?UTF-8?q?=E3=82=92=E8=A7=A3=E9=99=A4=E3=81=99=E3=82=8B=E3=81=A8=E3=82=A2?= =?UTF-8?q?=E3=82=AF=E3=82=BB=E3=82=B9=E3=81=A7=E3=81=8D=E3=81=AA=E3=81=8F?= =?UTF-8?q?=E3=81=AA=E3=82=8B=E4=B8=8D=E5=85=B7=E5=90=88=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/server/router.ts | 19 +++++++++++-------- src/services/misskey.ts | 9 +++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/server/router.ts b/src/server/router.ts index c55f9df..08e8a73 100644 --- a/src/server/router.ts +++ b/src/server/router.ts @@ -7,7 +7,7 @@ import { die } from './die'; import { v4 as uuid } from 'uuid'; import { config } from '../config'; import { upsertUser, getUser, getUserCount, updateUser, updateUsersMisshaiToken, getUserByMisshaiToken, deleteUser } from '../functions/users'; -import { api } from '../services/misskey'; +import { api, apiAvailable } from '../services/misskey'; import { getScores } from '../functions/get-scores'; import { AlertMode, alertModes } from '../types/AlertMode'; import { Users } from '../models'; @@ -53,18 +53,21 @@ const login = async (ctx: Context, user: Record, host: string, router.get('/', async ctx => { const token = ctx.cookies.get('token', { signed: true }); const user = token ? await getUserByMisshaiToken(token) : undefined; - if (!user) { - // 非ログイン - await ctx.render('welcome', { - usersCount: await getUserCount(), - welcomeMessage: welcomeMessage[Math.floor(Math.random() * welcomeMessage.length)], - }); - } else { + + const isAvailable = user && await apiAvailable(user.host, user.token); + console.log(isAvailable); + if (user && isAvailable) { await ctx.render('mypage', { user, usersCount: await getUserCount(), score: await getScores(user), }); + } else { + // 非ログイン + await ctx.render('welcome', { + usersCount: await getUserCount(), + welcomeMessage: welcomeMessage[Math.floor(Math.random() * welcomeMessage.length)], + }); } }); diff --git a/src/services/misskey.ts b/src/services/misskey.ts index 52dbf89..64cbd93 100644 --- a/src/services/misskey.ts +++ b/src/services/misskey.ts @@ -13,4 +13,13 @@ export const api = >(host: string, endpoint: string, a.i = i; } return axios.post(`https://${host}/api/${endpoint}`, a).then(res => res.data); +}; + +export const apiAvailable = async (host: string, i: string) : Promise => { + try { + const res = await api(host, 'i', {}, i); + return !res.error; + } catch { + return false; + } }; \ No newline at end of file