mirror of
https://github.com/MathMan05/Fermi.git
synced 2026-10-05 18:48:22 +00:00
add dummy button to subscribe to push notifications
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<void>;
|
||||
};
|
||||
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,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user