Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added streamlabs section to the settings menu and streamlabs donation node #147

Merged
merged 11 commits into from
Jul 8, 2023
63 changes: 63 additions & 0 deletions app/src/settings/Streamlabs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { z } from "zod";
import { createForm, zodForm } from "@modular-forms/solid";
import { streamlabs } from "@macrograph/packages";
import { Match, Switch } from "solid-js";
import { Button, Input } from "./ui";
import { None, Some } from "@macrograph/core";

const Schema = z.object({
socketToken: z.string(),
});

const Api = () => {
return (
<div class="flex flex-col space-y-2">
<span class="text-neutral-400 font-medium">Socket API</span>
<Switch fallback="Loading...">
<Match when={streamlabs.state().type === "disconnected"}>
{(_) => {
const [, { Form, Field }] = createForm({
validate: zodForm(Schema),
});

return (
<Form
onSubmit={(d) => {
streamlabs.setToken(Some(d.socketToken));
}}
class="flex flex-row space-x-4"
>
<Field name="socketToken">
{(field, props) => (
<Input
{...props}
type="password"
placeholder="Socket API Key"
value={field.value}
/>
)}
</Field>
<Button type="submit">Submit</Button>
</Form>
);
}}
</Match>
<Match when={streamlabs.state().type === "connected"}>
<div class="flex flex-row items-center space-x-4">
<Button onClick={() => streamlabs.setToken(None)}>
Disconnect
</Button>
</div>
</Match>
</Switch>
</div>
);
};

export default () => {
return (
<>
<Api />
</>
);
};
4 changes: 4 additions & 0 deletions app/src/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Twitch from "./Twitch";
import { Button } from "./ui";
import { SerializedProject, core } from "@macrograph/core";
import { useUIStore } from "~/UIStore";
import Streamlabs from "./Streamlabs";

export default () => {
const ui = useUIStore();
Expand Down Expand Up @@ -65,6 +66,9 @@ const SettingsDialog = () => {
<Section title="OBS">
<OBS />
</Section>
<Section title="Streamlabs">
<Streamlabs />
</Section>
</div>
</div>
</Dialog.Content>
Expand Down
1 change: 1 addition & 0 deletions packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@twurple/auth": "^6.2.0",
"discord.js": "^14.9.0",
"obs-websocket-js": "^5.0.2",
"socket.io-client": "^4.7.1",
"tmi.js": "^1.8.5",
"typescript-result-option": "^0.2.5",
"zod": "^3.21.4"
Expand Down
1 change: 1 addition & 0 deletions packages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * as discord from "./discord";
export * from "./localStorage";
export * from "./json";
export * from "./map";
export * as streamlabs from "./streamlabs";
35 changes: 35 additions & 0 deletions packages/src/streamlabs/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { z } from "zod";

export const STREAMLABS_DONATION = z.object({
message: z.array(
z.object({
name: z.string(),
amount: z.coerce.number(),
currency: z.string(),
formattedAmount: z.string(),
message: z.string(),
from: z.string(),
fromId: z.string(),
})
),
type: z.literal("donation"),
});

// export const EVENTS = z.union([STREAMLABS_DONATION]);
export const EVENT = STREAMLABS_DONATION;

export type Event = EventsToObject<z.infer<typeof STREAMLABS_DONATION>>;

type EventsToObject<T extends object> = T extends {
message: any[];
}
? { [key in EventToKey<T>]: T["message"][number] }
: never;

type EventToKey<T extends object> = T extends {
type: infer Type extends string;
}
? T extends { for: infer For extends string }
? `${For}.${Type}`
: `${Type}`
: never;
148 changes: 148 additions & 0 deletions packages/src/streamlabs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { Maybe, Option, core, t } from "@macrograph/core";
import { io, Socket } from "socket.io-client";
import {
createEffect,
createRoot,
createSignal,
on,
onCleanup,
} from "solid-js";
import { EVENT, Event } from "./events";

const pkg = core.createPackage<Event>({
name: "Streamlabs",
});

const STREAMLABS_TOKEN = "streamlabsToken";

const { setToken, token, state } = createRoot(() => {
const [state, setState] = createSignal<
| {
type: "disconnected";
}
| { type: "connecting" }
| {
type: "connected";
socket: Socket;
}
>({ type: "disconnected" });

const [token, setToken] = createSignal<Option<string>>(
Maybe(localStorage.getItem(STREAMLABS_TOKEN))
);

createEffect(
on(
() => token(),
(token) =>
token
.map((token) => (localStorage.setItem(STREAMLABS_TOKEN, token), true))
.unwrapOrElse(
() => (localStorage.removeItem(STREAMLABS_TOKEN), false)
)
)
);

createEffect(
on(
() => token(),
(token) => {
token
.map((token) => {
const socket = io(`https://sockets.streamlabs.com?token=${token}`, {
transports: ["websocket"],
autoConnect: false,
});

socket.on("event", (eventData) => {
const parsed = EVENT.safeParse(eventData);

if (!parsed.success) return;

if (parsed.data.type === "donation") {
pkg.emitEvent({
name: "donation",
data: parsed.data.message[0]!,
});
}
});

socket.on("connect", () => {
setState({ type: "connected", socket });
});

setState({
type: "connecting",
socket,
});

socket.connect();

onCleanup(() => {
socket.close();
setState({ type: "disconnected" });
});
})
.unwrapOrElse(() => setState({ type: "disconnected" }));
}
)
);

return {
token,
state,
setToken,
};
});

export { setToken, token, state };

pkg.createEventSchema({
name: "Streamlabs Donation",
event: "donation",
generateIO: (io) => {
io.execOutput({
id: "exec",
});
io.dataOutput({
name: "Name",
id: "name",
type: t.string(),
});
io.dataOutput({
name: "Amount",
id: "amount",
type: t.float(),
});
io.dataOutput({
name: "Message",
id: "message",
type: t.string(),
});
io.dataOutput({
name: "Currency",
id: "currency",
type: t.string(),
});
io.dataOutput({
name: "From",
id: "from",
type: t.string(),
});
io.dataOutput({
name: "From User Id",
id: "fromId",
type: t.string(),
});
},
run({ ctx, data }) {
ctx.setOutput("name", data.name);
ctx.setOutput("amount", data.amount);
ctx.setOutput("message", data.message);
ctx.setOutput("currency", data.currency);
ctx.setOutput("from", data.from);
ctx.setOutput("fromId", data.fromId);

ctx.exec("exec");
},
});
5 changes: 4 additions & 1 deletion packages/src/twitch/eventsub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ const { state } = createRoot(() => {

setWs({ type: "connecting" });

onCleanup(() => ws.close());
onCleanup(() => {
ws.close();
setSTate({ type: "disconnected" });
});
})
.unwrapOrElse(() => setWs({ type: "disconnected" }));
}
Expand Down
6 changes: 4 additions & 2 deletions packages/src/twitch/helix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ export const { client, userId, setUserId } = createRoot(() => {
path: "https://api.twitch.tv/helix",
fetchFn: async (url, args) => {
const user = await auth.getAccessTokenForUser(userId().unwrap());
const token = auth.tokens.get(userId().unwrap());
await auth.refreshAccessTokenForUser(token?.userId);
const token = Maybe(auth.tokens.get(userId().unwrap())).unwrap();

await auth.refreshAccessTokenForUser(token.userId);

if (args.body instanceof URLSearchParams) {
url = `${url}?${args.body.toString()}`;
}
Expand Down
32 changes: 17 additions & 15 deletions packages/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"compilerOptions": {
"composite": true,
"rootDir": ".",
"strict": true,
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"noUncheckedIndexedAccess": true,
"isolatedModules": true,
"skipLibCheck": true,
"outDir": "dist"
},
"include": ["src"]
"compilerOptions": {
"composite": true,
"rootDir": ".",
"strict": true,
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"noUncheckedIndexedAccess": true,
"isolatedModules": true,
"skipLibCheck": true,
"outDir": "dist",
},
"include": [
"src"
]
}
Loading