Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { initializeApp } from "firebase-admin/app";
import { getFirestore } from "firebase-admin/firestore";
import { onSchedule } from "firebase-functions/v2/scheduler";
import { onRequest } from "firebase-functions/v2/https";
// import { onRequest } from "firebase-functions/v2/https";
import { Streamer, Stream } from "../types";
import { YoutubeClient, TwitchClient, TwitCastingClient } from "./api";
import { defineConfig, sortStreams } from "./utils";
import { createMaster } from "./createMater";
// import { createMaster } from "./createMater";

initializeApp();
const config = defineConfig();
Expand Down Expand Up @@ -123,9 +123,9 @@ export const getStreams = onSchedule(
id: doc.id,
data: doc.data() as Stream,
}));
const streams = sortStreams(currentStreams, pastStreams);
const now = new Date();
const streams = sortStreams(currentStreams, pastStreams, now.getTime());

const endTime = new Date().toISOString();
for await (const { id, data } of streams.ended) {
let stream = data;

Expand All @@ -139,11 +139,14 @@ export const getStreams = onSchedule(
}
}

batch.update(streamRef.doc(id), { ...stream, endTime });
batch.update(streamRef.doc(id), {
...stream,
endTime: now.toISOString(),
});
}

for (const { id, data } of streams.updated)
batch.update(streamRef.doc(id), data);
// for (const { id, data } of streams.updated)
// batch.update(streamRef.doc(id), data);

for (const newStream of streams.new) {
const streamerId = master[newStream.platform].get(newStream.channelId);
Expand All @@ -154,12 +157,12 @@ export const getStreams = onSchedule(
}
);

export const createStreamerMaster = onRequest(
{
region: "asia-northeast1",
},
async (_, res) => {
createMaster(db, config.collection.master.value());
res.status(200).send("updated");
}
);
// export const createStreamerMaster = onRequest(
// {
// region: "asia-northeast1",
// },
// async (_, res) => {
// createMaster(db, config.collection.master.value());
// res.status(200).send("updated");
// }
// );
13 changes: 12 additions & 1 deletion functions/src/utils/sortStreams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ type Returns<T extends BaseStream> = {
ended: { id: string; data: T }[]; // {document id, document data}[]
};

const isStreamEnd = <T extends BaseStream>(stream: T, now: number) => {
return stream.endTime && new Date(stream.endTime).getTime() <= now;
};

const diff = <T extends BaseStream>(a: T, b: T) => {
return (
a.title !== b.title ||
Expand All @@ -49,7 +53,8 @@ const diff = <T extends BaseStream>(a: T, b: T) => {

export const sortStreams = <T extends BaseStream>(
currentStreams: T[],
pastStreams: { id: string; data: T }[]
pastStreams: { id: string; data: T }[],
now: number
): Returns<T> => {
const currentStreamMap = new Map(currentStreams.map((s) => [s.id, s]));

Expand All @@ -62,6 +67,12 @@ export const sortStreams = <T extends BaseStream>(
return result;
}

if (isStreamEnd(currentStream, now)) {
result.ended.push(pastStream);
currentStreamMap.delete(currentStream.id);
return result;
}

currentStreamMap.delete(currentStream.id);

if (diff(pastStream.data, currentStream))
Expand Down
213 changes: 213 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
"@hookform/resolvers": "^5.0.1",
"@radix-ui/react-avatar": "^1.1.7",
"@radix-ui/react-dialog": "^1.1.11",
"@radix-ui/react-scroll-area": "^1.2.8",
"@radix-ui/react-select": "^2.2.4",
"@radix-ui/react-slot": "^1.2.2",
"@radix-ui/react-switch": "^1.2.3",
"@radix-ui/react-toggle": "^1.1.8",
"@radix-ui/react-tooltip": "^1.2.6",
"@tailwindcss/vite": "^4.1.5",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
Expand Down
42 changes: 42 additions & 0 deletions src/components/toggleButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ComponentProps, ReactNode } from "react";
import { cn } from "@/lib/utils";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "./ui/tooltip";
import { Toggle } from "./ui/toggle";

type Props = {
children: ReactNode;
description: string;
} & ComponentProps<typeof Toggle>;

export function ToggleButton({
children,
className,
description,
...props
}: Props) {
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<Toggle
{...props}
variant="outline"
className={cn(
"data-[state=on]:bg-vspo-primary transition-colors data-[state=on]:border-secondary/30",
className
)}
children={children}
/>
</TooltipTrigger>
<TooltipContent side="bottom">
<p>{description}</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
);
}
Loading