diff --git a/package.json b/package.json index 5b347ef..87451a2 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "comty.js", - "version": "0.62.1", + "version": "0.63.0", "main": "./dist/index.js", "description": "Official Comty API for JavaScript", "homepage": "https://github.com/ragestudio/comty.js", @@ -18,7 +18,7 @@ "js-cookie": "^3.0.5", "jsonwebtoken": "^9.0.0", "jwt-decode": "^4.0.0", - "linebridge-client": "^1.0.0", + "linebridge-client": "^1.1.0", "luxon": "^3.6.0", "socket.io-client": "^4.8.1" }, diff --git a/src/index.js b/src/index.js index 6e5caca..c9778b9 100755 --- a/src/index.js +++ b/src/index.js @@ -67,8 +67,6 @@ export function createClient({ if (sessionToken) { config.headers["Authorization"] = `${globalThis.isServerMode ? "Server" : "Bearer"} ${sessionToken}` - } else { - console.warn("Making a request with no session token") } } diff --git a/src/models/events/index.js b/src/models/events/index.js new file mode 100644 index 0000000..31a9886 --- /dev/null +++ b/src/models/events/index.js @@ -0,0 +1,21 @@ +import request from "../../request" + +export default class EventsModel { + static async getFeatured() { + const { data } = await request({ + method: "GET", + url: "/featured/events", + }) + + return data + } + + static async data(id) { + const { data } = await request({ + method: "GET", + url: `/events/${id}/data`, + }) + + return data + } +} diff --git a/src/models/spectrum/index.js b/src/models/spectrum/index.js index c387093..fc44819 100644 --- a/src/models/spectrum/index.js +++ b/src/models/spectrum/index.js @@ -1,8 +1,10 @@ import axios from "axios" -import SessionService from "../session" -//import User from "comty.js/models/user" +import SessionModel from "../session" +import UserModel from "../user" +import { RTEngineClient } from "linebridge-client/src" +//import { RTEngineClient } from "../../../../linebridge/client/src" -async function injectUserData(list) { +async function injectUserDataOnList(list) { if (!Array.isArray(list)) { return list } @@ -11,7 +13,21 @@ async function injectUserData(list) { return item.user_id }) - //const users = await User.data(user_ids.join(",")) + let users = await UserModel.data({ user_id: user_ids.join(",") }) + + if (!Array.isArray(users)) { + users = [users] + } + + const userMap = new Map(users.map((user) => [user._id, user])) + + list = list.map((item) => { + const user = userMap.get(item.user_id) + return { + ...item, + user: user, + } + }) return list } @@ -28,9 +44,9 @@ export default class Streaming { }, }) - if (SessionService.token) { + if (SessionModel.token) { baseInstance.defaults.headers.common["Authorization"] = - `Bearer ${SessionService.token}` + `Bearer ${SessionModel.token}` } return baseInstance @@ -47,6 +63,19 @@ export default class Streaming { } } + static async getStream(stream_id) { + if (!stream_id) { + return null + } + + const { data } = await Streaming.base({ + method: "get", + url: `/streaming/${stream_id}`, + }) + + return data + } + static async getOwnProfiles() { const { data } = await Streaming.base({ method: "get", @@ -69,14 +98,11 @@ export default class Streaming { return data } - static async getStream({ profile_id }) { - if (!profile_id) { - return null - } - + static async createOrUpdateProfile(update) { const { data } = await Streaming.base({ - method: "get", - url: `/streaming/${profile_id}`, + method: "put", + url: `/streaming/profiles/self`, + data: update, }) return data @@ -95,22 +121,7 @@ export default class Streaming { return data } - static async createOrUpdateStream(update) { - const { data } = await Streaming.base({ - method: "put", - url: `/streaming/profiles/self`, - data: update, - }) - - return data - } - - static async getConnectionStatus({ profile_id }) { - console.warn("getConnectionStatus() | Not implemented") - return false - } - - static async getLivestreamsList({ limit, offset } = {}) { + static async list({ limit, offset } = {}) { let { data } = await Streaming.base({ method: "get", url: "/streaming/list", @@ -120,17 +131,41 @@ export default class Streaming { }, }) - data = await injectUserData(data) + data = await injectUserDataOnList(data) return data } - static async getLivestreamData(livestream_id) { - const { data } = await Streaming.base({ - method: "get", - url: `/streaming/${livestream_id}`, + static async createStreamWebsocket(stream_id, params = {}) { + if (!stream_id) { + console.error("stream_id is required") + return null + } + + const client = new RTEngineClient({ + ...params, + url: Streaming.apiHostname, + token: SessionModel.token, }) - return data + client._destroy = client.destroy + + client.destroy = () => { + client.emit("stream:leave", stream_id) + + if (typeof client._destroy === "function") { + client._destroy() + } + } + + client.requestState = async () => { + return await client.call("stream:state", stream_id) + } + + client.on("connected", () => { + client.emit("stream:join", stream_id) + }) + + return client } } diff --git a/src/ws.js b/src/ws.js index 971acbd..2e6506a 100644 --- a/src/ws.js +++ b/src/ws.js @@ -98,10 +98,10 @@ class WebsocketManager { return null } - if ( - socket.connected === true && - typeof socket.disconnect === "function" - ) { + const isConnected = + socket.connected === true || socket.state?.connected === true + + if (isConnected && typeof socket.disconnect === "function") { await socket.disconnect() }