link-stack/packages/bridge-ui/lib/signal.ts
2024-06-05 15:12:48 +02:00

45 lines
1.4 KiB
TypeScript

import { NextResponse } from "next/server";
import { db } from "@link-stack/bridge-common";
import { Configuration, DevicesApi } from "@link-stack/signal-api";
// import { revalidatePath } from "next/cache";
import { Service, ServiceParams } from "./service";
const fetchNoCache = async (url: string, options = {}) => {
// @ts-ignore
options.cache = options.cache || "no-store";
return fetch(url, options);
};
export class Signal extends Service {
async getBot({ params: { token } }: ServiceParams) {
const row = await db
.selectFrom("SignalBot")
.selectAll()
.where("token", "=", token as string)
.executeTakeFirstOrThrow();
const { name } = row;
if (!row.verified) {
const config = new Configuration({
basePath: process.env.BRIDGE_SIGNAL_URL,
fetchApi: fetchNoCache,
});
const devicesClient = new DevicesApi(config);
const blob: Blob = await devicesClient.v1QrcodelinkGet({
deviceName: name.replaceAll(" ", "_"),
});
const arrayBuffer = await blob.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
const qrString = buffer.toString("base64");
const qr = `data:${blob.type};base64,${qrString}`;
const finalRow = {
...row,
qr,
};
return NextResponse.json(finalRow);
} else {
return NextResponse.json(row);
}
}
}