Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions packages/say/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,3 +552,102 @@ QQ 附件下载失败时,参考编号对应日志中的 `download` 会记录
单条碎语详情底部、评论区域上方显示「上一条 / 下一条」。上一条指向更早发布的公开碎语,下一条指向更晚发布的公开碎语;同一发布时间按 ID 排序。私密、隐藏以及其他内容类型不参与导航,无相邻条目时不显示对应链接。

飞书、QQ 的 OpenID 白名单配置与过滤已移除(不是改为可选),历史 `feishuAllowedOpenIds` / `qqAllowedOpenIds` 值不再生效。任何能向该机器人发送合规消息的人,都可触发以配置作者发布;平台签名、Token、AppID 校验、机器人接收范围、去重及 `/whoami` 保留。微信公众号的 OpenID 白名单不受此调整影响。

## 用主题模板接管公开页面

在支持站点路由 `renderTheme` 的 Firekylin 核心上,当前启用主题可以覆盖 Say 的三种页面:

| 请求 | 主题内的文件 |
| --- | --- |
| `/says` | `plugins/say/list.eta` |
| `/says/tag/:tag`,或兼容入口 `/says?tag=标签` | `plugins/say/tag.eta` |
| `/says/:id` | `plugins/say/detail.eta` |

例如,独立项目的文件可放在 `themes/你的主题/plugins/say/list.eta`;旧目录部署放在 `www/theme/你的主题/plugins/say/list.eta`。只读取**当前启用主题**中的文件。

可以只覆盖其中一个页面。对应文件不存在时,Say 继续输出原来的内置页面;旧核心没有 `renderTheme` 时也保持原样。已有模板的语法错误、include/布局错误会正常报错,不会悄悄回退。空文件输出空页面。模板输出完整 HTML,可按当前主题的结构使用 Eta 布局、include、Widget 和公共主题上下文。此能力不改变 JSON API、发布接口、后台页面或已有 `say.list` / `Widget_Plugin_Say`。

### 页面数据:`firekylin.say`

三种模板读取同一结构,数据和链接由 Say 准备,不需要主题重新访问数据库或拼接路由:

- `view`:`list`、`tag` 或 `detail`。
- `title`、`heading`、`tag`:文档标题、页面标题、当前标签;无标签时 `tag` 为空字符串。
- `items`:当前页的公开碎语。保留内容字段:`id`、`pathname`、`kind`、`status`、`text`、`tags`、`images`、`mediaUrl`、`latitude`、`longitude`、`label`、`url`、`title`、`description`、`artist`、`cover`、`source`、`createdAt`、`updatedAt`、`allowComments`、`commentCount`。另提供 `detailUrl` 和 `tagLinks`(`{name, url}` 数组)。某种内容没有的字段可能为空或不存在。
- `item`:详情页的当前碎语,列表和标签页为 `null`。
- `pagination`:`page`、`pageSize`、`total`、`totalPages`、`previousUrl`、`nextUrl`。没有对应页时链接为 `null`。
- `navigation`:详情的 `previous` / `next`,值为 `{id, url}` 或 `null`。上一条为更早、下一条为更新的公开碎语,同一发布时间按 ID 排序;列表页两项均为 `null`。
- `urls`:`list` 是 `/says`,`tag` 是当前标签路径或 `null`,`current` 是当前内容/列表页生成的 URL。
- `html.items`:已转义的内置碎语组件,含媒体、标签及详情链接。样式由主题提供。
- `html.composer`:原有登录/管理员发布组件;详情页为空。包含当前请求所需的 CSRF 信息,不要跨请求缓存或重复插入。
- `html.comments`:详情的原生评论组件,或“评论已关闭”提示;列表页为空。保留存储 pathname 和 `type=3` 的评论目标,不要把它改成单独的数字 ID。

`html.*` 是可以使用 `<%~ ... %>` 输出的现有组件。正文等普通字段必须使用 `<%= ... %>` 转义,不要当 HTML 原样输出。页面模型不包含平台配置、发布 API Key、JWT、SMTP 密码或原始登录身份。高德 Web 服务 Key 也不会传给主题,所以 `html.items` 对位置碎语只输出位置文本;主题可以用经纬度自行实现地图。缺少模板时,原内置页面仍保留原来的静态地图。

公开/隐藏规则仍在模板渲染前由插件执行:非公开或不存在的详情返回 404,关闭插件后不显示公开数据。模板只改变展示,不授予发布或评论权限。列表继续发送 `Cache-Control: private, no-store`,详情继续 `no-store`,不能把含发布框的页面放入共享缓存。

### 最小完整模板

下面可直接作为 `plugins/say/list.eta`,也可复制成 `tag.eta` 和 `detail.eta` 后分别调整布局。保留 `html.composer` / `html.comments` 可保留原发布和评论交互;不插入某组件就不会显示它。

```eta
<% const say = firekylin.say; %>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title><%= say.title %></title>
</head>
<body>
<main>
<a href="<%= say.urls.list %>">全部碎语</a>
<h1><%= say.heading %></h1>
<%~ say.html.composer %>
<% if (say.items.length) { %>
<%~ say.html.items %>
<% } else { %>
<p>暂无碎语</p>
<% } %>
<% if (say.view === 'detail') { %>
<nav aria-label="相邻碎语">
<% if (say.navigation.previous) { %>
<a rel="prev" href="<%= say.navigation.previous.url %>">上一条</a>
<% } %>
<% if (say.navigation.next) { %>
<a rel="next" href="<%= say.navigation.next.url %>">下一条</a>
<% } %>
</nav>
<% } else { %>
<nav aria-label="碎语分页">
<% if (say.pagination.previousUrl) { %>
<a href="<%= say.pagination.previousUrl %>">上一页</a>
<% } %>
<% if (say.pagination.nextUrl) { %>
<a href="<%= say.pagination.nextUrl %>">下一页</a>
<% } %>
</nav>
<% } %>
<%~ say.html.comments %>
</main>
</body>
</html>
```

要自行排版正文,可以将 `html.items` 替换成对 `say.items` 的循环,例如:

```eta
<% for (const item of say.items) { %>
<article>
<p style="white-space:pre-wrap"><%= item.text %></p>
<% for (const tag of item.tagLinks) { %>
<a href="<%= tag.url %>">#<%= tag.name %></a>
<% } %>
<a href="<%= item.detailUrl %>">详情 / 评论 <%= item.commentCount %></a>
</article>
<% } %>
```

此示例只排版文字,图片/语音/链接等可按 `kind` 自行渲染,或继续使用完整的 `html.items`。

链接规则保持兼容:详情始终为 `/says/ID`,标签链接为 URL 编码后的 `/says/tag/标签`;分页保留当前列表路径,并保留既有 `page` 和 `tag` 查询参数。`/says?tag=标签` 的分页仍指向 `/says?page=N&tag=标签`,不会被改成标签路径。主题直接使用提供的 URL,无需自行编码标签或猜测相邻条目。
3 changes: 2 additions & 1 deletion packages/say/lib/comment-render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const {escapeHtml: esc} = require('./render');
const {detailUrl} = require('./urls');

function renderComments(id, result, config) {
const captcha = config.captcha;
Expand Down Expand Up @@ -63,7 +64,7 @@ function renderWalineComments(item, {options, public: publicConfig}) {
}
// Waline 3.16 decodes path once before sending it. Protect query-value escapes
// so core receives the stored pathname, not an ID or a partially decoded value.
const path = `/says/${item.id}?pathname=${encodeURIComponent(item.pathname)}&type=3`;
const path = `${detailUrl(item.id)}?pathname=${encodeURIComponent(item.pathname)}&type=3`;
const settings = options.comment || {};
const config = {
path: encodeURI(path),
Expand Down
3 changes: 2 additions & 1 deletion packages/say/lib/comments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {strings} = require('./say-service');
const {detailUrl} = require('./urls');

function fail(status, message) { throw Object.assign(new Error(message), {status, publicCommentError: true}) }
function idOf(value) {
Expand Down Expand Up @@ -79,7 +80,7 @@ class SayComments {

async create(id, body, request) {
const item = await this.target(id, true);
const path = `/says/${item.id}`;
const path = detailUrl(item.id);
const {options, captcha, captchaConfig} = await this.configuration();
const host = this.getService('comment');
if (!host?.create || !host?.context || !host?.validateCommentInput) fail(503, '当前核心缺少兼容的评论服务。');
Expand Down
5 changes: 3 additions & 2 deletions packages/say/lib/frontend-publish.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';
const {escapeHtml: esc} = require('./render');
const {listUrl} = require('./urls');

class FrontendPublish {
constructor({say, getModel, getService}) { Object.assign(this, {say, getModel, getService}) }
Expand Down Expand Up @@ -36,7 +37,7 @@ class FrontendPublish {

function renderComposer(identity, enabled) {
if (!enabled) return '';
if (!identity.loggedIn) return '<section class="say-item"><a href="/admin/login?redirect=%2Fsays">登录</a></section>';
if (!identity.loggedIn) return `<section class="say-item"><a href="/admin/login?redirect=${encodeURIComponent(listUrl)}">登录</a></section>`;
if (!identity.canPublish) return '';
return `<section class="say-item" aria-labelledby="say-publish-title"><h2 id="say-publish-title">发布微博</h2>
<form id="say-publish-form" data-csrf="${esc(identity.csrf)}">
Expand All @@ -56,7 +57,7 @@ function renderComposer(identity, enabled) {
const response = await fetch('/api/plugins/say/frontend-publish', {method: 'POST', credentials: 'same-origin',
headers: {'Content-Type': 'application/json', 'X-CSRF-Token': form.dataset.csrf},
body: JSON.stringify({text: form.elements.text.value})});
if (response.ok && (await response.json()).success) { location.assign('/says'); return; }
if (response.ok && (await response.json()).success) { location.assign('${listUrl}'); return; }
result.textContent = response.status === 403 ? '无法发布,请确认登录账号和发布权限,刷新页面后重试。'
: response.status === 400 ? '请输入 1 至 20000 字的微博内容。' : '发布未确认,请先刷新列表核对,勿立即重发。';
} catch { result.textContent = '发布未确认,请先刷新列表核对,勿立即重发。'; }
Expand Down
79 changes: 64 additions & 15 deletions packages/say/lib/render.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const {listUrl, detailUrl, tagUrl, pageUrl} = require('./urls');

function escapeHtml(value) {
return String(value === null || typeof value === 'undefined' ? '' : value).replace(/[&<>'"]/g, character => ({
'&': '&amp;', '<': '&lt;', '>': '&gt;', '\'': '&#39;', '"': '&quot;'
Expand Down Expand Up @@ -33,29 +35,76 @@ function media(item, options = {}) {
return '';
}

function renderSayPage(result, {title = '碎语', tag = '', amapKey = '', section = '', composer = '', navigation = null} = {}) {
const items = result.items.map(item => `<article class="say-item">
// Keep the template contract explicit: never spread settings, identities or model rows.
function publicItem(item) {
const data = {};
for (const key of ['id', 'pathname', 'allowComments', 'commentCount', 'kind', 'status', 'text',
'source', 'mediaUrl', 'latitude', 'longitude', 'label', 'url', 'title', 'description',
'artist', 'cover', 'createdAt', 'updatedAt']) {
if (item[key] !== undefined) data[key] = item[key];
}
data.images = [...(item.images || [])];
data.tags = [...(item.tags || [])];
data.detailUrl = detailUrl(item.id);
data.tagLinks = data.tags.map(name => ({name, url: tagUrl(name)}));
return data;
}

function renderItems(items, options = {}) {
return items.map(item => `<article class="say-item">
<div class="say-text">${escapeHtml(item.text).replaceAll('\n', '<br>')}</div>
${media(item, {amapKey})}
<footer>${item.tags.map(value => `<a href="/says/tag/${encodeURIComponent(value)}">#${escapeHtml(value)}</a>`).join(' ')}
<a href="/says/${Number(item.id)}">详情 / 评论 ${Number(item.commentCount) || 0}</a><time>${escapeHtml(item.createdAt)}</time></footer>
${media(item, options)}
<footer>${item.tagLinks.map(tag => `<a href="${escapeHtml(tag.url)}">#${escapeHtml(tag.name)}</a>`).join(' ')}
<a href="${escapeHtml(item.detailUrl)}">详情 / 评论 ${Number(item.commentCount) || 0}</a><time>${escapeHtml(item.createdAt)}</time></footer>
</article>`).join('');
const previous = result.page > 1 ? `<a href="?page=${result.page - 1}${tag ? `&tag=${encodeURIComponent(tag)}` : ''}">上一页</a>` : '';
const next = result.page * result.pageSize < result.total
? `<a href="?page=${result.page + 1}${tag ? `&tag=${encodeURIComponent(tag)}` : ''}">下一页</a>` : '';
const detailNavigation = navigation && (navigation.previous || navigation.next)
? `<nav aria-label="相邻微博">${navigation.previous ? `<a href="/says/${Number(navigation.previous)}" rel="prev">← 上一条</a>` : '<span></span>'}
${navigation.next ? `<a href="/says/${Number(navigation.next)}" rel="next">下一条 →</a>` : '<span></span>'}</nav>` : '';
}

function createSayViewModel(result, {title = '碎语', tag = '', section = '', composer = '',
navigation = null, view = navigation ? 'detail' : tag ? 'tag' : 'list', listPath = listUrl} = {}) {
const items = result.items.map(publicItem);
const neighbor = id => id ? {id: Number(id), url: detailUrl(id)} : null;
return {
view,
title: tag ? `#${tag} - ${title}` : title,
heading: tag ? `#${tag}` : title,
tag,
items,
item: view === 'detail' ? items[0] || null : null,
pagination: {
page: result.page, pageSize: result.pageSize, total: result.total,
totalPages: Math.ceil(result.total / result.pageSize),
previousUrl: result.page > 1 ? pageUrl(result.page - 1, tag, listPath) : null,
nextUrl: result.page * result.pageSize < result.total ? pageUrl(result.page + 1, tag, listPath) : null
},
navigation: {previous: neighbor(navigation?.previous), next: neighbor(navigation?.next)},
urls: {list: listUrl, tag: tag ? tagUrl(tag) : null,
current: view === 'detail' ? items[0]?.detailUrl || null : pageUrl(result.page, tag, listPath)},
// Reuse escaped components, never raw credentials. Custom themes receive location
// data/text, not the configured map service key embedded by the legacy page.
html: {items: renderItems(items), composer, comments: section}
};
}

function renderSayPage(result, {amapKey = '', viewModel, ...options} = {}) {
const page = viewModel || createSayViewModel(result, options);
const items = amapKey ? renderItems(page.items, {amapKey}) : page.html.items;
const {previousUrl, nextUrl} = page.pagination;
const previous = previousUrl ? `<a href="${escapeHtml(previousUrl)}">上一页</a>` : '';
const next = nextUrl ? `<a href="${escapeHtml(nextUrl)}">下一页</a>` : '';
const navigation = page.navigation;
const detailNavigation = navigation.previous || navigation.next
? `<nav aria-label="相邻微博">${navigation.previous ? `<a href="${escapeHtml(navigation.previous.url)}" rel="prev">← 上一条</a>` : '<span></span>'}
${navigation.next ? `<a href="${escapeHtml(navigation.next.url)}" rel="next">下一条 →</a>` : '<span></span>'}</nav>` : '';
return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>${escapeHtml(tag ? `#${tag} - ${title}` : title)}</title><style>
<title>${escapeHtml(page.title)}</title><style>
body{margin:0;background:#f5f5f5;color:#222;font:16px/1.7 system-ui,sans-serif}.say-wrap{max-width:720px;margin:0 auto;padding:32px 20px}
h1{font-size:28px}.say-item{background:#fff;border-radius:10px;padding:20px 24px;margin:16px 0;box-shadow:0 1px 3px #0001}
.say-images{display:grid;grid-template-columns:repeat(auto-fit,minmax(160px,1fr));gap:8px;margin-top:12px}.say-images img{width:100%;border-radius:6px}
.say-map{display:block;width:100%;max-height:240px;object-fit:cover;border-radius:6px}.say-card{display:flex;gap:12px;align-items:center;margin-top:12px}.say-card img{width:72px;height:72px;object-fit:cover;border-radius:6px}.say-card p{margin:0}.say-card small{display:block;color:#888}
footer{display:flex;gap:8px;flex-wrap:wrap;margin-top:14px;color:#888;font-size:13px}footer time{margin-left:auto}a{color:#1677ff;text-decoration:none}
nav{display:flex;justify-content:space-between;margin-top:24px}.say-empty{color:#888}</style></head><body><main class="say-wrap">
<h1>${escapeHtml(tag ? `#${tag}` : title)}</h1>${composer}${items || '<p class="say-empty">暂无碎语</p>'}${navigation ? detailNavigation : `<nav>${previous}<span></span>${next}</nav>`}
${section}</main></body></html>`;
<h1>${escapeHtml(page.heading)}</h1>${page.html.composer}${items || '<p class="say-empty">暂无碎语</p>'}${page.view === 'detail' ? detailNavigation : `<nav>${previous}<span></span>${next}</nav>`}
${page.html.comments}</main></body></html>`;
}

module.exports = {escapeHtml, renderSayPage};
module.exports = {escapeHtml, renderSayPage, createSayViewModel};
11 changes: 11 additions & 0 deletions packages/say/lib/urls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const listUrl = '/says';
const detailUrl = id => `${listUrl}/${Number(id)}`;
const tagUrl = tag => `${listUrl}/tag/${encodeURIComponent(tag)}`;
// Retain the existing tag query on both list and tag-path pagination. Do not
// carry arbitrary request queries (tokens, preview flags, etc.) into public links.
const pageUrl = (page, tag = '', pathname = listUrl) =>
`${pathname}?page=${page}${tag ? `&tag=${encodeURIComponent(tag)}` : ''}`;

module.exports = {listUrl, detailUrl, tagUrl, pageUrl};
Loading
Loading