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

Feat: Added eventapi metadata #1104

Merged
merged 6 commits into from
Oct 27, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG-nightly.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### 3.1.4.1000

- Fixed an issue with avatars not loading

### 3.1.3.1000

- Added option hide shared chat
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "7TV",
"description": "Improve your viewing experience on Twitch & YouTube with new features, emotes, vanity and performance.",
"private": true,
"version": "3.1.3",
"version": "3.1.4",
"dev_version": "1.0",
"scripts": {
"start": "NODE_ENV=dev yarn build:dev && NODE_ENV=dev vite --mode dev",
Expand Down
1 change: 0 additions & 1 deletion src/worker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ export enum EventAPIOpCode {
RESUME = 34,
SUBSCRIBE = 35,
UNSUBSCRIBE = 36,
BRIDGE = 38,

UNKNOWN = 1001,
}
Expand Down
2 changes: 2 additions & 0 deletions src/worker/worker.driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type WorkerEventName =
| "set_channel_presence"
| "identity_updated"
| "user_updated"
| "request_user_cosmetics"
| "imageformat_updated";

type WorkerTypedEvent<EVN extends WorkerEventName> = {
Expand All @@ -155,6 +156,7 @@ type WorkerTypedEvent<EVN extends WorkerEventName> = {
channel_data_fetched: CurrentChannel;
identity_updated: TwitchIdentity | YouTubeIdentity;
user_updated: SevenTV.User;
request_user_cosmetics: ["id" | "username", string][];
imageformat_updated: string;
}[EVN];

Expand Down
10 changes: 8 additions & 2 deletions src/worker/worker.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class EventAPI {
subscriptions: Record<string, SubscriptionRecord[]> = {};

url = import.meta.env.VITE_APP_API_EVENTS;
version = import.meta.env.VITE_APP_VERSION;
private socket: WebSocket | null = null;
private eventSource: EventSource | null = null;
private shouldResume = false;
Expand All @@ -40,10 +41,15 @@ export class EventAPI {
connect(transport: EventAPITransport): void {
if (this.eventSource || this.socket || !this.url) return;

const url = new URL(this.url);

url.searchParams.append("app", "7tv-extension");
url.searchParams.append("version", this.version);

this.transport = transport;

if (this.transport === "WebSocket") {
this.socket = new WebSocket(this.url);
this.socket = new WebSocket(url);
this.socket.onopen = () => this.onOpen();
this.socket.onclose = () => this.onClose();
this.socket.onmessage = (ev) => {
Expand Down Expand Up @@ -116,7 +122,7 @@ export class EventAPI {
);
}

private onDispatch(msg: EventAPIMessage<"DISPATCH">): void {
onDispatch(msg: EventAPIMessage<"DISPATCH">): void {
handleDispatchedEvent(this.getContext(), msg.data.type, msg.data.body);

log.debugWithObjects(["<EventAPI>", "Dispatch received"], [msg.data]);
Expand Down
33 changes: 33 additions & 0 deletions src/worker/worker.http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { convertBttvEmoteSet, convertFFZEmoteSet, convertFfzBadges } from "@/com
import { db } from "@/db/idb";
import type { WorkerDriver } from "./worker.driver";
import type { WorkerPort } from "./worker.port";
import { EventAPIMessage } from ".";

namespace API_BASE {
export const SEVENTV = import.meta.env.VITE_APP_API;
Expand Down Expand Up @@ -66,6 +67,21 @@ export class WorkerHttp {
if (!ev.port) return;
WorkerHttp.imageFormat = ev.port.imageFormat!;
});
driver.addEventListener("request_user_cosmetics", async (ev) => {
if (!ev.port) return;

const cosmeticEvents = await this.API()
.seventv.loadUserCosmetics(ev.detail)
.catch(() => void 0);

if (!cosmeticEvents) {
return;
}

for (const cosmeticEvent of cosmeticEvents) {
this.driver.eventAPI.onDispatch(cosmeticEvent);
}
});
}

public async fetchConfig(): Promise<SevenTV.Config> {
Expand Down Expand Up @@ -300,6 +316,23 @@ export const seventv = {

return Promise.resolve(userConn.user);
},

async loadUserCosmetics(identifiers: ["id" | "username", string][]): Promise<EventAPIMessage<"DISPATCH">[]> {
const body = {
identifiers: identifiers.map(([idType, id]) => `${idType}:${id}`),
};

const resp = await doRequest(API_BASE.SEVENTV, "bridge/event-api", "POST", body).catch((err) =>
Promise.reject(err),
);

if (!resp || resp.status !== 200) {
return Promise.reject(resp);
}

const cosmetics = (await resp.json()) as EventAPIMessage<"DISPATCH">[];
return Promise.resolve(cosmetics);
},
};

export const frankerfacez = {
Expand Down
12 changes: 1 addition & 11 deletions src/worker/worker.port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,7 @@ export class WorkerPort {
if (!Array.isArray(identifiers) || !this.platform || !kinds.length) break;

log.debugWithObjects(["Requesting cosmetics"], [identifiers, kinds]);
this.driver.eventAPI.sendMessage({
op: "BRIDGE",
data: {
command: "userstate",
body: {
identifiers: identifiers.map(([idType, id]) => `${idType}:${id}`),
platform: this.platform,
kinds,
},
},
});
this.driver.emit("request_user_cosmetics", identifiers, this);
break;
}
case "CLOSE":
Expand Down
Loading