Skip to content

Commit

Permalink
feat(egh): add method to add Egghead lessons to playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
Creeland committed Jan 24, 2025
1 parent 2d32fbb commit e19baf7
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
import Tree from '@/components/lesson-list/tree'
import { CreatePostForm } from '@/components/resources-crud/create-post-form'
import { SearchExistingLessons } from '@/components/resources-crud/search-existing-lessons'
import { addResourceToResource, createPost } from '@/lib/posts-query'
import {
addEggheadLessonToPlaylist,
addResourceToResource,
createPost,
} from '@/lib/posts-query'
import { createResource } from '@/lib/resources/create-resources'

import type { ContentResource } from '@coursebuilder/core/schemas'
Expand Down Expand Up @@ -93,6 +97,11 @@ export function ResourceResourcesList({
})

if (resourceItem) {
await addEggheadLessonToPlaylist({
eggheadLessonId: resourceItem.resource.fields?.eggheadLessonId,
eggheadPlaylistId: resource.fields?.eggheadPlaylistId,
})

updateState({
type: 'add-item',
itemId: resourceItem.resource.id,
Expand Down
10 changes: 9 additions & 1 deletion apps/egghead/src/lib/egghead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import slugify from '@sindresorhus/slugify'
export type EggheadLessonState = 'published' | 'approved' | 'retired'
export type EggheadLessonVisibilityState = 'indexed' | 'hidden'

export async function getEggheadUserProfile(userId: string) {
export const EGGHEAD_API_V1_BASE_URL = 'https://app.egghead.io/api/v1'

export async function getEggheadToken(userId: string) {
const user = await db.query.users.findFirst({
where: eq(users.id, userId),
with: {
Expand Down Expand Up @@ -48,6 +50,12 @@ export async function getEggheadUserProfile(userId: string) {
throw new Error('token-expired')
}

return eggheadToken
}

export async function getEggheadUserProfile(userId: string) {
const eggheadToken = await getEggheadToken(userId)

const eggheadUserUrl = 'https://app.egghead.io/api/v1/users/current'

const profile = await fetch(eggheadUserUrl, {
Expand Down
60 changes: 60 additions & 0 deletions apps/egghead/src/lib/posts-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { z } from 'zod'

import 'server-only'

import { EggheadApiError } from '@/errors/egghead-api-error'
import { POST_CREATED_EVENT } from '@/inngest/events/post-created'
import { inngest } from '@/inngest/inngest.server'

Expand All @@ -44,6 +45,8 @@ import {
determineEggheadAccess,
determineEggheadLessonState,
determineEggheadVisibilityState,
EGGHEAD_API_V1_BASE_URL,
getEggheadToken,
getEggheadUserProfile,
setPublishedAt,
updateEggheadLesson,
Expand Down Expand Up @@ -564,6 +567,63 @@ export async function getVideoDuration(
return 0
}

export async function addEggheadLessonToPlaylist({
eggheadPlaylistId,
eggheadLessonId,
position,
}: {
eggheadPlaylistId: string
eggheadLessonId: string
position?: string
}) {
try {
const { session, ability } = await getServerAuthSession()

if (!session?.user?.id || !ability.can('create', 'Content')) {
throw new Error('Unauthorized')
}

const eggheadToken = await getEggheadToken(session.user.id)

const response = await fetch(
`${EGGHEAD_API_V1_BASE_URL}/playlists/${eggheadPlaylistId}/items/add`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${eggheadToken}`,
'User-Agent': 'authjs',
},
body: JSON.stringify({
tracklistable: {
tracklistable_type: 'Lesson',
tracklistable_id: eggheadLessonId,
row_order_position: position || 'last',
},
}),
},
).then(async (res) => {
if (!res.ok) {
throw new EggheadApiError(res.statusText, res.status)
}
return await res.json()
})

return response
} catch (error) {
if (error instanceof Error && 'status' in error) {
if (error.status === 304) {
// Item already exists in playlist
console.log('Lesson already exists in playlist')
} else if (error.status === 403) {
// Not authorized
console.log('Not authorized to modify this playlist')
}
}
throw error
}
}

export const addResourceToResource = async ({
resource,
parentResourceId,
Expand Down

0 comments on commit e19baf7

Please sign in to comment.