push notifications working, server functions coming from backend

This commit is contained in:
metacryst
2026-03-26 02:32:59 -05:00
parent 472e69d3c0
commit d107d68bcc
9 changed files with 60 additions and 208 deletions

View File

@@ -1,4 +1,3 @@
import server from "../_/code/bridge/serverFunctions";
import util from "../util";
css(`
@@ -150,12 +149,13 @@ class Profile extends Shadow {
.onSubmit(async (e) => {
e.preventDefault();
const newBio = new FormData(e.target).get("bioinput");
if (newBio.trim() !== this.profile.bio.trim()) {
const result = await server.editBio(newBio, this.profile.id)
const { bio, updated_at } = result.data
global.profile.bio = bio
global.profile.updated_at = updated_at
this.profile = global.profile
console.log(this.profile)
if (newBio.trim() !== this.profile.bio?.trim()) {
const result = await server.editBio(newBio, this.profile.id)
const { bio, updated_at } = result.data
global.profile.bio = bio
global.profile.updated_at = updated_at
this.profile = global.profile
}
})
})

View File

@@ -1,34 +0,0 @@
export const IS_NODE =
typeof process !== "undefined" &&
process.versions?.node != null
async function bridgeSend(name, args) {
// Example browser implementation: send function call to server
const res = await global.Socket.send({
name: name,
args: args
})
return res
}
/**
* Wraps an object of functions so that:
* - Node calls the real function
* - Browser calls bridgeSend
*/
export function createBridge(funcs) {
return new Proxy(funcs, {
get(target, prop) {
const orig = target[prop]
return function (...args) {
if (IS_NODE) {
return orig(...args)
} else {
return bridgeSend(prop, args)
}
}
}
})
}

View File

@@ -1,10 +0,0 @@
import { createBridge, IS_NODE } from "./bridge.js"
let handlers = {}
if (IS_NODE) {
const mod = await import("./serverFunctions.js")
handlers = mod.default
}
export default createBridge(handlers)

View File

@@ -1,143 +0,0 @@
const handlers = {
async getStripeProfile(networkId) {
return global.payments.getProfile(networkId)
},
async addEvent(newEvent, networkId, creatorId) {
try {
return await global.db.events.add(newEvent, networkId, creatorId)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async editEvent(id, updatedEvent, networkId, userId) {
try {
return await global.db.events.edit(id, updatedEvent, networkId, userId);
} catch (e) {
return { status: e.status, error: e.message }
}
},
async deleteEvent(id, networkId, userId) {
try {
return await global.db.events.delete(id, networkId, userId);
} catch (e) {
return { status: e.status, error: e.message }
}
},
async getEvent(id) {
try {
return global.db.events.getById(id)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async getEvents(networkId) {
try {
return global.db.events.getByNetwork(networkId)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async addJob(newJob, networkId, creatorId) {
try {
return await global.db.jobs.add(newJob, networkId, creatorId);
} catch (e) {
return { status: e.status, error: e.message }
}
},
async editJob(id, updatedJob, networkId, userId) {
try {
return await global.db.jobs.edit(id, updatedJob, networkId, userId);
} catch (e) {
return { status: e.status, error: e.message }
}
},
async deleteJob(id, networkId, userId) {
try {
return await global.db.jobs.delete(id, networkId, userId);
} catch (e) {
return { status: e.status, error: e.message }
}
},
async getJob(id) {
try {
return await global.db.jobs.getById(id)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async getJobs(networkId) {
try {
return global.db.jobs.getByNetwork(networkId)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async addAnnouncement(text, networkId, userId) {
try {
return await global.db.announcements.add(text, networkId, userId)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async editAnnouncement(newAnnouncement, userId) {
try {
return await global.db.announcements.edit(newAnnouncement, userId);
} catch (e) {
return { status: e.status, error: e.message }
}
},
async deleteAnnouncement(id, userId) {
try {
return await global.db.announcements.delete(id, userId);
} catch (e) {
return { status: e.status, error: e.message }
}
},
async getAnnouncement(id) {
try {
return global.db.announcements.getById(id)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async getAnnouncements(networkId) {
try {
return global.db.announcements.getByNetwork(networkId)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async getUserAnnouncements(userId) {
try {
return global.db.announcements.getByCreator(userId)
} catch (e) {
return { status: e.status, error: e.message }
}
},
async editBio(newBio, userId) {
try {
return global.db.members.editBio(newBio, userId)
} catch (e) {
return { status: e.status, error: e.message }
}
}
}
export default handlers

View File

@@ -1,6 +1,4 @@
import { PushNotifications } from '@capacitor/push-notifications';
import server from './_/code/bridge/server.js'
import Socket from "/_/code/ws/Socket.js"
import "./Home/Home.js"
import "./Home/AuthPage/AuthPage.js"
@@ -160,31 +158,44 @@ let Global = class {
}
async setupPushNotifications() {
const permission = await PushNotifications.requestPermissions();
if (permission.receive === 'granted') {
await PushNotifications.register();
}
PushNotifications.addListener('registration', async (token) => {
console.log('Device token:', token.value)
const stored = localStorage.getItem('deviceToken')
if (stored === token.value) return;
console.log("new push token")
await server.updatePushToken(token.value)
localStorage.setItem('deviceToken', token.value)
});
PushNotifications.addListener('registrationError', (error) => {
console.error('Registration error:', error)
console.error('Registration error:', JSON.stringify(error))
});
PushNotifications.addListener('pushNotificationReceived', (notification) => {
console.log('Notification received:', notification)
});
PushNotifications.addListener('pushNotificationActionPerformed', (action) => {
console.log('Notification tapped:', action.notification)
// navigate somewhere based on action.notification.data
});
const permission = await PushNotifications.requestPermissions();
if (permission.receive === 'granted') {
await PushNotifications.register();
console.log('after registter:')
}
}
constructor() {
async init() {
try {
const module = await import(`${util.HOST}/@server/server.js`);
window.server = module.default;
} catch(E) {
console.error(E)
}
this.setupPushNotifications()
window.addEventListener("navigate", this.onNavigate)
@@ -201,6 +212,10 @@ let Global = class {
}
})
}
constructor() {
this.init()
}
}
window.global = new Global()