Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
yunochi committed Nov 23, 2024
2 parents ea9c1e5 + 202b889 commit fbdc0ff
Show file tree
Hide file tree
Showing 30 changed files with 78 additions and 119 deletions.
2 changes: 1 addition & 1 deletion src/app/_dto/misskey-callback/user-info.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MiUser } from '@/app/api/misskey-entities/user';
import type { MiUser } from '@/app/api/_misskey-entities/user';

export class misskeyUserInfoPayload {
user: MiUser;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextRequest } from 'next/server';
import { Logger } from '../logger/Logger';
import { Logger } from '@/utils/logger/Logger';

const logger = new Logger('get-ip-from-request');
export function getIpFromRequest(req: NextRequest) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Redis } from 'ioredis';
import { Logger } from '../logger/Logger';
import { Logger } from '@/utils/logger/Logger';

export class RateLimiterService {
private redis: Redis;
Expand Down
19 changes: 9 additions & 10 deletions src/app/api/db/create-question/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { CreateQuestionDto } from '@/app/_dto/create_question/create-question.dto';
import type { user } from '@prisma/client';
import { type NextRequest, NextResponse } from 'next/server';
import { verifyToken } from '../../functions/web/verify-jwt';
import { verifyToken } from '../../_utils/jwt/verify-jwt';
import { validateStrict } from '@/utils/validator/strictValidator';
import { sendErrorResponse } from '../../functions/web/errorResponse';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { getIpFromRequest } from '@/utils/getIp/get-ip-from-Request';
import { getIpHash } from '@/utils/getIp/get-ip-hash';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';
import { getIpFromRequest } from '@/app/api/_utils/getIp/get-ip-from-Request';
import { getIpHash } from '@/app/api/_utils/getIp/get-ip-hash';
const logger = new Logger('create-question');

export async function POST(req: NextRequest) {
const prisma = GetPrismaClient.getClient();
const token = req.cookies.get('jwtToken')?.value;
const tokenPayload = await verifyToken(token)
.then((payload) => payload)
.catch(() => { });
.catch(() => {});
if (tokenPayload) {
const limiter = RateLimiterService.getLimiter();
const limited = await limiter.limit(`create-question-${tokenPayload.handle}`, {
Expand Down Expand Up @@ -45,7 +44,7 @@ export async function POST(req: NextRequest) {
data = await validateStrict(CreateQuestionDto, await req.json());
} catch (errors) {
logger.warn(errors);
return sendErrorResponse(400, `${errors}`);
return sendApiError(400, `${errors}`);
}

const questionee_user = await prisma.user.findUniqueOrThrow({
Expand Down Expand Up @@ -78,7 +77,7 @@ export async function POST(req: NextRequest) {
}
} catch (err) {
logger.warn(`questioner verify ERROR! ${err}`);
return sendErrorResponse(403, `${err}`);
return sendApiError(403, `${err}`);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/app/api/db/delete-answer/route.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { DeleteAnswerDto } from '@/app/_dto/delete-answer/delete-answer.dto';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';
import { validateStrict } from '@/utils/validator/strictValidator';
import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import { verifyToken } from '../../functions/web/verify-jwt';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { verifyToken } from '../../_utils/jwt/verify-jwt';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';

const logger = new Logger('delete-answer');
export async function POST(req: NextRequest) {
Expand Down
10 changes: 5 additions & 5 deletions src/app/api/db/fetch-all-answers/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { AnswerDto } from '@/app/_dto/Answers.dto';
import { FetchAllAnswersReqDto } from '@/app/_dto/fetch-all-answers/fetch-all-answers.dto';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { getIpFromRequest } from '@/utils/getIp/get-ip-from-Request';
import { getIpHash } from '@/utils/getIp/get-ip-hash';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';
import { getIpFromRequest } from '@/app/api/_utils/getIp/get-ip-from-Request';
import { getIpHash } from '@/app/api/_utils/getIp/get-ip-hash';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';
import { validateStrict } from '@/utils/validator/strictValidator';
import { NextRequest, NextResponse } from 'next/server';

Expand Down
8 changes: 4 additions & 4 deletions src/app/api/db/fetch-my-profile/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { userProfileMeDto } from '@/app/_dto/fetch-profile/Profile.dto';
import { NextRequest, NextResponse } from 'next/server';
import { verifyToken } from '../../functions/web/verify-jwt';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { verifyToken } from '../../_utils/jwt/verify-jwt';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';

export async function GET(req: NextRequest) {
const prisma = GetPrismaClient.getClient();
Expand Down
8 changes: 4 additions & 4 deletions src/app/api/db/fetch-my-questions/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NextRequest, NextResponse } from 'next/server';
import { verifyToken } from '../../functions/web/verify-jwt';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { verifyToken } from '../../_utils/jwt/verify-jwt';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';

export async function GET(req: NextRequest) {
const prisma = GetPrismaClient.getClient();
Expand Down
12 changes: 6 additions & 6 deletions src/app/api/db/fetch-profile/[handle]/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { userProfileDto, userProfileWithHostnameDto } from '@/app/_dto/fetch-profile/Profile.dto';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { getIpFromRequest } from '@/utils/getIp/get-ip-from-Request';
import { getIpHash } from '@/utils/getIp/get-ip-hash';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { userProfileWithHostnameDto } from '@/app/_dto/fetch-profile/Profile.dto';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';
import { getIpFromRequest } from '@/app/api/_utils/getIp/get-ip-from-Request';
import { getIpHash } from '@/app/api/_utils/getIp/get-ip-hash';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';
import { NextRequest, NextResponse } from 'next/server';

const logger = new Logger('fetch-profile/handle');
Expand Down
13 changes: 6 additions & 7 deletions src/app/api/db/fetch-user-answers/route.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { FetchUserAnswersDto } from '@/app/_dto/fetch-user-answers/fetch-user-answers.dto';
import { validateStrict } from '@/utils/validator/strictValidator';
import { NextRequest, NextResponse } from 'next/server';
import { sendErrorResponse } from '../../functions/web/errorResponse';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { getIpHash } from '@/utils/getIp/get-ip-hash';
import { getIpFromRequest } from '@/utils/getIp/get-ip-from-Request';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';
import { getIpHash } from '@/app/api/_utils/getIp/get-ip-hash';
import { getIpFromRequest } from '@/app/api/_utils/getIp/get-ip-from-Request';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';

const logger = new Logger('fetch-user-answer');
export async function POST(req: NextRequest) {
Expand All @@ -17,7 +16,7 @@ export async function POST(req: NextRequest) {
try {
data = await validateStrict(FetchUserAnswersDto, await req.json());
} catch (err) {
return sendErrorResponse(400, `${err}`);
return sendApiError(400, `${err}`);
}

const limiter = RateLimiterService.getLimiter();
Expand Down
5 changes: 0 additions & 5 deletions src/app/api/functions/web/errorResponse.ts

This file was deleted.

40 changes: 4 additions & 36 deletions src/app/api/web/fetch-name-with-emoji/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { fetchNameWithEmojiReqDto } from '@/app/_dto/fetch-name-with-emoji/fetch-name-with-emoji.dto';
import { validateStrict } from '@/utils/validator/strictValidator';
import { NextRequest, NextResponse } from 'next/server';
import { sendErrorResponse } from '../../functions/web/errorResponse';
import detectInstance from '../../../../utils/detectInstance/detectInstance';
import { Logger } from '@/utils/logger/Logger';
import { sendApiError } from '../../_utils/apiErrorResponse/sendApiError';

const logger = new Logger('fetch-name-with-emoji');
export async function POST(req: NextRequest) {
let data;
try {
data = await validateStrict(fetchNameWithEmojiReqDto, await req.json());
} catch (err) {
return sendErrorResponse(400, `${err}`);
return sendApiError(400, `${err}`);
}

const { name, baseUrl }: fetchNameWithEmojiReqDto = data;
Expand Down Expand Up @@ -48,38 +48,6 @@ export async function POST(req: NextRequest) {
}

case 'misskey':
try {
if (emojiInUsername) {
for (let i = 0; i < emojiInUsername.length; i++) {
const emojiAddress = await fetch(`https://${baseUrl}/api/emoji`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
name: emojiInUsername[i],
}),
}).then((r) => r.json());

usernameEmojiAddress.push(emojiAddress.url);
}

for (const el in nameArray) {
usernameIndex.push(nameArray.indexOf(emojiInUsername[el]));
}
const filteredIndex = usernameIndex.filter((value) => value >= 0);

for (let i = 0; i < usernameEmojiAddress.length; i++) {
nameArray.splice(filteredIndex[i], 1, usernameEmojiAddress[i]);
}
}

return NextResponse.json({ nameWithEmoji: nameArray });
} catch (err) {
logger.error(err);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
}

case 'cherrypick':
try {
if (emojiInUsername) {
Expand Down Expand Up @@ -109,8 +77,8 @@ export async function POST(req: NextRequest) {

return NextResponse.json({ nameWithEmoji: nameArray });
} catch (err) {
logger.error(err);
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
logger.warn(err);
return NextResponse.json({ nameWithEmoji: [name] });
}

default:
Expand Down
17 changes: 8 additions & 9 deletions src/app/api/web/mastodon-login/route.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { loginReqDto } from '@/app/_dto/web/login/login.dto';
import { validateStrict } from '@/utils/validator/strictValidator';
import { NextRequest, NextResponse } from 'next/server';
import { sendErrorResponse } from '../../functions/web/errorResponse';
import { v4 as uuid } from 'uuid';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { GetPrismaClient } from '@/app/api/_utils//getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { getIpHash } from '@/utils/getIp/get-ip-hash';
import { getIpFromRequest } from '@/utils/getIp/get-ip-from-Request';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';
import { getIpHash } from '@/app/api/_utils/getIp/get-ip-hash';
import { getIpFromRequest } from '@/app/api/_utils/getIp/get-ip-from-Request';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';

const logger = new Logger('mastodon-login');
export async function POST(req: NextRequest) {
Expand All @@ -19,7 +18,7 @@ export async function POST(req: NextRequest) {
try {
data = await validateStrict(loginReqDto, await req.json());
} catch (err) {
return sendErrorResponse(400, `${err}`);
return sendApiError(400, `${err}`);
}

const limiter = RateLimiterService.getLimiter();
Expand Down Expand Up @@ -57,7 +56,7 @@ export async function POST(req: NextRequest) {
}).then((r) => r.json());

if (!res.id) {
return sendErrorResponse(500, `Mastodon Response: ${JSON.stringify(res)}`);
return sendApiError(500, `Mastodon Response: ${JSON.stringify(res)}`);
}
logger.log('New Mastodon OAuth2 App Created:', res);

Expand All @@ -84,7 +83,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json(session);
}
} catch (err) {
return sendErrorResponse(500, `login error... ${err}`);
return sendApiError(500, `login error... ${err}`);
}
}

Expand Down
21 changes: 10 additions & 11 deletions src/app/api/web/misskey-login/route.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { loginReqDto } from '@/app/_dto/web/login/login.dto';
import { validateStrict } from '@/utils/validator/strictValidator';
import { NextRequest, NextResponse } from 'next/server';
import { sendErrorResponse } from '../../functions/web/errorResponse';
import { MiApiError, MiAuthSession } from '@/app';
import detectInstance from '../../../../utils/detectInstance/detectInstance';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { RateLimiterService } from '@/utils/ratelimiter/rateLimiter';
import { getIpHash } from '@/utils/getIp/get-ip-hash';
import { getIpFromRequest } from '@/utils/getIp/get-ip-from-Request';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { RateLimiterService } from '@/app/api/_utils/ratelimiter/rateLimiter';
import { getIpHash } from '@/app/api/_utils/getIp/get-ip-hash';
import { getIpFromRequest } from '@/app/api/_utils/getIp/get-ip-from-Request';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';

const logger = new Logger('misskey-login');
export async function POST(req: NextRequest) {
Expand All @@ -18,7 +17,7 @@ export async function POST(req: NextRequest) {
try {
data = await validateStrict(loginReqDto, await req.json());
} catch (err) {
return sendErrorResponse(400, `${err}`);
return sendApiError(400, `${err}`);
}

const limiter = RateLimiterService.getLimiter();
Expand Down Expand Up @@ -58,7 +57,7 @@ export async function POST(req: NextRequest) {
body: JSON.stringify(payload),
});
if (!res.ok) {
return sendErrorResponse(500, `Login Error! From Remote: ${await res.text()}`);
return sendApiError(500, `Login Error! From Remote: ${await res.text()}`);
}
const data = await res.json();
const appSecret = data.secret;
Expand All @@ -80,7 +79,7 @@ export async function POST(req: NextRequest) {
});
const authRes = await initiateMisskeyAuthSession(misskeyHost, appSecret);
if (!authRes.ok) {
return sendErrorResponse(500, `Fail to Create Auth Session: ${await authRes.text()}`);
return sendApiError(500, `Fail to Create Auth Session: ${await authRes.text()}`);
}
const misskeyAuthSession = (await authRes.json()) as MiAuthSession;
logger.log(`New Misskey Auth Session Created: `, misskeyAuthSession);
Expand All @@ -99,14 +98,14 @@ export async function POST(req: NextRequest) {
data: { appSecret: null },
});
}
return sendErrorResponse(500, `Fail to Create Misskey Auth Session`);
return sendApiError(500, `Fail to Create Misskey Auth Session`);
}
const misskeyAuthSession = (await res.json()) as MiAuthSession;
logger.log(`New Misskey Auth Session Created: `, misskeyAuthSession);
return NextResponse.json(misskeyAuthSession);
}
} catch (err) {
return sendErrorResponse(500, `login error... ${err}`);
return sendApiError(500, `login error... ${err}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { profile, user } from '@prisma/client';
import { MiUser } from './api/misskey-entities/user';
import { MiUser } from './api/_misskey-entities/user';

interface MiAuthSession {
token: string;
Expand Down
6 changes: 3 additions & 3 deletions src/app/main/questions/action.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use server';

import type { mastodonTootAnswers, MkNoteAnswers, typedAnswer } from '@/app';
import { verifyToken } from '@/app/api/functions/web/verify-jwt';
import { sendApiError } from '@/utils/apiErrorResponse/sendApiError';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { verifyToken } from '@/app/api/_utils/jwt/verify-jwt';
import { sendApiError } from '@/app/api/_utils/apiErrorResponse/sendApiError';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { question } from '@prisma/client';
import { createHash } from 'crypto';
Expand Down
4 changes: 2 additions & 2 deletions src/app/main/settings/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import { cookies } from 'next/headers';
import { FormValue } from './page';
import { GetPrismaClient } from '@/utils/getPrismaClient/get-prisma-client';
import { GetPrismaClient } from '@/app/api/_utils/getPrismaClient/get-prisma-client';
import { Logger } from '@/utils/logger/Logger';
import { verifyToken } from '@/app/api/functions/web/verify-jwt';
import { verifyToken } from '@/app/api/_utils/jwt/verify-jwt';

export async function fetchUser() {
const logger = new Logger('fetchUser');
Expand Down
Loading

0 comments on commit fbdc0ff

Please sign in to comment.