redirecter for ao3 that adds opengraph metadata
1import DOM from "fauxdom" 2import { readFile } from 'node:fs/promises' 3import querystring from 'node:querystring' 4import { join } from 'node:path' 5import themes from '@/lib/themes.js' 6import baseFonts from '@/lib/baseFonts.js' 7import titleFonts from '@/lib/titleFonts.js' 8 9const getWork = async (workId, archive = null) => { 10 const domainParam = archive ? `?archive=${archive}` : '' 11 const data = await fetch(`http://${process.env.DOMAIN}/api/works/${workId}${domainParam}`) 12 const work = await data.json() 13 return work 14} 15 16const getHighestRating = async (works, archive = null) => { 17 const ratings = await Promise.all(works.map(async (w) => { 18 const work = await getWork(w.id, archive) 19 return work.rating 20 })) 21 if (ratings.includes("Not Rated")) { 22 return "NR" 23 } else if (ratings.includes("Explicit")) { 24 return "E" 25 } else if (ratings.includes("Mature")) { 26 return "M" 27 } else if (ratings.includes("Teen")) { 28 return "T" 29 } 30 return "G" 31} 32 33const getHighestWarning = async (works, archive = null) => { 34 const warnings = await Promise.all(works.map(async (w) => { 35 const work = await getWork(w.id, archive) 36 return work.tags.warnings 37 })) 38 const warningsUnique = warnings.reduce((a, b) => { return a.concat(b) }).filter((w, i) => { return i === warnings.indexOf(w) }) 39 if (warningsUnique.length === 1 && warningsUnique[0] === "Creator Chose Not To Use Archive Warnings") { 40 return "CNTW" 41 } else if (warningsUnique.length === 1 && warningsUnique[0] === "No Archive Warnings Apply") { 42 return "NW" 43 } 44 return "W" 45} 46 47const getCategory = async (works, archive = null) => { 48 const categories = await Promise.all(works.map(async (w) => { 49 const work = await getWork(w.id, archive) 50 return work.category 51 })) 52 const categoriesJoined = categories.reduce((a, b) => { return a.concat(b) }) 53 const categoriesUnique = categoriesJoined.filter((w, i) => { return i === categoriesJoined.indexOf(w) }) 54 55 if (categoriesUnique.length === 1) { 56 if (categoriesUnique[0] === "F/F") return "F" 57 if (categoriesUnique[0] === "M/M") return "M" 58 if (categoriesUnique[0] === "F/M") return "FM" 59 if (categoriesUnique[0] === "Gen") return "G" 60 if (categoriesUnique[0] === "Multi") return "MX" 61 if (categoriesUnique[0] === "Other") return "O" 62 } 63 return "MX" 64} 65 66const sanitizeProps = (props) => { 67 let propsParsed = {} 68 Object.keys(props).forEach((pr) => { 69 if (props[pr] === 'true') { 70 propsParsed[pr] = true 71 return 72 } else if (props[pr] === 'false') { 73 propsParsed[pr] = false 74 return 75 } else if (typeof parseInt(props[pr]) === 'Number') { 76 propsParsed[pr] = parseInt(props[pr]) 77 return 78 } 79 propsParsed[pr] = props[pr] 80 }) 81 return propsParsed 82} 83 84export default async function sanitizeData ({ type, data, props}) { 85 const propsParsed = sanitizeProps(querystring.parse(props.toString())) 86 const archive = props.has('archive') ? props.get('archive') : 'https://archiveofourown.org' 87 const baseFont = propsParsed.baseFont ? propsParsed.baseFont : process.env.DEFAULT_BASE_FONT 88 const baseFontData = baseFonts[baseFont] 89 const titleFont = propsParsed.titleFont ? propsParsed.titleFont : process.env.DEFAULT_TITLE_FONT 90 const titleFontData = titleFonts[titleFont] 91 const themeData = propsParsed.theme ? themes[propsParsed.theme] : themes[process.env.DEFAULT_THEME] 92 const parentWork = type === 'work' && data.chapterInfo ? await getWork(data.id, archive) : null 93 const bfs = await Promise.all(baseFontData.defs.map(async (bf) => { 94 return { 95 name: baseFontData.displayName, 96 data: await readFile( 97 join(process.cwd(), bf.path) 98 ), 99 style: bf.style, 100 weight: bf.weight 101 } 102 })).then(x => x) 103 const tfs = await Promise.all(titleFontData.defs.map(async (tf) => { 104 return { 105 name: titleFontData.displayName, 106 data: await readFile( 107 join(process.cwd(), tf.path) 108 ), 109 style: tf.style, 110 weight: tf.weight 111 } 112 })).then(x => x) 113 const authorsFormatted = data.authors 114 ? data.authors.map((a) => { 115 if (a.anonymous) return "Anonymous" 116 if (a.pseud !== a.username) return `${a.pseud} (${a.username})` 117 return a.username 118 }) 119 : [] 120 const rating = type === 'work' ? await getHighestRating([data], archive) : await getHighestRating(data.works, archive) 121 const warning = type === 'work' ? await getHighestWarning([data], archive) : await getHighestWarning(data.works, archive) 122 const category = type === 'work' ? await getCategory([data], archive) : await getCategory(data.works, archive) 123 const authorString = authorsFormatted.length > 1 124 ? authorsFormatted.slice(0, -1).join(", ") + " & " + 125 authorsFormatted.slice(-1)[0] 126 : authorsFormatted[0] 127 const summaryContent = type === 'work' 128 ? (propsParsed.summaryType === 'chapter' && data.chapterInfo && data.chapterInfo.summary ? data.chapterInfo.summary : (propsParsed.summaryType === 'custom' && propsParsed.customSummary !== '' ? propsParsed.customSummary : (data.summary ? data.summary : (parentWork ? parentWork.summary : '')))) 129 : (propsParsed.summaryType === 'custom' && propsParsed.customSummary !== '' ? propsParsed.customSummary : data.notes) 130 const formatter = new Intl.NumberFormat('en-US') 131 const words = formatter.format(data.words) 132 const summaryDOM = new DOM(summaryContent, {decodeEntities: true}); 133 const summaryFormatted = summaryDOM.innerHTML.replace(/\<br(?: \/)?\>/g, "\n").replace( 134 /(<([^>]+)>)/ig, 135 "", 136 ).split("\n") 137 const titleString = type === 'work' ? data.title : data.name 138 const chapterString = data.chapterInfo ? (data.chapterInfo.name 139 ? data.chapterInfo.name 140 : "Chapter " + data.chapterInfo.index) : null 141 const chapterCountString = type === 'work' ? (data.chapters 142 ? data.chapters.published+'/'+( 143 data.chapters.total 144 ? data.chapters.total 145 : '?' 146 ) 147 : '') : null 148 const fandomString = type === 'work' ? ( 149 data.fandoms.length > 1 150 ? ( 151 data.fandoms.length <= 2 152 ? data.fandoms.slice(0, -1).join(", ")+" & "+data.fandoms.slice(-1) 153 : data.fandoms.join(", ")+" (+"+(data.fandoms.length - 2)+")" 154 ) 155 : data.fandoms[0] 156 ) : ( 157 '' 158 ) 159 const charTags = type === 'work' ? data.tags.characters : data.works.map(w => w.tags.characters).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 160 const relTags = type === 'work' ? data.tags.relationships : data.works.map(w => w.tags.relationships).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 161 const freeTags = type === 'work' ? data.tags.additional : data.works.map(w => w.tags.additional).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 162 const warnings = type === 'work' ? data.tags.warnings : data.works.map(w => w.tags.warnings).reduce((a, b) => { return b ? (a ? a.concat(b) : []) : (a ? a : []) }).filter((w, i) => { return i === data.works.indexOf(w) }) 163 164 return { 165 topLine: fandomString, 166 titleLine: titleString, 167 authorLine: authorString, 168 chapterLine: chapterString, 169 chapterCount: chapterCountString, 170 words: words, 171 rating: rating, 172 warning: warning, 173 category: category, 174 summary: summaryFormatted, 175 theme: themeData, 176 charTags: charTags, 177 relTags: relTags, 178 freeTags: freeTags, 179 postedAt: type === 'work' ? data.publishedAt : data.startedAt, 180 updatedAt: data.updatedAt, 181 baseFont: baseFont, 182 titleFont: titleFont, 183 props: propsParsed, 184 opts: { 185 fonts: bfs.concat(tfs) 186 } 187 } 188}