brendmanager/src/linkedin.js
Peppa 35403eedbf
All checks were successful
peppa/lint lint: success за 0с
peppa/test test: success за 0с
ci / checks (pull_request) Successful in 3s
deploy / deploy (pull_request) Successful in 2s
BRM-17: публикация в LinkedIn по правилам режима B
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-15 15:01:52 +05:00

56 lines
2.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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;
},
};
}