Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有的时候在主页和搜索页面视频UP主名字显示不出来了 #242

Open
Cocoa-C opened this issue Sep 1, 2024 · 6 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@Cocoa-C
Copy link

Cocoa-C commented Sep 1, 2024

屏幕截图 2024-09-01 105432
屏幕截图 2024-09-01 105437
在历史界面就不会有这样的情况

@cfdxkk cfdxkk self-assigned this Sep 1, 2024
@cfdxkk
Copy link
Member

cfdxkk commented Sep 1, 2024

主页使用虚拟属性关联表。
历史记录使用 pipeline 关联表。

虚拟属性在网络波动时运行不太稳定,但是有 TyepScript 支持,写 pipeline 的话几乎没有 TyepScript 支持。
如果很频繁发生的话,就全改成 pipeline.

@otomad
Copy link
Member

otomad commented Sep 1, 2024

主页使用虚拟属性关联表。
历史记录使用 pipeline 关联表。

虚拟属性在网络波动时运行不太稳定,但是有 TyepScript 支持,写 pipeline 的话几乎没有 TyepScript 支持。
如果很频繁发生的话,就全改成 pipeline.

TypeScript 支持是什么鬼?看看后端代码?

@cfdxkk
Copy link
Member

cfdxkk commented Sep 1, 2024

TypeScript 支持是什么鬼?看看后端代码?

就是虚拟属性可以根据传入的 schame 推算出返回结果的类型,但是 pipeline 太自由了,没法推算返回结果的类型

@otomad
Copy link
Member

otomad commented Sep 1, 2024

TypeScript 支持是什么鬼?看看后端代码?

就是虚拟属性可以根据传入的 schame 推算出返回结果的类型,但是 pipeline 太自由了,没法推算返回结果的类型

试试类型体操?

@cfdxkk
Copy link
Member

cfdxkk commented Sep 10, 2024

TypeScript 支持是什么鬼?看看后端代码?

就是虚拟属性可以根据传入的 schame 推算出返回结果的类型,但是 pipeline 太自由了,没法推算返回结果的类型

试试类型体操?

Pipeline 太自由了,没法体操。
下方是获取视频评论的 pipeline,在步骤 2 的 lookup 中关联了 user-infos 表,但我没法推算出 user-infos 表的 UUID 字段。假如我不小心写成小写的 uuid 也不会报错,但是却会导致关联查询结果为空。

// 获取视频评论的 pipeline
const getVideoCommentsPipeline: PipelineStage[] = [
	// 1. 查询评论信息
	{
		$match: {
			videoId // 通过 videoId 筛选评论
		},
	},
	// 2. 关联用户表获取评论发送者信息
	{
		$lookup: {
			from: 'user-infos', // WARN: 别忘了变复数
			localField: 'UUID',
			foreignField: 'UUID',
			as: 'user_info_data',
		},
	},
	{
		$unwind: {
			path: '$user_info_data',
			preserveNullAndEmptyArrays: true, // 保留空数组和null值
		},
	},
	// 3. 按楼层升序排序
	{ $sort: { 'commentIndex': 1 } },
	// 4. 分页查询
	{ $skip: skip }, // 跳过指定数量的文档
	...(pageSize ? [{ $limit: pageSize }] : []), // 限制返回的文档数量
	// 5. 关联目标用户的点赞数据
	{
		$lookup: {
			from: 'video-comment-upvotes', // 用户视频评论点赞表名 // WARN: 别忘了变复数
			let: { commentId: { $toString: '$_id' } }, // 当前评论的 _id
			pipeline: [
				{
					$match: {
						$expr: {
							$and: [
								{ $eq: ['$commentId', '$$commentId'] }, // 匹配评论 ID
								{ $eq: ['$UUID', uuid] }, // 匹配用户 UUID
								{ $eq: ['$invalidFlag', false] }, // 只统计有效点赞
							],
						},
					},
				},
			],
			as: 'userUpvote',
		},
	},
	// 6. 只关联该用户的点踩数据
	{
		$lookup: {
			from: 'video-comment-downvotes', // 用户视频评论点踩表名 // WARN: 别忘了变复数
			let: { commentId: { $toString: '$_id' } },
			pipeline: [
				{
					$match: {
						$expr: {
							$and: [
								{ $eq: ['$commentId', '$$commentId'] },
								{ $eq: ['$UUID', uuid] }, // 匹配用户 UUID
								{ $eq: ['$invalidFlag', false] }, // 只统计有效点踩
							],
						},
					},
				},
			],
			as: 'userDownvote',
		},
	},
	// 7. 判断用户是否点赞或点踩
	{
		$addFields: {
			isUpvote: { $gt: [{ $size: '$userUpvote' }, 0] }, // 是否点赞
			isDownvote: { $gt: [{ $size: '$userDownvote' }, 0] }, // 是否点踩
		},
	},
	// 8. 清理不必要字段,返回所需数据
	{
		$project: {
			_id: 1, // 评论的 ID
			content: 1, // 评论内容
			commentRoute: 1, // 评论的路由
			videoId: 1,
			UUID: 1, // 评论发送者的 UUID
			uid: 1, // 评论发送者的 UID
			emitTime: 1, // 发送评论的时间
			text: 1, // 评论正文
			upvoteCount: 1, // 评论点赞数
			downvoteCount: 1, // 评论点踩数
			commentIndex: 1, // 评论楼层数
			subCommentsCount: 1, // 该评论的下一级子评论数量
			editDateTime: 1, // 最后编辑时间
			isUpvote: 1, // 是否已点赞
			isDownvote: 1, // 是否已点踩
			userInfo: {
				username: '$user_info_data.username', // 用户名
				userNickname: '$user_info_data.userNickname', // 用户昵称
				avatar: '$user_info_data.avatar', // 用户头像的链接
				signature: '$user_info_data.signature', // 用户的个性签名
				gender: '$user_info_data.gender' // 用户的性别
			},
		},
	},
]

@otomad
Copy link
Member

otomad commented Sep 11, 2024

意思是数组中每一项都是可能的值?感觉好像可以做成类型体操。

另外 UUID 应该改成小写,UID 都是小写,UUID应该也是小写。

@Aira-Sakuranomiya Aira-Sakuranomiya added the bug Something isn't working label Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants