This commit is contained in:
Rizky 2024-05-14 12:59:03 +07:00
parent a9ff40264e
commit 27a8ef343c
5 changed files with 60 additions and 50 deletions

View File

@ -4,7 +4,7 @@ import path from "path";
import { gzipAsync } from "../ws/sync/entity/zlib";
import { validate } from "uuid";
import { dir } from "dir";
import { existsAsync, readAsync } from "fs-jetpack";
import { existsAsync, readAsync, exists } from "fs-jetpack";
import { code } from "../ws/sync/code/code";
export const _ = {
@ -51,6 +51,9 @@ export const _ = {
},
select: { id: true, content_tree: true },
}),
public: readDirectoryRecursively(
code.path(site_id, "site", "src", "public")
),
site: await _db.site.findFirst({
where: { id: site_id },
select: {
@ -87,6 +90,7 @@ export function readDirectoryRecursively(
): Record<string, string> {
const result: Record<string, string> = {};
if (!exists(dirPath)) return result;
const contents = fs.readdirSync(dirPath);
for (const item of contents) {

View File

@ -30,9 +30,16 @@ declare global {
inputValue?: string;
notification: PushNotificationSchema;
}
interface NOTIF_ARG {
user_id: any;
body: string;
title: string;
data?: any;
}
const notif:
| {
loaded: (send: (data: any) => void) => void;
send: (data: NOTIF_ARG) => Promise<void>;
register: (user_id: string) => void;
onReceive: (notif: PushNotificationSchema) => void | Promise<void>;
onTap: (notif: null | ActionPerformed) => void | Promise<void>;
}

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@ -1,3 +1,4 @@
import { waitUntil } from "web-utils";
import { PG } from "../../ed/logic/ed-global";
import { VG } from "../render/global";
import { PrasiExt, prasi_ext } from "./types";
@ -32,7 +33,8 @@ interface ActionPerformed {
const w = window as unknown as {
notif:
| {
loaded: (send: (data: any) => void) => void;
send: (data: NOTIF_ARG) => Promise<void>;
register: (user_id: string) => void;
onReceive: (notif: PushNotificationSchema) => void | Promise<void>;
onTap: (notif: null | ActionPerformed) => void | Promise<void>;
}
@ -41,6 +43,13 @@ const w = window as unknown as {
export const initExtNotif = async (vi: VG, prasi_ext: PrasiExt) => {
const config = prasi_ext.notif;
w.notif = {
async send() {},
register() {},
onReceive(notif) {},
onTap(notif) {},
};
if (window.parent && config) {
window.addEventListener("message", async ({ data: raw }) => {
if (typeof raw === "object" && raw.mobile) {
@ -53,7 +62,7 @@ export const initExtNotif = async (vi: VG, prasi_ext: PrasiExt) => {
| { type: "notification-receive"; notif: PushNotificationSchema };
const waitUntil = async (fn: () => boolean) => {
if (!current.notif.onTap) {
if (!w.notif?.onTap) {
let ival = null as any;
let i = 0;
await new Promise(() => {
@ -76,10 +85,10 @@ export const initExtNotif = async (vi: VG, prasi_ext: PrasiExt) => {
config.token = data.token;
break;
case "notification-tap":
if (!current.notif.onTap) {
if (!w.notif?.onTap) {
waitUntil(() => {
if (current.notif.onTap) {
current.notif.onTap(data.notif);
if (w.notif?.onTap) {
w.notif?.onTap(data.notif);
return true;
}
return false;
@ -87,63 +96,53 @@ export const initExtNotif = async (vi: VG, prasi_ext: PrasiExt) => {
return;
}
if (current.notif.onTap) {
current.notif.onTap(data.notif);
if (w.notif?.onTap) {
w.notif?.onTap(data.notif);
}
break;
case "notification-receive":
if (!current.notif.onReceive) {
if (!w.notif?.onReceive) {
waitUntil(() => {
if (current.notif.onReceive) {
current.notif.onReceive(data.notif);
if (w.notif?.onReceive) {
w.notif?.onReceive(data.notif);
return true;
}
return false;
});
}
if (current.notif.onReceive) {
current.notif.onReceive(data.notif);
if (w.notif?.onReceive) {
w.notif?.onReceive(data.notif);
}
break;
}
}
});
const current = {
send: (msg: { type: "ready" }) => {
window.parent.postMessage({ mobile: true, ...msg }, "*");
},
config,
notif: {
register: async (user_id: any) => {
if (vi && vi.site.api) {
return await vi.site.api._notif("register", {
type: "register",
id: typeof user_id === "string" ? user_id : user_id.toString(),
token: config.token,
});
}
},
send: async (data: NOTIF_ARG) => {
if (vi && vi.site.api) {
return await vi.site.api._notif("send", {
type: "send",
id:
typeof data.user_id === "string"
? data.user_id
: data.user_id.toString(),
body: data.body,
title: data.title,
data: data.data,
});
}
},
onTap: null as null | Exclude<typeof w.notif, undefined>["onTap"],
onReceive: null as
| null
| Exclude<typeof w.notif, undefined>["onReceive"],
},
window.parent.postMessage({ mobile: true, type: "ready" }, "*");
w.notif.register = async (user_id: any) => {
await waitUntil(() => config.token);
if (vi && vi.site.api) {
return await vi.site.api._notif("register", {
type: "register",
id: typeof user_id === "string" ? user_id : user_id.toString(),
token: config.token,
});
}
};
w.notif.send = async (data: NOTIF_ARG) => {
if (vi && vi.site.api) {
return await vi.site.api._notif("send", {
type: "send",
id:
typeof data.user_id === "string"
? data.user_id
: data.user_id.toString(),
body: data.body,
title: data.title,
data: data.data,
});
}
};
current.send({ type: "ready" });
}
};