feat: multi-account system

This commit is contained in:
2023-04-14 12:33:19 +01:00
parent 723589fec7
commit fe788853c9
58 changed files with 966 additions and 1080 deletions

View File

@ -2,12 +2,19 @@ import Nostrich from "nostrich.webp";
import { TaggedRawEvent } from "@snort/nostr";
import { EventKind } from "@snort/nostr";
import type { NotificationRequest } from "State/Login";
import { MetadataCache } from "Cache";
import { getDisplayName } from "Element/ProfileImage";
import { MentionRegex } from "Const";
import { tagFilterOfTextRepost, unwrap } from "Util";
import { UserCache } from "Cache/UserCache";
import { LoginSession } from "Login";
export interface NotificationRequest {
title: string;
body: string;
icon: string;
timestamp: number;
}
export async function makeNotification(ev: TaggedRawEvent): Promise<NotificationRequest | null> {
switch (ev.kind) {
@ -52,3 +59,20 @@ function replaceTagsWithUser(ev: TaggedRawEvent, users: MetadataCache[]) {
})
.join();
}
export async function sendNotification(state: LoginSession, req: NotificationRequest) {
const hasPermission = "Notification" in window && Notification.permission === "granted";
const shouldShowNotification = hasPermission && req.timestamp > state.readNotifications;
if (shouldShowNotification) {
try {
const worker = await navigator.serviceWorker.ready;
worker.showNotification(req.title, {
tag: "notification",
vibrate: [500],
...req,
});
} catch (error) {
console.warn(error);
}
}
}