Files
metrics/source/plugins/posts/index.mjs

61 lines
2.4 KiB
JavaScript

//Setup
export default async function({login, data, imports, q, queries, account}, {enabled = false, extras = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!q.posts) || (!imports.metadata.plugins.posts.enabled(enabled, {extras})))
return null
//Load inputs
let {source, descriptions, covers, limit, user} = imports.metadata.plugins.posts.inputs({data, account, q})
//Retrieve posts
console.debug(`metrics/compute/${login}/plugins > posts > processing with source ${source}`)
let posts = null
let link = null
switch (source) {
//Dev.to
case "dev.to": {
user = user.toLowerCase()
console.debug(`metrics/compute/${login}/plugins > posts > querying api`)
posts = (await imports.axios.get(`https://dev.to/api/articles?username=${user}&state=fresh`)).data.map(({title, description, published_at: date, cover_image: image, url: link}) => ({title, description, date, image, link}))
link = `https://dev.to/${user}`
break
}
//Hashnode
case "hashnode": {
posts = (await imports.axios.post("https://api.hashnode.com", {query: queries.posts.hashnode({user})}, {headers: {"Content-type": "application/json"}})).data.data.user.publication.posts.map((
{title, brief: description, dateAdded: date, coverImage: image, slug},
) => ({title, description, date, image, link: `https://hashnode.com/post/${slug}`}))
link = `https://hashnode.com/@${user}`
break
}
//Unsupported
default:
throw {error: {message: `Unsupported source "${source}"`}}
}
//Format posts
if (Array.isArray(posts)) {
//Limit posts
if (limit > 0) {
console.debug(`metrics/compute/${login}/plugins > posts > keeping only ${limit} posts`)
posts.splice(limit)
}
//Cover images
if (covers) {
console.debug(`metrics/compute/${login}/plugins > posts > formatting cover images`)
posts = await Promise.all(posts.map(async ({image, ...post}) => ({image: await imports.imgb64(image, {width: 144, height: -1}), ...post})))
}
//Results
return {user, source, link, descriptions, covers, list: posts}
}
//Unhandled error
throw {error: {message: "Failed to retrieve posts"}}
}
//Handle errors
catch (error) {
throw imports.format.error(error)
}
}