Skip to content

Commit

Permalink
✨ 获取全部或过滤后的用户浏览历史,按对某一内容的最后访问时间降序排序
Browse files Browse the repository at this point in the history
  • Loading branch information
cfdxkk committed Jun 13, 2024
1 parent 6198983 commit bf5769a
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 53 deletions.
34 changes: 27 additions & 7 deletions src/controller/BrowsingHistoryController.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
import { createBrowsingHistoryService } from '../service/BrowsingHistoryService.js'
import { createOrUpdateBrowsingHistoryService, getUserBrowsingHistoryWithFilterService } from '../service/BrowsingHistoryService.js'
import { koaCtx, koaNext } from '../type/koaTypes.js'
import { CreateBrowsingHistoryRequestDto } from './BrowsingHistoryControllerDto.js'
import { CreateOrUpdateBrowsingHistoryRequestDto, GetUserBrowsingHistoryWithFilterRequestDto } from './BrowsingHistoryControllerDto.js'

/**
* 创建用户浏览历史
* 更新或创建用户浏览历史
* @param ctx context
* @param next context
*/
export const createUserBrowsingHistoryController = async (ctx: koaCtx, next: koaNext) => {
const data = ctx.request.body as Partial<CreateBrowsingHistoryRequestDto>
export const createOrUpdateUserBrowsingHistoryController = async (ctx: koaCtx, next: koaNext) => {
const data = ctx.request.body as Partial<CreateOrUpdateBrowsingHistoryRequestDto>
const uid = parseInt(ctx.cookies.get('uid'), 10)
const token = ctx.cookies.get('token')
const createBrowsingHistoryRequest: CreateBrowsingHistoryRequestDto = {
const createOrUpdateBrowsingHistoryRequest: CreateOrUpdateBrowsingHistoryRequestDto = {
/** 用户的 UID - 非空 */
uid: data.uid,
/** 浏览的内容的类型,比如说 video, photo 等 - 非空 */
type: data.type,
/** 浏览的内容的唯一 ID - 非空 */
id: data.id,
/** 浏览的定位锚点,如果是视频就是播放时间,如果是相册可能是上次浏览到相册第n张图片,为了兼容性使用 String */
anchor: data.anchor ?? null,
}
const createBrowsingHistoryResponse = await createBrowsingHistoryService(createBrowsingHistoryRequest, uid, token)
const createBrowsingHistoryResponse = await createOrUpdateBrowsingHistoryService(createOrUpdateBrowsingHistoryRequest, uid, token)
ctx.body = createBrowsingHistoryResponse
await next()
}

/**
* 获取全部或过滤后的用户浏览历史,按对某一内容的最后访问时间降序排序
* @param ctx context
* @param next context
*/
export const getUserBrowsingHistoryWithFilterController = async (ctx: koaCtx, next: koaNext) => {
const videoTitle = ctx.query.videoTitle as string
const uid = parseInt(ctx.cookies.get('uid'), 10)
const token = ctx.cookies.get('token')
const getUserBrowsingHistoryWithFilterRequest: GetUserBrowsingHistoryWithFilterRequestDto = {
/** 过滤条件 - 视频标题 */
videoTitle,
}
const getUserBrowsingHistoryWithFilterResponse = await getUserBrowsingHistoryWithFilterService(getUserBrowsingHistoryWithFilterRequest, uid, token)
ctx.body = getUserBrowsingHistoryWithFilterResponse
await next()
}

29 changes: 27 additions & 2 deletions src/controller/BrowsingHistoryControllerDto.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ThumbVideoResponseDto } from './VideoControllerDto.js'

/**
* 用户浏览历史
*/
Expand All @@ -8,21 +10,44 @@ type BrowsingHistory = {
type: 'video' | 'photo' | 'comment';
/** 浏览的内容的唯一 ID - 非空 */
id: string;
/** 浏览的定位锚点,如果是视频就是播放时间,如果是相册可能是上次浏览到相册第n张图片,为了兼容性使用 String */
anchor?: string;
}

/**
* 创建用户浏览历史的请求载荷
*/
export type CreateBrowsingHistoryRequestDto = BrowsingHistory & {}
export type CreateOrUpdateBrowsingHistoryRequestDto = BrowsingHistory & {}

/**
* 创建用户浏览历史的请求响应
*/
export type CreateBrowsingHistoryResponseDto = {
export type CreateOrUpdateBrowsingHistoryResponseDto = {
/** 是否请求成功 */
success: boolean;
/** 附加的文本消息 */
message?: string;
/** 如果成功,返回创建的这个浏览历史数据 */
result?: BrowsingHistory;
}

/**
* 获取用户浏览历史的请求载荷
* 主要是用爱装过滤条件
*/
export type GetUserBrowsingHistoryWithFilterRequestDto = {
/** 过滤条件 - 视频标题 */
videoTitle?: string;
}

/**
* 获取用户浏览历史的请求响应,全部或过滤后的用户浏览历史
*/
export type GetUserBrowsingHistoryWithFilterResponseDto = {
/** 是否请求成功 */
success: boolean;
/** 附加的文本消息 */
message?: string;
/** 如果成功,返回创建的这个浏览历史数据 */
result?: (BrowsingHistory & ThumbVideoResponseDto['videos'][number])[];
}
7 changes: 4 additions & 3 deletions src/dbPool/DbClusterPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ export const selectDataByAggregateFromMongoDB = async <T>(schema: Schema<T>, col
}

try {
const result = (await mongoModel.aggregate(props)).map(results => results.toObject({ virtuals: true }) as T)
const result = (await mongoModel.aggregate(props)) as T[]
return { success: true, message: '数据聚合查询成功', result }
} catch (error) {
console.error('ERROR', '数据聚合查询失败:', error)
Expand Down Expand Up @@ -280,9 +280,10 @@ export const updateData4MongoDB = async <T, P = DbPoolOptionsMarkerType>(where:
* @param schema MongoDB Schema 对象
* @param collectionName 查询数据时使用的 MongoDB 集合的名字(输入单数名词会自动创建该名词的复数形式的集合名)
* @param options 设置项
* @param upsert 如果没有找到,是否创建(默认会创建)
* @returns 更新后的数据
*/
export const findOneAndUpdateData4MongoDB = async <T, P = DbPoolOptionsMarkerType>(where: QueryType<T>, update: UpdateType<T>, schema: Schema<T>, collectionName: string, options?: DbPoolOptions<T, P>): Promise< DbPoolResultType<T> > => {
export const findOneAndUpdateData4MongoDB = async <T, P = DbPoolOptionsMarkerType>(where: QueryType<T>, update: UpdateType<T>, schema: Schema<T>, collectionName: string, options?: DbPoolOptions<T, P>, upsert: boolean = true): Promise< DbPoolResultType<T> > => {
try {
// 检查是否存在事务 session,如果存在,则设置 readPreference 为'primary'
if (options?.session) {
Expand All @@ -297,7 +298,7 @@ export const findOneAndUpdateData4MongoDB = async <T, P = DbPoolOptionsMarkerTyp
mongoModel = mongoose.model<T>(collectionName, schema)
}
try {
const updateResult = (await mongoModel.findOneAndUpdate(where, { $set: update }, { new: true, upsert: true, ...options })).toObject() as T
const updateResult = (await mongoModel.findOneAndUpdate(where, { $set: update }, { new: true, upsert, ...options })).toObject() as T

if (updateResult) {
return { success: true, message: '数据更新成功', result: updateResult }
Expand Down
4 changes: 4 additions & 0 deletions src/dbPool/schema/BrowsingHistorySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class BrowsingHistorySchemaFactory {
type: { type: String, required: true },
/** 浏览的内容的唯一 ID - 非空 */
id: { type: String, required: true },
/** 浏览的定位锚点,如果是视频就是播放时间,如果是相册可能是上次浏览到相册第n张图片,为了兼容性使用 String */
anchor: { type: String },
/** 最后编辑时间,用户历史记录页面排序,原则上与下方的系统专用最后编辑时间相同 - 非空 */
lastUpdateDateTime: { type: Number, required: true },
/** 系统专用字段-最后编辑时间 - 非空 */
editDateTime: { type: Number, required: true },
}
Expand Down
11 changes: 8 additions & 3 deletions src/route/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Router from 'koa-router'
import { createUserBrowsingHistoryController } from '../controller/BrowsingHistoryController.js'
import { createOrUpdateUserBrowsingHistoryController, getUserBrowsingHistoryWithFilterController } from '../controller/BrowsingHistoryController.js'
import { emitDanmakuController, getDanmakuListByKvidController } from '../controller/DanmakuController.js'
import { helloWorld } from '../controller/HelloWorld.js'
import { checkUserTokenController, getSelfUserInfoController, getUserAvatarUploadSignedUrlController, getUserInfoByUidController, getUserSettingsController, updateOrCreateUserInfoController, updateOrCreateUserSettingsController, updateUserEmailController, userExistsCheckController, userLoginController, userLogoutController, userRegistrationController } from '../controller/UserController.js'
Expand Down Expand Up @@ -285,15 +285,20 @@ router.post('/video/tag/get', getVideoTagByTagIdController) // 根据 TAG ID 在



router.post('/history/create', createUserBrowsingHistoryController) // 创建用户浏览历史
// https://localhost:9999/history/create
router.post('/history/merge', createOrUpdateUserBrowsingHistoryController) // 更新或创建用户浏览历史
// https://localhost:9999/history/merge
// cookie: uid, token
// {
// "uid": 2,
// "type": "video",
// "id": "32"
// }

router.get('/history/filter', getUserBrowsingHistoryWithFilterController) // 获取全部或过滤后的用户浏览历史,按对某一内容的最后访问时间降序排序
// https://localhost:9999/history/filter?videoTitle=foo
// cookie: uid, token
// > 或者你可以不包含 URL 查询以获取当前用户全部浏览历史 -> https://localhost:9999/history/filter




Expand Down
Loading

0 comments on commit bf5769a

Please sign in to comment.