|
|
@@ -37,7 +37,6 @@ import {
|
|
|
} from '../homepage/homepage.constants';
|
|
|
import { CategoryDto } from '../homepage/dto/homepage.dto';
|
|
|
import { VideoListItemDto } from './dto/video-list-response.dto';
|
|
|
-import { VideoListItemDto } from './dto/video-list-response.dto';
|
|
|
|
|
|
/**
|
|
|
* VideoService provides read-only access to video data from Redis cache.
|
|
|
@@ -65,9 +64,7 @@ export class VideoService {
|
|
|
* Reads from appVideoHomeSectionKey (LIST of videoIds).
|
|
|
* Returns video details for each ID.
|
|
|
*/
|
|
|
- async getHomeSectionVideos(
|
|
|
- channelId: string
|
|
|
- ): Promise<any[]> {
|
|
|
+ async getHomeSectionVideos(channelId: string): Promise<any[]> {
|
|
|
try {
|
|
|
const channel = await this.mongoPrisma.channel.findUnique({
|
|
|
where: { channelId },
|
|
|
@@ -76,11 +73,14 @@ export class VideoService {
|
|
|
const result: { tag: string; records: VideoListItemDto[] }[] = [];
|
|
|
|
|
|
for (const tag of channel.tagNames) {
|
|
|
- const records = await this.getVideoList({
|
|
|
- random: true,
|
|
|
- tag,
|
|
|
- size: 7,
|
|
|
- }, 3600 * 24);
|
|
|
+ const records = await this.getVideoList(
|
|
|
+ {
|
|
|
+ random: true,
|
|
|
+ tag,
|
|
|
+ size: 7,
|
|
|
+ },
|
|
|
+ 3600 * 24,
|
|
|
+ );
|
|
|
|
|
|
result.push({
|
|
|
tag,
|
|
|
@@ -103,18 +103,18 @@ export class VideoService {
|
|
|
* Reads video IDs from Redis cache, fetches full details from MongoDB,
|
|
|
* and returns paginated results.
|
|
|
*/
|
|
|
- async getVideoList(dto: VideoListRequestDto, ttl?: number): Promise<VideoListItemDto[]> {
|
|
|
+ async getVideoList(
|
|
|
+ dto: VideoListRequestDto,
|
|
|
+ ttl?: number,
|
|
|
+ ): Promise<VideoListItemDto[]> {
|
|
|
const { page, size, tag, keyword, random } = dto;
|
|
|
const start = (page - 1) * size;
|
|
|
|
|
|
- const cacheKey = `video:list:${Buffer.from(
|
|
|
- JSON.stringify(dto),
|
|
|
- ).toString('base64')}`;
|
|
|
- const cacheKey = `video:list:${Buffer.from(
|
|
|
- JSON.stringify(dto),
|
|
|
- ).toString('base64')}`;
|
|
|
+ const cacheKey = `video:list:${Buffer.from(JSON.stringify(dto)).toString(
|
|
|
+ 'base64',
|
|
|
+ )}`;
|
|
|
|
|
|
- if(!ttl){
|
|
|
+ if (!ttl) {
|
|
|
ttl = random ? 15 : 300;
|
|
|
}
|
|
|
|
|
|
@@ -125,11 +125,11 @@ export class VideoService {
|
|
|
return cache;
|
|
|
}
|
|
|
|
|
|
- let where: any = {
|
|
|
- status: 'Completed'
|
|
|
+ const where: any = {
|
|
|
+ status: 'Completed',
|
|
|
};
|
|
|
|
|
|
- if(random){
|
|
|
+ if (random) {
|
|
|
if (tag) {
|
|
|
where.secondTags = tag;
|
|
|
}
|
|
|
@@ -138,7 +138,7 @@ export class VideoService {
|
|
|
where.title = {
|
|
|
$regex: keyword,
|
|
|
$options: 'i',
|
|
|
- }
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
fallbackRecords = (await this.mongoPrisma.videoMedia.aggregateRaw({
|
|
|
@@ -157,7 +157,7 @@ export class VideoService {
|
|
|
},
|
|
|
],
|
|
|
})) as unknown as VideoListItemDto[];
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
if (tag) {
|
|
|
where.secondTags = {
|
|
|
has: tag,
|
|
|
@@ -166,8 +166,9 @@ export class VideoService {
|
|
|
|
|
|
if (keyword) {
|
|
|
where.title = {
|
|
|
- contains: keyword, mode: 'insensitive'
|
|
|
- }
|
|
|
+ contains: keyword,
|
|
|
+ mode: 'insensitive',
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
fallbackRecords = (await this.mongoPrisma.videoMedia.findMany({
|
|
|
@@ -175,8 +176,6 @@ export class VideoService {
|
|
|
orderBy: [{ addedTime: 'desc' }, { createdAt: 'desc' }],
|
|
|
skip: start,
|
|
|
take: size,
|
|
|
- skip: start,
|
|
|
- take: size,
|
|
|
select: {
|
|
|
id: true,
|
|
|
title: true,
|
|
|
@@ -186,13 +185,10 @@ export class VideoService {
|
|
|
preFileName: true,
|
|
|
},
|
|
|
})) as VideoListItemDto[];
|
|
|
- })) as VideoListItemDto[];
|
|
|
}
|
|
|
|
|
|
if (fallbackRecords.length > 0) {
|
|
|
await this.redis.setJson(cacheKey, fallbackRecords, ttl);
|
|
|
- if (fallbackRecords.length > 0) {
|
|
|
- await this.redis.setJson(cacheKey, fallbackRecords, ttl);
|
|
|
}
|
|
|
|
|
|
return fallbackRecords;
|
|
|
@@ -203,14 +199,6 @@ export class VideoService {
|
|
|
);
|
|
|
return [];
|
|
|
}
|
|
|
- return fallbackRecords;
|
|
|
- } catch (err) {
|
|
|
- this.logger.error(
|
|
|
- `Error fetching videos from MongoDB`,
|
|
|
- err instanceof Error ? err.stack : String(err),
|
|
|
- );
|
|
|
- return [];
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -486,7 +474,10 @@ export class VideoService {
|
|
|
async getGuessLikeVideos(tag: string): Promise<VideoItemDto[]> {
|
|
|
try {
|
|
|
// Try to fetch from Redis cache first
|
|
|
- const cached = await this.readCachedVideoList(tsCacheKeys.video.guess() + encodeURIComponent(tag), 'guess like videos');
|
|
|
+ const cached = await this.readCachedVideoList(
|
|
|
+ tsCacheKeys.video.guess() + encodeURIComponent(tag),
|
|
|
+ 'guess like videos',
|
|
|
+ );
|
|
|
|
|
|
if (cached && Array.isArray(cached) && cached.length > 0) {
|
|
|
return cached;
|
|
|
@@ -508,9 +499,15 @@ export class VideoService {
|
|
|
this.mapVideoToDto(v),
|
|
|
);
|
|
|
|
|
|
- this.redis.setJson(tsCacheKeys.video.guess() + encodeURIComponent(tag), items, 3600).catch(err => {
|
|
|
- this.logger.warn("Redis setJson video.guess failed", err);
|
|
|
- });
|
|
|
+ this.redis
|
|
|
+ .setJson(
|
|
|
+ tsCacheKeys.video.guess() + encodeURIComponent(tag),
|
|
|
+ items,
|
|
|
+ 3600,
|
|
|
+ )
|
|
|
+ .catch((err) => {
|
|
|
+ this.logger.warn('Redis setJson video.guess failed', err);
|
|
|
+ });
|
|
|
|
|
|
return items;
|
|
|
} catch (error) {
|