56 lines
2.3 KiB
JavaScript
56 lines
2.3 KiB
JavaScript
// LinkedIn: публикация текстового поста в личный профиль (UGC Posts API).
|
||
// Токен, автор и статус одобрения приложения — только из env.
|
||
// LINKEDIN_APP_STATUS: waiting (по умолчанию) | received | denied.
|
||
const WEEK = 7 * 24 * 3600 * 1000;
|
||
const LIMITS = [{ limit: 1, periodMs: 24 * 3600 * 1000 }, { limit: 5, periodMs: WEEK }];
|
||
|
||
export function createLinkedIn({
|
||
token = process.env.LINKEDIN_ACCESS_TOKEN,
|
||
author = process.env.LINKEDIN_AUTHOR_URN, // urn:li:person:...
|
||
status = process.env.LINKEDIN_APP_STATUS ?? "waiting",
|
||
fetchImpl = fetch,
|
||
} = {}) {
|
||
return {
|
||
channel: "linkedin",
|
||
secrets: () => [token].filter(Boolean),
|
||
approval: () => status,
|
||
defaultLimits: () => LIMITS,
|
||
connected: () => Boolean(token && author),
|
||
async send(text) {
|
||
if (!token || !author) throw new Error("нет доступов к LinkedIn");
|
||
const r = await fetchImpl("https://api.linkedin.com/v2/ugcPosts", {
|
||
method: "POST",
|
||
headers: { authorization: `Bearer ${token}`, "content-type": "application/json", "x-restli-protocol-version": "2.0.0" },
|
||
body: JSON.stringify({
|
||
author, lifecycleState: "PUBLISHED",
|
||
specificContent: { "com.linkedin.ugc.ShareContent": { shareCommentary: { text }, shareMediaCategory: "NONE" } },
|
||
visibility: { "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC" },
|
||
}),
|
||
});
|
||
const j = await r.json().catch(() => ({}));
|
||
if (!r.ok || !j.id) throw new Error(j.message ?? `ошибка LinkedIn ${r.status}`);
|
||
return { id: j.id, url: `https://www.linkedin.com/feed/update/${j.id}/` };
|
||
},
|
||
};
|
||
}
|
||
|
||
// Заглушка с контрактом UGC Posts API до одобрения приложения LinkedIn.
|
||
export function mockLinkedIn({ fail, status = "received" } = {}) {
|
||
const sent = [];
|
||
return {
|
||
channel: "linkedin",
|
||
sent,
|
||
secrets: () => ["MOCK_LI_TOKEN"],
|
||
approval: () => status,
|
||
defaultLimits: () => LIMITS,
|
||
connected: () => true,
|
||
async send(text) {
|
||
if (fail) throw new Error(fail);
|
||
const id = `urn:li:share:${1000 + sent.length}`;
|
||
const m = { id, text, url: `https://www.linkedin.com/feed/update/${id}/` };
|
||
sent.push(m);
|
||
return m;
|
||
},
|
||
};
|
||
}
|