diff --git a/src/webpage/jsontypes.ts b/src/webpage/jsontypes.ts index 16d1793..97ba5ac 100644 --- a/src/webpage/jsontypes.ts +++ b/src/webpage/jsontypes.ts @@ -1525,3 +1525,47 @@ export { addInfoBan, templateSkim, }; + +export interface PushNotification { + type: string; + receiving_user_id?: string; + notif_type_id?: number; + notif_instance_id?: string; + title?: string; + subtitle?: string; + expand_subtitle?: boolean; + icon_url?: string; + silent?: boolean; + sent_at_ms?: number; + channel_ids?: string[]; + mention_type?: string; + join_id?: string; +} + +export interface MessagePush extends PushNotification { + type: "MESSAGE_CREATE"; + message?: { + author: userjson; + mentions: string[]; + embeds: embedjson[]; + components: component[]; + sticker_items: string[]; + //Not gonna send the poll at least for now :P + }; + message_id: string; + message_type_: number; + message_content: string; + message_flags?: number; + channel_id: string; + channel_type: number; + channel_name: string; + channel_icon?: string | null; + guild_id?: string; + user_id: string; + user_username: string; + user_discriminator?: string; + user_avatar?: string; + user_guild_avatar?: string; +} + +export type pushNotifsReal = MessagePush; diff --git a/src/webpage/localuser.ts b/src/webpage/localuser.ts index a435f5d..243c0c7 100644 --- a/src/webpage/localuser.ts +++ b/src/webpage/localuser.ts @@ -2628,6 +2628,31 @@ class Localuser { const donate = settings.addButton(I18n.donate.donate(), {initable: false}); donate.addMDText(new MarkDown(I18n.donate.mdText(window.location.origin + "/donate"))); donate.addText(I18n.donate.desc()); + donate.addButtonInput("", "sub", async () => { + const json = (await ( + await fetch(this.info.api + "/users/@me/devices/vapid", { + headers: this.headers, + }) + ).json()) as {public: string}; + const {token} = (await ( + await fetch(this.info.api + "/users/@me/devices/sync-token", { + headers: this.headers, + }) + ).json()) as {token: string}; + const sub = await SW.registration.pushManager.subscribe({ + userVisibleOnly: true, + applicationServerKey: json.public, + }); + + await fetch(this.info.api + "/users/@me/devices/vapid", { + headers: this.headers, + method: "POST", + body: JSON.stringify({ + token, + subscription_info: sub, + }), + }); + }); } settings.addButton(I18n.localuser.general(), { head: true, diff --git a/src/webpage/service.ts b/src/webpage/service.ts index 3bd8919..8d18895 100644 --- a/src/webpage/service.ts +++ b/src/webpage/service.ts @@ -1,3 +1,4 @@ +import {pushNotifsReal} from "./jsontypes"; import {messageFrom, messageTo} from "./utils/serviceType"; async function deleteoldcache() { @@ -408,3 +409,27 @@ self.addEventListener("message", (message) => { } } }); +interface PushEvent extends ExtendableEvent { + data: PushMessageData; +} +declare var registration: { + showNotification: ( + title: string, + opts: { + body: string; + }, + ) => Promise; +}; +self.addEventListener("push", async (event) => { + const e = event as PushEvent; + const payload = e.data?.text(); + if (!payload) { + return; + } + const obj = JSON.parse(payload) as pushNotifsReal; + e.waitUntil( + registration.showNotification(obj.user_username, { + body: obj.message_content, + }), + ); +});