Skip to content

集合:树洞互动(comments)

字数
1042 字
阅读时间
5 分钟

1. 目的与使用场景

  • 存储树洞帖子的回复、抱抱、收藏等互动记录,为社区互动流、温和守护和埋点 treehole_care_action 提供数据。由 /api/treehole/posts 子接口、互动云函数、风控审核使用。

2. Schema 定义

字段类型必填默认值约束/校验说明隐私分级
_idstring-TCB 自动主键P2
_openidstring-TCB 自动互动者 openidP2
user_id指针id-指向 users._id用户P2
post_id指针id-指向 posts._id所属帖子P2
parent_id指针id-指向 comments._id楼中楼引用P2
action_typestringreply枚举(reply,hug,save,report,moderator_note)互动类型P1
contentstring-长度 ≤ 1000文本内容,仅 reply/moderator_noteP3
mood_thermometernumber-0-100回复时的情绪P3
is_anonymousbooltrue-匿名展示P1
alias_namestring-长度 ≤ 20匿名昵称P1
metadataobject-{emoji:'hug_blue'}附加信息P1
risk_flagstringnone枚举(none,watch,escalated)风险标记P3
statusstringactive枚举(active,hidden,removed)状态P1
createdAtdate-服务端时间创建时间P1
updatedAtdate-服务端时间更新时间P1

3. 索引与唯一约束

  • 单字段索引:post_idaction_typestatus
  • 复合索引:[post_id, createdAt asc][post_id, action_type]
  • 唯一性约束:[post_id, user_id, action_type='hug'] 防止重复抱抱
  • 设计理由:帖子详情按创建时间排序;抱抱唯一约束避免刷量。

4. 访问控制(TCB 权限)

json
{
  "read": "status == 'active' || _openid == auth.openid || auth.role in ['moderator','counselor']",
  "write": "_openid == auth.openid || auth.role in ['moderator']"
}

守护员可隐藏或恢复互动;作者可删除自身回复。

  1. 关系与级联
  • post_idposts
  • parent_idcomments
  • user_idusers
  • 删除策略:帖子被移除时批量将互动 status 置为 hidden。
  • 反范式:metadata 可存储 hug 颜色、收藏渠道等扩展字段。
  1. 数据生命周期与合规
  • 留存:互动记录保留 1 年;被删除或举报的内容保留 180 天审计。
  • 匿名化:导出时去除 alias_name,替换为 role-based 标签。
  • 导出流程:用户申请导出 → 返回自身发表的互动记录。
  • 审计字段:createdAtupdatedAtstatusrisk_flag
  1. API/云函数契约映射 | 接口/函数 | 读/写 | 使用字段 | 过滤条件 | 排序/分页 | 备注 | | /api/treehole/posts/:id/comments GET | 读 | action_type, content, alias_name, createdAt | post_id = :id, status='active' | createdAt asc | 帖子详情 | | /api/treehole/posts/:id/comments POST | 写 | action_type, content, mood_thermometer, alias_name | _openid = auth.openid | - | 发布互动 | | 抱抱云函数 toggleHug | 写 | action_type='hug', status | post_id, _openid | - | 抱抱/取消 | | 风控函数 moderateComment | 写 | status, risk_flag | _id | - | 隐藏违规互动 |

  2. 示例文档(≥3条)

json
{
  "_id": "cmt_5001",
  "_openid": "oAbcd222",
  "user_id": "usr_010",
  "post_id": "post_900",
  "action_type": "reply",
  "content": "可以和室友约个安静时间段,互相提醒。",
  "alias_name": "同路人",
  "mood_thermometer": 60,
  "status": "active",
  "createdAt": "2024-04-05T15:35:00Z",
  "updatedAt": "2024-04-05T15:35:00Z"
}
{
  "_id": "cmt_5002",
  "_openid": "oAbcd123",
  "user_id": "usr_001",
  "post_id": "post_900",
  "action_type": "hug",
  "metadata": {"emoji": "hug_blue"},
  "status": "active",
  "createdAt": "2024-04-05T15:40:00Z",
  "updatedAt": "2024-04-05T15:40:00Z"
}
{
  "_id": "cmt_5003",
  "_openid": "oAbcd789",
  "user_id": "usr_guard_01",
  "post_id": "post_900",
  "action_type": "moderator_note",
  "content": "如需即时帮助可查看我的页资源地图。",
  "status": "active",
  "risk_flag": "watch",
  "createdAt": "2024-04-05T16:00:00Z",
  "updatedAt": "2024-04-05T16:00:00Z"
}
  1. 常用查询样例(≥3条)
javascript
// 获取帖子回复
const replies = await db.collection('comments').where({
  post_id: postId,
  action_type: 'reply',
  status: 'active'
}).orderBy('createdAt', 'asc').get();

// 抱抱统计
const hugCount = await db.collection('comments').where({
  post_id: postId,
  action_type: 'hug',
  status: 'active'
}).count();

// 审核员查看被举报互动
const reported = await db.collection('comments').where({
  status: 'hidden',
  risk_flag: db.command.in(['watch','escalated'])
}).orderBy('updatedAt', 'desc').limit(100).get();
  1. 边界与错误码
  • 重复抱抱:命中唯一约束返回 E_HUG_DUPLICATE
  • 违规内容:审核函数将 status 改为 hidden 并返回 E_COMMENT_BLOCKED
  • 越权删除:尝试删除非本人互动返回 E_FORBIDDEN
  1. 变更影响评估
  • 字段变更需同步互动 API、埋点 treehole_care_action、社区看板统计。
  • 索引调整影响帖子详情加载与抱抱操作延迟。
  1. 假设与待确认
  • 假设收藏(save) 也写入该集合,metadata 标记来源;待确认是否需要单独收藏表。
  • 待确认 moderator_note 是否需要额外的审核流程字段。

贡献者

The avatar of contributor named as Cai Hongyu Cai Hongyu

文件历史

撰写