video.service.ts 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. // box-app-api/src/feature/video/video.service.ts
  2. import { Injectable, Logger } from '@nestjs/common';
  3. import { RedisService } from '@box/db/redis/redis.service';
  4. import { PrismaMongoService } from '../../prisma/prisma-mongo.service';
  5. import { tsCacheKeys } from '@box/common/cache/ts-cache-key.provider';
  6. import type { VideoHomeSectionKey } from '@box/common/cache/ts-cache-key.provider';
  7. import {
  8. RawVideoPayloadRow,
  9. toVideoPayload,
  10. VideoPayload,
  11. parseVideoPayload,
  12. VideoCacheHelper,
  13. } from '@box/common/cache/video-cache.helper';
  14. import {
  15. VideoCategoryDto,
  16. VideoTagDto,
  17. VideoDetailDto,
  18. VideoPageDto,
  19. VideoCategoryWithTagsResponseDto,
  20. VideoListRequestDto,
  21. VideoListResponseDto,
  22. VideoSearchByTagRequestDto,
  23. VideoClickDto,
  24. RecommendedVideosDto,
  25. VideoItemDto,
  26. } from './dto';
  27. import {
  28. RabbitmqPublisherService,
  29. StatsVideoClickEventPayload,
  30. } from '../../rabbitmq/rabbitmq-publisher.service';
  31. import { randomUUID } from 'crypto';
  32. import { nowEpochMsBigInt } from '@box/common/time/time.util';
  33. import {
  34. CategoryType,
  35. RECOMMENDED_CATEGORY_ID,
  36. RECOMMENDED_CATEGORY_NAME,
  37. } from '../homepage/homepage.constants';
  38. import { CategoryDto } from '../homepage/dto/homepage.dto';
  39. /**
  40. * VideoService provides read-only access to video data from Redis cache.
  41. * All data is prebuilt and maintained by box-mgnt-api cache builders.
  42. * Follows the new Redis cache semantics where:
  43. * - Video list keys store video IDs only (not JSON objects)
  44. * - Tag metadata keys store tag JSON objects
  45. * - Video details are fetched separately using video IDs
  46. */
  47. @Injectable()
  48. export class VideoService {
  49. private readonly logger = new Logger(VideoService.name);
  50. private readonly cacheHelper: VideoCacheHelper;
  51. constructor(
  52. private readonly redis: RedisService,
  53. private readonly mongoPrisma: PrismaMongoService,
  54. private readonly rabbitmqPublisher: RabbitmqPublisherService,
  55. ) {
  56. this.cacheHelper = new VideoCacheHelper(redis);
  57. }
  58. /**
  59. * Get video detail by videoId.
  60. * Reads from appVideoDetailKey (JSON).
  61. */
  62. async getVideoDetail(videoId: string): Promise<VideoDetailDto | null> {
  63. try {
  64. const key = tsCacheKeys.video.detail(videoId);
  65. const cached = await this.redis.getJson<VideoDetailDto | null>(key);
  66. return cached ?? null;
  67. } catch (err) {
  68. this.logger.error(
  69. `Error fetching video detail for videoId=${videoId}`,
  70. err instanceof Error ? err.stack : String(err),
  71. );
  72. return null;
  73. }
  74. }
  75. /**
  76. * Get home section videos for a channel.
  77. * Reads from appVideoHomeSectionKey (LIST of videoIds).
  78. * Returns video details for each ID.
  79. */
  80. async getHomeSectionVideos(
  81. channelId: string,
  82. section: VideoHomeSectionKey,
  83. ): Promise<VideoDetailDto[]> {
  84. try {
  85. const key = tsCacheKeys.video.homeSection(channelId, section);
  86. // Use helper to read all video IDs from the LIST
  87. const videoIds = await this.cacheHelper.getVideoIdList(key);
  88. if (!videoIds || videoIds.length === 0) {
  89. return [];
  90. }
  91. // Fetch details for all videoIds
  92. const details = await this.getVideoDetailsBatch(videoIds);
  93. return details.filter((d) => d !== null) as VideoDetailDto[];
  94. } catch (err) {
  95. this.logger.error(
  96. `Error fetching home section videos for channelId=${channelId}, section=${section}`,
  97. err instanceof Error ? err.stack : String(err),
  98. );
  99. return [];
  100. }
  101. }
  102. private async getVideoDetailsBatch(
  103. videoIds: string[],
  104. ): Promise<(VideoDetailDto | null)[]> {
  105. if (!videoIds || videoIds.length === 0) {
  106. return [];
  107. }
  108. try {
  109. const keys = videoIds.map((id) => tsCacheKeys.video.detail(id));
  110. const results: (VideoDetailDto | null)[] = [];
  111. // Fetch all in parallel
  112. for (const key of keys) {
  113. const cached = await this.redis.getJson<VideoDetailDto | null>(key);
  114. results.push(cached ?? null);
  115. }
  116. return results;
  117. } catch (err) {
  118. this.logger.error(
  119. `Error fetching video details batch`,
  120. err instanceof Error ? err.stack : String(err),
  121. );
  122. return videoIds.map(() => null);
  123. }
  124. }
  125. private async getVideoPayloadsByIds(
  126. videoIds: string[],
  127. ): Promise<VideoPayload[]> {
  128. if (!videoIds || videoIds.length === 0) {
  129. return [];
  130. }
  131. try {
  132. const keys = videoIds.map((id) => tsCacheKeys.video.payload(id));
  133. const cached = await this.redis.mget(keys);
  134. const payloadMap = new Map<string, VideoPayload>();
  135. const missing = new Set<string>();
  136. cached.forEach((raw, idx) => {
  137. const id = videoIds[idx];
  138. if (!raw) {
  139. missing.add(id);
  140. return;
  141. }
  142. const parsed = parseVideoPayload(raw);
  143. if (!parsed) {
  144. missing.add(id);
  145. return;
  146. }
  147. payloadMap.set(id, parsed);
  148. });
  149. if (missing.size > 0) {
  150. const records = await this.mongoPrisma.videoMedia.findMany({
  151. where: { id: { in: Array.from(missing) } },
  152. select: {
  153. id: true,
  154. title: true,
  155. coverImg: true,
  156. coverImgNew: true,
  157. videoTime: true,
  158. country: true,
  159. firstTag: true,
  160. secondTags: true,
  161. preFileName: true,
  162. desc: true,
  163. size: true,
  164. updatedAt: true,
  165. filename: true,
  166. fieldNameFs: true,
  167. ext: true,
  168. },
  169. });
  170. if (records.length > 0) {
  171. const pipelineEntries = records.map((row: RawVideoPayloadRow) => ({
  172. key: tsCacheKeys.video.payload(row.id),
  173. value: toVideoPayload(row),
  174. }));
  175. await this.redis.pipelineSetJson(pipelineEntries);
  176. for (const row of records) {
  177. payloadMap.set(row.id, toVideoPayload(row));
  178. missing.delete(row.id);
  179. }
  180. }
  181. }
  182. return videoIds
  183. .map((id) => payloadMap.get(id))
  184. .filter((payload): payload is VideoPayload => Boolean(payload));
  185. } catch (err) {
  186. this.logger.error(
  187. `Error fetching video payloads for ids=${videoIds.join(',')}`,
  188. err instanceof Error ? err.stack : String(err),
  189. );
  190. return [];
  191. }
  192. }
  193. /**
  194. * Get paginated list of videos for a category with optional tag filtering.
  195. * Reads video IDs from Redis cache, fetches full details from MongoDB,
  196. * and returns paginated results.
  197. */
  198. async getVideoList(dto: VideoListRequestDto): Promise<VideoListResponseDto> {
  199. const { page, size, tagName } = dto;
  200. const categoryId = dto.categoryId;
  201. let key: string;
  202. let tagId: string | undefined;
  203. // If tagName is provided but categoryId is not, fallback to searchVideosByTagName
  204. if (tagName && !categoryId) {
  205. this.logger.debug(
  206. `tagName provided without categoryId, falling back to searchVideosByTagName`,
  207. );
  208. return this.searchVideosByTagName({ page, size, tagName });
  209. }
  210. // Validate categoryId is provided when no tagName
  211. if (!categoryId) {
  212. this.logger.debug(`categoryId is required for getVideoList`);
  213. return {
  214. page,
  215. size,
  216. total: 0,
  217. tagName,
  218. items: [],
  219. };
  220. }
  221. // Step 1: Resolve the Redis key
  222. if (!tagName) {
  223. // No tag filter - use category list
  224. key = tsCacheKeys.video.categoryList(categoryId);
  225. } else {
  226. // Tag filter - need to find tag ID first
  227. try {
  228. const tagKey = tsCacheKeys.tag.metadataByCategory(categoryId);
  229. const tags = await this.cacheHelper.getTagListForCategory(tagKey);
  230. if (!tags || tags.length === 0) {
  231. this.logger.debug(
  232. `No tags found for categoryId=${categoryId}, tagName=${tagName}`,
  233. );
  234. return {
  235. page,
  236. size,
  237. total: 0,
  238. tagName,
  239. items: [],
  240. };
  241. }
  242. const tag = tags.find((t) => t.name === tagName || t.id === tagName);
  243. if (!tag) {
  244. this.logger.debug(
  245. `Tag not found: categoryId=${categoryId}, tagName=${tagName}`,
  246. );
  247. return {
  248. page,
  249. size,
  250. total: 0,
  251. tagName,
  252. items: [],
  253. };
  254. }
  255. tagId = tag.id;
  256. key = tsCacheKeys.video.tagList(categoryId, tagId);
  257. } catch (err) {
  258. this.logger.error(
  259. `Error fetching tag for categoryId=${categoryId}, tagName=${tagName}`,
  260. err instanceof Error ? err.stack : String(err),
  261. );
  262. return {
  263. page,
  264. size,
  265. total: 0,
  266. tagName,
  267. items: [],
  268. };
  269. }
  270. }
  271. type VideoPayloadWithTags = RawVideoPayloadRow & { tagIds?: string[] };
  272. // Step 2: Compute pagination indices
  273. const start = (page - 1) * size;
  274. const stop = start + size - 1;
  275. let total = 0;
  276. let pageVideoIds: string[] = [];
  277. let fallbackRecords: VideoPayloadWithTags[] = [];
  278. let videoTagMap = new Map<string, string[]>();
  279. let listKeyExists: boolean;
  280. try {
  281. listKeyExists = (await this.redis.exists(key)) > 0;
  282. } catch (err) {
  283. this.logger.error(
  284. `Error checking list key existence for key=${key}`,
  285. err instanceof Error ? err.stack : String(err),
  286. );
  287. return {
  288. page,
  289. size,
  290. total: 0,
  291. tagName,
  292. items: [],
  293. };
  294. }
  295. if (listKeyExists) {
  296. try {
  297. total = await this.redis.llen(key);
  298. } catch (err) {
  299. this.logger.error(
  300. `Error getting list length for key=${key}`,
  301. err instanceof Error ? err.stack : String(err),
  302. );
  303. return {
  304. page,
  305. size,
  306. total: 0,
  307. tagName,
  308. items: [],
  309. };
  310. }
  311. if (total === 0) {
  312. this.logger.debug(`Empty video list for key=${key}`);
  313. return {
  314. page,
  315. size,
  316. total: 0,
  317. tagName,
  318. items: [],
  319. };
  320. }
  321. if (start >= total) {
  322. this.logger.debug(
  323. `Page out of range: page=${page}, size=${size}, total=${total}, key=${key}`,
  324. );
  325. return {
  326. page,
  327. size,
  328. total,
  329. tagName,
  330. items: [],
  331. };
  332. }
  333. try {
  334. pageVideoIds = await this.redis.lrange(key, start, stop);
  335. } catch (err) {
  336. this.logger.error(
  337. `Error fetching video IDs from key=${key}`,
  338. err instanceof Error ? err.stack : String(err),
  339. );
  340. return {
  341. page,
  342. size,
  343. total,
  344. tagName,
  345. items: [],
  346. };
  347. }
  348. if (!pageVideoIds || pageVideoIds.length === 0) {
  349. this.logger.debug(`No video IDs found for key=${key}`);
  350. return {
  351. page,
  352. size,
  353. total,
  354. tagName,
  355. items: [],
  356. };
  357. }
  358. } else {
  359. this.logger.debug(`Cache miss for video list key=${key}`);
  360. try {
  361. const where = tagId
  362. ? {
  363. categoryIds: { has: categoryId },
  364. status: 'Completed',
  365. tagIds: { has: tagId },
  366. }
  367. : {
  368. categoryIds: { has: categoryId },
  369. status: 'Completed',
  370. };
  371. fallbackRecords = (await this.mongoPrisma.videoMedia.findMany({
  372. where,
  373. orderBy: [{ addedTime: 'desc' }, { createdAt: 'desc' }],
  374. select: {
  375. id: true,
  376. title: true,
  377. coverImg: true,
  378. coverImgNew: true,
  379. videoTime: true,
  380. country: true,
  381. firstTag: true,
  382. secondTags: true,
  383. preFileName: true,
  384. desc: true,
  385. size: true,
  386. updatedAt: true,
  387. filename: true,
  388. fieldNameFs: true,
  389. ext: true,
  390. tagIds: true,
  391. },
  392. })) as VideoPayloadWithTags[];
  393. } catch (err) {
  394. this.logger.error(
  395. `Error fetching videos from MongoDB for fallback key=${key}`,
  396. err instanceof Error ? err.stack : String(err),
  397. );
  398. return {
  399. page,
  400. size,
  401. total: 0,
  402. tagName,
  403. items: [],
  404. };
  405. }
  406. const allVideoIds = fallbackRecords.map((video) => video.id);
  407. total = allVideoIds.length;
  408. try {
  409. await this.cacheHelper.saveVideoIdList(key, allVideoIds);
  410. } catch (err) {
  411. this.logger.error(
  412. `Error saving video ID list for key=${key}`,
  413. err instanceof Error ? err.stack : String(err),
  414. );
  415. }
  416. const entries = fallbackRecords.map((video) => ({
  417. key: tsCacheKeys.video.payload(video.id),
  418. value: toVideoPayload(video),
  419. }));
  420. try {
  421. await this.redis.pipelineSetJson(entries);
  422. } catch (err) {
  423. this.logger.error(
  424. `Error writing payload cache for fallback key=${key}`,
  425. err instanceof Error ? err.stack : String(err),
  426. );
  427. }
  428. if (total === 0) {
  429. this.logger.debug(`No videos found for fallback key=${key}`);
  430. return {
  431. page,
  432. size,
  433. total: 0,
  434. tagName,
  435. items: [],
  436. };
  437. }
  438. if (start >= total) {
  439. this.logger.debug(
  440. `Page out of range: page=${page}, size=${size}, total=${total}, key=${key}`,
  441. );
  442. return {
  443. page,
  444. size,
  445. total,
  446. tagName,
  447. items: [],
  448. };
  449. }
  450. const slicedIds = allVideoIds.slice(start, stop + 1);
  451. pageVideoIds = slicedIds;
  452. videoTagMap = new Map(
  453. fallbackRecords.map((video) => [
  454. video.id,
  455. Array.isArray(video.tagIds) ? video.tagIds : [],
  456. ]),
  457. );
  458. }
  459. if (!pageVideoIds.length) {
  460. return {
  461. page,
  462. size,
  463. total,
  464. tagName,
  465. items: [],
  466. };
  467. }
  468. if (!videoTagMap.size) {
  469. try {
  470. const tagRows = await this.mongoPrisma.videoMedia.findMany({
  471. where: { id: { in: pageVideoIds } },
  472. select: { id: true, tagIds: true },
  473. });
  474. for (const row of tagRows) {
  475. videoTagMap.set(row.id, Array.isArray(row.tagIds) ? row.tagIds : []);
  476. }
  477. } catch (err) {
  478. this.logger.error(
  479. `Error fetching video tag IDs for ids=${pageVideoIds.join(',')}`,
  480. err instanceof Error ? err.stack : String(err),
  481. );
  482. }
  483. }
  484. const allTagIds = new Set<string>();
  485. for (const ids of videoTagMap.values()) {
  486. if (Array.isArray(ids)) {
  487. for (const tid of ids) {
  488. allTagIds.add(tid);
  489. }
  490. }
  491. }
  492. let tagsById = new Map<string, string>();
  493. if (allTagIds.size > 0) {
  494. try {
  495. const tagsList = await this.mongoPrisma.tag.findMany({
  496. where: { id: { in: Array.from(allTagIds) } },
  497. select: { id: true, name: true },
  498. });
  499. tagsById = new Map(tagsList.map((tag) => [tag.id, tag.name]));
  500. } catch (err) {
  501. this.logger.error(
  502. `Error fetching tags from MongoDB`,
  503. err instanceof Error ? err.stack : String(err),
  504. );
  505. }
  506. }
  507. let category;
  508. try {
  509. category = await this.mongoPrisma.category.findUnique({
  510. where: { id: categoryId },
  511. });
  512. } catch (err) {
  513. this.logger.error(
  514. `Error fetching category for categoryId=${categoryId}`,
  515. err instanceof Error ? err.stack : String(err),
  516. );
  517. }
  518. const payloads = await this.getVideoPayloadsByIds(pageVideoIds);
  519. const payloadMap = new Map(
  520. payloads.map((payload) => [payload.id, payload]),
  521. );
  522. const items = pageVideoIds
  523. .map((videoId) => {
  524. const payload = payloadMap.get(videoId);
  525. if (!payload) {
  526. this.logger.debug(`Video payload missing for videoId=${videoId}`);
  527. return null;
  528. }
  529. const tags: string[] = [];
  530. const videoTagIds = videoTagMap.get(videoId);
  531. if (videoTagIds && Array.isArray(videoTagIds)) {
  532. for (const tid of videoTagIds) {
  533. const tagName = tagsById.get(tid);
  534. if (tagName) {
  535. tags.push(tagName);
  536. }
  537. }
  538. }
  539. return {
  540. id: payload.id,
  541. title: payload.title ?? '',
  542. coverImg: payload.coverImg ?? undefined,
  543. duration: payload.videoTime ?? undefined,
  544. categoryId,
  545. name: category?.name ?? '',
  546. subtitle: category?.subtitle ?? undefined,
  547. tags,
  548. updateAt: payload.updatedAt ?? new Date().toISOString(),
  549. };
  550. })
  551. .filter((item): item is NonNullable<typeof item> => item !== null);
  552. return {
  553. page,
  554. size,
  555. total,
  556. tagName,
  557. items,
  558. };
  559. }
  560. async searchVideosByTagName(
  561. dto: VideoSearchByTagRequestDto,
  562. ): Promise<VideoListResponseDto> {
  563. const { page, size, tagName } = dto;
  564. // Step 1: Load all categories
  565. let categories: Array<{
  566. id: string;
  567. name: string;
  568. subtitle?: string;
  569. }>;
  570. try {
  571. const categoriesKey = tsCacheKeys.category.all();
  572. categories = await this.redis.getJson<
  573. Array<{
  574. id: string;
  575. name: string;
  576. subtitle?: string;
  577. }>
  578. >(categoriesKey);
  579. if (!categories || categories.length === 0) {
  580. // Fallback to MongoDB if Redis cache is empty
  581. this.logger.debug(
  582. 'Categories not found in Redis, fetching from MongoDB',
  583. );
  584. const categoriesFromDb = await this.mongoPrisma.category.findMany({
  585. select: {
  586. id: true,
  587. name: true,
  588. subtitle: true,
  589. },
  590. });
  591. categories = categoriesFromDb.map((c) => ({
  592. id: c.id,
  593. name: c.name ?? '',
  594. subtitle: c.subtitle ?? undefined,
  595. }));
  596. }
  597. } catch (err) {
  598. this.logger.error(
  599. 'Error loading categories for tag search',
  600. err instanceof Error ? err.stack : String(err),
  601. );
  602. return {
  603. page,
  604. size,
  605. total: 0,
  606. tagName,
  607. items: [],
  608. };
  609. }
  610. if (!categories || categories.length === 0) {
  611. this.logger.debug('No categories found');
  612. return {
  613. page,
  614. size,
  615. total: 0,
  616. tagName,
  617. items: [],
  618. };
  619. }
  620. // Step 2 & 3: For each category, find matching tags and collect (categoryId, tagId) pairs
  621. const categoryTagPairs: Array<{ categoryId: string; tagId: string }> = [];
  622. for (const category of categories) {
  623. try {
  624. const tagKey = tsCacheKeys.tag.metadataByCategory(category.id);
  625. const tagsMetadata =
  626. await this.cacheHelper.getTagListForCategory(tagKey);
  627. const matchingTags = (tagsMetadata ?? []).filter(
  628. (t) => t.name === tagName,
  629. );
  630. for (const tag of matchingTags) {
  631. categoryTagPairs.push({
  632. categoryId: category.id,
  633. tagId: tag.id,
  634. });
  635. }
  636. } catch (err) {
  637. this.logger.debug(
  638. `Error fetching tags for categoryId=${category.id}`,
  639. err instanceof Error ? err.stack : String(err),
  640. );
  641. // Continue with next category
  642. }
  643. }
  644. if (categoryTagPairs.length === 0) {
  645. this.logger.debug(`No categories found with tag: ${tagName}`);
  646. return {
  647. page,
  648. size,
  649. total: 0,
  650. tagName,
  651. items: [],
  652. };
  653. }
  654. // Step 4: For each (categoryId, tagId) pair, read all video IDs
  655. const allVideoIds: string[] = [];
  656. for (const pair of categoryTagPairs) {
  657. try {
  658. const key = tsCacheKeys.video.tagList(pair.categoryId, pair.tagId);
  659. const videoIds = await this.redis.lrange(key, 0, -1);
  660. if (videoIds && videoIds.length > 0) {
  661. allVideoIds.push(...videoIds);
  662. }
  663. } catch (err) {
  664. this.logger.debug(
  665. `Error reading video IDs for categoryId=${pair.categoryId}, tagId=${pair.tagId}`,
  666. err instanceof Error ? err.stack : String(err),
  667. );
  668. // Continue with next pair
  669. }
  670. }
  671. if (allVideoIds.length === 0) {
  672. this.logger.debug(`No videos found for tag: ${tagName}`);
  673. return {
  674. page,
  675. size,
  676. total: 0,
  677. tagName,
  678. items: [],
  679. };
  680. }
  681. // Step 5: Deduplicate and compute total
  682. const uniqueVideoIds = Array.from(new Set(allVideoIds));
  683. const total = uniqueVideoIds.length;
  684. // Step 6: Apply in-memory pagination
  685. const start = (page - 1) * size;
  686. const end = start + size;
  687. const pagedIds = uniqueVideoIds.slice(start, end);
  688. if (pagedIds.length === 0) {
  689. return {
  690. page,
  691. size,
  692. total,
  693. tagName,
  694. items: [],
  695. };
  696. }
  697. // Step 7: Fetch videos from MongoDB
  698. let videos: Awaited<
  699. ReturnType<typeof this.mongoPrisma.videoMedia.findMany>
  700. >;
  701. try {
  702. videos = await this.mongoPrisma.videoMedia.findMany({
  703. where: {
  704. id: { in: pagedIds },
  705. },
  706. });
  707. } catch (err) {
  708. this.logger.error(
  709. `Error fetching videos from MongoDB for tag search`,
  710. err instanceof Error ? err.stack : String(err),
  711. );
  712. return {
  713. page,
  714. size,
  715. total,
  716. tagName,
  717. items: [],
  718. };
  719. }
  720. if (!videos || videos.length === 0) {
  721. return {
  722. page,
  723. size,
  724. total,
  725. tagName,
  726. items: [],
  727. };
  728. }
  729. // Fetch category data for each video
  730. const categoryIdSet = new Set<string>();
  731. for (const video of videos) {
  732. if (video.categoryIds && Array.isArray(video.categoryIds)) {
  733. for (const cid of video.categoryIds) {
  734. categoryIdSet.add(cid);
  735. }
  736. }
  737. }
  738. const categoryIdsList = Array.from(categoryIdSet);
  739. const categoriesMap = new Map(
  740. categories
  741. .filter((c) => categoryIdsList.includes(c.id))
  742. .map((c) => [c.id, c]),
  743. );
  744. // Fetch all tags for mapping tag IDs to names
  745. const allTagIds = Array.from(
  746. new Set(
  747. videos.flatMap((v) =>
  748. v.tagIds && Array.isArray(v.tagIds) ? v.tagIds : [],
  749. ),
  750. ),
  751. );
  752. const tagsById = new Map<string, string>();
  753. try {
  754. if (allTagIds.length > 0) {
  755. const tags = await this.mongoPrisma.tag.findMany({
  756. where: {
  757. id: { in: allTagIds },
  758. },
  759. select: {
  760. id: true,
  761. name: true,
  762. },
  763. });
  764. for (const tag of tags) {
  765. if (tag.name) {
  766. tagsById.set(tag.id, tag.name);
  767. }
  768. }
  769. }
  770. } catch (err) {
  771. this.logger.error(
  772. 'Error fetching tags for search results',
  773. err instanceof Error ? err.stack : String(err),
  774. );
  775. // Continue without tag names
  776. }
  777. // Step 8: Map to VideoListItemDto (maintain order of pagedIds)
  778. const videoMap = new Map(videos.map((v) => [v.id, v]));
  779. const items = pagedIds
  780. .map((videoId) => {
  781. const video = videoMap.get(videoId);
  782. if (!video) {
  783. return null;
  784. }
  785. // Use first category ID from categoryIds array
  786. const firstCategoryId =
  787. Array.isArray(video.categoryIds) && video.categoryIds.length > 0
  788. ? video.categoryIds[0]
  789. : undefined;
  790. const category = firstCategoryId
  791. ? categoriesMap.get(firstCategoryId)
  792. : undefined;
  793. // Map tag IDs to tag names
  794. const tags: string[] = [];
  795. if (video.tagIds && Array.isArray(video.tagIds)) {
  796. for (const tid of video.tagIds) {
  797. const tagName = tagsById.get(tid);
  798. if (tagName) {
  799. tags.push(tagName);
  800. }
  801. }
  802. }
  803. return {
  804. id: video.id,
  805. title: video.title ?? '',
  806. coverImg: video.coverImg ?? undefined,
  807. duration: video.videoTime ?? undefined,
  808. categoryId: firstCategoryId ?? '',
  809. name: category?.name ?? '',
  810. subtitle: category?.subtitle ?? undefined,
  811. tags,
  812. updateAt: video.updatedAt?.toString() ?? new Date().toISOString(),
  813. };
  814. })
  815. .filter((item): item is NonNullable<typeof item> => item !== null);
  816. return {
  817. page,
  818. size,
  819. total,
  820. tagName,
  821. items,
  822. };
  823. }
  824. /**
  825. * Record video click event.
  826. * Publishes a stats.video.click event to RabbitMQ for analytics processing.
  827. * Uses fire-and-forget pattern for non-blocking operation.
  828. *
  829. * @param uid - User ID from JWT
  830. * @param body - Video click data from client
  831. * @param ip - Client IP address
  832. * @param userAgent - User agent string (unused but kept for compatibility)
  833. */
  834. async recordVideoClick(
  835. uid: string,
  836. body: VideoClickDto,
  837. ip: string,
  838. userAgent: string,
  839. ): Promise<void> {
  840. const clickedAt = nowEpochMsBigInt();
  841. const payload: StatsVideoClickEventPayload = {
  842. messageId: randomUUID(),
  843. uid,
  844. videoId: body.videoId,
  845. clickedAt,
  846. ip,
  847. };
  848. // Fire-and-forget: don't await, log errors asynchronously
  849. this.rabbitmqPublisher.publishStatsVideoClick(payload).catch((error) => {
  850. const message = error instanceof Error ? error.message : String(error);
  851. const stack = error instanceof Error ? error.stack : undefined;
  852. this.logger.error(
  853. `Failed to publish stats.video.click for videoId=${body.videoId}, uid=${uid}: ${message}`,
  854. stack,
  855. );
  856. });
  857. this.logger.debug(
  858. `Initiated stats.video.click publish for videoId=${body.videoId}, uid=${uid}`,
  859. );
  860. }
  861. /**
  862. * Fisher-Yates shuffle for random ordering
  863. */
  864. private shuffle<T>(array: T[]): T[] {
  865. const shuffled = [...array];
  866. for (let i = shuffled.length - 1; i > 0; i--) {
  867. const j = Math.floor(Math.random() * (i + 1));
  868. [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
  869. }
  870. return shuffled;
  871. }
  872. /**
  873. * Get video categories for homepage
  874. * Returns shuffled categories with "推荐" as first item
  875. */
  876. async getCategories(): Promise<CategoryDto[]> {
  877. try {
  878. const categories = await this.mongoPrisma.category.findMany({
  879. where: {
  880. status: 1, // active only
  881. },
  882. orderBy: {
  883. seq: 'asc',
  884. },
  885. });
  886. // Shuffle regular categories (keep recommended first)
  887. const recommended: CategoryDto = {
  888. id: RECOMMENDED_CATEGORY_ID,
  889. name: RECOMMENDED_CATEGORY_NAME,
  890. type: CategoryType.RECOMMENDED,
  891. isDefault: true,
  892. seq: 0,
  893. };
  894. const regular = this.shuffle(
  895. categories.map((c, idx) => ({
  896. id: c.id,
  897. name: c.name,
  898. type: CategoryType.REGULAR,
  899. isDefault: false,
  900. seq: idx + 1,
  901. })),
  902. );
  903. return [recommended, ...regular];
  904. } catch (error) {
  905. this.logger.warn(
  906. 'Category collection not found or error fetching categories',
  907. );
  908. return [
  909. {
  910. id: RECOMMENDED_CATEGORY_ID,
  911. name: RECOMMENDED_CATEGORY_NAME,
  912. type: CategoryType.RECOMMENDED,
  913. isDefault: true,
  914. seq: 0,
  915. },
  916. ];
  917. }
  918. }
  919. /**
  920. * Get recommended videos (7 random videos for homepage)
  921. */
  922. async getRecommendedVideos(): Promise<RecommendedVideosDto> {
  923. try {
  924. // Try to fetch from Redis cache first
  925. const cached = await this.redis.getJson<VideoItemDto[]>(
  926. tsCacheKeys.video.recommended(),
  927. );
  928. if (cached && Array.isArray(cached) && cached.length > 0) {
  929. this.logger.debug(
  930. `[getRecommendedVideos] Returning ${cached.length} videos from cache`,
  931. );
  932. return {
  933. items: cached,
  934. total: cached.length,
  935. };
  936. }
  937. // Fallback to MongoDB if cache miss
  938. this.logger.warn(
  939. '[getRecommendedVideos] Cache miss, falling back to MongoDB',
  940. );
  941. const videos = await this.mongoPrisma.videoMedia.aggregateRaw({
  942. pipeline: [
  943. { $match: { status: 'Completed' } },
  944. { $sample: { size: 7 } },
  945. ],
  946. });
  947. const items = (Array.isArray(videos) ? videos : []).map((v: any) =>
  948. this.mapVideoToDto(v),
  949. );
  950. return {
  951. items,
  952. total: items.length,
  953. };
  954. } catch (error) {
  955. this.logger.warn('Error fetching recommended videos, returning empty');
  956. return {
  957. items: [],
  958. total: 0,
  959. };
  960. }
  961. }
  962. /**
  963. * Map raw video from MongoDB to VideoItemDto
  964. */
  965. mapVideoToDto(video: any): VideoItemDto {
  966. return {
  967. id: video._id?.$oid ?? video._id?.toString() ?? video.id,
  968. title: video.title ?? '',
  969. coverImg: video.coverImg ?? undefined,
  970. coverImgNew: video.coverImgNew ?? undefined,
  971. videoTime: video.videoTime ?? undefined,
  972. publish: video.publish ?? undefined,
  973. secondTags: Array.isArray(video.secondTags) ? video.secondTags : [],
  974. updatedAt: video.updatedAt?.$date
  975. ? new Date(video.updatedAt.$date)
  976. : video.updatedAt
  977. ? new Date(video.updatedAt)
  978. : undefined,
  979. filename: video.filename ?? undefined,
  980. fieldNameFs: video.fieldNameFs ?? undefined,
  981. width: video.width ?? undefined,
  982. height: video.height ?? undefined,
  983. tags: Array.isArray(video.tags) ? video.tags : [],
  984. preFileName: video.preFileName ?? undefined,
  985. actors: Array.isArray(video.actors) ? video.actors : [],
  986. size:
  987. video.size !== undefined && video.size !== null
  988. ? String(video.size)
  989. : undefined,
  990. };
  991. }
  992. /**
  993. * Read the cached video list key built by box-mgnt-api.
  994. */
  995. async getVideoListFromCache(): Promise<VideoItemDto[]> {
  996. const key = tsCacheKeys.video.list();
  997. try {
  998. const raw = await this.redis.get(key);
  999. if (!raw) {
  1000. return [];
  1001. }
  1002. const parsed = JSON.parse(raw);
  1003. if (Array.isArray(parsed)) {
  1004. return parsed;
  1005. }
  1006. } catch (err) {
  1007. this.logger.error(
  1008. `Failed to read video list cache (${key})`,
  1009. err instanceof Error ? err.stack : String(err),
  1010. );
  1011. }
  1012. return [];
  1013. }
  1014. /**
  1015. * Search the cached video list by secondTags, with fallback for videos that have no secondTags.
  1016. */
  1017. async searchVideosBySecondTags(tags?: string): Promise<VideoItemDto[]> {
  1018. const videos = await this.getVideoListFromCache();
  1019. if (!tags) {
  1020. return videos;
  1021. }
  1022. const requestedTags = tags
  1023. .split(',')
  1024. .map((tag) => tag.trim())
  1025. .filter((tag) => tag.length > 0);
  1026. if (requestedTags.length === 0) {
  1027. return videos;
  1028. }
  1029. const tagSet = new Set(requestedTags);
  1030. return videos.filter((video) => this.matchesSecondTags(video, tagSet));
  1031. }
  1032. private matchesSecondTags(
  1033. video: VideoItemDto,
  1034. filters: Set<string>,
  1035. ): boolean {
  1036. const secondTags = Array.isArray(video.secondTags)
  1037. ? video.secondTags
  1038. .map((tag) => tag?.trim())
  1039. .filter(
  1040. (tag): tag is string => typeof tag === 'string' && tag.length > 0,
  1041. )
  1042. : [];
  1043. if (secondTags.length === 0) {
  1044. // return true;
  1045. }
  1046. return secondTags.some((tag) => filters.has(tag));
  1047. }
  1048. }