@happyvertical/smrt-social (欢快的垂直)
S-m-r-t生态系统多平台出版的社交媒体账户管理. 管理OAuth的连接,出站调度,以及横跨YouTube,Treads,X(Twitter)和Bluesky的分析跟踪.
安装
pnpm add @happyvertical/smrt-social
使用量
import { SocialAccount, SocialPost, OAuthState } from '@happyvertical/smrt-social';
// Connect a social account
const account = new SocialAccount({
name: 'Bentley News YouTube',
platform: 'youtube',
platformUsername: 'Bentley News',
accessToken: 'encrypted-token',
refreshToken: 'encrypted-refresh',
tokenExpiresAt: new Date('2026-06-01'),
defaultHashtags: ['news', 'local'],
linkBehavior: 'description',
});
await account.save();
// Check readiness before publishing (active + connected + token present + not expired)
if (account.isReady) {
const post = new SocialPost({
socialAccountId: account.id,
title: 'Breaking News from Bentley',
description: 'Latest updates from the town council meeting.',
hashtags: ['news', 'local', 'bentley'],
linkUrl: 'https://example.com/article',
scheduledAt: new Date('2026-03-05T18:00:00Z'),
status: 'scheduled',
});
await post.save();
}
// OAuth flow: create state, redirect user, verify callback
const state = new OAuthState({
platform: 'youtube',
state: OAuthState.generateState(),
codeVerifier: OAuthState.generateCodeVerifier(),
redirectUri: 'https://app.example.com/oauth/callback',
scopes: ['youtube.upload', 'youtube.readonly'],
createdByUserId: currentUser.id, // reject a different user on callback
returnTo: '/settings/social', // host-validated same-origin path
});
await state.save();
// On callback: state.verifyState(callbackState), check the user, then
// `await states.consume(state)` (false = already used) before exchanging the code
正在连接账户( OAuth)
@happyvertical/smrt-social/connect 拥有连接流量, 所以主机挂载 2
路径和写入无平台代码。 X 是第一个提供者( OAuth 2. 0)
授权代码与 PKCE, 保密客户端; 向
registerSocialConnectProvider。 。 。 .
import {
completeSocialConnect,
startSocialConnect,
} from '@happyvertical/smrt-social/connect';
// GET /connect/x (host has authorized the caller and set the tenant)
const { authorizeUrl } = await startSocialConnect({
db,
platform: 'x',
tenantId,
redirectUri: 'https://app.example.com/social/callback/x',
});
// redirect to authorizeUrl
// GET /callback/x
const { account } = await completeSocialConnect({
db,
platform: 'x',
tenantId,
redirectUri: 'https://app.example.com/social/callback/x',
query: { code, state, error },
});
客户资格来自提供者的巢穴名称(X: X_CLIENT_ID, (中文(简体) ),
X_CLIENT_SECRET后退为 TWITTER_CLIENT_ID编号 :TWITTER_CLIENT_SECRET)或为: client 选项。 托肯存储通过
SocialAccount.setCredentials (smrt-secret,每个租户)作为JSON有效载荷
包括 expiresAt;没有写入普通符号栏或日志.
州属单用途、租户制,并与注册人核对
重定向 URI 。 在平台默认发布模式下启动新账户
(单位:千美元)dry_run x); 重新连接更新同一账户,绝不扩大其账户
模式。 失败投出 SocialConnectError 有安全、用户可显示的信息.
过 userId 给这两通电话: 状态与启动用户绑定, 因此任何其他人都会拒绝一个转发的授权链接 。 包里没有角色检查 登记内容相同 redirectUri 与
平台。 YouTube、Facebook页面和线索供应商尚未建立.
API 密码
模型
| 导出 | 说明 |
|---|---|
SocialAccount | 与 OAuth 证书和发布设置连接的平台账户 |
SocialPost | 已排定或公布员额,并附有分析跟踪; createdByUserId 记录海报 |
OAuthState | 具有CSRF保护和PKCE支持的临时OAuth流量状态; createdByUserId 财务报告和审定财务报表 returnTo 将其绑入发起人并返回页面 |
类型
| 导出 | 说明 |
|---|---|
SocialPlatformType | 平台标识符 : youtube, (中文(简体) ), threads, (中文(简体) ), x, (中文(简体) ), bluesky |
AccountStatus | 连接状态 : connected, (中文(简体) ), expired, (中文(简体) ), error |
PostStatus | 生命周期状态 : draft, (中文(简体) ), scheduled, (中文(简体) ), publishing, (中文(简体) ), published, (中文(简体) ), failed |
LinkBehavior | 链接处理策略 : description, (中文(简体) ), reply, (中文(简体) ), none |
PostAnalytics | 参与度量标准:视图、喜欢、评论、股份、点击 |
SocialAccountOptions | 账户创建选项 |
SocialPostOptions | 创建后选项 |
OAuthStateOptions | OAuth 状态创建选项 |
密钥计算属性
SocialAccount.isReady-- 门检查: 活动 + 连接 + 令牌 存在 + 未过期SocialAccount.isTokenExpired-- 在实际期满前用5分钟缓冲器进行检查SocialPost.isEditable-- 起草或失败时为真SocialPost.fullText-- 描述+已格式化标签OAuthState.isValid- 未过期, 状态符号存在OAuthState.verifyState(callback)- 对OAuth召回的CSRF核查OAuthState.generateCodeChallenge(verifier)- PCCE S256 挑战生成OAuthStateCollection.consume(state)- 单一用途消费;false当另一个来电者先消费的时候
依赖关系
@happyvertical/smrt-core-- ORM 和代码生成@happyvertical/smrt-config-- 配置装入@happyvertical/smrt-content-- 内容模型@happyvertical/smrt-tenancy-- 多租户范围界定@happyvertical/smrt-video-- 视频内容参考