Signal API updates

This commit is contained in:
Darren Clarke 2024-06-05 15:12:48 +02:00
parent 83653ef23b
commit c729a46a0c
25 changed files with 501 additions and 279 deletions

View file

@ -82,7 +82,7 @@ export const Detail: FC<DetailProps> = ({ service, row }) => {
<QRCode
name={field.name}
label={field.label}
getValue={field.getValue}
getValue={field.getQRCode}
refreshInterval={field.refreshInterval}
token={token}
verified={row.verified as boolean}

View file

@ -9,7 +9,7 @@ type QRCodeProps = {
token: string;
verified: boolean;
helperText?: string;
getValue?: (id: string) => Promise<string>;
getValue?: (id: string) => Promise<Record<string, string>>;
refreshInterval?: number;
};
@ -23,13 +23,16 @@ export const QRCode: FC<QRCodeProps> = ({
refreshInterval,
}) => {
const [value, setValue] = useState("");
const [kind, setKind] = useState("data");
const { white } = colors;
useEffect(() => {
if (!verified && getValue && refreshInterval) {
const interval = setInterval(async () => {
const result = await getValue(token);
setValue(result);
const { qr, kind } = await getValue(token);
console.log({ kind });
setValue(qr);
setKind(kind);
}, refreshInterval * 1000);
return () => clearInterval(interval);
}
@ -37,7 +40,11 @@ export const QRCode: FC<QRCodeProps> = ({
return !verified ? (
<Box sx={{ backgroundColor: white, m: 2 }}>
<QRCodeInternal value={value} />
{kind === "data" ? (
<QRCodeInternal value={value} />
) : (
<img src={value} alt={name} />
)}
<Box>{helperText}</Box>
</Box>
) : null;

View file

@ -1,5 +1,13 @@
import { ServiceConfig } from "../lib/service";
const getQRCode = async (token: string): Promise<Record<string, string>> => {
const url = `/api/signal/bots/${token}`;
const result = await fetch(url, { cache: "no-store" });
const { qr } = await result.json();
return { qr, kind: "image" };
};
export const signalConfig: ServiceConfig = {
entity: "signal",
table: "SignalBot",
@ -59,6 +67,15 @@ export const signalConfig: ServiceConfig = {
label: "Token",
copyable: true,
},
{
name: "qrcode",
label: "QR Code",
kind: "qrcode",
size: 4,
getQRCode,
helperText: "Go to link devices in the app, then scan the code",
refreshInterval: 15,
},
],
listColumns: [
{

View file

@ -6,7 +6,7 @@ const getQRCode = async (token: string) => {
const result = await fetch(url, { cache: "no-store" });
const { qr } = await result.json();
return qr ?? "";
return { qr, kind: "data" };
};
export const whatsappConfig: ServiceConfig = {
@ -73,8 +73,8 @@ export const whatsappConfig: ServiceConfig = {
label: "QR Code",
kind: "qrcode",
size: 4,
getValue: getQRCode,
helperText: "Go ahead, scan it",
getQRCode,
helperText: "Go to link devices in the app, then scan the code",
refreshInterval: 15,
},
],

View file

@ -24,6 +24,7 @@ export type FieldDescription = {
label: string;
kind?: "text" | "phone" | "select" | "multi" | "qrcode";
getValue?: (token: string) => Promise<string>;
getQRCode?: (token: string) => Promise<Record<string, string>>;
refreshInterval?: number;
getOptions?: (formState: any) => Promise<SelectOption[]>;
autogenerated?: "token";

View file

@ -1,3 +1,45 @@
import { Service } from "./service";
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";
export class Signal extends 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);
}
}
}