Skip to content

Commit

Permalink
Implement server side metadata with auto episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
bcomnes committed Mar 5, 2023
1 parent b847e6a commit 58aac1c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
60 changes: 50 additions & 10 deletions routes/api/bookmarks/put-bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ import { resolveEpisode } from '../episodes/resolve-episode.js'
import { getBookmarksQuery } from './get-bookmarks-query.js'
import { fullBookmarkPropsWithEpisodes } from './mixed-bookmark-props.js'

const autoEpisodeHostnames = {
'www.youtube.com': 'video',
'm.youtube.com': 'video',
'vimeo.com': 'video'
}

function normalizeURL (urlObj) {
if (urlObj.host === 'm.youtube.com') urlObj.host = 'www.youtube.com'
return {
normalizedURL: urlObj.toString()
}
}

export async function putBookmarks (fastify, opts) {
// Create bookmark
fastify.put(
Expand All @@ -28,6 +41,21 @@ export async function putBookmarks (fastify, opts) {
type: 'boolean',
default: false,
description: 'If set to true, bookmarks that already exist at URL are redirected to to the specific bookmark endpoint which will process the request as a bookmark update. Otherwise, this creates or returns the existing bookmark.'
},
meta: {
type: 'boolean',
default: false,
description: 'Extract page metadata on the server.'
},
normalize: {
type: 'boolean',
default: true,
description: 'Normalize URLs when looking them up or creating them.'
},
autoEpisodes: {
type: 'boolean',
default: true,
description: 'Automatically create episodes on common episode sources.'
}
},
response: {
Expand Down Expand Up @@ -66,19 +94,28 @@ export async function putBookmarks (fastify, opts) {
return fastify.pg.transact(async client => {
const userId = request.user.id
const {
url,
title,
note,
toread,
sensitive,
tags = [],
archive_urls = []
} = request.body
let { url } = request.body
const urlObj = new URL(url)

const {
update
update,
meta,
normalize,
autoEpisodes
} = request.query

if (normalize) {
const { normalizedURL } = normalizeURL(urlObj)
url = normalizedURL
}

const checkForExistingQuery = getBookmarksQuery({
ownerId: userId,
url,
Expand All @@ -103,6 +140,8 @@ export async function putBookmarks (fastify, opts) {
}
}

const serverMeta = meta ? await fastify.getSiteMetaData({ url }) : {}

const createBookmark = SQL`
insert into bookmarks (
url,
Expand All @@ -114,8 +153,8 @@ export async function putBookmarks (fastify, opts) {
owner_id
) values (
${url},
${title ?? null},
${note ?? null},
${title ?? serverMeta?.title ?? null},
${note ?? serverMeta?.summary ?? null},
${toread ?? false},
${sensitive ?? false},
${archive_urls.length > 0 ? archive_urls : SQL`'{}'`},
Expand All @@ -126,12 +165,13 @@ export async function putBookmarks (fastify, opts) {
const results = await client.query(createBookmark)
const bookmark = results.rows[0]

if (tags.length > 0) {
if (tags.length > 0 || serverMeta.tags.length > 0) {
const activeTagSet = tags.length > 0 ? tags : serverMeta.tags
const createTags = SQL`
INSERT INTO tags (name, owner_id)
VALUES
${SQL.glue(
tags.map(tag => SQL`(${tag},${userId})`),
activeTagSet.map(tag => SQL`(${tag},${userId})`),
' , '
)}
ON CONFLICT (name, owner_id)
Expand All @@ -156,14 +196,14 @@ export async function putBookmarks (fastify, opts) {
fastify.metrics.tagAppliedCounter.inc(tagsResults.rows.length)
}

if (request?.body?.createEpisode) {
if (request?.body?.createEpisode || (autoEpisodes && autoEpisodeHostnames[urlObj.hostname])) {
const { id: episodeId, medium: episodeMedium, url: episodeURL } = await createEpisode({
client,
userId,
bookmarkId: bookmark.id,
type: request?.body?.createEpisode.type,
medium: request?.body?.createEpisode.medium,
url: request?.body?.createEpisode.url ?? url
type: request?.body?.createEpisode?.type ?? 'redirect',
medium: request?.body?.createEpisode?.medium ?? autoEpisodeHostnames[urlObj.hostname],
url: request?.body?.createEpisode?.url ?? url
})

await client.query('commit')
Expand Down
5 changes: 1 addition & 4 deletions routes/api/episodes/resolve-episode.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ export async function resolveEpisode ({
const filename = `${metadata.title}.${metadata.ext}`
videoData.push(SQL`filename = ${filename}`)
}
console.log({
metaTitle: metadata.title,
bookmarkTitle
})

if (metadata.title != null && metadata.title !== bookmarkTitle) {
// TODO: when bookmarks have auto-extract, maybe remove this
videoData.push(SQL`title = ${metadata.title.trim().substring(0, 255)}`)
Expand Down
17 changes: 11 additions & 6 deletions web/bookmarks/add/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,26 @@ export const page = Component(() => {
const queryUrl = query.get('url')
const ver = query.get('ver')
setBookmarkletVersion(ver)
if (ver !== version) setBookmarkletUpdateAvailable(true)
if (ver !== version || query.get('description')) setBookmarkletUpdateAvailable(true)

if (!queryUrl) {
setFallbackBookmark()
return
}

const payload = {
url: queryUrl,
title: query.get('title'),
note: query.get('note') || query.get('description'),
tags: query.getAll('tags').filter(t => Boolean(t))
url: queryUrl
// title: query.get('title'),
// note: query.get('note') || query.get('description'),
// tags: query.getAll('tags').filter(t => Boolean(t))
}

const response = await fetch(`${state.apiUrl}/bookmarks`, {
const params = new URLSearchParams()

const serverMeta = query.get('meta')
if (serverMeta) params.set('meta', 'true')

const response = await fetch(`${state.apiUrl}/bookmarks?${params.toString()}`, {
method: 'put',
body: JSON.stringify(payload),
headers: {
Expand Down

0 comments on commit 58aac1c

Please sign in to comment.