feat: time sync

closes #75
This commit is contained in:
2023-11-14 15:27:35 +00:00
parent 27cf614048
commit 080955532c
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 type { EmojiPack, Emoji } from "types";
import { LIVE_STREAM_CHAT } from "const"; import { LIVE_STREAM_CHAT } from "const";
import { SnortContext } from "@snort/system-react"; import { SnortContext } from "@snort/system-react";
import { unixNowMs } from "@snort/shared";
import { TimeSync } from "index";
export function WriteMessage({ link, emojiPacks }: { link: NostrLink; emojiPacks: EmojiPack[] }) { export function WriteMessage({ link, emojiPacks }: { link: NostrLink; emojiPacks: EmojiPack[] }) {
const system = useContext(SnortContext); 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)); const emoji = [...emojiNames].map(name => emojis.find(e => e.at(1) === name));
eb.kind(LIVE_STREAM_CHAT as EventKind) eb.kind(LIVE_STREAM_CHAT as EventKind)
.content(chat) .content(chat)
.createdAt(Math.floor((unixNowMs() - TimeSync) / 1000))
.tag(["a", `${link.kind}:${link.author}:${link.id}`, "", "root"]) .tag(["a", `${link.kind}:${link.author}:${link.id}`, "", "root"])
.processContent(); .processContent();
for (const e of emoji) { for (const e of emoji) {

View File

@ -24,6 +24,7 @@ import { register } from "serviceWorker";
import { IntlProvider } from "intl"; import { IntlProvider } from "intl";
import { WidgetsPage } from "pages/widgets"; import { WidgetsPage } from "pages/widgets";
import { AlertsPage } from "pages/alerts"; import { AlertsPage } from "pages/alerts";
import { unixNowMs } from "@snort/shared";
export enum StreamState { export enum StreamState {
Live = "live", Live = "live",
@ -45,12 +46,23 @@ Object.entries(defaultRelays).forEach(params => {
System.ConnectToRelay(relay, settings); System.ConnectToRelay(relay, settings);
}); });
export let TimeSync = 0;
const router = createBrowserRouter([ const router = createBrowserRouter([
{ {
element: <LayoutPage />, element: <LayoutPage />,
loader: async () => { loader: async () => {
db.ready = await db.isAvailable(); db.ready = await db.isAvailable();
await System.Init(); 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; return null;
}, },
children: [ children: [