Feat: Include user location in ntfy notifications

This commit is contained in:
2026-02-17 15:47:03 -08:00
parent 652d3434a2
commit d4955f2e7f

View File

@@ -74,6 +74,7 @@ export class MyDurableObject {
this.hbActive = false; this.hbActive = false;
this.age = 0; this.age = 0;
this.messages = []; this.messages = [];
this.location = 'Unknown';
} }
async resetStorage() { async resetStorage() {
@@ -92,9 +93,10 @@ export class MyDurableObject {
notify(msg, pri = 3, tags = []) { notify(msg, pri = 3, tags = []) {
if (!this.env.NTFY_URL) return; if (!this.env.NTFY_URL) return;
const headers = { Title: 'Sune ORP', Priority: `${pri}`, Tags: tags.join(',') }; const headers = { Title: 'Sune ORP', Priority: `${pri}`, Tags: tags.join(',') };
const body = `[${this.location}] ${msg}`;
this.state.waitUntil(fetch(this.env.NTFY_URL, { this.state.waitUntil(fetch(this.env.NTFY_URL, {
method: 'POST', method: 'POST',
body: msg, body,
headers, headers,
}).catch(e => console.error('ntfy failed:', e))); }).catch(e => console.error('ntfy failed:', e)));
} }
@@ -181,6 +183,9 @@ export class MyDurableObject {
} }
async fetch(req) { async fetch(req) {
const cf = req.cf || {};
this.location = [cf.city, cf.region, cf.country].filter(Boolean).join(', ') || 'Unknown';
if (req.method === 'OPTIONS') return new Response(null, { status: 204, headers: CORS_HEADERS }); if (req.method === 'OPTIONS') return new Response(null, { status: 204, headers: CORS_HEADERS });
if (req.headers.get('Upgrade') === 'websocket') { if (req.headers.get('Upgrade') === 'websocket') {
@@ -499,7 +504,7 @@ export class MyDurableObject {
const role = m.role === 'assistant' ? 'model' : 'user'; const role = m.role === 'assistant' ? 'model' : 'user';
const msgContent = Array.isArray(m.content) ? m.content : [{ type: 'text', text: String(m.content ?? '') }]; const msgContent = Array.isArray(m.content) ? m.content : [{ type: 'text', text: String(m.content ?? '') }];
const parts = msgContent.map(p => { const parts = msgContent.map(p => {
if (p.type === 'text') return { text: p.text || '' }; if (p.text) return { text: p.text };
if (p.type === 'image_url' && p.image_url?.url) { if (p.type === 'image_url' && p.image_url?.url) {
const match = p.image_url.url.match(/^data:(image\/\w+);base64,(.*)$/); const match = p.image_url.url.match(/^data:(image\/\w+);base64,(.*)$/);
if (match) return { inline_data: { mime_type: match[1], data: match[2] } }; if (match) return { inline_data: { mime_type: match[1], data: match[2] } };
@@ -515,4 +520,3 @@ export class MyDurableObject {
return contents; return contents;
} }
} }