Files
rainblogweb/lib/author.js
T

41 lines
2.0 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.
// lib/author.js —— 作者显示信息组装(论坛/博客/评论/公开主页共用)
//
// SQL 侧需为作者 JOIN 补选(见 routes/forum.js / routes/blog.js):
// u.username AS author_name, u.nickname AS author_nickname, u.title AS author_title,
// u.title_color AS author_title_color, u.role AS author_role
//u.title 用 AS 别名,避免与帖子/文章的 title 列冲突)
// 前端最终拿到的作者字段:author_id, author_name(=nickname||username),
// author_username(原始登录名,恒存在——站长帖判断等需用它), author_avatar,
// author_title, author_title_color, author_role。
const { authorAvatar } = require('./avatar');
// 在 JOIN 行上直接写入作者显示字段(幂等:已有 author_avatar / author_username 则保留)
function attachAuthor(p) {
if (!p) return p;
if (p.author_avatar === undefined) p.author_avatar = authorAvatar(p);
// 覆盖 author_name 前先保存原始登录名(前端「站长帖」判断等依赖原始 username)
if (p.author_username === undefined) p.author_username = String(p.author_name || '').trim();
p.author_name = String(p.author_nickname || '').trim() || p.author_name || p.username || '';
p.author_title = String(p.author_title || '').trim();
p.author_title_color = String(p.author_title_color || '').trim();
p.author_role = p.author_role || 'user';
return p;
}
// 组装作者显示信息(返回独立对象,供测试或需要独立作者对象的场景)
function authorInfo(p) {
if (!p) return null;
return {
id: p.author_id != null ? p.author_id : p.uid,
username: String(p.author_username || p.author_name || '').trim(),
name: String(p.author_nickname || '').trim() || p.author_name || p.username || '',
avatar: p.author_avatar || '',
title: String(p.author_title || '').trim(),
title_color: String(p.author_title_color || '').trim(),
role: p.author_role || 'user',
};
}
module.exports = { attachAuthor, authorInfo };