import { d as defineEventHandler, u as useLogger, c as createError, w as publicSettings, ak as readRawBody, al as APActivityBaseSchema, am as verifyHTTPSignature, an as handleActivity, ab as setResponseStatus, ao as setResponseHeader } from '../../../../nitro/nitro.mjs';
import 'luxon';
import '@sequelize/core';
import 'html-to-text';
import 'zod';
import 'crypto';
import 'node:fs';
import 'sharp';
import 'path';
import 'node:stream';
import 'jsdom';
import 'dompurify';
import 'mime-types';
import 'fs';
import 'fs/promises';
import 'linkify-html';
import '@peertube/http-signature';
import 'jsonld';
import 'lodash.intersection';
import 'email-templates';
import 'node:http';
import 'node:https';
import 'node:crypto';
import 'node:events';
import 'node:buffer';
import 'node:path';
import 'vue';
import 'vue-router';
import 'dotenv';
import 'url';
import 'bcryptjs';
import 'slugify';
import 'umzug';
import 'consola';
import '@sequelize/core/decorators-legacy';
import 'util';
import 'mitt';
import 'ical.js';
import 'node:url';
import 'fast-xml-parser';

const inbox_post = defineEventHandler(async (event) => {
  const log = useLogger("AP inbox");
  const actorName = event.context?.params?.name;
  if (!actorName) {
    log.info("Invalid /inbox request");
    throw createError({ status: 401, message: "Invalid actor name" });
  }
  if (actorName !== publicSettings.instance_name) {
    log.info(`No record found for ${actorName} (applicationActor is ${publicSettings.instance_name})`);
    throw createError({ status: 404, message: `No actor found for ${actorName} (applicationActor is ${publicSettings.instance_name})` });
  }
  const rawBody = await readRawBody(event);
  if (!rawBody) throw createError({ status: 400, message: "Empty body" });
  const activity = JSON.parse(rawBody.toString());
  const { success, data: activityData, error } = APActivityBaseSchema.safeParse(activity);
  if (!success) {
    log.warn("Wrong AP activity: %s", error.issues[0]?.message);
    log.debug(activity);
    return;
  }
  const activityActor = await verifyHTTPSignature(event, rawBody, activityData).catch((e) => {
    log.info(e);
    throw e;
  });
  if (!activityActor) return false;
  try {
    await handleActivity(activity, activityActor);
  } catch (error2) {
    log.error("Error handling activity: %s", error2);
    throw createError({ status: 500, message: "Error handling activity" });
  }
  setResponseStatus(event, 202);
  setResponseHeader(event, "Content-Type", "application/activity+json");
});

export { inbox_post as default };
