From 982bbdf66ed2b043d94f034372533ae9dbc3d59d Mon Sep 17 00:00:00 2001 From: Andre601 Date: Tue, 12 Sep 2023 17:16:34 +0200 Subject: [PATCH] Update code --- source/plugins/mastodon/examples.yml | 1 + source/plugins/mastodon/index.mjs | 36 +++++++++++-------- source/plugins/mastodon/metadata.yml | 10 ++++-- .../templates/classic/partials/mastodon.ejs | 2 +- 4 files changed, 31 insertions(+), 18 deletions(-) diff --git a/source/plugins/mastodon/examples.yml b/source/plugins/mastodon/examples.yml index 6121ac3e949..0db9bfded95 100644 --- a/source/plugins/mastodon/examples.yml +++ b/source/plugins/mastodon/examples.yml @@ -16,4 +16,5 @@ plugin_mastodon_attachments: no plugin_mastodon_sensitive: no plugin_mastodon_replies: no + plugin_mastodon_boosts: no plugin_mastodon_limit: 1 \ No newline at end of file diff --git a/source/plugins/mastodon/index.mjs b/source/plugins/mastodon/index.mjs index cadde5cc7db..5d1512676b9 100644 --- a/source/plugins/mastodon/index.mjs +++ b/source/plugins/mastodon/index.mjs @@ -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})`) + //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) + + const profileData = {avatar: profile_image, id: profile.id} + //Retrieve posts console.debug(`metrics/compute/${login}/plugins > mastodon > processing posts`) let link = `${source}/@${username}` - 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()) + + const formattedPosts = [] //Format posts - await Promise.all(posts.map(async post => { - //Add important values + posts.forEach((post) => { + console.debug(`metrics/compute/${login}/plugins > mastodon > formatting post ${post.id}`) + + // Formatting mentions post.mentionedUsers = post.mentions?.map(({username}) => username) ?? [] - + //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"), '@$1') @@ -43,11 +49,13 @@ export default async function({login, imports, data, q, account}, {enabled = fal ) //Line breaks .replace(/\n/g, "
"), - {"&": true}, + {"&": true} ) - })) + + formattedPosts[formattedPosts.length + 1] = post + }) - return {username, profile, list: posts} + return {username, profileData, sensitive, list: formattedPosts} } //Handle errors catch (error) { diff --git a/source/plugins/mastodon/metadata.yml b/source/plugins/mastodon/metadata.yml index ede40c370e5..e26558ab944 100644 --- a/source/plugins/mastodon/metadata.yml +++ b/source/plugins/mastodon/metadata.yml @@ -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 @@ -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 + type: boolean + default: no plugin_mastodon_limit: description: | Number of posts to display at most diff --git a/source/templates/classic/partials/mastodon.ejs b/source/templates/classic/partials/mastodon.ejs index 291e83b9368..3fcd5a05933 100644 --- a/source/templates/classic/partials/mastodon.ejs +++ b/source/templates/classic/partials/mastodon.ejs @@ -9,7 +9,7 @@ <% if (plugins.mastodon.error) { %>
- <%= plugins.posts.error.message %> + <%= plugins.mastodon.error.message %>
<% } else { %>