mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-23 02:17:40 -07:00
Compare commits
5 commits
ebe2ff0e21
...
3804b66d1d
Author | SHA1 | Date | |
---|---|---|---|
3804b66d1d | |||
|
a9886aa46e | ||
|
c9d87b5373 | ||
|
3aa57588c1 | ||
|
d21fb75592 |
6 changed files with 68 additions and 12 deletions
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "iceshrimp",
|
"name": "iceshrimp",
|
||||||
"version": "2023.12.9-jormungandr.23",
|
"version": "2023.12.9-jormungandr.23.1",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://iceshrimp.dev/limepotato/jormungandr-bite.git"
|
"url": "https://iceshrimp.dev/limepotato/jormungandr-bite.git"
|
||||||
|
|
|
@ -8,10 +8,8 @@ import type { IPoll } from "@/models/entities/poll.js";
|
||||||
|
|
||||||
export async function extractPollFromQuestion(
|
export async function extractPollFromQuestion(
|
||||||
source: string | IObject,
|
source: string | IObject,
|
||||||
resolver?: Resolver,
|
resolver: Resolver,
|
||||||
): Promise<IPoll> {
|
): Promise<IPoll> {
|
||||||
if (resolver == null) resolver = new Resolver();
|
|
||||||
|
|
||||||
const question = await resolver.resolve(source);
|
const question = await resolver.resolve(source);
|
||||||
|
|
||||||
if (!isQuestion(question)) {
|
if (!isQuestion(question)) {
|
||||||
|
@ -52,7 +50,7 @@ export async function extractPollFromQuestion(
|
||||||
*/
|
*/
|
||||||
export async function updateQuestion(
|
export async function updateQuestion(
|
||||||
value: string | IQuestion,
|
value: string | IQuestion,
|
||||||
resolver?: Resolver,
|
resolver: Resolver,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
const uri = typeof value === "string" ? value : getApId(value);
|
const uri = typeof value === "string" ? value : getApId(value);
|
||||||
|
|
||||||
|
@ -68,8 +66,7 @@ export async function updateQuestion(
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
// resolve new Question object
|
// resolve new Question object
|
||||||
const _resolver = resolver ?? new Resolver();
|
const question = (await resolver.resolve(value)) as IQuestion;
|
||||||
const question = (await _resolver.resolve(value)) as IQuestion;
|
|
||||||
apLogger.debug(`fetched question: ${JSON.stringify(question, null, 2)}`);
|
apLogger.debug(`fetched question: ${JSON.stringify(question, null, 2)}`);
|
||||||
|
|
||||||
if (question.type !== "Question") throw new Error("object is not a Question");
|
if (question.type !== "Question") throw new Error("object is not a Question");
|
||||||
|
|
|
@ -104,6 +104,8 @@ async function fetchAny(
|
||||||
if (await shouldBlockInstance(extractDbHost(uri))) return null;
|
if (await shouldBlockInstance(extractDbHost(uri))) return null;
|
||||||
|
|
||||||
const dbResolver = new DbResolver();
|
const dbResolver = new DbResolver();
|
||||||
|
const resolver = new Resolver();
|
||||||
|
resolver.setUser(me);
|
||||||
|
|
||||||
const [user, note] = await Promise.all([
|
const [user, note] = await Promise.all([
|
||||||
dbResolver.getUserFromApId(uri),
|
dbResolver.getUserFromApId(uri),
|
||||||
|
@ -115,7 +117,7 @@ async function fetchAny(
|
||||||
// Update questions if the stored (remote) note contains the poll
|
// Update questions if the stored (remote) note contains the poll
|
||||||
const key = `pollFetched:${note.uri}`;
|
const key = `pollFetched:${note.uri}`;
|
||||||
if ((await redisClient.exists(key)) === 0) {
|
if ((await redisClient.exists(key)) === 0) {
|
||||||
if (await updateQuestion(note.uri)) {
|
if (await updateQuestion(note.uri, resolver)) {
|
||||||
local.object.poll = await populatePoll(note, me?.id ?? null);
|
local.object.poll = await populatePoll(note, me?.id ?? null);
|
||||||
}
|
}
|
||||||
// Allow fetching the poll again after 1 minute
|
// Allow fetching the poll again after 1 minute
|
||||||
|
@ -126,8 +128,6 @@ async function fetchAny(
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetching Object once from remote
|
// fetching Object once from remote
|
||||||
const resolver = new Resolver();
|
|
||||||
resolver.setUser(me);
|
|
||||||
const object = await resolver.resolve(uri);
|
const object = await resolver.resolve(uri);
|
||||||
|
|
||||||
// /@user If a URI other than the id is specified,
|
// /@user If a URI other than the id is specified,
|
||||||
|
|
|
@ -14,7 +14,10 @@ import { detectType } from "@/misc/get-file-info.js";
|
||||||
import { convertToWebp } from "@/services/drive/image-processor.js";
|
import { convertToWebp } from "@/services/drive/image-processor.js";
|
||||||
import { GenerateVideoThumbnail } from "@/services/drive/generate-video-thumbnail.js";
|
import { GenerateVideoThumbnail } from "@/services/drive/generate-video-thumbnail.js";
|
||||||
import { StatusError } from "@/misc/fetch.js";
|
import { StatusError } from "@/misc/fetch.js";
|
||||||
import { FILE_TYPE_BROWSERSAFE } from "@/const.js";
|
import { FILE_TYPE_BROWSERSAFE, MINUTE } from "@/const.js";
|
||||||
|
import { IEndpointMeta } from "@/server/api/endpoints.js";
|
||||||
|
import { getIpHash } from "@/misc/get-ip-hash.js";
|
||||||
|
import { limiter } from "@/server/api/limiter.js";
|
||||||
|
|
||||||
const _filename = fileURLToPath(import.meta.url);
|
const _filename = fileURLToPath(import.meta.url);
|
||||||
const _dirname = dirname(_filename);
|
const _dirname = dirname(_filename);
|
||||||
|
@ -31,6 +34,31 @@ const commonReadableHandlerGenerator =
|
||||||
export default async function (ctx: Koa.Context) {
|
export default async function (ctx: Koa.Context) {
|
||||||
const key = ctx.params.key;
|
const key = ctx.params.key;
|
||||||
|
|
||||||
|
// koa will automatically load the `X-Forwarded-For` header if `proxy: true` is configured in the app.
|
||||||
|
let limitActor: string;
|
||||||
|
limitActor = getIpHash(ctx.ip);
|
||||||
|
|
||||||
|
const limit: IEndpointMeta["limit"] = {
|
||||||
|
key: `drive-file:${key}`,
|
||||||
|
duration: MINUTE * 10,
|
||||||
|
max: 10
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rate limit
|
||||||
|
await limiter(
|
||||||
|
limit as IEndpointMeta["limit"] & { key: NonNullable<string> },
|
||||||
|
limitActor,
|
||||||
|
).catch((e) => {
|
||||||
|
const remainingTime = e.remainingTime
|
||||||
|
? `Please try again in ${e.remainingTime}.`
|
||||||
|
: "Please try again later.";
|
||||||
|
|
||||||
|
ctx.status = 429;
|
||||||
|
ctx.body = "Rate limit exceeded. " + remainingTime;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (ctx.status == 429) return;
|
||||||
|
|
||||||
// Fetch drive file
|
// Fetch drive file
|
||||||
const file = await DriveFiles.createQueryBuilder("file")
|
const file = await DriveFiles.createQueryBuilder("file")
|
||||||
.where("file.accessKey = :accessKey", { accessKey: key })
|
.where("file.accessKey = :accessKey", { accessKey: key })
|
||||||
|
|
|
@ -9,9 +9,12 @@ import { createTemp } from "@/misc/create-temp.js";
|
||||||
import { downloadUrl } from "@/misc/download-url.js";
|
import { downloadUrl } from "@/misc/download-url.js";
|
||||||
import { detectType } from "@/misc/get-file-info.js";
|
import { detectType } from "@/misc/get-file-info.js";
|
||||||
import { StatusError } from "@/misc/fetch.js";
|
import { StatusError } from "@/misc/fetch.js";
|
||||||
import { FILE_TYPE_BROWSERSAFE } from "@/const.js";
|
import { FILE_TYPE_BROWSERSAFE, MINUTE } from "@/const.js";
|
||||||
import { serverLogger } from "../index.js";
|
import { serverLogger } from "../index.js";
|
||||||
import { isMimeImage } from "@/misc/is-mime-image.js";
|
import { isMimeImage } from "@/misc/is-mime-image.js";
|
||||||
|
import { getIpHash } from "@/misc/get-ip-hash.js";
|
||||||
|
import { limiter } from "@/server/api/limiter.js";
|
||||||
|
import { IEndpointMeta } from "@/server/api/endpoints.js";
|
||||||
|
|
||||||
export async function proxyMedia(ctx: Koa.Context) {
|
export async function proxyMedia(ctx: Koa.Context) {
|
||||||
const url = "url" in ctx.query ? ctx.query.url : `https://${ctx.params.url}`;
|
const url = "url" in ctx.query ? ctx.query.url : `https://${ctx.params.url}`;
|
||||||
|
@ -21,6 +24,33 @@ export async function proxyMedia(ctx: Koa.Context) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// koa will automatically load the `X-Forwarded-For` header if `proxy: true` is configured in the app.
|
||||||
|
let limitActor: string;
|
||||||
|
limitActor = getIpHash(ctx.ip);
|
||||||
|
|
||||||
|
const parsedUrl = new URL(url);
|
||||||
|
|
||||||
|
const limit: IEndpointMeta["limit"] = {
|
||||||
|
key: `media-proxy:${parsedUrl.host}:${parsedUrl.pathname}`,
|
||||||
|
duration: MINUTE * 10,
|
||||||
|
max: 10
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rate limit
|
||||||
|
await limiter(
|
||||||
|
limit as IEndpointMeta["limit"] & { key: NonNullable<string> },
|
||||||
|
limitActor,
|
||||||
|
).catch((e) => {
|
||||||
|
const remainingTime = e.remainingTime
|
||||||
|
? `Please try again in ${e.remainingTime}.`
|
||||||
|
: "Please try again later.";
|
||||||
|
|
||||||
|
ctx.status = 429;
|
||||||
|
ctx.body = "Rate limit exceeded. " + remainingTime;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (ctx.status == 429) return;
|
||||||
|
|
||||||
const { hostname } = new URL(url);
|
const { hostname } = new URL(url);
|
||||||
let resolvedIps;
|
let resolvedIps;
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -79,6 +79,7 @@ const showTicker =
|
||||||
justify-self: flex-end;
|
justify-self: flex-end;
|
||||||
border-radius: var(--radius-big);
|
border-radius: var(--radius-big);
|
||||||
font-size: 0.8em;
|
font-size: 0.8em;
|
||||||
|
width: 100%;
|
||||||
> .avatar {
|
> .avatar {
|
||||||
width: 3.7em;
|
width: 3.7em;
|
||||||
height: 3.7em;
|
height: 3.7em;
|
||||||
|
|
Loading…
Reference in a new issue