|
| 1 | +// import process from "node:process"; |
| 2 | +import makeDebug from "debug"; |
| 3 | +import { IndiekitError } from "@indiekit/error"; |
| 4 | +import { createPost, userInfo } from "./lib/linkedin.js"; |
| 5 | + |
| 6 | +const debug = makeDebug(`indiekit-syndicator:linkedin`); |
| 7 | + |
| 8 | +const UID = "https://www.linkedin.com/"; |
| 9 | + |
| 10 | +const DEFAULTS = { |
| 11 | + // accessToken: process.env.LINKEDIN_ACCESS_TOKEN, |
| 12 | + // The character limit for a LinkedIn post is 3000 characters. |
| 13 | + // https://www.linkedin.com/help/linkedin/answer/a528176 |
| 14 | + characterLimit: 3000, |
| 15 | + checked: false, |
| 16 | + // https://learn.microsoft.com/en-us/linkedin/marketing/versioning |
| 17 | + // https://learn.microsoft.com/en-us/linkedin/marketing/community-management/shares/posts-api |
| 18 | + postsAPIVersion: "202401", |
| 19 | +}; |
| 20 | + |
| 21 | +const retrieveAccessToken = async () => { |
| 22 | + // the access token could be stored in an environment variable, in a database, etc |
| 23 | + debug( |
| 24 | + `retrieve LinkedIn access token from environment variable LINKEDIN_ACCESS_TOKEN`, |
| 25 | + ); |
| 26 | + |
| 27 | + return process.env.LINKEDIN_ACCESS_TOKEN === undefined |
| 28 | + ? { |
| 29 | + error: new Error(`environment variable LINKEDIN_ACCESS_TOKEN not set`), |
| 30 | + } |
| 31 | + : { value: process.env.LINKEDIN_ACCESS_TOKEN }; |
| 32 | +}; |
| 33 | + |
| 34 | +export default class LinkedInSyndicator { |
| 35 | + /** |
| 36 | + * @param {object} [options] - Plug-in options |
| 37 | + * @param {string} [options.accessToken] - Linkedin OAuth app access token |
| 38 | + * @param {string} [options.authorId] - LinkedIn ID of the author. See https://learn.microsoft.com/en-us/linkedin/shared/api-guide/concepts/urns |
| 39 | + * @param {string} [options.authorName] - Full name of the author |
| 40 | + * @param {string} [options.authorProfileUrl] - LinkedIn profile URL of the author |
| 41 | + * @param {number} [options.characterLimit] - LinkedIn post character limit |
| 42 | + * @param {boolean} [options.checked] - Check syndicator in UI |
| 43 | + * @param {string} [options.postsAPIVersion] - Version of the Linkedin /posts API to use |
| 44 | + */ |
| 45 | + constructor(options = {}) { |
| 46 | + this.name = "LinkedIn syndicator"; |
| 47 | + this.options = { ...DEFAULTS, ...options }; |
| 48 | + } |
| 49 | + |
| 50 | + get environment() { |
| 51 | + return ["LINKEDIN_ACCESS_TOKEN", "LINKEDIN_AUTHOR_PROFILE_URL"]; |
| 52 | + } |
| 53 | + |
| 54 | + get info() { |
| 55 | + const service = { |
| 56 | + name: "LinkedIn", |
| 57 | + photo: "/assets/@indiekit-syndicator-linkedin/icon.svg", |
| 58 | + url: "https://www.linkedin.com/", |
| 59 | + }; |
| 60 | + |
| 61 | + const name = this.options.authorName || "unknown LinkedIn author name"; |
| 62 | + const uid = this.options.authorProfileUrl || UID; |
| 63 | + const url = |
| 64 | + this.options.authorProfileUrl || "unknown LinkedIn author profile URL"; |
| 65 | + |
| 66 | + return { |
| 67 | + checked: this.options.checked, |
| 68 | + name, |
| 69 | + service, |
| 70 | + uid, |
| 71 | + user: { name, url }, |
| 72 | + }; |
| 73 | + } |
| 74 | + |
| 75 | + get prompts() { |
| 76 | + return [ |
| 77 | + { |
| 78 | + type: "text", |
| 79 | + name: "postsAPIVersion", |
| 80 | + message: "What is the LinkedIn Posts API version you want to use?", |
| 81 | + description: "e.g. 202401", |
| 82 | + }, |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + async syndicate(properties, publication) { |
| 87 | + debug(`syndicate properties %O`, properties); |
| 88 | + debug(`syndicate publication %O: `, { |
| 89 | + categories: publication.categories, |
| 90 | + me: publication.me, |
| 91 | + }); |
| 92 | + |
| 93 | + const { error: tokenError, value: accessToken } = |
| 94 | + await retrieveAccessToken(); |
| 95 | + |
| 96 | + if (tokenError) { |
| 97 | + throw new IndiekitError(tokenError.message, { |
| 98 | + cause: tokenError, |
| 99 | + plugin: this.name, |
| 100 | + status: 500, |
| 101 | + }); |
| 102 | + } |
| 103 | + |
| 104 | + let authorName; |
| 105 | + let authorUrn; |
| 106 | + try { |
| 107 | + const userinfo = await userInfo({ accessToken }); |
| 108 | + authorName = userinfo.name; |
| 109 | + authorUrn = userinfo.urn; |
| 110 | + } catch (error) { |
| 111 | + throw new IndiekitError(error.message, { |
| 112 | + cause: error, |
| 113 | + plugin: this.name, |
| 114 | + status: error.statusCode, |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + // TODO: switch on properties['post-type'] // e.g. article, note |
| 119 | + const text = properties.content.text; |
| 120 | + |
| 121 | + try { |
| 122 | + const { url } = await createPost({ |
| 123 | + accessToken, |
| 124 | + authorName, |
| 125 | + authorUrn, |
| 126 | + text, |
| 127 | + versionString: this.options.postsAPIVersion, |
| 128 | + }); |
| 129 | + debug(`post created, online at ${url}`); |
| 130 | + return url; |
| 131 | + } catch (error) { |
| 132 | + // Axios Error |
| 133 | + // https://axios-http.com/docs/handling_errors |
| 134 | + const status = error.response.status; |
| 135 | + const message = `could not create LinkedIn post: ${error.response.statusText}`; |
| 136 | + throw new IndiekitError(message, { |
| 137 | + cause: error, |
| 138 | + plugin: this.name, |
| 139 | + status, |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + init(Indiekit) { |
| 145 | + Indiekit.addSyndicator(this); |
| 146 | + } |
| 147 | +} |
0 commit comments