Skip to content

Commit

Permalink
rework channel loading and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBrax committed Nov 18, 2023
1 parent 1fd3159 commit d2e9b1d
Show file tree
Hide file tree
Showing 4 changed files with 442 additions and 119 deletions.
27 changes: 24 additions & 3 deletions server/src/Core/LiveStreamDVR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,25 @@ export class LiveStreamDVR {
// TwitchHelper.refreshUserAccessToken();
}

public static migrateChannelConfig(channel: ChannelConfig): void {
if (channel.provider == "twitch") {
if (!channel.internalName && channel.login) {
channel.internalName = channel.login;
}
} else if (channel.provider == "youtube") {
if (!channel.internalName && channel.channel_id) {
channel.internalName = channel.channel_id;
}
if (!channel.internalId && channel.channel_id) {
channel.internalId = channel.channel_id;
}
} else if (channel.provider == "kick") {
if (!channel.internalName && channel.slug) {
channel.internalName = channel.slug;
}
}
}

/**
* @test disable
* @returns
Expand Down Expand Up @@ -255,6 +274,8 @@ export class LiveStreamDVR {
);
needsSave = true;
}

/*
if (!channel.internalName || !channel.internalId) {
if (channel.provider == "twitch") {
channel.internalName = channel.login || "";
Expand All @@ -267,6 +288,8 @@ export class LiveStreamDVR {
// throw new Error(`Channel ${channel.uuid} does not have an internalName`);
}
}
*/
LiveStreamDVR.migrateChannelConfig(channel);
}

this.channels_config = data;
Expand Down Expand Up @@ -324,9 +347,7 @@ export class LiveStreamDVR {
let ch: TwitchChannel;

try {
ch = await TwitchChannel.loadFromLogin(
channel.internalName
);
ch = await TwitchChannel.load(channel.uuid);
} catch (th) {
log(
LOGLEVEL.FATAL,
Expand Down
104 changes: 57 additions & 47 deletions server/src/Core/Providers/Twitch/TwitchChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,32 +1653,43 @@ export class TwitchChannel extends BaseChannel {

// TODO: load by uuid?
public static async loadAbstract(
channel_id: string
// channel_id: string
uuid: string
): Promise<TwitchChannel> {
log(
LOGLEVEL.DEBUG,
"tw.channel.loadAbstract",
`Load channel ${channel_id}`
);
log(LOGLEVEL.DEBUG, "tw.channel.loadAbstract", `Load channel ${uuid}`);

const channel_memory = LiveStreamDVR.getInstance()
.getChannels()
.find<TwitchChannel>(
(channel): channel is TwitchChannel =>
isTwitchChannel(channel) &&
channel.internalId === channel_id
isTwitchChannel(channel) && channel.uuid === uuid
);
if (channel_memory) {
log(
LOGLEVEL.WARNING,
"tw.channel.loadAbstract",
`Channel ${channel_id} already loaded`
`Channel ${uuid} (${channel_memory.internalName}) already exists in memory, returning`
);
return channel_memory;
}

const channel_config = LiveStreamDVR.getInstance().channels_config.find(
(c) => c.provider == "twitch" && c.uuid === uuid
);

if (!channel_config)
throw new Error(`Could not find channel config for uuid ${uuid}`);

const channel_id =
channel_config.internalId ||
(await this.channelIdFromLogin(channel_config.internalName));

if (!channel_id)
throw new Error(
`Could not get channel id for login ${channel_config.internalName}`
);

const channel = new this();
// channel.userid = channel_id;

const channel_data = await this.getUserDataById(channel_id);
if (!channel_data)
Expand All @@ -1688,16 +1699,6 @@ export class TwitchChannel extends BaseChannel {

const channel_login = channel_data.login;

const channel_config = LiveStreamDVR.getInstance().channels_config.find(
(c) =>
c.provider == "twitch" &&
(c.login === channel_login || c.internalName === channel_login)
);
if (!channel_config)
throw new Error(
`Could not find channel config in memory for channel login: ${channel_login}`
);

channel.uuid = channel_config.uuid;
channel.channel_data = channel_data;
channel.config = channel_config;
Expand Down Expand Up @@ -1826,18 +1827,19 @@ export class TwitchChannel extends BaseChannel {
public static async create(
config: TwitchChannelConfig
): Promise<TwitchChannel> {
// check if channel already exists in config
const exists_config = LiveStreamDVR.getInstance().channels_config.find(
(ch) =>
ch.provider == "twitch" &&
(ch.login === config.login ||
ch.internalName === config.login ||
(ch.login === config.internalName ||
ch.internalName === config.internalName)
);
if (exists_config)
throw new Error(
`Channel ${config.internalName} already exists in config`
);

// check if channel already exists in memory
const exists_channel = LiveStreamDVR.getInstance()
.getChannels()
.find<TwitchChannel>(
Expand All @@ -1850,6 +1852,7 @@ export class TwitchChannel extends BaseChannel {
`Channel ${config.internalName} already exists in channels`
);

// fetch channel data
const data = await TwitchChannel.getUserDataByLogin(
config.internalName
);
Expand All @@ -1863,7 +1866,8 @@ export class TwitchChannel extends BaseChannel {
LiveStreamDVR.getInstance().channels_config.push(config);
LiveStreamDVR.getInstance().saveChannelsConfig();

const channel = await TwitchChannel.loadFromLogin(config.internalName);
// const channel = await TwitchChannel.loadFromLogin(config.internalName);
const channel = await TwitchChannel.load(config.uuid);
if (!channel || !channel.internalName)
throw new Error(
`Channel ${config.internalName} could not be loaded`
Expand Down Expand Up @@ -2006,7 +2010,7 @@ export class TwitchChannel extends BaseChannel {
let response;

if (!TwitchHelper.hasAxios()) {
throw new Error("Axios is not initialized");
throw new Error("Axios is not initialized (getStreams)");
}

try {
Expand Down Expand Up @@ -2047,26 +2051,24 @@ export class TwitchChannel extends BaseChannel {
return json.data ?? false;
}

/**
* Load channel class using login, don't call this. Used internally.
*
* @internal
* @param login
* @returns
*/
public static async loadFromLogin(login: string): Promise<TwitchChannel> {
if (!login) throw new Error("Streamer login is empty");
if (typeof login !== "string")
throw new TypeError("Streamer login is not a string");
log(
LOGLEVEL.DEBUG,
"tw.channel.loadFromLogin",
`Load from login ${login}`
public static async load(uuid: string): Promise<TwitchChannel> {
/*
const channel_config = LiveStreamDVR.getInstance().channels_config.find(
(c) => c.uuid === uuid
);
const channel_id = await this.channelIdFromLogin(login);
if (!channel_id)
throw new Error(`Could not get channel id from login: ${login}`);
return this.loadAbstract(channel_id); // $channel;
if (!channel_config)
throw new Error(`Could not find channel config for uuid: ${uuid}`);
const channel_data = await this.getUserDataByLogin(
channel_config.internalName
);
if (!channel_data)
throw new Error(
`Could not get channel data for channel login: ${channel_config.internalName}`
);
*/
return await this.loadAbstract(uuid);
}

public static async channelIdFromLogin(
Expand Down Expand Up @@ -2141,6 +2143,10 @@ export class TwitchChannel extends BaseChannel {
`Fetching user data for ${method} ${identifier}, force: ${force}`
);

if (identifier == undefined || identifier == null || identifier == "") {
throw new Error(`getUserDataProxy: identifier is empty`);
}

// check cache first
if (!force) {
const channelData =
Expand Down Expand Up @@ -2202,7 +2208,7 @@ export class TwitchChannel extends BaseChannel {
*/

if (!TwitchHelper.hasAxios()) {
throw new Error("Axios is not initialized");
throw new Error("Axios is not initialized (getUserDataProxy)");
}

let response;
Expand Down Expand Up @@ -2478,7 +2484,7 @@ export class TwitchChannel extends BaseChannel {
);

if (!TwitchHelper.hasAxios()) {
throw new Error("Axios is not initialized");
throw new Error("Axios is not initialized (getChannelDataById)");
}

let response;
Expand Down Expand Up @@ -2660,7 +2666,9 @@ export class TwitchChannel extends BaseChannel {
};

if (!TwitchHelper.hasAxios()) {
throw new Error("Axios is not initialized");
throw new Error(
"Axios is not initialized (subscribeToIdWithWebhook)"
);
}

let response;
Expand Down Expand Up @@ -3006,7 +3014,9 @@ export class TwitchChannel extends BaseChannel {
};

if (!TwitchHelper.hasAxios()) {
throw new Error("Axios is not initialized");
throw new Error(
"Axios is not initialized (subscribeToIdWithWebsocket)"
);
}

let response;
Expand Down
Loading

0 comments on commit d2e9b1d

Please sign in to comment.