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
6 changes: 3 additions & 3 deletions packages/say/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ QQ、企业微信、公众号、飞书和外部 API 的处理失败会返回经

### 开启与审核

1. 在插件列表中打开 Say 设置,开启「启用碎语评论」并保存。默认关闭,升级不会自动开放历史碎语。
2. 在「微博列表」编辑需要开放评论的碎语,开启「允许评论」。后台新建、外部 API 以及消息渠道发布的碎语默认不开启评论;API 发布可明确传入 `allowComments: true`。全局开关和单条开关必须同时打开。
1. 新发布碎语默认允许评论,无需开启全局评论开关。旧的 `commentsEnabled` 配置不再生效。
2. 每条碎语保留「允许评论」开关,可随时关闭或开启;API 发布可明确传入 `allowComments: false` 禁止评论。历史碎语的单条设置不变,不批量开启;私密和隐藏碎语仍不可公开评论。
3. 打开公开碎语的 `/says/ID` 页面提交评论。访客填写昵称;邮箱、网站和字数要求沿用站点评论设置。站点开启强制登录时,先通过博客登录页登录。身份由服务器验证,客户端不能指定评论作者或审核状态。
4. 在 Firekylin 后台「评论管理」中审核。**包括管理员在内的所有新评论都先进入待审核**;命中站点禁用词的评论进入垃圾评论。只有审核通过的评论公开展示。拒绝、转垃圾或删除后,不再公开展示。
5. 关闭任一评论开关会隐藏该条碎语的评论及表单,但保留记录;重新开启后可再次展示。碎语设为私密、隐藏或插件关闭后,详情与评论接口不再公开访问。
Expand Down Expand Up @@ -550,7 +550,7 @@ QQ、企业微信、公众号、飞书和外部 API 的处理失败会返回经

`/says` 和标签列表顶部提供文字发布入口。未登录时仅显示「登录」,跳转到后台登录并在登录成功后返回 `/says`;已登录管理员显示输入框。发布权限与现有后台插件表单一致,仅管理员可用;普通评论账号、编辑账号不显示发布入口或提示,也没有发布权限。服务器重新检查账号是否有效、是否仍为管理员,作者使用当前登录用户,不能通过请求指定他人。

输入文字(支持 `#标签`),点击「公开发布」后返回列表查看结果。首版是文字公开发布,评论默认关闭;图片等其他类型、状态和评论开关仍在后台编辑。发布中按钮禁用,失败保留输入,不自动重试;网络异常时请先刷新列表核实是否已发布。
输入文字(支持 `#标签`),点击「公开发布」后返回列表查看结果。首版是文字公开发布,评论默认允许;图片等其他类型、状态和评论开关仍在后台编辑。发布中按钮禁用,失败保留输入,不自动重试;网络异常时请先刷新列表核实是否已发布。

使用独立的 `POST /api/plugins/say/frontend-publish`,要求后台会话、会话绑定的 CSRF 令牌、同源 Origin 和 JSON。不会把外部发布 API Key 放进页面。列表响应使用 `private, no-store`,部署时也不要在 CDN 上强制缓存这些带登录状态的页面。关闭插件公开发布开关会隐藏发布入口并拒绝提交。

Expand Down
2 changes: 1 addition & 1 deletion packages/say/lib/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SayComments {
const settings = await this.say.settings();
const item = await this.say.get(id);
if (!settings.enabled || item.status !== 'public') fail(404, '碎语不存在。');
if (!settings.commentsEnabled || !item.allowComments) fail(writing ? 403 : 404, '这条碎语的评论已关闭。');
if (!item.allowComments) fail(writing ? 403 : 404, '这条碎语的评论已关闭。');
return item;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/say/lib/frontend-publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FrontendPublish {
}
// Author, kind, visibility and source are server-owned for this simple composer.
const item = await this.say.create({kind: 'text', status: 'public', text: body.text,
source: 'Firekylin', allowComments: false}, identity.id);
source: 'Firekylin', allowComments: true}, identity.id);
return {success: true, id: item.id};
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/say/lib/say-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const KINDS = new Set(['text', 'image', 'textpic', 'voice', 'location', 'link',
const STATUSES = new Set(['public', 'private', 'hidden']);
const DEFAULT_SETTINGS = Object.freeze({
enabled: true,
commentsEnabled: false,
pageSize: 10,
apiKey: '',
authorId: 1,
Expand Down Expand Up @@ -72,7 +71,6 @@ function normalizeSettings(value = {}, previous = {}) {
? value.qqAppSecret.trim().slice(0, 256) : prior.qqAppSecret,
qqAllowedOpenIds: typeof value.qqAllowedOpenIds === 'string'
? [...new Set(strings(value.qqAllowedOpenIds))].join('\n') : prior.qqAllowedOpenIds,
commentsEnabled: typeof value.commentsEnabled === 'boolean' ? value.commentsEnabled : prior.commentsEnabled,
enabled: typeof value.enabled === 'boolean' ? value.enabled : prior.enabled,
pageSize: integer(value.pageSize, prior.pageSize, 1, 100),
apiKey: typeof value.apiKey === 'string' && value.apiKey ? value.apiKey.slice(0, 256) : prior.apiKey,
Expand Down Expand Up @@ -139,7 +137,7 @@ function normalize(input = {}) {
if (!KINDS.has(kind)) throw Object.assign(new Error('Invalid say kind'), {status: 400});
if (!STATUSES.has(status)) throw Object.assign(new Error('Invalid say status'), {status: 400});
const text = String(input.text || '').trim().slice(0, 20000);
const data = {kind, status, allowComments: input.allowComments === true, text, tags: tags(input.tags, text), source: String(input.source || 'Firekylin').slice(0, 80)};
const data = {kind, status, allowComments: input.allowComments === undefined ? true : input.allowComments === true, text, tags: tags(input.tags, text), source: String(input.source || 'Firekylin').slice(0, 80)};
if (['image', 'textpic'].includes(kind)) {
data.images = strings(input.images).slice(0, 20).map((item, index) => safeUrl(item, `Image ${index + 1}`));
if (!data.images.length) throw Object.assign(new Error('At least one image URL is required'), {status: 400});
Expand Down
11 changes: 5 additions & 6 deletions packages/say/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const statusOptions = [
const publishFields = [
{name: 'kind', type: 'radio', label: '类型', required: true, options: kindOptions},
{name: 'status', type: 'radio', label: '状态', required: true, options: statusOptions},
{name: 'allowComments', type: 'switch', label: '允许评论', description: '还需在碎语设置中启用评论;提交后须后台审核。'},
{name: 'allowComments', type: 'switch', label: '允许评论', description: '默认允许评论;提交后须后台审核。'},
{name: 'text', type: 'code', label: '内容', description: '文本类型必填;其他类型可作为附言。使用 #标签 可自动提取标签。'},
{name: 'tags', type: 'text', label: '标签', description: '可选,使用逗号分隔。'},
{name: 'images', type: 'code', label: '图片 URL', required: true, visibleWhen: {field: 'kind', values: ['image', 'textpic']},
Expand All @@ -53,12 +53,12 @@ const publishFields = [
];
const textPublishFields = [
{name: 'status', type: 'radio', label: '状态', required: true, options: statusOptions},
{name: 'allowComments', type: 'switch', label: '允许评论', description: '还需在碎语设置中启用评论;提交后须后台审核。'},
{name: 'allowComments', type: 'switch', label: '允许评论', description: '默认允许评论;提交后须后台审核。'},
{name: 'text', type: 'code', label: '内容', required: true, description: '使用 #标签 可自动提取标签。'}
];

function defaultPublish() {
return {kind: 'text', status: 'public', allowComments: false, text: '', tags: '', images: '', mediaUrl: '', latitude: '',
return {kind: 'text', status: 'public', allowComments: true, text: '', tags: '', images: '', mediaUrl: '', latitude: '',
longitude: '', label: '', url: '', title: '', description: '', artist: '', cover: ''};
}

Expand Down Expand Up @@ -444,7 +444,7 @@ module.exports = {
const item = await service.get(params.id);
if (!settings.enabled || item.status !== 'public') return request.throw(404);
let section = '<p>评论已关闭。</p>';
if (settings.commentsEnabled && item.allowComments) {
if (item.allowComments) {
const config = await comments.configuration();
section = renderWalineComments(item, config);
}
Expand Down Expand Up @@ -511,7 +511,7 @@ module.exports = {
});

ctx.admin.registerForm({id: 'publish-say', page: 'publish', title: '碎语内容', submitLabel: '发布', fields: textPublishFields,
load: async() => ({status: 'public', allowComments: false, text: ''}), submit: async(values, context) => {
load: async() => ({status: 'public', allowComments: true, text: ''}), submit: async(values, context) => {
return feedback.adminResult(() => service.create({...values, kind: 'text'}, context.user.id),
'publish', pluginLogger);
}});
Expand Down Expand Up @@ -540,7 +540,6 @@ module.exports = {
ctx.admin.registerForm({id: 'say-basic-settings', page: 'settings', title: '碎语设置',
submitLabel: '保存全部设置', fields: [
{name: 'enabled', type: 'switch', label: '启用公开列表和消息发布'},
{name: 'commentsEnabled', type: 'switch', label: '启用碎语评论', description: '默认关闭;同时开启单条碎语的允许评论后生效。所有评论须在后台评论管理中审核。'},
{name: 'pageSize', type: 'number', label: '每页数量', required: true},
{name: 'authorId', type: 'number', label: '发布作者 ID', required: true,
description: '外部 API、企业微信、微信公众号、飞书和 QQ 发布的碎语都会归属到这个用户。'},
Expand Down
10 changes: 8 additions & 2 deletions packages/say/test/admin-settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function setup(t) {
const forms = [];
const pages = [];
const noop = () => {};
const settings = {enabled: true, commentsEnabled: false, pageSize: 20, authorId: 1, amapKey: 'private-map-key',
const settings = {enabled: true, pageSize: 20, authorId: 1, amapKey: 'private-map-key',
apiKey: 'private-api-key-123456', wecomCorpId: 'corp', wecomCorpSecret: 'private-corp-secret',
wecomToken: 'private-corp-token', wecomEncodingAESKey: 'a'.repeat(43),
wechatAppId: 'wx_app', wechatAppSecret: 'private-wx-secret', wechatToken: 'private-wx-token',
Expand All @@ -23,7 +23,7 @@ function setup(t) {
registerPanel: noop, registerPage: page => pages.push(page),
registerForm: form => forms.push(form), registerAction: noop
}});
return {forms: forms.filter(form => form.page === 'settings'), pages, settings, load};
return {allForms: forms, forms: forms.filter(form => form.page === 'settings'), pages, settings, load};
}

test('references one registered settings resource, not a separate Feishu endpoint', async t => {
Expand Down Expand Up @@ -104,3 +104,9 @@ test('saves all providers concurrently and preserves existing partial-save behav
assert.ok(!Object.hasOwn(reloaded, 'settingsGroup'));
}
});

test('new post forms allow comments and settings have no global comment switch', async t => {
const {allForms, forms} = setup(t);
assert.equal((await allForms.find(form => form.id === 'publish-say').load()).allowComments, true);
assert.ok(forms.every(form => form.fields.every(field => field.name !== 'commentsEnabled')));
});
8 changes: 4 additions & 4 deletions packages/say/test/comments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {renderComments} = require('../lib/comment-render');

function fixture() {
const rows = [];
const settings = {enabled: true, commentsEnabled: true};
const settings = {enabled: true, commentsEnabled: false}; // Obsolete stored option must have no effect.
const item = {id: 7, status: 'public', allowComments: true};
const options = {comment: {}};
let user;
Expand Down Expand Up @@ -47,7 +47,7 @@ test('stores pending comments with canonical target and ignores forged identity/

test('requires public, enabled, commentable targets for reads and writes', async() => {
for (const change of [f => { f.item.status = 'private' }, f => { f.item.status = 'hidden' },
f => { f.settings.enabled = false }, f => { f.settings.commentsEnabled = false }, f => { f.item.allowComments = false }]) {
f => { f.settings.enabled = false }, f => { f.item.allowComments = false }]) {
const f = fixture(); change(f);
await assert.rejects(f.comments.list(7));
await assert.rejects(f.comments.submit(7, body, f.request));
Expand Down Expand Up @@ -170,7 +170,7 @@ test('registered detail and comment routes respect visibility without changing n
const request = {set() {}, throw(status) {throw Object.assign(new Error(), {status})}};
await detail.handler({ctx: request, params: {id: '7'}, query: {}});
assert.match(request.body, /评论已关闭/);
t.mock.method(SayService.prototype, 'settings', async() => ({enabled: true, commentsEnabled: true}));
t.mock.method(SayService.prototype, 'settings', async() => ({enabled: true, commentsEnabled: false}));
get.mock.mockImplementation(async() => ({id: 7, pathname: 'example', status: 'public', allowComments: true, text: 'hello', tags: []}));
t.mock.method(SayComments.prototype, 'configuration', async() => ({options: {}, public: {}}));
await detail.handler({ctx: request, params: {id: '7'}, query: {}});
Expand Down Expand Up @@ -231,7 +231,7 @@ test('native comment filter retains Say switches and moderation without adding a
const data = {url: '/says?pathname=example&type=3', status: 'approved'};
assert.equal((await f.comments.beforeNativeCreate(data)).status, 'waiting');
assert.equal((await f.comments.beforeNativeCreate({...data, status: 'spam'})).status, 'spam');
f.settings.commentsEnabled = false;
f.item.allowComments = false;
await assert.rejects(f.comments.beforeNativeCreate(data), {status: 403});
type = 0;
assert.equal(await f.comments.beforeNativeCreate(data), data);
Expand Down
2 changes: 1 addition & 1 deletion packages/say/test/frontend-publish.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test('renders only login for guests, nothing for other accounts and composer for
test('publishes only text under authenticated author, ignoring forged fields', async() => {
const f = setup();
assert.deepEqual(await f.publisher.submit(f.request, {text: 'hello #tag', user_id: 1, kind: 'image', status: 'private', allowComments: true}), {success: true, id: 9});
assert.deepEqual(f.writes, [{author: 7, input: {kind: 'text', status: 'public', text: 'hello #tag', source: 'Firekylin', allowComments: false}}]);
assert.deepEqual(f.writes, [{author: 7, input: {kind: 'text', status: 'public', text: 'hello #tag', source: 'Firekylin', allowComments: true}}]);
});
test('denies missing, banned, demoted and non-admin accounts, disabled publishing and invalid requests', async() => {
for (const change of [f => { f.session = null }, f => { f.user = {} }, f => { f.user.status = 0 },
Expand Down
20 changes: 19 additions & 1 deletion packages/say/test/say-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ test('normalizes content, tags, URLs and settings', () => {
assert.throws(() => normalize({kind: 'unknown', text: 'content'}), /Invalid say kind/);
assert.throws(() => normalize({status: 'privte', text: 'content'}), /Invalid say status/);
assert.deepEqual(normalizeSettings({pageSize: 999, apiKey: ''}, {apiKey: 'keep'}),
{enabled: true, commentsEnabled: false, pageSize: 100, apiKey: 'keep', authorId: 1, amapKey: '', wecomCorpId: '',
{enabled: true, pageSize: 100, apiKey: 'keep', authorId: 1, amapKey: '', wecomCorpId: '',
wecomCorpSecret: '', wecomToken: '', wecomEncodingAESKey: '', wechatAppId: '', wechatAppSecret: '',
wechatToken: '', wechatEncodingAESKey: '', wechatAllowedOpenIds: '', feishuAppId: '',
feishuAppSecret: '', feishuVerificationToken: '', feishuEncryptKey: '', feishuAllowedOpenIds: '',
Expand Down Expand Up @@ -190,3 +190,21 @@ test('comment switches persist and comments prevent destructive operations', asy
comments.length = 0;
assert.equal(await service.remove(a.id), 1);
});

test('new says allow comments by default while explicit and historical opt-outs persist', async() => {
const {service, rows} = fixture();
for (const source of ['Firekylin', 'API', 'QQ', '飞书', '企业微信', '微信公众号']) {
const item = await service.create({text: source, source}, 1);
assert.equal((await service.get(item.id)).allowComments, true);
}
const closed = await service.create({text: 'closed', allowComments: false}, 1);
assert.equal((await service.get(closed.id)).allowComments, false);
await service.update(closed.id, {text: 'edited'});
assert.equal((await service.get(closed.id)).allowComments, false);
const copy = await service.duplicate(closed.id, 1);
assert.equal(copy.allowComments, false);
rows[0].allow_comment = 0;
await service.update(rows[0].id, {text: 'historical'});
assert.equal((await service.get(rows[0].id)).allowComments, false);
assert.equal(Object.hasOwn(normalizeSettings({commentsEnabled: true}, {commentsEnabled: false}), 'commentsEnabled'), false);
});
Loading