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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,6 @@ web/.tanstack
.idea/
*.iws
*.iml
*.ipr
*.ipr

.claude/worktrees
77 changes: 32 additions & 45 deletions server/src/api/channelsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ import { MaterializeProgramGroupings } from '../commands/MaterializeProgramGroup
import { MaterializeProgramsCommand } from '../commands/MaterializeProgramsCommand.ts';
import { RegenerateChannelLineupCommand } from '../commands/RegenerateChannelLineupCommand.ts';
import { container } from '../container.ts';
import { sessionToApiSession } from '../db/converters/sessionConverter.ts';
import { transcodeConfigOrmToDto } from '../db/converters/transcodeConfigConverters.ts';
import type { ChannelAndLineup } from '../db/interfaces/IChannelDB.ts';
import type { ChannelOrmWithRelations } from '../db/schema/derivedTypes.ts';
import type { SessionType } from '../stream/Session.ts';
import { Result } from '../types/result.ts';
import { PagingParams } from '../types/schemas.ts';

Expand Down Expand Up @@ -114,23 +114,11 @@ export const channelsApi: RouterPluginAsyncCallback = async (fastify) => {
const sessions = sessionsByChannel[channelAndLineup.channel.uuid];
const apiSessions = reduce(
sessions,
(prev, session, sessionType) => {
(prev, session) => {
if (!session) {
return prev;
}
prev.push({
connections: map(
session.connections(),
(connection, token) => ({
...connection,
lastHeartbeat: session?.lastHeartbeat(token),
}),
),
type: sessionType as SessionType,
state: session.state,
numConnections: session.numConnections(),
} satisfies ChannelSession);

prev.push(sessionToApiSession(session));
return prev;
},
[] as ChannelSession[],
Expand Down Expand Up @@ -166,38 +154,37 @@ export const channelsApi: RouterPluginAsyncCallback = async (fastify) => {
const channelAndLineup =
await req.serverCtx.channelDB.loadChannelAndLineupOrm(req.params.id);

if (!isNil(channelAndLineup)) {
// TODO: This is super gnarly and we're doing this sorta custom everywhere.
// We need a centralized way to either load ALL of the relevant metadata
// for channels OR have the frontend request which fields it needs and we
// service that.
const [channelFillers, channelSubtitles] = await Promise.all([
req.serverCtx.fillerDB.getFillersFromChannel(req.params.id),
req.serverCtx.channelDB.getChannelSubtitlePreferences(
req.params.id,
),
]);

const apiChannel = dbChannelToApiChannel({
...channelAndLineup,
channel: {
...channelAndLineup.channel,
subtitlePreferences: channelSubtitles,
},
});

const channelWithFillers = {
...apiChannel,
fillerCollections: channelFillers.map((cf) => ({
id: cf.fillerShow.uuid,
cooldownSeconds: cf.cooldown,
weight: cf.weight,
})),
};
return res.send(channelWithFillers);
} else {
if (isNil(channelAndLineup)) {
return res.status(404).send();
}

const [channelFillers, channelSubtitles] = await Promise.all([
req.serverCtx.fillerDB.getFillersFromChannel(req.params.id),
req.serverCtx.channelDB.getChannelSubtitlePreferences(req.params.id),
]);

const sessions = req.serverCtx.sessionManager.getAllSessionsForChannel(
channelAndLineup.channel.uuid,
);

const apiChannel = dbChannelToApiChannel({
...channelAndLineup,
channel: {
...channelAndLineup.channel,
subtitlePreferences: channelSubtitles,
},
});

const channelWithFillers = {
...apiChannel,
fillerCollections: channelFillers.map((cf) => ({
id: cf.fillerShow.uuid,
cooldownSeconds: cf.cooldown,
weight: cf.weight,
})),
sessions: sessions.map(sessionToApiSession),
};
return res.send(channelWithFillers);
} catch (err) {
logger.error(err, req.routeOptions.config.url);
return res.status(500).send();
Expand Down
23 changes: 23 additions & 0 deletions server/src/db/converters/sessionConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { ChannelSession } from '@tunarr/types';
import dayjs from 'dayjs';
import { isNil, map } from 'lodash-es';
import type { Session } from '../../stream/Session.ts';

export function sessionToApiSession(session: Session): ChannelSession {
return {
connections: map(session.connections(), (connection, token) => {
const lastHeartbeat = session?.lastHeartbeat(token);
console.log(dayjs(lastHeartbeat).format());
return {
...connection,
lastHeartbeat,
lastHeartbeatStr: !isNil(lastHeartbeat)
? dayjs(lastHeartbeat).format()
: undefined,
};
}),
type: session.sessionType,
state: session.state,
numConnections: session.numConnections(),
} satisfies ChannelSession;
}
2 changes: 1 addition & 1 deletion types/src/schemas/channelSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export const ChannelConcatStreamModes = [
export type ChannelStreamMode = TupleToUnion<typeof ChannelStreamModes>;
export const ChannelStreamModeSchema = z.enum(ChannelStreamModes);


export type ChannelConcatStreamMode = TupleToUnion<
typeof ChannelConcatStreamModes
>;
Expand All @@ -113,6 +112,7 @@ export const StreamConnectionDetailsSchema = z.object({
ip: z.ipv4().or(z.ipv6()),
userAgent: z.string().optional(),
lastHeartbeat: z.number().nonnegative().optional(),
lastHeartbeatStr: z.iso.datetime({ offset: true }).optional(),
});

export const ChannelSessionSchema = z.object({
Expand Down
Loading