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

View File

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