Files
metrics/source/plugins/posts/index.mjs
Simon Lecoq b649d4811e Fix svg height, add conversion options and options to use differents data from GitHub account (#35)
* Display all features on web instance but disable them when they're not enabled

* Resize dynamically SVG output and add support to convert images to jpeg/png

* Disable animations when computing height

* Add option to disable animations

* Add options to specify different data from used GitHub account

* Update tests
2021-01-02 01:31:04 +01:00

45 lines
1.7 KiB
JavaScript

//Setup
export default async function ({login, imports, q}, {enabled = false} = {}) {
//Plugin execution
try {
//Check if plugin is enabled and requirements are met
if ((!enabled)||(!q.posts))
return null
//Parameters override
let {"posts.source":source = "", "posts.limit":limit = 4, "posts.user":user = login} = q
//Limit
limit = Math.max(1, Math.min(30, Number(limit)))
//Retrieve posts
console.debug(`metrics/compute/${login}/plugins > posts > processing with source ${source}`)
let posts = null
switch (source) {
//Dev.to
case "dev.to":{
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, readable_publish_date:date}) => ({title, date}))
break
}
//Unsupported
default:
throw {error:{message:`Unsupported source "${source}"`}}
}
//Format posts
if (Array.isArray(posts)) {
//Limit tracklist
if (limit > 0) {
console.debug(`metrics/compute/${login}/plugins > posts > keeping only ${limit} posts`)
posts.splice(limit)
}
//Results
return {source, list:posts}
}
//Unhandled error
throw {error:{message:`An error occured (could not retrieve posts)`}}
}
//Handle errors
catch (error) {
if (error.error?.message)
throw error
throw {error:{message:"An error occured", instance:error}}
}
}