Skip to content

Commit

Permalink
Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre601 committed Sep 12, 2023
1 parent 7385a95 commit 982bbdf
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
1 change: 1 addition & 0 deletions source/plugins/mastodon/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
plugin_mastodon_attachments: no
plugin_mastodon_sensitive: no
plugin_mastodon_replies: no
plugin_mastodon_boosts: no
plugin_mastodon_limit: 1
36 changes: 22 additions & 14 deletions source/plugins/mastodon/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,38 @@ export default async function({login, imports, data, q, account}, {enabled = fal
return null

//Load inputs
let {source, user: username, attachments, sensitive, replies, limit} = imports.metadata.plugins.mastodon.inputs({data, account, q})
let {source, user: username, sensitive, replies, boosts, limit} = imports.metadata.plugins.mastodon.inputs({data, account, q})

//Load user profile
console.debug(`metrics/compute/${login}/plugins > mastodon > Loading Mastodon profile (@${username}@${source})`)
const {data: {data: profile = null}} = await imports.axios.get(`https://${source}/api/v1/accounts/lookup?acct=${username}`)
const profile = await imports.axios.get(`https://${source}/api/v1/accounts/lookup?acct=${username}`).then(_ => _.json())

console.debug(`metrics/compute/${login}/plugins > mastodon > Obtained user data (${data})`)

Check failure on line 17 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Trailing spaces not allowed
//Load profile image
console.debug(`metrics/compute/${login}/plugins > mastodon > Loading profile image`)
profile.profile_image = await imports.imgb64(profile.avatar_static)

const profile_image = await imports.imgb64(profile.avatar_static)

Check failure on line 21 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Trailing spaces not allowed
const profileData = {avatar: profile_image, id: profile.id}

Check failure on line 23 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Trailing spaces not allowed
//Retrieve posts
console.debug(`metrics/compute/${login}/plugins > mastodon > processing posts`)
let link = `${source}/@${username}`

Check failure on line 26 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

'link' is assigned a value but never used

const {data: {data: posts = []}} = await imports.axios.get(`https://${source}/api/v1/accounts/${profile.id}/statuses`)

const posts = await imports.axios.get(`https://${source}/api/v1/accounts/${profile.id}/statuses?limit=${limit}&exclude_replies=${!replies}&exclude_reblogs=${!boosts}`).then(_ => _.json())

Check failure on line 28 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Check Spelling

`reblogs` is not a recognized word. (unrecognized-spelling)

Check failure on line 29 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Trailing spaces not allowed
const formattedPosts = []
//Format posts
await Promise.all(posts.map(async post => {
//Add important values
posts.forEach((post) => {

Check failure on line 32 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Unexpected parentheses around single function argument
console.debug(`metrics/compute/${login}/plugins > mastodon > formatting post ${post.id}`)

Check failure on line 34 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Trailing spaces not allowed
// Formatting mentions

Check failure on line 35 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Unexpected space or tab after '//' in comment
post.mentionedUsers = post.mentions?.map(({username}) => username) ?? []

Check failure on line 37 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Trailing spaces not allowed
//Format Text
console.debug(`metrics/compute/${login}/plugins > mastodon > formatting post ${post.id}`)
post.createdAt = `${imports.format.date(post.created_at, {time: true})} on ${imports.format.date(post.created_at, {date: true})}`
post.text = imports.htmlescape(
//Escape tags
//Escape HTML tags
imports.htmlescape(post.content, {"<": true, ">": true})
//Mentions
.replace(new RegExp(`@(${post.mentionedUsers.join("|")})`, "gi"), '<span class="mention">@$1</span>')
Expand All @@ -43,11 +49,13 @@ export default async function({login, imports, data, q, account}, {enabled = fal
)
//Line breaks
.replace(/\n/g, "<br/>"),
{"&": true},
{"&": true}
)
}))

Check failure on line 54 in source/plugins/mastodon/index.mjs

View workflow job for this annotation

GitHub Actions / Lint code

Trailing spaces not allowed
formattedPosts[formattedPosts.length + 1] = post
})

return {username, profile, list: posts}
return {username, profileData, sensitive, list: formattedPosts}
}
//Handle errors
catch (error) {
Expand Down
10 changes: 7 additions & 3 deletions source/plugins/mastodon/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ inputs:
plugin_mastodon_user:
description: |
User ID to display posts from
type: number
default: 0
type: string
default: ""
example: Mastodon
min: 1
plugin_mastodon_attachments:
description: |
Whether attachments should be included
Expand All @@ -46,6 +45,11 @@ inputs:
Whether to include posts that are replies to others
type: boolean
default: no
plugin_mastodon_boosts:
description: |
Whether to include boosted (reblogged) posts

Check failure on line 50 in source/plugins/mastodon/metadata.yml

View workflow job for this annotation

GitHub Actions / Check Spelling

`reblogged` is not a recognized word. (unrecognized-spelling)
type: boolean
default: no
plugin_mastodon_limit:
description: |
Number of posts to display at most
Expand Down
2 changes: 1 addition & 1 deletion source/templates/classic/partials/mastodon.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<% if (plugins.mastodon.error) { %>
<div class="field error">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M2.343 13.657A8 8 0 1113.657 2.343 8 8 0 012.343 13.657zM6.03 4.97a.75.75 0 00-1.06 1.06L6.94 8 4.97 9.97a.75.75 0 101.06 1.06L8 9.06l1.97 1.97a.75.75 0 101.06-1.06L9.06 8l1.97-1.97a.75.75 0 10-1.06-1.06L8 6.94 6.03 4.97z"></path></svg>
<%= plugins.posts.error.message %>
<%= plugins.mastodon.error.message %>
</div>
<% } else { %>
<div class="field <%= !plugins.mastodon.profile ? 'error' : '' %>">
Expand Down

0 comments on commit 982bbdf

Please sign in to comment.