feat: time sync

closes #75
This commit is contained in:
Kieran 2023-11-14 15:27:35 +00:00
parent 27cf614048
commit 080955532c
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
2 changed files with 15 additions and 0 deletions

View File

@ -10,6 +10,8 @@ import { EmojiPicker } from "element/emoji-picker";
import type { EmojiPack, Emoji } from "types";
import { LIVE_STREAM_CHAT } from "const";
import { SnortContext } from "@snort/system-react";
import { unixNowMs } from "@snort/shared";
import { TimeSync } from "index";
export function WriteMessage({ link, emojiPacks }: { link: NostrLink; emojiPacks: EmojiPack[] }) {
const system = useContext(SnortContext);
@ -39,6 +41,7 @@ export function WriteMessage({ link, emojiPacks }: { link: NostrLink; emojiPacks
const emoji = [...emojiNames].map(name => emojis.find(e => e.at(1) === name));
eb.kind(LIVE_STREAM_CHAT as EventKind)
.content(chat)
.createdAt(Math.floor((unixNowMs() - TimeSync) / 1000))
.tag(["a", `${link.kind}:${link.author}:${link.id}`, "", "root"])
.processContent();
for (const e of emoji) {

View File

@ -24,6 +24,7 @@ import { register } from "serviceWorker";
import { IntlProvider } from "intl";
import { WidgetsPage } from "pages/widgets";
import { AlertsPage } from "pages/alerts";
import { unixNowMs } from "@snort/shared";
export enum StreamState {
Live = "live",
@ -45,12 +46,23 @@ Object.entries(defaultRelays).forEach(params => {
System.ConnectToRelay(relay, settings);
});
export let TimeSync = 0;
const router = createBrowserRouter([
{
element: <LayoutPage />,
loader: async () => {
db.ready = await db.isAvailable();
await System.Init();
try {
const req = await fetch("https://api.zap.stream/api/time");
const nowAtServer = (await req.json()).time as number;
const now = unixNowMs();
TimeSync = now - nowAtServer;
console.debug("Time clock sync", TimeSync);
} catch {
// ignore
}
return null;
},
children: [