Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 82 additions & 3 deletions resources/js/electron-plugin/dist/server/api/notification.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,105 @@
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import express from 'express';
import { Notification } from 'electron';
import { notifyLaravel } from "../utils.js";
import fs from 'fs';
let player;
try {
player = require('play-sound')();
}
catch (e) {
player = null;
}
const isLocalFile = (sound) => {
if (typeof sound !== 'string')
return false;
if (/^https?:\/\//i.test(sound))
return false;
return sound.startsWith('/') || sound.startsWith('file:') || /^[a-zA-Z]:\\/.test(sound);
};
const normalizePath = (raw) => {
if (raw.startsWith('file://'))
return raw.replace(/^file:\/\//, '');
return raw;
};
const playSound = (sound) => __awaiter(void 0, void 0, void 0, function* () {
const filePath = normalizePath(sound);
try {
yield fs.promises.access(filePath, fs.constants.R_OK);
}
catch (err) {
return Promise.reject(new Error(`sound file not accessible: ${filePath}`));
}
return new Promise((resolve, reject) => {
if (player) {
player.play(filePath, (err) => {
if (err)
return reject(err);
resolve();
});
return;
}
const { exec } = require('child_process');
exec(`afplay ${JSON.stringify(filePath)}`, (err) => {
if (err)
return reject(err);
resolve();
});
});
});
const router = express.Router();
router.post('/', (req, res) => {
const { title, body, subtitle, silent, icon, hasReply, timeoutType, replyPlaceholder, sound, urgency, actions, closeButtonText, toastXml, event: customEvent, reference, } = req.body;
const eventName = customEvent !== null && customEvent !== void 0 ? customEvent : '\\Native\\Laravel\\Events\\Notifications\\NotificationClicked';
const notificationReference = reference !== null && reference !== void 0 ? reference : (Date.now() + '.' + Math.random().toString(36).slice(2, 9));
const notification = new Notification({
const usingLocalFile = isLocalFile(sound);
const createNotification = (opts) => {
try {
if (typeof Notification === 'function') {
return new Notification(opts);
}
}
catch (e) {
}
return {
show: () => { },
on: (_, __) => { },
};
};
const notification = createNotification({
title,
body,
subtitle,
silent,
silent: usingLocalFile ? true : silent,
icon,
hasReply,
timeoutType,
replyPlaceholder,
sound,
sound: usingLocalFile ? undefined : sound,
urgency,
actions,
closeButtonText,
toastXml
});
if (usingLocalFile && typeof sound === 'string') {
playSound(sound).catch((err) => {
notifyLaravel('events', {
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationSoundFailed',
payload: {
reference: notificationReference,
error: String(err),
},
});
});
}
notification.on("click", (event) => {
notifyLaravel('events', {
event: eventName || '\\Native\\Laravel\\Events\\Notifications\\NotificationClicked',
Expand Down
80 changes: 77 additions & 3 deletions resources/js/electron-plugin/src/server/api/notification.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,51 @@
import express from 'express';
import { Notification } from 'electron';
import {notifyLaravel} from "../utils.js";
import fs from 'fs';
declare const require: any;

let player: any;
try {
player = require('play-sound')();
} catch (e) {
player = null;
}

const isLocalFile = (sound: unknown) => {
if (typeof sound !== 'string') return false;
if (/^https?:\/\//i.test(sound)) return false;
return sound.startsWith('/') || sound.startsWith('file:') || /^[a-zA-Z]:\\/.test(sound);
};

const normalizePath = (raw: string) => {
if (raw.startsWith('file://')) return raw.replace(/^file:\/\//, '');
return raw;
};

const playSound = async (sound: string) => {
const filePath = normalizePath(sound);
try {
await fs.promises.access(filePath, fs.constants.R_OK);
} catch (err) {
return Promise.reject(new Error(`sound file not accessible: ${filePath}`));
}

return new Promise<void>((resolve, reject) => {
if (player) {
player.play(filePath, (err: any) => {
if (err) return reject(err);
resolve();
});
return;
}

const { exec } = require('child_process');
exec(`afplay ${JSON.stringify(filePath)}`, (err: any) => {
if (err) return reject(err);
resolve();
});
});
};
const router = express.Router();

router.post('/', (req, res) => {
Expand All @@ -26,22 +71,51 @@ router.post('/', (req, res) => {

const notificationReference = reference ?? (Date.now() + '.' + Math.random().toString(36).slice(2, 9));

const notification = new Notification({
const usingLocalFile = isLocalFile(sound);

const createNotification = (opts: any) => {
try {
if (typeof (Notification as any) === 'function') {
return new (Notification as any)(opts);
}
} catch (e) {

}

return {
show: () => {},
on: (_: string, __: Function) => {},
};
};

const notification = createNotification({
title,
body,
subtitle,
silent,
silent: usingLocalFile ? true : silent,
icon,
hasReply,
timeoutType,
replyPlaceholder,
sound,
sound: usingLocalFile ? undefined : sound,
urgency,
actions,
closeButtonText,
toastXml
});

if (usingLocalFile && typeof sound === 'string') {
playSound(sound).catch((err) => {
notifyLaravel('events', {
event: '\\Native\\Laravel\\Events\\Notifications\\NotificationSoundFailed',
payload: {
reference: notificationReference,
error: String(err),
},
});
});
}

notification.on("click", (event) => {
notifyLaravel('events', {
event: eventName || '\\Native\\Laravel\\Events\\Notifications\\NotificationClicked',
Expand Down
31 changes: 31 additions & 0 deletions resources/js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion resources/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
"nodemon": "^3.1.9",
"ps-node": "^0.1.6",
"tree-kill": "^1.2.2",
"yauzl": "^3.2.0"
"yauzl": "^3.2.0",
"play-sound": "^1.1.3"
},
"devDependencies": {
"@babel/plugin-proposal-decorators": "^7.25.9",
Expand Down
Loading