feat: fix presence endpoints

This commit is contained in:
Bas950
2024-11-17 21:02:07 +01:00
parent 154acbc802
commit b67226dcdd
3 changed files with 41 additions and 5 deletions

View File

@@ -13,6 +13,7 @@ import { Socket } from "../classes/Socket.js";
import { resolvers } from "../graphql/resolvers/v5/index.js";
import { sessionKeepAlive } from "../routes/sessionKeepAlive.js";
import { featureFlags } from "../constants.js";
import { presences } from "../routes/presences.js";
import createRedis from "./createRedis.js";
export interface FastifyContext {
@@ -89,6 +90,7 @@ export default async function createServer() {
});
app.post("/v5/session-keep-alive", sessionKeepAlive);
app.get("/v5/presence/:service/:file", presences);
return app;
}

View File

@@ -0,0 +1,34 @@
import { Presence } from "@premid/db";
import { type } from "arktype";
import type { FastifyReply, FastifyRequest } from "fastify";
const schema = type({
service: "string.trim",
file: "'metadata.json'|'presence.js'|'iframe.js'",
});
export async function presences(request: FastifyRequest, reply: FastifyReply) {
const out = schema(request.params);
if (out instanceof type.errors)
return reply.status(400).send({ code: "INVALID_PARAMS", message: out.message });
const service = decodeURIComponent(out.service);
const { file } = out;
const presence = await Presence.findOne({ "metadata.service": service });
if (!presence)
return reply.status(404).send({ code: "PRESENCE_NOT_FOUND", message: "The presence was not found" });
switch (file) {
case "metadata.json":
return reply.status(200).type("application/json").send(presence.metadata);
case "presence.js":
return reply.status(200).type("application/javascript").send(presence.presenceJs);
case "iframe.js":
if (!presence.iframeJs)
return reply.status(404).send({ code: "IFRAME_NOT_FOUND", message: "The presence does not have an iframe" });
return reply.status(200).type("application/javascript").send(presence.iframeJs);
}
}

View File

@@ -62,15 +62,15 @@ const PresenceMetadataSettingSchema = new Schema<PresenceMetadataSetting>({
placeholder: { type: String },
title: { type: String },
value: Schema.Types.Mixed,
values: { type: [String] },
values: { type: [Schema.Types.Mixed], default: undefined },
});
const PresenceMetadataSchema = new Schema<PresenceMetadata>({
$schema: { required: true, type: String },
altnames: { type: [String] },
altnames: { type: [String], default: undefined },
author: { required: true, type: PresenceMetadataContributorSchema },
category: { required: true, type: String },
color: { required: true, type: String },
contributors: { type: [PresenceMetadataContributorSchema] },
contributors: { type: [PresenceMetadataContributorSchema], default: undefined },
description: { required: true, type: Schema.Types.Mixed },
iFrameRegExp: { type: String },
iframe: { type: Boolean },
@@ -78,10 +78,10 @@ const PresenceMetadataSchema = new Schema<PresenceMetadata>({
readLogs: { type: Boolean },
regExp: { type: String },
service: { required: true, type: String },
settings: { type: [PresenceMetadataSettingSchema] },
settings: { type: [PresenceMetadataSettingSchema], default: undefined },
tags: { required: true, type: [String] },
thumbnail: { required: true, type: String },
url: { required: true, type: [String] },
url: { required: true, type: Schema.Types.Mixed },
version: { required: true, type: String },
apiVersion: { required: true, type: Number },
});